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>
29 extern bool ignore_errors;
31 bool DrizzleDumpDatabaseDrizzle::populateTables()
33 drizzle_result_st *result;
37 if (not dcon->setDB(databaseName))
41 std::cerr << _(
"-- Retrieving table structures for ") << databaseName <<
"..." << std::endl;
43 query=
"SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, AUTO_INCREMENT, TABLE_COMMENT, IS_REPLICATED FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
44 query.append(databaseName);
45 query.append(
"' ORDER BY TABLE_NAME");
47 result= dcon->query(query);
52 while ((row= drizzle_row_next(result)))
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))
62 table->displayName= displayName;
63 table->collate= row[1];
64 table->engineName= row[2];
65 table->autoIncrement= boost::lexical_cast<uint64_t>(row[3]);
67 table->comment= DrizzleDumpData::escape(row[4], row_sizes[4]);
71 table->replicate= strcmp(row[5],
"1") == 0;
72 table->database=
this;
73 if ((not table->populateFields()) or (not table->populateIndexes()) or
74 (not table->populateFkeys()))
77 if (not ignore_errors)
82 tables.push_back(table);
85 dcon->freeResult(result);
90 bool DrizzleDumpDatabaseDrizzle::populateTables(
const std::vector<std::string> &table_names)
92 drizzle_result_st *result;
96 if (not dcon->setDB(databaseName))
100 std::cerr << _(
"-- Retrieving table structures for ") << databaseName <<
"..." << std::endl;
101 for (std::vector<std::string>::const_iterator it= table_names.begin(); it != table_names.end(); ++it)
103 std::string tableName= *it;
104 std::string displayName(tableName);
105 cleanTableName(displayName);
106 if (not ignoreTable(displayName))
109 query=
"SELECT TABLE_NAME, TABLE_COLLATION, ENGINE, IS_REPLICATED FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
110 query.append(databaseName);
111 query.append(
"' AND TABLE_NAME = '");
112 query.append(tableName);
115 result= dcon->query(query);
119 std::cerr <<
"Error: Could not obtain schema for table " << displayName << std::endl;
123 if ((row= drizzle_row_next(result)))
126 table->displayName= displayName;
127 table->collate= row[1];
128 table->engineName= row[2];
129 table->replicate= strcmp(row[3],
"1") == 0;
130 table->autoIncrement= 0;
131 table->database=
this;
132 if ((not table->populateFields()) or (not table->populateIndexes()))
134 std::cerr <<
"Error: Could not get fields and/ot indexes for table " << displayName << std::endl;
136 dcon->freeResult(result);
137 if (not ignore_errors)
142 tables.push_back(table);
143 dcon->freeResult(result);
147 std::cerr <<
"Error: Table " << displayName <<
" not found." << std::endl;
148 dcon->freeResult(result);
149 if (not ignore_errors)
160 void DrizzleDumpDatabaseDrizzle::setCollate(
const char* newCollate)
165 collate=
"utf8_general_ci";
168 bool DrizzleDumpTableDrizzle::populateFields()
170 drizzle_result_st *result;
175 std::cerr << _(
"-- Retrieving fields for ") << tableName <<
"..." << std::endl;
177 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='";
178 query.append(database->databaseName);
179 query.append(
"' AND TABLE_NAME='");
180 query.append(tableName);
183 result= dcon->query(query);
188 while ((row= drizzle_row_next(result)))
190 std::string fieldName(row[0]);
193 field->convertDateTime=
false;
195 field->setType(row[1], row[8]);
197 field->defaultValue= row[2];
199 field->defaultValue=
"";
201 field->isNull= (boost::lexical_cast<uint32_t>(row[4])) ?
true :
false;
202 field->isAutoIncrement= (boost::lexical_cast<uint32_t>(row[9])) ?
true :
false;
203 field->defaultIsNull= (boost::lexical_cast<uint32_t>(row[3])) ?
true :
false;
204 field->enumValues= (row[10]) ? row[10] :
"";
205 field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
206 field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
207 field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
208 field->comment= (row[11]) ? row[11] :
"";
210 fields.push_back(field);
213 dcon->freeResult(result);
218 bool DrizzleDumpTableDrizzle::populateIndexes()
220 drizzle_result_st *result;
224 bool firstIndex=
true;
228 std::cerr << _(
"-- Retrieving indexes for ") << tableName <<
"..." << std::endl;
230 query=
"SELECT INDEX_NAME, COLUMN_NAME, IS_USED_IN_PRIMARY, IS_UNIQUE, COMPARE_LENGTH FROM DATA_DICTIONARY.INDEX_PARTS WHERE TABLE_SCHEMA='";
231 query.append(database->databaseName);
232 query.append(
"' AND TABLE_NAME='");
233 query.append(tableName);
236 result= dcon->query(query);
241 while ((row= drizzle_row_next(result)))
243 std::string indexName(row[0]);
244 if (indexName.compare(lastKey) != 0)
247 indexes.push_back(index);
249 index->isPrimary= (strcmp(row[0],
"PRIMARY") == 0);
250 index->isUnique= boost::lexical_cast<uint32_t>(row[3]);
255 uint32_t length= (row[4]) ? boost::lexical_cast<uint32_t>(row[4]) : 0;
256 index->columns.push_back(std::make_pair(row[1],length));
259 indexes.push_back(index);
261 dcon->freeResult(result);
265 bool DrizzleDumpTableDrizzle::populateFkeys()
267 drizzle_result_st *result;
273 std::cerr << _(
"-- Retrieving foreign keys for ") << tableName <<
"..." << std::endl;
275 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='";
276 query.append(database->databaseName);
277 query.append(
"' AND CONSTRAINT_TABLE='");
278 query.append(tableName);
281 result= dcon->query(query);
286 while ((row= drizzle_row_next(result)))
289 fkey->parentColumns= row[1];
290 fkey->childTable= row[2];
291 fkey->childColumns= row[3];
292 fkey->matchOption= (strcmp(row[4],
"NONE") != 0) ? row[4] :
"";
293 fkey->deleteRule= (strcmp(row[5],
"UNDEFINED") != 0) ? row[5] :
"";
294 fkey->updateRule= (strcmp(row[6],
"UNDEFINED") != 0) ? row[6] :
"";
296 fkeys.push_back(fkey);
298 dcon->freeResult(result);
315 void DrizzleDumpFieldDrizzle::setType(
const char* raw_type,
const char* raw_collation)
317 collation= raw_collation;
318 if (strcmp(raw_type,
"BLOB") == 0)
320 if (strcmp(raw_collation,
"binary") != 0)
327 if (strcmp(raw_type,
"VARCHAR") == 0)
329 if (strcmp(raw_collation,
"binary") != 0)
336 if (strcmp(raw_type,
"INTEGER") == 0)
350 query=
"SELECT * FROM `";
351 query.append(table->displayName);
354 result= dcon->query(query);
357 throw std::exception();
360 DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
362 dcon->freeResult(result);