1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2010 Andrew Hutchings
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.
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.
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
20
#include "drizzledump_data.h"
21
#include "drizzledump_drizzle.h"
22
#include "client_priv.h"
25
#include <drizzled/gettext.h>
26
#include <boost/lexical_cast.hpp>
30
bool DrizzleDumpDatabaseDrizzle::populateTables()
32
drizzle_result_st *result;
36
if (not dcon->setDB(databaseName))
40
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
42
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
43
query.append(databaseName);
44
query.append("' ORDER BY TABLE_NAME");
46
result= dcon->query(query);
51
while ((row= drizzle_row_next(result)))
53
std::string tableName(row[0]);
54
std::string displayName(tableName);
55
cleanTableName(displayName);
56
if (not ignoreTable(displayName))
59
DrizzleDumpTable *table = new DrizzleDumpTableDrizzle(tableName, dcon);
60
table->displayName= displayName;
61
table->collate= row[1];
62
table->engineName= row[2];
63
table->autoIncrement= 0;
64
table->database= this;
65
if ((not table->populateFields()) or (not table->populateIndexes()))
70
tables.push_back(table);
73
dcon->freeResult(result);
78
bool DrizzleDumpDatabaseDrizzle::populateTables(const std::vector<std::string> &table_names)
80
drizzle_result_st *result;
84
if (not dcon->setDB(databaseName))
88
std::cerr << _("-- Retrieving table structures for ") << databaseName << "..." << std::endl;
89
for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
91
std::string tableName= *it;
92
std::string displayName(tableName);
93
cleanTableName(displayName);
94
if (not ignoreTable(displayName))
97
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
98
query.append(databaseName);
99
query.append("' AND TABLE_NAME = '");
100
query.append(tableName);
103
result= dcon->query(query);
107
std::cerr << "Error: Could not obtain schema for table " << displayName << std::endl;
111
if ((row= drizzle_row_next(result)))
113
DrizzleDumpTableDrizzle *table = new DrizzleDumpTableDrizzle(tableName, dcon);
114
table->displayName= displayName;
115
table->collate= row[1];
116
table->engineName= row[2];
117
table->autoIncrement= 0;
118
table->database= this;
119
if ((not table->populateFields()) or (not table->populateIndexes()))
121
std::cerr << "Error: Could not get fields and/ot indexes for table " << displayName << std::endl;
123
dcon->freeResult(result);
126
tables.push_back(table);
127
dcon->freeResult(result);
131
std::cerr << "Error: Table " << displayName << " not found." << std::endl;
132
dcon->freeResult(result);
141
void DrizzleDumpDatabaseDrizzle::setCollate(const char* newCollate)
146
collate= "utf8_general_ci";
149
bool DrizzleDumpTableDrizzle::populateFields()
151
drizzle_result_st *result;
156
std::cerr << _("-- Retrieving fields for ") << tableName << "..." << std::endl;
158
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='";
159
query.append(database->databaseName);
160
query.append("' AND TABLE_NAME='");
161
query.append(tableName);
164
result= dcon->query(query);
169
while ((row= drizzle_row_next(result)))
171
std::string fieldName(row[0]);
172
DrizzleDumpField *field = new DrizzleDumpFieldDrizzle(fieldName, dcon);
173
/* Stop valgrind warning */
174
field->convertDateTime= false;
175
/* Also sets collation */
176
field->setType(row[1], row[8]);
178
field->defaultValue= row[2];
180
field->defaultValue= "";
182
field->isNull= (strcmp(row[4], "YES") == 0) ? true : false;
183
field->isAutoIncrement= (strcmp(row[9], "YES") == 0) ? true : false;
184
field->defaultIsNull= (strcmp(row[3], "YES") == 0) ? true : false;
185
field->enumValues= (row[10]) ? row[10] : "";
186
field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
187
field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
188
field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
191
fields.push_back(field);
194
dcon->freeResult(result);
199
bool DrizzleDumpTableDrizzle::populateIndexes()
201
drizzle_result_st *result;
205
bool firstIndex= true;
206
DrizzleDumpIndex *index;
209
std::cerr << _("-- Retrieving indexes for ") << tableName << "..." << std::endl;
211
query= "SELECT INDEX_NAME, COLUMN_NAME, IS_USED_IN_PRIMARY, IS_UNIQUE FROM DATA_DICTIONARY.INDEX_PARTS WHERE TABLE_NAME='";
212
query.append(tableName);
215
result= dcon->query(query);
220
while ((row= drizzle_row_next(result)))
222
std::string indexName(row[0]);
223
if (indexName.compare(lastKey) != 0)
226
indexes.push_back(index);
227
index = new DrizzleDumpIndexDrizzle(indexName, dcon);
228
index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
229
index->isUnique= (strcmp(row[3], "YES") == 0);
234
index->columns.push_back(row[1]);
237
indexes.push_back(index);
239
dcon->freeResult(result);
243
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
247
return new DrizzleDumpDataDrizzle(this, dcon);
256
void DrizzleDumpFieldDrizzle::setType(const char* raw_type, const char* raw_collation)
258
collation= raw_collation;
259
if (strcmp(raw_type, "BLOB") == 0)
261
if (strcmp(raw_collation, "binary") != 0)
268
if (strcmp(raw_type, "VARCHAR") == 0)
270
if (strcmp(raw_collation, "binary") != 0)
277
if (strcmp(raw_type, "INTEGER") == 0)
286
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable,
287
DrizzleDumpConnection *connection)
288
: DrizzleDumpData(dataTable, connection)
291
query= "SELECT * FROM `";
292
query.append(table->displayName);
295
result= dcon->query(query);
301
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
303
dcon->freeResult(result);