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
#ifndef CLIENT_DRIZZLEDUMP_DATA_H
21
#define CLIENT_DRIZZLEDUMP_DATA_H
23
#define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
24
#include "client_priv.h"
25
#include "server_detect.h"
33
class DrizzleDumpConnection;
34
class DrizzleDumpDatabase;
35
class DrizzleDumpData;
37
class DrizzleDumpForeignKey
40
DrizzleDumpConnection *dcon;
41
std::string constraintName;
43
DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
48
virtual ~DrizzleDumpForeignKey() { }
50
std::string parentColumns;
51
std::string childColumns;
52
std::string childTable;
53
std::string matchOption;
54
std::string deleteRule;
55
std::string updateRule;
57
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj);
60
class DrizzleDumpIndex
63
DrizzleDumpConnection *dcon;
64
std::string indexName;
66
DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
74
virtual ~DrizzleDumpIndex() { }
81
std::vector<std::string> columns;
82
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
85
class DrizzleDumpField
88
DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
93
isAutoIncrement(false),
95
convertDateTime(false),
99
virtual ~DrizzleDumpField() { }
100
DrizzleDumpConnection *dcon;
102
std::stringstream errmsg;
104
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
105
std::string fieldName;
111
bool isAutoIncrement;
113
bool convertDateTime;
115
std::string defaultValue;
116
std::string collation;
120
std::string enumValues;
122
/* For decimal/double */
123
uint32_t decimalPrecision;
124
uint32_t decimalScale;
126
virtual void setType(const char*, const char*) { }
130
class DrizzleDumpTable
133
DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
139
virtual ~DrizzleDumpTable() { }
140
DrizzleDumpConnection *dcon;
142
std::stringstream errmsg;
144
virtual bool populateFields() { return false; }
145
virtual bool populateIndexes() { return false; }
146
virtual bool populateFkeys() { return false; }
147
virtual DrizzleDumpData* getData() { return NULL; }
148
std::vector<DrizzleDumpField*> fields;
149
std::vector<DrizzleDumpIndex*> indexes;
150
std::vector<DrizzleDumpForeignKey*> fkeys;
152
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
153
std::string tableName;
154
std::string displayName;
155
std::string engineName;
159
// Currently MySQL only, hard to do in Drizzle
160
uint64_t autoIncrement;
161
DrizzleDumpDatabase* database;
164
class DrizzleDumpDatabase
167
DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
169
databaseName(database)
171
DrizzleDumpConnection *dcon;
173
virtual ~DrizzleDumpDatabase() { }
175
std::stringstream errmsg;
177
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
179
virtual bool populateTables(void) { return false; }
180
virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
181
virtual void setCollate(const char*) { }
182
void cleanTableName(std::string &tableName);
183
bool ignoreTable(std::string tableName);
184
std::vector<DrizzleDumpTable*> tables;
186
const std::string databaseName;
190
class DrizzleDumpData
193
DrizzleDumpConnection *dcon;
194
std::stringstream errmsg;
195
DrizzleDumpTable *table;
196
drizzle_result_st *result;
197
DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
203
virtual ~DrizzleDumpData() { }
204
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
206
virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
207
std::string convertHex(const unsigned char* from, size_t from_size) const;
208
static std::string escape(const char* from, size_t from_size);
211
class DrizzleDumpConnection
215
drizzle_con_st connection;
216
std::string hostName;
217
bool drizzleProtocol;
218
ServerDetect::server_type serverType;
219
std::string serverVersion;
222
DrizzleDumpConnection(std::string &host, uint16_t port,
223
std::string &username, std::string &password, bool drizzle_protocol);
224
~DrizzleDumpConnection();
225
void errorHandler(drizzle_result_st *res, drizzle_return_t ret, const char *when);
226
drizzle_result_st* query(std::string &str_query);
227
bool queryNoResult(std::string &str_query);
229
drizzle_result_st* query(const char* ch_query)
231
std::string str_query(ch_query);
232
return query(str_query);
234
bool queryNoResult(const char* ch_query)
236
std::string str_query(ch_query);
237
return queryNoResult(str_query);
240
void freeResult(drizzle_result_st* result);
241
bool setDB(std::string databaseName);
242
bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
243
bool getServerType(void) const { return serverType; }
244
std::string getServerVersion(void) const { return serverVersion; }
247
class DrizzleStringBuf : public std::streambuf
250
DrizzleStringBuf(int size) :
254
ptr.resize(buffSize);
255
setp(&ptr[0], &ptr.back());
257
virtual ~DrizzleStringBuf()
262
void writeString(std::string &str)
264
if (not connection->queryNoResult(str))
265
throw std::exception();
268
void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
271
DrizzleDumpConnection *connection;
274
std::vector<char> ptr;
280
size_t len = size_t(pptr() - pbase());
282
ptr.resize(buffSize*resize);
283
setp(&ptr[0], &ptr.back());
284
/* setp resets current pointer, put it back */
294
size_t len = size_t(pptr() - pbase());
295
std::string temp(pbase(), len);
298
temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
300
if (temp.compare(0, 2, "--") == 0)
303
setp(pbase(), epptr());
305
if (temp.find(";") != std::string::npos)
308
setp(pbase(), epptr());
314
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */