~drizzle-trunk/drizzle/development

1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Andrew Hutchings
5
 *
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.
9
 *
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.
14
 *
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
18
 */
19
20
#ifndef CLIENT_DRIZZLEDUMP_DATA_H
21
#define CLIENT_DRIZZLEDUMP_DATA_H
22
23
#define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
24
#include "client_priv.h"
2187.3.2 by Andrew Hutchings
Separate the server detection functions into a .h file
25
#include "server_detect.h"
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
26
#include <string>
27
#include <iostream>
28
#include <iomanip>
29
#include <vector>
30
#include <sstream>
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
31
#include <algorithm>
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
32
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
33
class DrizzleDumpConnection;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
34
class DrizzleDumpDatabase;
35
class DrizzleDumpData;
36
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
37
class DrizzleDumpForeignKey
38
{
39
  public:
40
    DrizzleDumpConnection *dcon;
41
    std::string constraintName;
42
43
    DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
44
      dcon(connection),
45
      constraintName(name)
46
    { }
47
48
    virtual ~DrizzleDumpForeignKey() { }
49
50
    std::string parentColumns;
51
    std::string childColumns;
52
    std::string childTable;
53
    std::string matchOption;
54
    std::string deleteRule;
55
    std::string updateRule;
56
57
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj);
58
};
59
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
60
class DrizzleDumpIndex
61
{
62
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
63
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
64
    std::string indexName;
65
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
66
    DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
67
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
68
      indexName(index),
69
      isPrimary(false),
70
      isUnique(false),
71
      isHash(false)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
72
    { }
73
74
    virtual ~DrizzleDumpIndex() { }
75
76
    bool isPrimary;
77
    bool isUnique;
78
    bool isHash;
79
2228.3.1 by Andrew Hutchings
Fix multi-part index length processing in drizzledump
80
    /* Stores the column name and the length of the index part */
81
    typedef std::pair<std::string,uint32_t> columnData;
82
    std::vector<columnData> columns;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
83
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
84
};
85
86
class DrizzleDumpField
87
{
88
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
89
    DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
90
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
91
      fieldName(field),
92
      isNull(false),
93
      isUnsigned(false),
94
      isAutoIncrement(false),
95
      defaultIsNull(false),
1971.5.8 by Andrew Hutchings
Fix uninitialized bool
96
      convertDateTime(false),
97
      rangeCheck(false)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
98
    { }
99
100
    virtual ~DrizzleDumpField() { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
101
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
102
103
    std::stringstream errmsg;
104
105
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
106
    std::string fieldName;
107
108
    std::string type;
109
    uint32_t length;
110
    bool isNull;
111
    bool isUnsigned;
112
    bool isAutoIncrement;
113
    bool defaultIsNull;
114
    bool convertDateTime;
1971.5.6 by Andrew Hutchings
Make BIGINT range check only happen on MySQL BIGINT UNSIGNED as it is tripping up on negative BIGINT values.
115
    bool rangeCheck;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
116
    std::string defaultValue;
117
    std::string collation;
1976.4.3 by Andrew Hutchings
Add field level comments back in to drizzledump
118
    std::string comment;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
119
120
    /* For enum type */
121
    std::string enumValues;
122
123
    /* For decimal/double */
124
    uint32_t decimalPrecision;
125
    uint32_t decimalScale;
126
127
    virtual void setType(const char*, const char*) { }
128
129
};
130
131
class DrizzleDumpTable
132
{
133
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
134
    DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
135
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
136
      tableName(table),
2228.2.1 by Andrew Hutchings
Add REPLICATE=FALSE support to drizzledump
137
      replicate(true),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
138
      database(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
139
    { }
140
141
    virtual ~DrizzleDumpTable() { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
142
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
143
144
    std::stringstream errmsg;
145
146
    virtual bool populateFields() { return false; }
147
    virtual bool populateIndexes() { return false; }
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
148
    virtual bool populateFkeys() { return false; }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
149
    virtual DrizzleDumpData* getData() { return NULL; }
150
    std::vector<DrizzleDumpField*> fields;
151
    std::vector<DrizzleDumpIndex*> indexes;
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
152
    std::vector<DrizzleDumpForeignKey*> fkeys;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
153
154
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
155
    std::string tableName;
1751.4.25 by Andrew Hutchings
Fix error handling
156
    std::string displayName;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
157
    std::string engineName;
158
    std::string collate;
1810.1.1 by Andrew Hutchings
Add table comments
159
    std::string comment;
2228.2.1 by Andrew Hutchings
Add REPLICATE=FALSE support to drizzledump
160
    bool replicate;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
161
162
    // Currently MySQL only, hard to do in Drizzle
163
    uint64_t autoIncrement;
164
    DrizzleDumpDatabase* database;
165
};
166
167
class DrizzleDumpDatabase
168
{
169
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
170
    DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
171
      dcon(connection),
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
172
      databaseName(database)
173
    { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
174
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
175
176
    virtual ~DrizzleDumpDatabase() { }
177
178
    std::stringstream errmsg;
179
180
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
181
1751.4.23 by Andrew Hutchings
Fix various bugs
182
    virtual bool populateTables(void) { return false; }
183
    virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
184
    virtual void setCollate(const char*) { }
1751.4.25 by Andrew Hutchings
Fix error handling
185
    void cleanTableName(std::string &tableName);
1751.4.24 by Andrew Hutchings
Fix ignore tables
186
    bool ignoreTable(std::string tableName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
187
    std::vector<DrizzleDumpTable*> tables;
188
189
    const std::string databaseName;
190
    std::string collate;
191
};
192
193
class DrizzleDumpData
194
{
195
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
196
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
197
    std::stringstream errmsg;
198
    DrizzleDumpTable *table;
199
    drizzle_result_st *result;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
200
    DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
201
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
202
      table(dataTable),
203
      result(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
204
    { }
205
206
    virtual ~DrizzleDumpData() { }
207
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
208
1802.11.1 by Andrew Hutchings
1. date regex was broken
209
    virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
1751.4.26 by Andrew Hutchings
Fix up for the drizzledump test cases
210
    std::string convertHex(const unsigned char* from, size_t from_size) const;
1810.1.1 by Andrew Hutchings
Add table comments
211
    static std::string escape(const char* from, size_t from_size);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
212
};
213
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
214
class DrizzleDumpConnection
215
{
216
  private:
217
    drizzle_st drizzle;
218
    drizzle_con_st connection;
219
    std::string hostName;
220
    bool drizzleProtocol;
2187.3.2 by Andrew Hutchings
Separate the server detection functions into a .h file
221
    ServerDetect::server_type serverType;
222
    std::string serverVersion;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
223
224
  public:
225
    DrizzleDumpConnection(std::string &host, uint16_t port,
226
      std::string &username, std::string &password, bool drizzle_protocol);
227
    ~DrizzleDumpConnection();
228
    void errorHandler(drizzle_result_st *res,  drizzle_return_t ret, const char *when);
229
    drizzle_result_st* query(std::string &str_query);
230
    bool queryNoResult(std::string &str_query);
231
232
    drizzle_result_st* query(const char* ch_query)
233
    {
234
      std::string str_query(ch_query);
235
      return query(str_query);
236
    }
237
    bool queryNoResult(const char* ch_query)
238
    {
239
      std::string str_query(ch_query);
240
      return queryNoResult(str_query);
241
    }
242
243
    void freeResult(drizzle_result_st* result);
244
    bool setDB(std::string databaseName);
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
245
    bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
246
    bool getServerType(void) const { return serverType; }
2187.3.2 by Andrew Hutchings
Separate the server detection functions into a .h file
247
    std::string getServerVersion(void) const { return serverVersion; }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
248
};
249
250
class DrizzleStringBuf : public std::streambuf
251
{
252
  public:
253
    DrizzleStringBuf(int size) :
254
      buffSize(size)
255
    {
256
      resize= 1;
257
      ptr.resize(buffSize);
258
      setp(&ptr[0], &ptr.back());
259
    }
260
    virtual ~DrizzleStringBuf() 
261
    {
262
        sync();
263
    }
264
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
265
    void writeString(std::string &str)
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
266
    {
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
267
      if (not connection->queryNoResult(str))
1966.3.1 by Monty Taylor
Use std::exception instead of catch(...)
268
        throw std::exception();
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
269
    }
270
271
    void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
272
273
  private:
274
    DrizzleDumpConnection *connection;
275
    size_t buffSize;
276
    uint32_t resize;
277
    std::vector<char> ptr;
278
279
    int	overflow(int c)
280
    {
281
        if (c != EOF)
282
        {
283
          size_t len = size_t(pptr() - pbase());
284
          resize++;
285
          ptr.resize(buffSize*resize);
286
          setp(&ptr[0], &ptr.back());
287
          /* setp resets current pointer, put it back */
288
          pbump(len);
289
          sputc(c);
290
        }
291
292
        return 0;
293
    }
294
295
    int	sync()
296
    {
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
297
      size_t len = size_t(pptr() - pbase());
298
      std::string temp(pbase(), len);
299
300
      /* Drop newlines */
301
      temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
302
303
      if (temp.compare(0, 2, "--") == 0)
304
      {
305
        /* Drop comments */
306
        setp(pbase(), epptr());
307
      }
308
      if (temp.find(";") != std::string::npos)
309
      {
310
        writeString(temp);
311
        setp(pbase(), epptr());
312
      }
313
      return 0;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
314
    }
315
};
316
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
317
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */