~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"
25
#include <string>
26
#include <iostream>
27
#include <iomanip>
28
#include <vector>
29
#include <sstream>
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
30
#include <algorithm>
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
31
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
32
class DrizzleDumpConnection;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
33
class DrizzleDumpDatabase;
34
class DrizzleDumpData;
35
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
36
class DrizzleDumpForeignKey
37
{
38
  public:
39
    DrizzleDumpConnection *dcon;
40
    std::string constraintName;
41
42
    DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
43
      dcon(connection),
44
      constraintName(name)
45
    { }
46
47
    virtual ~DrizzleDumpForeignKey() { }
48
49
    std::string parentColumns;
50
    std::string childColumns;
51
    std::string childTable;
52
    std::string matchOption;
53
    std::string deleteRule;
54
    std::string updateRule;
55
56
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj);
57
};
58
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
59
class DrizzleDumpIndex
60
{
61
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
62
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
63
    std::string indexName;
64
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
65
    DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
66
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
67
      indexName(index),
68
      isPrimary(false),
69
      isUnique(false),
70
      isHash(false)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
71
    { }
72
73
    virtual ~DrizzleDumpIndex() { }
74
75
    bool isPrimary;
76
    bool isUnique;
77
    bool isHash;
1802.2.1 by Andrew Hutchings
Fix index lengths not shown for part-indexed columns.
78
    uint32_t length;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
79
80
    std::vector<std::string> columns;
81
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
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
103
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
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),
135
      database(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
136
    { }
137
138
    virtual ~DrizzleDumpTable() { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
139
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
140
141
    std::stringstream errmsg;
142
143
    virtual bool populateFields() { return false; }
144
    virtual bool populateIndexes() { return false; }
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
145
    virtual bool populateFkeys() { return false; }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
146
    virtual DrizzleDumpData* getData() { return NULL; }
147
    std::vector<DrizzleDumpField*> fields;
148
    std::vector<DrizzleDumpIndex*> indexes;
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
149
    std::vector<DrizzleDumpForeignKey*> fkeys;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
150
151
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
152
    std::string tableName;
1751.4.25 by Andrew Hutchings
Fix error handling
153
    std::string displayName;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
154
    std::string engineName;
155
    std::string collate;
1810.1.1 by Andrew Hutchings
Add table comments
156
    std::string comment;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
157
158
    // Currently MySQL only, hard to do in Drizzle
159
    uint64_t autoIncrement;
160
    DrizzleDumpDatabase* database;
161
};
162
163
class DrizzleDumpDatabase
164
{
165
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
166
    DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
167
      dcon(connection),
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
168
      databaseName(database)
169
    { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
170
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
171
172
    virtual ~DrizzleDumpDatabase() { }
173
174
    std::stringstream errmsg;
175
176
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
177
1751.4.23 by Andrew Hutchings
Fix various bugs
178
    virtual bool populateTables(void) { return false; }
179
    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
180
    virtual void setCollate(const char*) { }
1751.4.25 by Andrew Hutchings
Fix error handling
181
    void cleanTableName(std::string &tableName);
1751.4.24 by Andrew Hutchings
Fix ignore tables
182
    bool ignoreTable(std::string tableName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
183
    std::vector<DrizzleDumpTable*> tables;
184
185
    const std::string databaseName;
186
    std::string collate;
187
};
188
189
class DrizzleDumpData
190
{
191
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
192
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
193
    std::stringstream errmsg;
194
    DrizzleDumpTable *table;
195
    drizzle_result_st *result;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
196
    DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
197
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
198
      table(dataTable),
199
      result(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
200
    { }
201
202
    virtual ~DrizzleDumpData() { }
203
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
204
1802.11.1 by Andrew Hutchings
1. date regex was broken
205
    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
206
    std::string convertHex(const unsigned char* from, size_t from_size) const;
1810.1.1 by Andrew Hutchings
Add table comments
207
    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
208
};
209
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
210
class DrizzleDumpConnection
211
{
212
  private:
213
    drizzle_st drizzle;
214
    drizzle_con_st connection;
215
    std::string hostName;
216
    bool drizzleProtocol;
217
    int serverType;
218
219
  public:
220
    enum server_type {
221
      SERVER_MYSQL_FOUND,
222
      SERVER_DRIZZLE_FOUND,
223
      SERVER_UNKNOWN_FOUND
224
    };
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; }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
247
    const char* getServerVersion(void) { return drizzle_con_server_version(&connection); }
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 */