~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
1976.4.3 by Andrew Hutchings
Add field level comments back in to drizzledump
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='";
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
2187.3.1 by Monty Taylor
Make boolean return as tinyint through the mysql_protocol, so that php and
198
    field->isNull= (boost::lexical_cast<uint32_t>(row[4])) ? true : false;
199
    field->isAutoIncrement= (boost::lexical_cast<uint32_t>(row[9])) ? true : false;
200
    field->defaultIsNull= (boost::lexical_cast<uint32_t>(row[3])) ? true : false;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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;
1976.4.3 by Andrew Hutchings
Add field level comments back in to drizzledump
205
    field->comment= (row[11]) ? row[11] : "";
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
1810.6.5 by Andrew Hutchings
Fix possibility of grabbing wrong indexes for a table in a dump
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='");
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
230
  query.append(tableName);
231
  query.append("'");
232
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
233
  result= dcon->query(query);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
234
1751.4.25 by Andrew Hutchings
Fix error handling
235
  if (result == NULL)
236
    return false;
237
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
238
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
239
  {
240
    std::string indexName(row[0]);
241
    if (indexName.compare(lastKey) != 0)
242
    {
243
      if (!firstIndex)
244
        indexes.push_back(index);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
245
      index = new DrizzleDumpIndexDrizzle(indexName, dcon);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
246
      index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
2187.3.1 by Monty Taylor
Make boolean return as tinyint through the mysql_protocol, so that php and
247
      index->isUnique= boost::lexical_cast<uint32_t>(row[3]);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
248
      index->isHash= 0;
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
249
      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
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
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
258
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
259
  return true;
260
}
261
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
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
1810.6.9 by Andrew Hutchings
Fix drizzledump support again
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='";
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
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];
1810.6.6 by Andrew Hutchings
Add foriegn key support for MySQL
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] : "";
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
292
293
    fkeys.push_back(fkey);
294
  }
295
  dcon->freeResult(result);
296
  return true;
297
}
298
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
299
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
300
{
1751.4.25 by Andrew Hutchings
Fix error handling
301
  try
302
  {
303
    return new DrizzleDumpDataDrizzle(this, dcon);
304
  }
305
  catch(...)
306
  {
307
    return NULL;
308
  }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
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
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
342
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable,
343
  DrizzleDumpConnection *connection)
344
  : DrizzleDumpData(dataTable, connection)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
345
{
346
  std::string query;
347
  query= "SELECT * FROM `";
1751.4.25 by Andrew Hutchings
Fix error handling
348
  query.append(table->displayName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
349
  query.append("`");
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
350
351
  result= dcon->query(query);
1751.4.25 by Andrew Hutchings
Fix error handling
352
353
  if (result == NULL)
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
354
    throw std::exception();
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
355
}
356
357
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
358
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
359
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
360
}