~drizzle-trunk/drizzle/development

1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
28
extern bool verbose;
29
extern bool ignore_errors;
1751.4.23 by Andrew Hutchings
Fix various bugs
30
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
31
bool DrizzleDumpDatabaseDrizzle::populateTables()
32
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
33
  drizzle_result_st *result;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
34
  drizzle_row_t row;
35
  std::string query;
36
1751.4.25 by Andrew Hutchings
Fix error handling
37
  if (not dcon->setDB(databaseName))
38
    return false;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
39
1751.4.23 by Andrew Hutchings
Fix various bugs
40
  if (verbose)
41
    std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
42
1810.1.1 by Andrew Hutchings
Add table comments
43
  query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT, TABLE_COMMENT FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
44
  query.append(databaseName);
45
  query.append("' ORDER BY TABLE_NAME");
46
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
47
  result= dcon->query(query);
48
1751.4.25 by Andrew Hutchings
Fix error handling
49
  if (result == NULL)
50
    return false;
51
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
52
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
53
  {
1810.1.1 by Andrew Hutchings
Add table comments
54
    size_t* row_sizes= drizzle_row_field_sizes(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
55
    std::string tableName(row[0]);
1751.4.25 by Andrew Hutchings
Fix error handling
56
    std::string displayName(tableName);
57
    cleanTableName(displayName);
58
    if (not ignoreTable(displayName))
1751.4.24 by Andrew Hutchings
Fix ignore tables
59
      continue;
60
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
61
    DrizzleDumpTable *table = new DrizzleDumpTableDrizzle(tableName, dcon);
1751.4.25 by Andrew Hutchings
Fix error handling
62
    table->displayName= displayName;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
63
    table->collate= row[1];
64
    table->engineName= row[2];
1802.5.1 by Andrew Hutchings
Adds auto_increment to data_dictionary.tables and drizzledump (when connecting to a Drizzle server)
65
    table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
1810.1.1 by Andrew Hutchings
Add table comments
66
    if (row[4])
67
      table->comment= DrizzleDumpData::escape(row[4], row_sizes[4]);
68
    else
69
      table->comment= "";
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
70
    table->database= this;
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
71
    if ((not table->populateFields()) or (not table->populateIndexes()) or
72
      (not table->populateFkeys()))
1751.4.25 by Andrew Hutchings
Fix error handling
73
    {
74
      delete table;
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
75
      if (not ignore_errors)
76
        return false;
77
      else
78
        continue;
1751.4.25 by Andrew Hutchings
Fix error handling
79
    }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
80
    tables.push_back(table);
81
  }
82
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
83
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
84
85
  return true;
86
}
87
1751.4.23 by Andrew Hutchings
Fix various bugs
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
1751.4.25 by Andrew Hutchings
Fix error handling
94
  if (not dcon->setDB(databaseName))
95
    return false;
1751.4.23 by Andrew Hutchings
Fix various bugs
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;
1751.4.25 by Andrew Hutchings
Fix error handling
102
    std::string displayName(tableName);
103
    cleanTableName(displayName);
104
    if (not ignoreTable(displayName))
1751.4.24 by Andrew Hutchings
Fix ignore tables
105
      continue;
106
1751.4.23 by Andrew Hutchings
Fix various bugs
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
1751.4.25 by Andrew Hutchings
Fix error handling
115
    if (result == NULL)
116
    {
117
      std::cerr << "Error: Could not obtain schema for table " << displayName << std::endl;
118
      return false;
119
    }
120
1751.4.23 by Andrew Hutchings
Fix various bugs
121
    if ((row= drizzle_row_next(result)))
122
    {
1751.4.24 by Andrew Hutchings
Fix ignore tables
123
      DrizzleDumpTableDrizzle *table = new DrizzleDumpTableDrizzle(tableName, dcon);
1751.4.25 by Andrew Hutchings
Fix error handling
124
      table->displayName= displayName;
1751.4.23 by Andrew Hutchings
Fix various bugs
125
      table->collate= row[1];
126
      table->engineName= row[2];
127
      table->autoIncrement= 0;
128
      table->database= this;
1751.4.25 by Andrew Hutchings
Fix error handling
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);
1799.7.5 by Andrew Hutchings
Fix test cases
134
        if (not ignore_errors)
135
          return false;
136
        else
137
          continue;
1751.4.25 by Andrew Hutchings
Fix error handling
138
      }
1751.4.23 by Andrew Hutchings
Fix various bugs
139
      tables.push_back(table);
140
      dcon->freeResult(result);
141
    }
142
    else
143
    {
1751.4.25 by Andrew Hutchings
Fix error handling
144
      std::cerr << "Error: Table " << displayName << " not found." << std::endl;
1751.4.23 by Andrew Hutchings
Fix various bugs
145
      dcon->freeResult(result);
1799.7.5 by Andrew Hutchings
Fix test cases
146
      if (not ignore_errors)
147
        return false;
148
      else
149
        continue;
1751.4.23 by Andrew Hutchings
Fix various bugs
150
    }
151
  }
152
153
  return true;
154
155
}
156
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
167
  drizzle_result_st *result;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
168
  drizzle_row_t row;
169
  std::string query;
170
1751.4.23 by Andrew Hutchings
Fix various bugs
171
  if (verbose)
172
    std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
173
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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 FROM DATA_DICTIONARY.COLUMNS WHERE TABLE_SCHEMA='";
175
  query.append(database->databaseName);
176
  query.append("' AND TABLE_NAME='");
177
  query.append(tableName);
