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>
28
extern drizzle_con_st dcon;
30
bool DrizzleDumpDatabaseDrizzle::populateTables()
32
drizzle_result_st result;
37
if (drizzle_select_db(&dcon, &result, databaseName.c_str(), &ret) ==
38
NULL || ret != DRIZZLE_RETURN_OK)
40
errmsg << _("Could not set db '") << databaseName << "'";
43
drizzle_result_free(&result);
45
query="SELECT TABLE_NAME, TABLE_COLLATION, ENGINE FROM DATA_DICTIONARY.TABLES WHERE TABLE_SCHEMA='";
46
query.append(databaseName);
47
query.append("' ORDER BY TABLE_NAME");
49
if (drizzle_query_str(&dcon, &result, query.c_str(), &ret) == NULL ||
50
ret != DRIZZLE_RETURN_OK)
52
if (ret == DRIZZLE_RETURN_ERROR_CODE)
54
errmsg << _("Could not get tables list due to error: ") <<
55
drizzle_result_error(&result);
56
drizzle_result_free(&result);
60
errmsg << _("Could not get tables list due to error: ") <<
61
drizzle_con_error(&dcon);
66
if (drizzle_result_buffer(&result) != DRIZZLE_RETURN_OK)
68
errmsg << _("Could not get tables list due to error: ") <<
69
drizzle_con_error(&dcon);
73
while ((row= drizzle_row_next(&result)))
75
std::string tableName(row[0]);
76
DrizzleDumpTable *table = new DrizzleDumpTableDrizzle(tableName);
77
table->collate= row[1];
78
table->engineName= row[2];
79
table->autoIncrement= 0;
80
table->database= this;
81
table->populateFields();
82
table->populateIndexes();
83
tables.push_back(table);
86
drizzle_result_free(&result);
91
void DrizzleDumpDatabaseDrizzle::setCollate(const char* newCollate)
96
collate= "utf8_general_ci";
99
bool DrizzleDumpTableDrizzle::populateFields()
101
drizzle_result_st result;
103
drizzle_return_t ret;
106
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='";
107
query.append(database->databaseName);
108
query.append("' AND TABLE_NAME='");
109
query.append(tableName);
110
query.append("' ORDER BY ORDINAL_POSITION");
112
if (drizzle_query_str(&dcon, &result, query.c_str(), &ret) == NULL ||
113
ret != DRIZZLE_RETURN_OK)
115
if (ret == DRIZZLE_RETURN_ERROR_CODE)
117
errmsg << _("Could not get tables list due to error: ") <<
118
drizzle_result_error(&result);
119
drizzle_result_free(&result);
123
errmsg << _("Could not get tables list due to error: ") <<
124
drizzle_con_error(&dcon);
129
if (drizzle_result_buffer(&result) != DRIZZLE_RETURN_OK)
131
errmsg << _("Could not get tables list due to error: ") <<
132
drizzle_con_error(&dcon);
135
while ((row= drizzle_row_next(&result)))
137
std::string fieldName(row[0]);
138
DrizzleDumpField *field = new DrizzleDumpFieldDrizzle(fieldName);
139
/* Stop valgrind warning */
140
field->convertDateTime= false;
141
/* Also sets collation */
142
field->setType(row[1], row[8]);
144
field->defaultValue= row[2];
146
field->defaultValue= "";
148
field->isNull= (strcmp(row[4], "YES") == 0) ? true : false;
149
field->isAutoIncrement= (strcmp(row[9], "YES") == 0) ? true : false;
150
field->defaultIsNull= (strcmp(row[3], "YES") == 0) ? true : false;
151
field->enumValues= (row[10]) ? row[10] : "";
152
field->length= (row[5]) ? boost::lexical_cast<uint32_t>(row[5]) : 0;
153
field->decimalPrecision= (row[6]) ? boost::lexical_cast<uint32_t>(row[6]) : 0;
154
field->decimalScale= (row[7]) ? boost::lexical_cast<uint32_t>(row[7]) : 0;
157
fields.push_back(field);
160
drizzle_result_free(&result);
165
bool DrizzleDumpTableDrizzle::populateIndexes()
167
drizzle_result_st result;
169
drizzle_return_t ret;
172
bool firstIndex= true;
173
DrizzleDumpIndex *index;
175
query= "SELECT INDEX_NAME, COLUMN_NAME, IS_USED_IN_PRIMARY, IS_UNIQUE FROM DATA_DICTIONARY.INDEX_PARTS WHERE TABLE_NAME='";
176
query.append(tableName);
179
if (drizzle_query_str(&dcon, &result, query.c_str(), &ret) == NULL ||
180
ret != DRIZZLE_RETURN_OK)
182
if (ret == DRIZZLE_RETURN_ERROR_CODE)
184
errmsg << _("Could not get tables list due to error: ") <<
185
drizzle_result_error(&result);
186
drizzle_result_free(&result);
190
errmsg << _("Could not get tables list due to error: ") <<
191
drizzle_con_error(&dcon);
196
if (drizzle_result_buffer(&result) != DRIZZLE_RETURN_OK)
198
errmsg << _("Could not get tables list due to error: ") <<
199
drizzle_con_error(&dcon);
202
while ((row= drizzle_row_next(&result)))
204
std::string indexName(row[0]);
205
if (indexName.compare(lastKey) != 0)
208
indexes.push_back(index);
209
index = new DrizzleDumpIndexDrizzle(indexName);
210
index->isPrimary= (strcmp(row[0], "PRIMARY") == 0);
211
index->isUnique= (strcmp(row[3], "YES") == 0);
216
index->columns.push_back(row[1]);
219
indexes.push_back(index);
221
drizzle_result_free(&result);
225
DrizzleDumpData* DrizzleDumpTableDrizzle::getData(void)
227
return new DrizzleDumpDataDrizzle(this);
231
void DrizzleDumpFieldDrizzle::setType(const char* raw_type, const char* raw_collation)
233
collation= raw_collation;
234
if (strcmp(raw_type, "BLOB") == 0)
236
if (strcmp(raw_collation, "binary") != 0)
243
if (strcmp(raw_type, "VARCHAR") == 0)
245
if (strcmp(raw_collation, "binary") != 0)
252
if (strcmp(raw_type, "INTEGER") == 0)
261
DrizzleDumpDataDrizzle::DrizzleDumpDataDrizzle(DrizzleDumpTable *dataTable) :
262
DrizzleDumpData(dataTable)
264
drizzle_return_t ret;
266
query= "SELECT * FROM `";
267
query.append(table->tableName);
269
result= new drizzle_result_st;
271
if (drizzle_query_str(&dcon, result, query.c_str(), &ret) == NULL ||
272
ret != DRIZZLE_RETURN_OK)
274
if (ret == DRIZZLE_RETURN_ERROR_CODE)
276
errmsg << _("Could not get tables list due to error: ") <<
277
drizzle_result_error(result);
278
drizzle_result_free(result);
282
errmsg << _("Could not get tables list due to error: ") <<
283
drizzle_con_error(&dcon);
288
if (drizzle_result_buffer(result) != DRIZZLE_RETURN_OK)
290
errmsg << _("Could not get tables list due to error: ") <<
291
drizzle_con_error(&dcon);
296
DrizzleDumpDataDrizzle::~DrizzleDumpDataDrizzle()
298
drizzle_result_free(result);
299
if (result) delete result;