~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_drizzle.cc

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

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) 2010 Andrew Hutchings
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; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "drizzledump_data.h"
21
 
#include "drizzledump_drizzle.h"
22
 
#include "client_priv.h"
23
 
#include <string>
24
 
#include <iostream>
25
 
#include <drizzled/gettext.h>
26
 
#include <boost/lexical_cast.hpp>
27
 
 
28
 
extern bool verbose;
29
 
extern bool ignore_errors;
30
 
 
31
 
bool DrizzleDumpDatabaseDrizzle::populateTables()
32
 
{
33
 
  drizzle_result_st *result;
34
 
  drizzle_row_t row;
35
 
  std::string query;
36
 
 
37
 
  if (not dcon->setDB(databaseName))
38
 
    return false;
39
 
 
40
 
  if (verbose)
41
 
    std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
42
 
 
43
 
  query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT, TABLE_COMMENT FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
44
 
  query.append(databaseName);
45
 
  query.append("' ORDER BY TABLE_NAME");
46
 
 
47
 
  result= dcon->query(query);
48
 
 
49
 
  if (result == NULL)
50
 
    return false;
51
 
 
52
 
  while ((row= drizzle_row_next(result)))
53
 
  {
54
 
    size_t* row_sizes= drizzle_row_field_sizes(result);
55
 
    std::string tableName(row[0]);
56
 
    std::string displayName(tableName);
57
 
    cleanTableName(displayName);
58
 
    if (not ignoreTable(displayName))
59
 
      continue;
60
 
 
61
 
    DrizzleDumpTable *table = new DrizzleDumpTableDrizzle(tableName, dcon);
62
 
    table->displayName= displayName;
63
 
    table->collate= row[1];
64
 
    table->engineName= row[2];
65
 
    table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
66
 
    if (row[4])
67
 
      table->comment= DrizzleDumpData::escape(row[4], row_sizes[4]);
68
 
    else
69
 
      table->comment= "";
70
 
    table->database= this;
71
 
    if ((not table->populateFields()) or (not table->populateIndexes()) or
72
 
      (not table->populateFkeys()))
73
 
    {
74
 
      delete table;
75
 
      if (not ignore_errors)
76
 
        return false;
77
 
      else
78
 
        continue;
79
 
    }
80
 
    tables.push_back(table);
81
 
  }
82
 
 
83
 
  dcon->freeResult(result);
84
 
 
85
 
  return true;
86
 
}
87
 
 
88
 
bool DrizzleDumpDatabaseDrizzle::populateTables(const std::vector<std::string> &table_names)
89
 
{
90
 
  drizzle_result_st *result;
91
 
  drizzle_row_t row;
92
 
  std::string query;
93
 
 
94
 
  if (not dcon->setDB(databaseName))
95
 
    return false;
96
 
 
97
 
  if (verbose)
98
 
    std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
99
 
  for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
100
 
  {
101
 
    std::string tableName= *it;
102
 
    std::string displayName(tableName);
103
 
    cleanTableName(displayName);
104
 
    if (not ignoreTable(displayName))
105
 
      continue;
106
 
 
107
 
    query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
108
 
    query.append(databaseName);
109
 
    query.append("' AND TABLE_NAME = '");
110
 
    query.append(tableName);
111
 
    query.append("'");
112
 
 
113
 
    result= dcon->query(query);
114
 
 
115
 
    if (result == NULL)
116
 
    {
117
 
      std::cerr << "Error: Could not obtain schema for table " << displayName << std::endl;
118
 
      return false;
119
 
    }
120
 
 
121
 
    if ((row= drizzle_row_next(result)))
122
 
    {
123
 
      DrizzleDumpTableDrizzle *table = new DrizzleDumpTableDrizzle(tableName, dcon);
124
 
      table->displayName= displayName;
125
 
      table->collate= row[1];
126
 
      table->engineName= row[2];
127
 
      table->autoIncrement= 0;
128
 
      table->database= this;
129
 
      if ((not table->populateFields()) or (not table->populateIndexes()))
130
 
      {
131
 
        std::cerr  << "Error: Could not get fields and/ot indexes for table " << displayName << std::endl;
132
 
        delete table;
133
 
        dcon->freeResult(result);
134
 
        if (not ignore_errors)
135
 
          return false;
136
 
        else
137
 
          continue;
138
 
      }
139
 
      tables.push_back(table);
140
 
      dcon->freeResult(result);
141
 
    }
142
 
    else
143
 
    {
144
 
      std::cerr << "Error: Table " << displayName << " not found." << std::endl;
145
 
      dcon->freeResult(result);
146
 
      if (not ignore_errors)
147
 
        return false;
148
 
      else
149
 
        continue;
150
 
    }
151
 
  }
152
 
 
153
 
  return true;
154
 
 
155
 
}
156
 
 
157
 
