~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;
1751.4.25 by Andrew Hutchings
Fix error handling
71
    if ((not table->populateFields()) or (not table->populateIndexes()))
72
    {
73
      delete table;
1799.7.4 by Andrew Hutchings
Fix up some more options and the docs
74
      if (not ignore_errors)
75
        return false;
76
      else
77
        continue;
1751.4.25 by Andrew Hutchings
Fix error handling
78
    }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
79
    tables.push_back(table);
80
  }
81
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
82
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
83
84
  return true;
85
}
86
1751.4.23 by Andrew Hutchings
Fix various bugs
87
bool DrizzleDumpDatabaseDrizzle::populateTables(const std::vector<std::string> &table_names)
88
{
89
  drizzle_result_st *result;
90
  drizzle_row_t row;
91
  std::string query;
92
1751.4.25 by Andrew Hutchings
Fix error handling
93
  if (not dcon->setDB(databaseName))
94
    return false;
1751.4.23 by Andrew Hutchings
Fix various bugs
95
96
  if (verbose)
97
    std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
98
  for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
99
  {
100
    std::string tableName= *it;
1751.4.25 by Andrew Hutchings
Fix error handling
101
    std::string displayName(tableName);
102
    cleanTableName(displayName);
103
    if (not ignoreTable(displayName))
1751.4.24 by Andrew Hutchings
Fix ignore tables
104
      continue;
105
1751.4.23 by Andrew Hutchings
Fix various bugs
106
    query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
107
    query.append(databaseName);
108
    query.append("' AND TABLE_NAME = '");
109
    query.append(tableName);
110
    query.append("'");
111
112
    result= dcon->query(query);
113
1751.4.25 by Andrew Hutchings
Fix error handling
114
    if (result == NULL)
115
    {
116
      std::cerr << "Error: Could not obtain schema for table " << displayName << std::endl;
117
      return false;
118
    }
119
1751.4.23 by Andrew Hutchings
Fix various bugs
120
    if ((row= drizzle_row_next(result)))
121
    {
1751.4.24 by Andrew Hutchings
Fix ignore tables
122
      DrizzleDumpTableDrizzle *table = new DrizzleDumpTableDrizzle(tableName, dcon);
1751.4.25 by Andrew Hutchings
Fix error handling
123
      table->displayName= displayName;
1751.4.23 by Andrew Hutchings
Fix various bugs
124
      table->collate= row[1];
125
      table->engineName= row[2];
126
      table->autoIncrement= 0;
127
      table->database= this;
1751.4.25 by Andrew Hutchings
Fix error handling
128
      if ((not table->populateFields()) or (not table->populateIndexes()))
129
      {
130
        std::cerr  << "Error: Could not get fields and/ot indexes for table " << displayName << std::endl;
131
        delete table;
132
        dcon->freeResult(result);
1799.7.5 by Andrew Hutchings
Fix test cases
133
        if (not ignore_errors)
134
          return false;
135
        else
136
          continue;
1751.4.25 by Andrew Hutchings
Fix error handling
137
      }
1751.4.23 by Andrew Hutchings
Fix various bugs
138
      tables.push_back(table);
139
      dcon->freeResult(result);
140
    }
141
    else
142
    {
1751.4.25 by Andrew Hutchings
Fix error handling
143
      std::cerr << "Error: Table " << displayName << " not found." << std::endl;
1751.4.23 by Andrew Hutchings
Fix various bugs
144
      dcon->freeResult(result);
1799.7.5 by Andrew Hutchings
Fix test cases
145
      if (not ignore_errors)
146
        return false;
147
      else
148
        continue;
1751.4.23 by Andrew Hutchings
Fix various bugs
149
    }
150
  }
151
152
  return true;
153
154
}
155
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
156
void DrizzleDumpDatabaseDrizzle::setCollate(const char* newCollate)
157
{
158
  if (newCollate)
159
    collate= newCollate;
160
  else
161
    collate= "utf8_general_ci";
162
}
163
164
bool DrizzleDumpTableDrizzle::populateFields()
165
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
166
  drizzle_result_st *result;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
167
  drizzle_row_t row;
168
  std::string query;
169
1751.4.23 by Andrew Hutchings
Fix various bugs
170
  if (verbose)
171
    std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
172
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
173
  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='";
174
  query.append(database->databaseName);
