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 "client_priv.h"
22
#include <drizzled/gettext.h>
26
extern bool opt_no_create_info;
27
extern bool opt_no_data;
28
extern bool opt_create_db;
29
extern bool opt_disable_keys;
30
extern bool extended_insert;
31
extern bool opt_replace_into;
33
extern uint32_t show_progress_size;
34
extern int connected_server_type;
42
std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj)
46
os << " PRIMARY KEY ";
48
else if (obj.isUnique)
50
os << " UNIQUE KEY `" << obj.indexName << "` ";
54
os << " KEY `" << obj.indexName << "` ";
59
std::vector<std::string>::iterator i;
60
std::vector<std::string> fields = obj.columns;
61
for (i= fields.begin(); i != fields.end(); ++i)
63
if (i != fields.begin())
65
std::string field= *i;
66
os << "`" << field << "`";
74
std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj)
76
os << " `" << obj.fieldName << "` ";
78
if (((obj.type.compare("VARCHAR") == 0) or
79
(obj.type.compare("VARBINARY") == 0)) and
82
os << "(" << obj.length << ")";
84
else if ((obj.type.compare("DECIMAL") == 0) or
85
(obj.type.compare("DOUBLE") == 0))
87
os << "(" << obj.decimalPrecision << "," << obj.decimalScale << ")";
89
else if (obj.type.compare("ENUM") == 0)
91
os << "(" << obj.enumValues << ")";
99
if ((not obj.collation.empty()) and (obj.collation.compare("binary") != 0))
101
os << " COLLATE " << obj.collation;
104
if (obj.isAutoIncrement)
105
os << " AUTO_INCREMENT";
107
if (not obj.defaultValue.empty())
109
if (obj.defaultValue.compare("CURRENT_TIMESTAMP") != 0)
110
os << " DEFAULT '" << obj.defaultValue << "'";
112
os << " DEFAULT CURRENT_TIMESTAMP";
114
else if ((obj.collation.empty()) and (obj.defaultIsNull))
116
os << " DEFAULT NULL";
122
std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj)
124
os << "--" << std::endl
125
<< "-- Current Database: `" << obj.databaseName << "`" << std::endl
126
<< "--" << std::endl << std::endl;
128
/* Love that this variable is the opposite of its name */
129
if (not opt_create_db)
131
os << "CREATE DATABASE IF NOT EXISTS `" << obj.databaseName
132
<< "` COLLATE = " << obj.collate << ";" << std::endl << std::endl;
135
os << "USE `" << obj.databaseName << "`;" << std::endl << std::endl;
137
std::vector<DrizzleDumpTable*>::iterator i;
138
std::vector<DrizzleDumpTable*> output_tables = obj.tables;
139
for (i= output_tables.begin(); i != output_tables.end(); ++i)
141
DrizzleDumpTable *table= *i;
142
if (not opt_no_create_info)
146
DrizzleDumpData *data= table->getData();
156
std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj)
158
bool new_insert= true;
164
if (drizzle_result_row_count(obj.result) < 1)
166
os << "--" << std::endl
167
<< "-- No data to dump for table `" << obj.table->tableName << "`" << std::endl
168
<< "--" << std::endl << std::endl;
173
os << "--" << std::endl
174
<< "-- Dumping data for table `" << obj.table->tableName << "`" << std::endl
175
<< "--" << std::endl << std::endl;
177
if (opt_disable_keys)
178
os << "ALTER TABLE `" << obj.table->tableName << "` DISABLE KEYS;" << std::endl;
180
std::streampos out_position= os.tellp();
182
while((row= drizzle_row_next(obj.result)))
185
if ((rownr % show_progress_size) == 0)
187
std::cerr << "-- %" << rownr << _(" rows dumped for table ") << obj.table->tableName << std::endl;
190
size_t* row_sizes= drizzle_row_field_sizes(obj.result);
196
os << ");" << std::endl;
203
if (opt_replace_into)
207
os << "INTO `" << obj.table->tableName << "` VALUES (";
211
for (uint32_t i= 0; i < drizzle_result_column_count(obj.result); i++)
217
/* time/date conversion for MySQL connections */
218
else if (obj.table->fields[i]->convertDateTime)
220
os << obj.checkDateTime(os, row[i], i);
224
if (obj.table->fields[i]->type.compare("INT") != 0)
226
/* Hex blob processing or escape text */
227
if (((obj.table->fields[i]->type.compare("BLOB") == 0) or
228
(obj.table->fields[i]->type.compare("VARBINARY") == 0)))
229
os << obj.convertHex(row[i], row_sizes[i]);
231
os << "'" << obj.escape(row[i], row_sizes[i]) << "'";
236
if (i != obj.table->fields.size() - 1)
239
/* Break insert up if it is too long */
240
if (extended_insert and
241
((os.tellp() - out_position) >= DRIZZLE_MAX_LINE_LENGTH))
243
os << ");" << std::endl;
245
out_position= os.tellp();
248
os << ");" << std::endl;
250
if (opt_disable_keys)
251
os << "ALTER TABLE `" << obj.table->tableName << "` ENABLE KEYS;" << std::endl;
258
std::string DrizzleDumpData::convertHex(const char* from, size_t from_size) const
260
std::ostringstream output;
263
while (from_size > 0)
265
output << std::uppercase << std::hex << std::setw(2) << std::setfill('0') << int(*from);
273
/* Ripped out of libdrizzle, hopefully a little safer */
274
std::string DrizzleDumpData::escape(const char* from, size_t from_size) const
278
while (from_size > 0)
291
output.push_back('\\');
296
output.push_back(*from);
304
std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj)
306
os << "--" << std::endl
307
<< "-- Table structure for table `" << obj.tableName << "`" << std::endl
308
<< "--" << std::endl << std::endl;
311
os << "DROP TABLE IF EXISTS `" << obj.tableName << "`;" << std::endl;
313
os << "CREATE TABLE `" << obj.tableName << "` (" << std::endl;
314
std::vector<DrizzleDumpField*>::iterator i;
315
std::vector<DrizzleDumpField*> output_fields = obj.fields;
316
for (i= output_fields.begin(); i != output_fields.end(); ++i)
318
if (i != output_fields.begin())
319
os << "," << std::endl;
320
DrizzleDumpField *field= *i;
324
std::vector<DrizzleDumpIndex*>::iterator j;
325
std::vector<DrizzleDumpIndex*> output_indexes = obj.indexes;
326
for (j= output_indexes.begin(); j != output_indexes.end(); ++j)
328
os << "," << std::endl;;
329
DrizzleDumpIndex *index= *j;
333
os << ") ENGINE=" << obj.engineName << " ";
334
if ((connected_server_type == SERVER_MYSQL_FOUND) and (obj.autoIncrement > 0))
335
os << "AUTO_INCREMENT=" << obj.autoIncrement << " ";
337
os << "COLLATE = " << obj.collate << ";" << std::endl << std::endl;