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"
32
class DrizzleDumpConnection;
33
class DrizzleDumpDatabase;
34
class DrizzleDumpData;
36
class DrizzleDumpIndex
39
DrizzleDumpConnection *dcon;
40
std::string indexName;
42
DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
47
virtual ~DrizzleDumpIndex() { }
53
std::vector<std::string> columns;
54
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
57
class DrizzleDumpField
60
DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
65
virtual ~DrizzleDumpField() { }
66
DrizzleDumpConnection *dcon;
68
std::stringstream errmsg;
70
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
71
std::string fieldName;
80
std::string defaultValue;
81
std::string collation;
84
std::string enumValues;
86
/* For decimal/double */
87
uint32_t decimalPrecision;
88
uint32_t decimalScale;
90
virtual void setType(const char*, const char*) { }
94
class DrizzleDumpTable
97
DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
102
virtual ~DrizzleDumpTable() { }
103
DrizzleDumpConnection *dcon;
105
std::stringstream errmsg;
107
virtual bool populateFields() { return false; }
108
virtual bool populateIndexes() { return false; }
109
virtual DrizzleDumpData* getData() { return NULL; }
110
std::vector<DrizzleDumpField*> fields;
111
std::vector<DrizzleDumpIndex*> indexes;
113
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
114
std::string tableName;
115
std::string displayName;
116
std::string engineName;
119
// Currently MySQL only, hard to do in Drizzle
120
uint64_t autoIncrement;
121
DrizzleDumpDatabase* database;
124
class DrizzleDumpDatabase
127
DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
129
databaseName(database)
131
DrizzleDumpConnection *dcon;
133
virtual ~DrizzleDumpDatabase() { }
135
std::stringstream errmsg;
137
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
139
virtual bool populateTables(void) { return false; }
140
virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
141
virtual void setCollate(const char*) { }
142
void cleanTableName(std::string &tableName);
143
bool ignoreTable(std::string tableName);
144
std::vector<DrizzleDumpTable*> tables;
146
const std::string databaseName;
150
class DrizzleDumpData
153
DrizzleDumpConnection *dcon;
154
std::stringstream errmsg;
155
DrizzleDumpTable *table;
156
drizzle_result_st *result;
157
DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
162
virtual ~DrizzleDumpData() { }
163
friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
165
virtual std::ostream& checkDateTime(std::ostream &os, const char*, uint32_t) const { return os; }
166
std::string convertHex(const unsigned char* from, size_t from_size) const;
167
std::string escape(const char* from, size_t from_size) const;
170
class DrizzleDumpConnection
174
drizzle_con_st connection;
175
std::string hostName;
176
bool drizzleProtocol;
182
SERVER_DRIZZLE_FOUND,
185
DrizzleDumpConnection(std::string &host, uint16_t port,
186
std::string &username, std::string &password, bool drizzle_protocol);
187
~DrizzleDumpConnection();
188
void errorHandler(drizzle_result_st *res, drizzle_return_t ret, const char *when);
189
drizzle_result_st* query(std::string &str_query);
190
bool queryNoResult(std::string &str_query);
192
drizzle_result_st* query(const char* ch_query)
194
std::string str_query(ch_query);
195
return query(str_query);
197
bool queryNoResult(const char* ch_query)
199
std::string str_query(ch_query);
200
return queryNoResult(str_query);
203
void freeResult(drizzle_result_st* result);
204
bool setDB(std::string databaseName);
205
bool usingDrizzleProtocol(void) { return drizzleProtocol; }
206
bool getServerType(void) { return serverType; }
207
const char* getServerVersion(void) { return drizzle_con_server_version(&connection); }
210
class DrizzleStringBuf : public std::streambuf
213
DrizzleStringBuf(int size) :
217
ptr.resize(buffSize);
218
setp(&ptr[0], &ptr.back());
220
virtual ~DrizzleStringBuf()
225
void writeString(std::string &str)
227
connection->queryNoResult(str);
230
void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
233
DrizzleDumpConnection *connection;
236
std::vector<char> ptr;
242
size_t len = size_t(pptr() - pbase());
244
ptr.resize(buffSize*resize);
245
setp(&ptr[0], &ptr.back());
246
/* setp resets current pointer, put it back */
256
size_t len = size_t(pptr() - pbase());
257
std::string temp(pbase(), len);
260
temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
262
if (temp.compare(0, 2, "--") == 0)
265
setp(pbase(), epptr());
267
if (temp.find(";") != std::string::npos)
270
setp(pbase(), epptr());
276
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */