Drizzled Public API Documentation

drizzledump_data.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Andrew Hutchings
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #pragma once
00021 
00022 #define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
00023 #include "client_priv.h"
00024 #include "server_detect.h"
00025 #include <cstdio>
00026 #include <string>
00027 #include <iosfwd>
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     std::string parentColumns;
00049     std::string childColumns;
00050     std::string childTable;
00051     std::string matchOption;
00052     std::string deleteRule;
00053     std::string updateRule;
00054 
00055     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpForeignKey &obj);
00056 };
00057 
00058 class DrizzleDumpIndex
00059 {
00060   public:
00061     DrizzleDumpConnection *dcon;
00062     std::string indexName;
00063 
00064     DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
00065       dcon(connection),
00066       indexName(index),
00067       isPrimary(false),
00068       isUnique(false),
00069       isHash(false)
00070     { }
00071 
00072     virtual ~DrizzleDumpIndex() { }
00073 
00074     bool isPrimary;
00075     bool isUnique;
00076     bool isHash;
00077 
00078     /* Stores the column name and the length of the index part */
00079     typedef std::pair<std::string,uint32_t> columnData;
00080     std::vector<columnData> columns;
00081     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpIndex &obj);
00082 };
00083 
00084 class DrizzleDumpField
00085 {
00086   public:
00087     DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
00088       dcon(connection),
00089       fieldName(field),
00090       isNull(false),
00091       isUnsigned(false),
00092       isAutoIncrement(false),
00093       defaultIsNull(false),
00094       convertDateTime(false),
00095       rangeCheck(false)
00096     { }
00097 
00098     virtual ~DrizzleDumpField() { }
00099     DrizzleDumpConnection *dcon;
00100 
00101     std::stringstream errmsg;
00102 
00103     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpField &obj);
00104     std::string fieldName;
00105 
00106     std::string type;
00107     uint32_t length;
00108     bool isNull;
00109     bool isUnsigned;
00110     bool isAutoIncrement;
00111     bool defaultIsNull;
00112     bool convertDateTime;
00113     bool rangeCheck;
00114     std::string defaultValue;
00115     std::string collation;
00116     std::string comment;
00117 
00118     /* For enum type */
00119     std::string enumValues;
00120 
00121     /* For decimal/double */
00122     uint32_t decimalPrecision;
00123     uint32_t decimalScale;
00124 
00125     virtual void setType(const char*, const char*) { }
00126 
00127 };
00128 
00129 class DrizzleDumpTable
00130 {
00131   public:
00132     DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
00133       dcon(connection),
00134       tableName(table),
00135       replicate(true),
00136       database(NULL)
00137     { }
00138 
00139     virtual ~DrizzleDumpTable() { }
00140     DrizzleDumpConnection *dcon;
00141 
00142     std::stringstream errmsg;
00143 
00144     virtual bool populateFields() { return false; }
00145     virtual bool populateIndexes() { return false; }
00146     virtual bool populateFkeys() { return false; }
00147     virtual DrizzleDumpData* getData() { return NULL; }
00148     std::vector<DrizzleDumpField*> fields;
00149     std::vector<DrizzleDumpIndex*> indexes;
00150     std::vector<DrizzleDumpForeignKey*> fkeys;
00151 
00152     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpTable &obj);
00153     std::string tableName;
00154     std::string displayName;
00155     std::string engineName;
00156     std::string collate;
00157     std::string comment;
00158     bool replicate;
00159 
00160     // Currently MySQL only, hard to do in Drizzle
00161     uint64_t autoIncrement;
00162     DrizzleDumpDatabase* database;
00163 };
00164 
00165 class DrizzleDumpDatabase
00166 {
00167   public:
00168     DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
00169       dcon(connection),
00170       databaseName(database)
00171     { }
00172     DrizzleDumpConnection *dcon;
00173 
00174     virtual ~DrizzleDumpDatabase() { }
00175 
00176     std::stringstream errmsg;
00177 
00178     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpDatabase &obj);
00179 
00180     virtual bool populateTables(void) { return false; }
00181     virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
00182     virtual void setCollate(const char*) { }
00183     void cleanTableName(std::string &tableName);
00184     bool ignoreTable(std::string tableName);
00185     std::vector<DrizzleDumpTable*> tables;
00186 
00187     const std::string databaseName;
00188     std::string collate;
00189 };
00190 
00191 class DrizzleDumpData
00192 {
00193   public:
00194     DrizzleDumpConnection *dcon;
00195     std::stringstream errmsg;
00196     DrizzleDumpTable *table;
00197     drizzle_result_st *result;
00198     DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
00199       dcon(connection),
00200       table(dataTable),
00201       result(NULL)
00202     { }
00203 
00204     virtual ~DrizzleDumpData() { }
00205     friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpData &obj);
00206 
00207     virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
00208     std::string convertHex(const unsigned char* from, size_t from_size) const;
00209     static std::string escape(const char* from, size_t from_size);
00210 };
00211 
00212 class DrizzleDumpConnection
00213 {
00214   private:
00215     drizzle_st *drizzle;
00216     drizzle_con_st *connection;
00217     std::string hostName;
00218     bool drizzleProtocol;
00219     ServerDetect::server_type serverType;
00220     std::string serverVersion;
00221 
00222   public:
00223     DrizzleDumpConnection(std::string &host,
00224                           uint16_t port,
00225                           std::string &username,
00226                           std::string &password,
00227                           bool drizzle_protocol);
00228 
00229     ~DrizzleDumpConnection();
00230 
00231     void errorHandler(drizzle_result_st *res,  drizzle_return_t ret, const char *when);
00232     drizzle_result_st* query(std::string &str_query);
00233     bool queryNoResult(std::string &str_query);
00234 
00235     drizzle_result_st* query(const char* ch_query)
00236     {
00237       std::string str_query(ch_query);
00238       return query(str_query);
00239     }
00240 
00241     bool queryNoResult(const char* ch_query)
00242     {
00243       std::string str_query(ch_query);
00244       return queryNoResult(str_query);
00245     }
00246 
00247     void freeResult(drizzle_result_st* result);
00248     bool setDB(std::string databaseName);
00249     bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
00250     bool getServerType(void) const { return serverType; }
00251     std::string getServerVersion(void) const { return serverVersion; }
00252 };
00253 
00254 class DrizzleStringBuf : public std::streambuf
00255 {
00256   public:
00257     DrizzleStringBuf(int size) :
00258       buffSize(size)
00259     {
00260       resize= 1;
00261       ptr.resize(buffSize);
00262       setp(&ptr[0], &ptr.back());
00263     }
00264     virtual ~DrizzleStringBuf() 
00265     {
00266         sync();
00267     }
00268 
00269     void writeString(std::string &str)
00270     {
00271       if (not connection->queryNoResult(str))
00272       {
00273         throw std::exception();
00274       }
00275     }
00276 
00277     void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
00278 
00279   private:
00280     DrizzleDumpConnection *connection;
00281     size_t buffSize;
00282     uint32_t resize;
00283     std::vector<char> ptr;
00284 
00285     int overflow(int c)
00286     {
00287         if (c != EOF)
00288         {
00289           size_t len = size_t(pptr() - pbase());
00290           resize++;
00291           ptr.resize(buffSize*resize);
00292           setp(&ptr[0], &ptr.back());
00293           /* setp resets current pointer, put it back */
00294           pbump(len);
00295           sputc(c);
00296         }
00297 
00298         return 0;
00299     }
00300 
00301     int sync()
00302     {
00303       size_t len = size_t(pptr() - pbase());
00304       std::string temp(pbase(), len);
00305 
00306       /* Drop newlines */
00307       temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
00308 
00309       if (temp.compare(0, 2, "--") == 0)
00310       {
00311         /* Drop comments */
00312         setp(pbase(), epptr());
00313       }
00314       if (temp.find(";") != std::string::npos)
00315       {
00316         writeString(temp);
00317         setp(pbase(), epptr());
00318       }
00319       return 0;
00320     }
00321 };