void DrizzleDumpDatabaseDrizzle::setCollate(const char* newCollate)
158
 
{
159
 
  if (newCollate)
160
 
    collate= newCollate;
161
 
  else
162
 
    collate= "utf8_general_ci";
163
 
}
164
 
 
165
 
bool DrizzleDumpTableDrizzle::populateFields()
166
 
{
167
 
  drizzle_result_st *result;
168
 
  drizzle_row_t row;
169
 
  std::string query;
170
 
 
171
 
  if (verbose)
172
 
    std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
173
 
 
174
 
  query= "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, COLUMN_DEFAULT_IS_NULL, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME, IS_AUTO_INCREMENT, ENUM_VALUES, COLUMN_COMMENT FROM DATA_DICTIONARY.COLUMNS WHERE TABLE_SCHEMA='";
175
 
  query.append(database->databaseName);
176
 
  query.append("' AND TABLE_NAME='");
177
 
  query.append(tableName);
178
 
  query.append("'");
179
 
 
180
 
  result= dcon->query(query);
181
 
 
182
 
  if (result == NULL)
183
 
    return false;
184
 
 
185
 
  while ((row= drizzle_row_next(result)))
186
 
  {
187
 
    std::string fieldName(row[0]);
188
 
    DrizzleDumpField *field = new DrizzleDumpFieldDrizzle(fieldName, dcon);
189
 
    /* Stop valgrind warning */
190
 
    field->convertDateTime= false;
191
 
    /* Also sets collation */
192
 
    field->setType(row[1], row[8]);
193
 
    if (row[2])
194
 
      field->defaultValue= row[2];
195
 
    else
196
 
      field->defaultValue= "";
197
 
 
198
 
    field->isNull= (strcmp(row[4], "YES") == 0) ? true : false;
199
 
    field->isAutoIncrement= (strcmp(row[9], "YES") == 0) ? true : false;
200
 
    field->defaultIsNull= (strcmp(row[3], "YES") == 0) ? true : false;
201
 
    field->enumValues= (row[10]) ? row[10] : "";
202
 
    field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
203
 
    field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
204
 
    field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
205
 
    field->comment= (row[11]) ? row[11] : "";
206
 
 
207
 
    fields.push_back(field);
208
 
  }
209
 
 
210
 
  dcon->freeResult(result);
211
 
  return true;
212
 
}
213
 
 
214
 
 
215
 
bool DrizzleDumpTableDrizzle::populateIndexes()
216
 
{
217
 
  drizzle_result_st *result;
218
 
  drizzle_row_t row;
219
 
  std::string query;
220
 
  std::string lastKey;
221
 
  bool firstIndex= true;
222
 
  DrizzleDumpIndex *index;
223
 
 
224
 
  if (verbose)
225
 
    std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
226
 
 
227
 
  query= "SELECT INDEX_NAME, COLUMN_NAME, IS_USED_IN_PRIMARY, IS_UNIQUE, COMPARE_LENGTH FROM DATA_DICTIONARY.INDEX_PARTS WHERE TABLE_SCHEMA='";
228
 
  query.append(database->databaseName);
229
 
  query.append("' AND TABLE_NAME='");
230
 
  query.append(tableName);
231
 
  query.append("'");
232
 
 
233
 
  result= dcon->query(query);
234
 
 
235
 
  if (result == NULL)
236
 
    return false;
237
 
 
238
 
  while ((row= drizzle_row_next(result)))
239
 
  {
240
 
    std::string indexName(row[0]);
241
 
    if (indexName.compare(lastKey) != 0)
242
 
    {
243
 
      if (!firstIndex)
244
 
        indexes.push_back(index);
245
 
      index = new DrizzleDumpIndexDrizzle(indexName, dcon);
246
 
      index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
247
 
      index->isUnique= (strcmp(row[3], "YES") == 0);
248
 
      index->isHash= 0;
249
 
      index->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
250
 
      lastKey= row[0];
251
 
      firstIndex= false;
252
 
    }
253
 
    index->columns.push_back(row[1]);
254
 
  }
255
 
  if (!firstIndex)
256
 
    indexes.push_back(index);
257
 
 
258
 
  dcon->freeResult(result);
259
 
  return true;
260
 
}
261
 
 
262
 
