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