175
  query.append("' AND TABLE_NAME='");
176
  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
177
  query.append("'");
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
178
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
179
  result= dcon->query(query);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
180
1751.4.25 by Andrew Hutchings
Fix error handling
181
  if (result == NULL)
182
    return false;
183
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
184
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
185
  {
186
    std::string fieldName(row[0]);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
187
    DrizzleDumpField *field = new DrizzleDumpFieldDrizzle(fieldName, dcon);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
188
    /* Stop valgrind warning */
189
    field->convertDateTime= false;
190
    /* Also sets collation */
191
    field->setType(row[1], row[8]);
192
    if (row[2])
193
      field->defaultValue= row[2];
194
    else
195
      field->defaultValue= "";
196
197
    field->isNull= (strcmp(row[4], "YES") == 0) ? true : false;
198
    field->isAutoIncrement= (strcmp(row[9], "YES") == 0) ? true : false;
199
    field->defaultIsNull= (strcmp(row[3], "YES") == 0) ? true : false;
200
    field->enumValues= (row[10]) ? row[10] : "";
201
    field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
202
    field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
203
    field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
204
205
206
    fields.push_back(field);
207
  }
208
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
209
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
210
  return true;
211
}
212
213
214
bool DrizzleDumpTableDrizzle::populateIndexes()
215
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
216
  drizzle_result_st *result;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
217
  drizzle_row_t row;
218
  std::string query;
219
  std::string lastKey;
220
  bool firstIndex= true;
221
  DrizzleDumpIndex *index;
222
1751.4.23 by Andrew Hutchings
Fix various bugs
223
  if (verbose)
224
    std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
225
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
226
  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
227
  query.append(tableName);
228
  query.append("'");
229
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
230
  result= dcon->query(query);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
231
1751.4.25 by Andrew Hutchings
Fix error handling
232
  if (result == NULL)
233
    return false;
234
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
235
  while ((row= drizzle_row_next(result)))
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
236
  {
237
    std::string indexName(row[0]);
238
    if (indexName.compare(lastKey) != 0)
239
    {
240
      if (!firstIndex)
241
        indexes.push_back(index);
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
242
      index = new DrizzleDumpIndexDrizzle(indexName, dcon);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
243
      index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
244
      index->isUnique= (strcmp(row[3], "YES") == 0);
245
      index->isHash= 0;
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
246
      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
247
      lastKey= row[0];
248
      firstIndex= false;
249
    }
250
    index->columns.push_back(row[1]);
251
  }
252
  if (!firstIndex)
253
    indexes.push_back(index);
254
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
255
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
256
  return true;
257
}
258
259
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
260
{
1751.4.25 by Andrew Hutchings
Fix error handling
261
  try
262
  {
263
    return new DrizzleDumpDataDrizzle(this, dcon);
264
  }
265
  catch(...)
266
  {
267
    return NULL;
268
  }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
269
}
270
271
272
void DrizzleDumpFieldDrizzle::setType(const char* raw_type, const char* raw_collation)
273
{
274
  collation= raw_collation;
275
  if (strcmp(raw_type, "BLOB") == 0)
276
  {
277
    if (strcmp(raw_collation, "binary") != 0)
278
      type= "TEXT";
279
    else
280
      type= raw_type;
281
    return;
282
  }
283
284
  if (strcmp(raw_type, "VARCHAR") == 0)
285
  {
286
    if (strcmp(raw_collation, "binary") != 0)
287
      type= "VARCHAR";
288
    else
289
      type= "VARBINARY";
290
    return;
291
  }
292
293
  if (strcmp(raw_type, "INTEGER") == 0)
294
  {
295
    type= "INT";
296
    return;
297
  }
298
299
  type= raw_type;
300
}
301
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
302
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable,
303
  DrizzleDumpConnection *connection)
304
  : DrizzleDumpData(dataTable, connection)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
305
{
306
  std::string query;
307
  query= "SELECT * FROM `";
1751.4.25 by Andrew Hutchings
Fix error handling
308
  query.append(table->displayName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
309
  query.append("`");
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
310
311
  result= dcon->query(query);
1751.4.25 by Andrew Hutchings
Fix error handling
312
313
  if (result == NULL)
314
    throw 1;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
315
}
316
317
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
318
{
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
319
  dcon->freeResult(result);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
320
}