1751.4.28 by Andrew Hutchings
Remove "ORDER BY ORDINAL_POSITION", eats RAM, sucks performance and it is already ordered correctly anyway
178
  query.append("'");
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
179
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
180
  result= dcon->query(query);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
181
1751.4.25 by Andrew Hutchings
Fix error handling
182
  if (result == NULL)
183
    return false;
184
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
185
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
186
  {
187
    std::string fieldName(row[0]);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
188
    DrizzleDumpField *field = new DrizzleDumpFieldDrizzle(fieldName, dcon);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
206
207
    fields.push_back(field);
208
  }
209
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
210
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
211
  return true;
212
}
213
214
215
bool DrizzleDumpTableDrizzle::populateIndexes()
216
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
217
  drizzle_result_st *result;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
218
  drizzle_row_t row;
219
  std::string query;
220
  std::string lastKey;
221
  bool firstIndex= true;
222
  DrizzleDumpIndex *index;
223
1751.4.23 by Andrew Hutchings
Fix various bugs
224
  if (verbose)
225
    std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
226
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
227
  query= "SELECT INDEX_NAME, COLUMN_NAME, IS_USED_IN_PRIMARY, IS_UNIQUE, COMPARE_LENGTH FROM DATA_DICTIONARY.INDEX_PARTS WHERE TABLE_NAME='";
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
228
  query.append(tableName);
229
  query.append("'");
230
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
231
  result= dcon->query(query);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
232
1751.4.25 by Andrew Hutchings
Fix error handling
233
  if (result == NULL)
234
    return false;
235
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
236
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
237
  {
238
    std::string indexName(row[0]);
239
    if (indexName.compare(lastKey) != 0)
240
    {
241
      if (!firstIndex)
242
        indexes.push_back(index);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
243
      index = new DrizzleDumpIndexDrizzle(indexName, dcon);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
244
      index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
245
      index->isUnique= (strcmp(row[3], "YES") == 0);
246
      index->isHash= 0;
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
247
      index->length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
248
      lastKey= row[0];
249
      firstIndex= false;
250
    }
251
    index->columns.push_back(row[1]);
252
  }
253
  if (!firstIndex)
254
    indexes.push_back(index);
255
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
256
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
257
  return true;
258
}
259
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
260
bool DrizzleDumpTableDrizzle::populateFkeys()
261
{
262
  drizzle_result_st *result;
263
  drizzle_row_t row;
264
  std::string query;
265
  DrizzleDumpForeignKey *fkey;
266
267
  if (verbose)
268
    std::cerr << _("-- Retrieving foreign keys for ") << tableName << "..." << std::endl;
269
270
  query= "SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, MATCH_OPTION, DELETE_RULE, UPDATE_RULE FROM DATA_DICTIONARY.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA='";
271
  query.append(database->databaseName);
272
  query.append("' AND CONSTRAINT_TABLE='");
273
  query.append(tableName);
274
  query.append("'");
275
276
  result= dcon->query(query);
277
278
  if (result == NULL)
279
    return false;
280
281
  while ((row= drizzle_row_next(result)))
282
  {
283
    fkey= new DrizzleDumpForeignKey(row[0], dcon);
284
    fkey->parentColumns= row[1];
285
    fkey->childTable= row[2];
286
    fkey->childColumns= row[3];
287
    if (strcmp(row[4], "NONE") != 0)
288
      fkey->matchOption= row[4];
289
    else
290
      fkey->matchOption= "";
291
292
    if (strcmp(row[5], "UNDEFINED") != 0)
293
      fkey->deleteRule= row[5];
294
    else
295
      fkey->deleteRule= "";
296
297
    if (strcmp(row[6], "UNDEFINED") != 0)
298
      fkey->updateRule= row[6];
299
    else
300
      fkey->updateRule= "";
301
302
    fkeys.push_back(fkey);
303
  }
304
  dcon->freeResult(result);
305
  return true;
306
}
307
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
308
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
309
{
1751.4.25 by Andrew Hutchings
Fix error handling
310
  try
311
  {
312
    return new DrizzleDumpDataDrizzle(this, dcon);
313
  }
314
  catch(...)
315
  {
316
    return NULL;
317
  }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
318
}
319
320
321
void DrizzleDumpFieldDrizzle::setType(const char* raw_type, const char* raw_collation)
322
{
323
  collation= raw_collation;
324
  if (strcmp(raw_type, "BLOB") == 0)
325
  {
326
    if (strcmp(raw_collation, "binary") != 0)
327
      type= "TEXT";
328
    else
329
      type= raw_type;
330
    return;
331
  }
332
333
  if (strcmp(raw_type, "VARCHAR") == 0)
334
  {
335
    if (strcmp(raw_collation, "binary") != 0)
336
      type= "VARCHAR";
337
    else
338
      type= "VARBINARY";
339
    return;
340
  }
341
342
  if (strcmp(raw_type, "INTEGER") == 0)
343
  {
344
    type= "INT";
345
    return;
346
  }
347
348
  type= raw_type;
349
}
350
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
351
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable,
352
  DrizzleDumpConnection *connection)
353
  : DrizzleDumpData(dataTable, connection)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
354
{
355
  std::string query;
356
  query= "SELECT * FROM `";
1751.4.25 by Andrew Hutchings
Fix error handling
357
  query.append(table->displayName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
358
  query.append("`");
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
359
360
  result= dcon->query(query);
1751.4.25 by Andrew Hutchings
Fix error handling
361
362
  if (result == NULL)
363
    throw 1;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
364
}
365
366
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
367
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
368
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
369
}