~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/message/table_reader.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 18:24:57 UTC
  • mfrom: (1999.6.3 trunk)
  • Revision ID: brian@tangent.org-20101218182457-yi1wd0so2hml1k1w
Merge in Lee's copyright header fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#include <string.h>
 
27
#include <stdio.h>
 
28
#include <errno.h>
 
29
#include <unistd.h>
 
30
 
1
31
#include <iostream>
2
 
#include <fstream>
3
32
#include <string>
4
 
#include "table.pb.h"
 
33
#include <drizzled/message/statement_transform.h>
 
34
#include <google/protobuf/io/zero_copy_stream.h>
 
35
#include <google/protobuf/io/zero_copy_stream_impl.h>
 
36
 
5
37
using namespace std;
 
38
using namespace drizzled;
 
39
using namespace google;
6
40
 
7
 
/* 
 
41
/*
8
42
  Written from Google proto example
9
43
*/
10
44
 
11
 
void printType(const drizzle::Table::Field *field) 
12
 
{
13
 
  switch (field->type())
14
 
  {
15
 
  case drizzle::Table::DOUBLE:
16
 
    cout << " DOUBLE ";
17
 
    break;
18
 
  case drizzle::Table::VARCHAR:
19
 
    cout << " VARCHAR(" << field->length() << ")";
20
 
    break;
21
 
  case drizzle::Table::TEXT:
22
 
    cout << " TEXT ";
23
 
    break;
24
 
  case drizzle::Table::BLOB:
25
 
    cout << " BLOB ";
26
 
    break;
27
 
  case drizzle::Table::ENUM:
28
 
    {
29
 
      int x;
30
 
 
31
 
      cout << " ENUM(";
32
 
      for (x= 0; x < field->values_size() ; x++)
33
 
      {
34
 
        const string type= field->values(x);
35
 
 
36
 
        if (x != 0)
37
 
          cout << ",";
38
 
        cout << "'" << type << "'";
39
 
      }
40
 
      cout << ") ";
41
 
      break;
42
 
    }
43
 
  case drizzle::Table::SET:
44
 
    cout << " SET ";
45
 
    break;
46
 
  case drizzle::Table::TINYINT:
47
 
    cout << " TINYINNT ";
48
 
    break;
49
 
  case drizzle::Table::SMALLINT:
50
 
    cout << " SMALLINT ";
51
 
    break;
52
 
  case drizzle::Table::INTEGER:
53
 
    cout << " INTEGER ";
54
 
    break;
55
 
  case drizzle::Table::BIGINT:
56
 
    cout << " BIGINT ";
57
 
    break;
58
 
  case drizzle::Table::DECIMAL:
59
 
    cout << " DECIMAL(" << field->length() << "," << field->scale() << ") ";
60
 
    break;
61
 
  case drizzle::Table::VARBINARY:
62
 
    cout << " VARBINARY(" << field->length() << ") ";
63
 
    break;
64
 
  case drizzle::Table::DATE:
65
 
    cout << " DATE ";
66
 
    break;
67
 
  case drizzle::Table::TIME:
68
 
    cout << " TIME ";
69
 
    break;
70
 
  case drizzle::Table::TIMESTAMP:
71
 
    cout << " TIMESTAMP ";
72
 
    break;
73
 
  case drizzle::Table::DATETIME:
74
 
    cout << " DATETIME ";
75
 
    break;
76
 
  }
77
 
 
78
 
  if (field->has_characterset())
79
 
    cout << " CHARACTER SET " << field->characterset();
80
 
 
81
 
  if (field->has_collation())
82
 
    cout << " COLLATE " << field->collation();
83
 
 
84
 
  if (field->is_notnull())
85
 
    cout << " NOT NULL ";
86
 
 
87
 
  if (field->has_default_value())
88
 
    cout << " DEFAULT `" << field->default_value() << "` " ;
89
 
 
90
 
  if (field->on_update())
91
 
    cout << " ON UPDATE CURRENT_TIMESTAMP";
92
 
 
93
 
  if (field->autoincrement())
94
 
    cout << " AUTOINCREMENT ";
95
 
 
96
 
  if (field->has_comment())
97
 
    cout << " COMMENT `" << field->comment() << "` ";
98
 
}
99
 
 
100
 