bool DrizzleDumpTableDrizzle::populateFkeys()
263
 
{
264
 
  drizzle_result_st *result;
265
 
  drizzle_row_t row;
266
 
  std::string query;
267
 
  DrizzleDumpForeignKey *fkey;
268
 
 
269
 
  if (verbose)
270
 
    std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
271
 
 
272
 
  query= "SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, MATCH_OPTION, DELETE_RULE, UPDATE_RULE FROM DATA_DICTIONARY.FOREIGN_KEYS WHERE CONSTRAINT_SCHEMA='";
273
 
  query.append(database->databaseName);
274
 
  query.append("' AND CONSTRAINT_TABLE='");
275
 
  query.append(tableName);
276
 
  query.append("'");
277
 
 
278
 
  result= dcon->query(query);
279
 
 
280
 
  if (result == NULL)
281
 
    return false;
282
 
 
283
 
  while ((row= drizzle_row_next(result)))
284
 
  {
285
 
    fkey= new DrizzleDumpForeignKey(row[0], dcon);
286
 
    fkey->parentColumns= row[1];
287
 
    fkey->childTable= row[2];
288
 
    fkey->childColumns= row[3];
289
 
    fkey->matchOption= (strcmp(row[4], "NONE") != 0) ? row[4] : "";
290
 
    fkey->deleteRule= (strcmp(row[5], "UNDEFINED") != 0) ? row[5] : "";
291
 
    fkey->updateRule= (strcmp(row[6], "UNDEFINED") != 0) ? row[6] : "";
292
 
 
293
 
    fkeys.push_back(fkey);
294
 
  }
295
 
  dcon->freeResult(result);
296
 
  return true;
297
 
}
298
 
 
299
 
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
300
 
{
301
 
  try
302
 
  {
303
 
    return new DrizzleDumpDataDrizzle(this, dcon);
304
 
  }
305
 
  catch(...)
306
 
  {
307
 
    return NULL;
308
 
  }
309
 
}
310
 
 
311
 
 
312
 
void DrizzleDumpFieldDrizzle::setType(const char* raw_type, const char* raw_collation)
313
 
{
314
 
  collation= raw_collation;
315
 
  if (strcmp(raw_type, "BLOB") == 0)
316
 
  {
317
 
    if (strcmp(raw_collation, "binary") != 0)
318
 
      type= "TEXT";
319
 
    else
320
 
      type= raw_type;
321
 
    return;
322
 
  }
323
 
 
324
 
  if (strcmp(raw_type, "VARCHAR") == 0)
325
 
  {
326
 
    if (strcmp(raw_collation, "binary") != 0)
327
 
      type= "VARCHAR";
328
 
    else
329
 
      type= "VARBINARY";
330
 
    return;
331
 
  }
332
 
 
333
 
  if (strcmp(raw_type, "INTEGER") == 0)
334
 
  {
335
 
    type= "INT";
336
 
    return;
337
 
  }
338
 
 
339
 
  type= raw_type;
340
 
}
341
 
 
342
 
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable,
343
 
  DrizzleDumpConnection *connection)
344
 
  : DrizzleDumpData(dataTable, connection)
345
 
{
346
 
  std::string query;
347
 
  query= "SELECT * FROM `";
348
 
  query.append(table->displayName);
349
 
  query.append("`");
350
 
 
351
 
  result= dcon->query(query);
352
 
 
353
 
  if (result == NULL)
354
 
    throw std::exception();
355
 
}
356
 
 
357
 
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
358
 
{
359
 
  dcon->freeResult(result);
360
 
}