00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef CLIENT_DRIZZLEDUMP_DATA_H
00021 #define CLIENT_DRIZZLEDUMP_DATA_H
00022
00023 #define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
00024 #include "client_priv.h"
00025 #include "server_detect.h"
00026 #include <string>
00027 #include <iostream>
00028 #include <iomanip>
00029 #include <vector>
00030 #include <sstream>
00031 #include <algorithm>
00032
00033 class DrizzleDumpConnection;
00034 class DrizzleDumpDatabase;
00035 class DrizzleDumpData;
00036
00037 class DrizzleDumpForeignKey
00038 {
00039 public:
00040 DrizzleDumpConnection *dcon;
00041 std::string constraintName;
00042
00043 DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
00044 dcon(connection),
00045 constraintName(name)
00046 { }
00047
00048 virtual ~DrizzleDumpForeignKey() { }
00049
00050 std::string parentColumns;
00051 std::string childColumns;
00052 std::string childTable;
00053 std::string matchOption;
00054 std::string deleteRule;
00055 std::string updateRule;
00056
00057 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj);
00058 };
00059
00060 class DrizzleDumpIndex
00061 {
00062 public:
00063 DrizzleDumpConnection *dcon;
00064 std::string indexName;
00065
00066 DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
00067 dcon(connection),
00068 indexName(index),
00069 isPrimary(false),
00070 isUnique(false),
00071 isHash(false)
00072 { }
00073
00074 virtual ~DrizzleDumpIndex() { }
00075
00076 bool isPrimary;
00077 bool isUnique;
00078 bool isHash;
00079
00080
00081 typedef std::pair<std::string,uint32_t> columnData;
00082 std::vector<columnData> columns;
00083 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
00084 };
00085
00086 class DrizzleDumpField
00087 {
00088 public:
00089 DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
00090 dcon(connection),
00091 fieldName(field),
00092 isNull(false),
00093 isUnsigned(false),
00094 isAutoIncrement(false),
00095 defaultIsNull(false),
00096 convertDateTime(false),
00097 rangeCheck(false)
00098 { }
00099
00100 virtual ~DrizzleDumpField() { }
00101 DrizzleDumpConnection *dcon;
00102
00103 std::stringstream errmsg;
00104
00105 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
00106 std::string fieldName;
00107
00108 std::string type;
00109 uint32_t length;
00110 bool isNull;
00111 bool isUnsigned;
00112 bool isAutoIncrement;
00113 bool defaultIsNull;
00114 bool convertDateTime;
00115 bool rangeCheck;
00116 std::string defaultValue;
00117 std::string collation;
00118 std::string comment;
00119
00120
00121 std::string enumValues;
00122
00123
00124 uint32_t decimalPrecision;
00125 uint32_t decimalScale;
00126
00127 virtual void setType(const char*, const char*) { }
00128
00129 };
00130
00131 class DrizzleDumpTable
00132 {
00133 public:
00134 DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
00135 dcon(connection),
00136 tableName(table),
00137 replicate(true),
00138 database(NULL)
00139 { }
00140
00141 virtual ~DrizzleDumpTable() { }
00142 DrizzleDumpConnection *dcon;
00143
00144 std::stringstream errmsg;
00145
00146 virtual bool populateFields() { return false; }
00147 virtual bool populateIndexes() { return false; }
00148 virtual bool populateFkeys() { return false; }
00149 virtual DrizzleDumpData* getData() { return NULL; }
00150 std::vector<DrizzleDumpField*> fields;
00151 std::vector<DrizzleDumpIndex*> indexes;
00152 std::vector<DrizzleDumpForeignKey*> fkeys;
00153
00154 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
00155 std::string tableName;
00156 std::string displayName;
00157 std::string engineName;
00158 std::string collate;
00159 std::string comment;
00160 bool replicate;
00161
00162
00163 uint64_t autoIncrement;
00164 DrizzleDumpDatabase* database;
00165 };
00166
00167 class DrizzleDumpDatabase
00168 {
00169 public:
00170 DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
00171 dcon(connection),
00172 databaseName(database)
00173 { }
00174 DrizzleDumpConnection *dcon;
00175
00176 virtual ~DrizzleDumpDatabase() { }
00177
00178 std::stringstream errmsg;
00179
00180 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
00181
00182 virtual bool populateTables(void) { return false; }
00183 virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
00184 virtual void setCollate(const char*) { }
00185 void cleanTableName(std::string &tableName);
00186 bool ignoreTable(std::string tableName);
00187 std::vector<DrizzleDumpTable*> tables;
00188
00189 const std::string databaseName;
00190 std::string collate;
00191 };
00192
00193 class DrizzleDumpData
00194 {
00195 public:
00196 DrizzleDumpConnection *dcon;
00197 std::stringstream errmsg;
00198 DrizzleDumpTable *table;
00199 drizzle_result_st *result;
00200 DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
00201 dcon(connection),
00202 table(dataTable),
00203 result(NULL)
00204 { }
00205
00206 virtual ~DrizzleDumpData() { }
00207 friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
00208
00209 virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
00210 std::string convertHex(const unsigned char* from, size_t from_size) const;
00211 static std::string escape(const char* from, size_t from_size);
00212 };
00213
00214 class DrizzleDumpConnection
00215 {
00216 private:
00217 drizzle_st drizzle;
00218 drizzle_con_st connection;
00219 std::string hostName;
00220 bool drizzleProtocol;
00221 ServerDetect::server_type serverType;
00222 std::string serverVersion;
00223
00224 public:
00225 DrizzleDumpConnection(std::string &host, uint16_t port,
00226 std::string &username, std::string &password, bool drizzle_protocol);
00227 ~DrizzleDumpConnection();
00228 void errorHandler(drizzle_result_st *res, drizzle_return_t ret, const char *when);
00229 drizzle_result_st* query(std::string &str_query);
00230 bool queryNoResult(std::string &str_query);
00231
00232 drizzle_result_st* query(const char* ch_query)
00233 {
00234 std::string str_query(ch_query);
00235 return query(str_query);
00236 }
00237 bool queryNoResult(const char* ch_query)
00238 {
00239 std::string str_query(ch_query);
00240 return queryNoResult(str_query);
00241 }
00242
00243 void freeResult(drizzle_result_st* result);
00244 bool setDB(std::string databaseName);
00245 bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
00246 bool getServerType(void) const { return serverType; }
00247 std::string getServerVersion(void) const { return serverVersion; }
00248 };
00249
00250 class DrizzleStringBuf : public std::streambuf
00251 {
00252 public:
00253 DrizzleStringBuf(int size) :
00254 buffSize(size)
00255 {
00256 resize= 1;
00257 ptr.resize(buffSize);
00258 setp(&ptr[0], &ptr.back());
00259 }
00260 virtual ~DrizzleStringBuf()
00261 {
00262 sync();
00263 }
00264
00265 void writeString(std::string &str)
00266 {
00267 if (not connection->queryNoResult(str))
00268 throw std::exception();
00269 }
00270
00271 void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
00272
00273 private:
00274 DrizzleDumpConnection *connection;
00275 size_t buffSize;
00276 uint32_t resize;
00277 std::vector<char> ptr;
00278
00279 int overflow(int c)
00280 {
00281 if (c != EOF)
00282 {
00283 size_t len = size_t(pptr() - pbase());
00284 resize++;
00285 ptr.resize(buffSize*resize);
00286 setp(&ptr[0], &ptr.back());
00287
00288 pbump(len);
00289 sputc(c);
00290 }
00291
00292 return 0;
00293 }
00294
00295 int sync()
00296 {
00297 size_t len = size_t(pptr() - pbase());
00298 std::string temp(pbase(), len);
00299
00300
00301 temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
00302
00303 if (temp.compare(0, 2, "--") == 0)
00304 {
00305
00306 setp(pbase(), epptr());
00307 }
00308 if (temp.find(";") != std::string::npos)
00309 {
00310 writeString(temp);
00311 setp(pbase(), epptr());
00312 }
00313 return 0;
00314 }
00315 };
00316
00317 #endif