void printTable(const drizzle::Table *table) 
101
 
{
102
 
  uint32_t x;
103
 
 
104
 
  cout << "CREATE TABLE";
105
 
 
106
 
  if (table->temp())
107
 
    cout << " TEMPORARY";
108
 
 
109
 
  cout << " `" << table->name() << "` (" << endl;
110
 
  
111
 
  for (x= 0; x < table->field_size() ; x++)
112
 
  {
113
 
    const drizzle::Table::Field field = table->field(x);
114
 
 
115
 
    if (x != 0)
116
 
      cout << "," << endl;;
117
 
 
118
 
    cout << "\t`" << field.name() << "`";
119
 
    printType(&field);
120
 
  }
121
 
 
122
 
  for (x= 0; x < table->index_size() ; x++)
123
 
  {
124
 
    const drizzle::Table::Index index = table->index(x);
125
 
 
126
 
    cout << "," << endl;;
127
 
 
128
 
    cout << "\t";
129
 
 
130
 
    if (index.primary())
131
 
      cout << " PRIMARY KEY (`" << index.name() << "`)";
132
 
    else
133
 
    {
134
 
      int x;
135
 
 
136
 
      cout << " UNIQUE KEY `" << index.name() << "` (";
137
 
      for (x= 0; x < index.values_size() ; x++)
138
 
      {
139
 
        const drizzle::Table::KeyPart key= index.values(x);
140
 
 
141
 
        if (x != 0)
142
 
          cout << ",";
143
 
        cout << "`" << key.name() << "`";
144
 
      }
145
 
      cout << ")";
146
 
    }
147
 
  }
148
 
  cout << endl;
149
 
 
150
 
  cout << ") " << endl;
151
 
  if (table->has_collation())
152
 
    cout << " COLLATE = `" << table->collation() << "` " << endl;;
153
 
  if (table->has_characterset())
154
 
    cout << " CHARACTER SET = `" << table->characterset() << "` " << endl;;
155
 
  if (table->has_comment())
156
 
    cout << " COMMENT = `" << table->comment() << "` " << endl;;
157
 
  if (table->has_engine())
158
 
  if (table->has_data_directory())
159
 
    cout << " DATA DIRECTORY = `" << table->data_directory() << "` " << endl;;
160
 
  if (table->has_index_directory())
161
 
    cout << " INDEX DIRECTORY = `" << table->index_directory() << "`" << endl;
162
 
  cout << " ENGINE = " << table->engine() << ";"  << endl;
163
 
}
164
 
 
165
 
int main(int argc, char* argv[]) 
 
45
int main(int argc, char* argv[])
166
46
{
167
47
  GOOGLE_PROTOBUF_VERIFY_VERSION;
168
48
 
171
51
    return -1;
172
52
  }
173
53
 
174
 
  drizzle::Table table;
 
54
  message::Table table;
175
55
 
176
56
  {
177
 
    // Read the existing address book.
178
 
    fstream input(argv[1], ios::in | ios::binary);
179
 
    if (!table.ParseFromIstream(&input)) 
 
57
    int fd= open(argv[1], O_RDONLY);
 
58
 
 
59
    if(fd==-1)
 
60
    {
 
61
      perror("Failed to open table definition file");
 
62
      return -1;
 
63
    }
 
64
 
 
65
    protobuf::io::ZeroCopyInputStream* input=
 
66
      new protobuf::io::FileInputStream(fd);
 
67
 
 
68
    if (!table.ParseFromZeroCopyStream(input))
180
69
    {
181
70
      cerr << "Failed to parse table." << endl;
 
71
      close(fd);
182
72
      return -1;
183
73
    }
 
74
 
 
75
    close(fd);
184
76
  }
185
77
 
186
 
  printTable(&table);
 
78
  string output;
 
79
  (void) message::transformTableDefinitionToSql(table, output, message::DRIZZLE, true);
 
80
 
 
81
  cout << output << endl;
187
82
 
188
83
  return 0;
189
84
}