~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),
94
      convertDateTime(false)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
95
    { }
96
97
    virtual ~DrizzleDumpField() { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
98
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
99
100
    std::stringstream errmsg;
101
102
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
103
    std::string fieldName;
104
105
    std::string type;
106
    uint32_t length;
107
    bool isNull;
108
    bool isUnsigned;
109
    bool isAutoIncrement;
110
    bool defaultIsNull;
111
    bool convertDateTime;
112
    std::string defaultValue;
113
    std::string collation;
114
115
    /* For enum type */
116
    std::string enumValues;
117
118
    /* For decimal/double */
119
    uint32_t decimalPrecision;
120
    uint32_t decimalScale;
121
122
    virtual void setType(const char*, const char*) { }
123
124
};
125
126
class DrizzleDumpTable
127
{
128
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
129
    DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
130
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
131
      tableName(table),
132
      database(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
133
    { }
134
135
    virtual ~DrizzleDumpTable() { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
136
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
137
138
    std::stringstream errmsg;
139
140
    virtual bool populateFields() { return false; }
141
    virtual bool populateIndexes() { return false; }
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
142
    virtual bool populateFkeys() { return false; }
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
143
    virtual DrizzleDumpData* getData() { return NULL; }
144
    std::vector<DrizzleDumpField*> fields;
145
    std::vector<DrizzleDumpIndex*> indexes;
1810.6.4 by Andrew Hutchings
Add foreign keys to Drizzle server
146
    std::vector<DrizzleDumpForeignKey*> fkeys;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
147
148
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
149
    std::string tableName;
1751.4.25 by Andrew Hutchings
Fix error handling
150
    std::string displayName;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
151
    std::string engineName;
152
    std::string collate;
1810.1.1 by Andrew Hutchings
Add table comments
153
    std::string comment;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
154
155
    // Currently MySQL only, hard to do in Drizzle
156
    uint64_t autoIncrement;
157
    DrizzleDumpDatabase* database;
158
};
159
160
class DrizzleDumpDatabase
161
{
162
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
163
    DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
164
      dcon(connection),
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
165
      databaseName(database)
166
    { }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
167
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
168
169
    virtual ~DrizzleDumpDatabase() { }
170
171
    std::stringstream errmsg;
172
173
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
174
1751.4.23 by Andrew Hutchings
Fix various bugs
175
    virtual bool populateTables(void) { return false; }
176
    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
177
    virtual void setCollate(const char*) { }
1751.4.25 by Andrew Hutchings
Fix error handling
178
    void cleanTableName(std::string &tableName);
1751.4.24 by Andrew Hutchings
Fix ignore tables
179
    bool ignoreTable(std::string tableName);
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
180
    std::vector<DrizzleDumpTable*> tables;
181
182
    const std::string databaseName;
183
    std::string collate;
184
};
185
186
class DrizzleDumpData
187
{
188
  public:
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
189
    DrizzleDumpConnection *dcon;
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
190
    std::stringstream errmsg;
191
    DrizzleDumpTable *table;
192
    drizzle_result_st *result;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
193
    DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
194
      dcon(connection),
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
195
      table(dataTable),
196
      result(NULL)
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
197
    { }
198
199
    virtual ~DrizzleDumpData() { }
200
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
201
1802.11.1 by Andrew Hutchings
1. date regex was broken
202
    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
203
    std::string convertHex(const unsigned char* from, size_t from_size) const;
1810.1.1 by Andrew Hutchings
Add table comments
204
    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
205
};
206
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
207
class DrizzleDumpConnection
208
{
209
  private:
210
    drizzle_st drizzle;
211
    drizzle_con_st connection;
212
    std::string hostName;
213
    bool drizzleProtocol;
214
    int serverType;
215
216
  public:
217
    enum server_type {
218
      SERVER_MYSQL_FOUND,
219
      SERVER_DRIZZLE_FOUND,
220
      SERVER_UNKNOWN_FOUND
221
    };
222
    DrizzleDumpConnection(std::string &host, uint16_t port,
223
      std::string &username, std::string &password, bool drizzle_protocol);
224
    ~DrizzleDumpConnection();
225
    void errorHandler(drizzle_result_st *res,  drizzle_return_t ret, const char *when);
226
    drizzle_result_st* query(std::string &str_query);
227
    bool queryNoResult(std::string &str_query);
228
229
    drizzle_result_st* query(const char* ch_query)
230
    {
231
      std::string str_query(ch_query);
232
      return query(str_query);
233
    }
234
    bool queryNoResult(const char* ch_query)
235
    {
236
      std::string str_query(ch_query);
237
      return queryNoResult(str_query);
238
    }
239
240
    void freeResult(drizzle_result_st* result);
241
    bool setDB(std::string databaseName);
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
242
    bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
243
    bool getServerType(void) const { return serverType; }
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
244
    const char* getServerVersion(void) { return drizzle_con_server_version(&connection); }
245
};
246
247
class DrizzleStringBuf : public std::streambuf
248
{
249
  public:
250
    DrizzleStringBuf(int size) :
251
      buffSize(size)
252
    {
253
      resize= 1;
254
      ptr.resize(buffSize);
255
      setp(&ptr[0], &ptr.back());
256
    }
257
    virtual ~DrizzleStringBuf() 
258
    {
259
        sync();
260
    }
261
1751.4.21 by Andrew Hutchings
Add database destination settings and connect it all up
262
    void writeString(std::string &str)
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
263
    {
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
264
      if (not connection->queryNoResult(str))
265
        throw 1;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
266
    }
267
268
    void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
269
270
  private:
271
    DrizzleDumpConnection *connection;
272
    size_t buffSize;
273
    uint32_t resize;
274
    std::vector<char> ptr;
275
276
    int	overflow(int c)
277
    {
278
        if (c != EOF)
279
        {
280
          size_t len = size_t(pptr() - pbase());
281
          resize++;
282
          ptr.resize(buffSize*resize);
283
          setp(&ptr[0], &ptr.back());
284
          /* setp resets current pointer, put it back */
285
          pbump(len);
286
          sputc(c);
287
        }
288
289
        return 0;
290
    }
291
292
    int	sync()
293
    {
1855.1.1 by Andrew Hutchings
Fix several bugs dumping tables with many rows
294
      size_t len = size_t(pptr() - pbase());
295
      std::string temp(pbase(), len);
296
297
      /* Drop newlines */
298
      temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
299
300
      if (temp.compare(0, 2, "--") == 0)
301
      {
302
        /* Drop comments */
303
        setp(pbase(), epptr());
304
      }
305
      if (temp.find(";") != std::string::npos)
306
      {
307
        writeString(temp);
308
        setp(pbase(), epptr());
309
      }
310
      return 0;
1751.4.20 by Andrew Hutchings
Add database connection class and the start of a database output ostream
311
    }
312
};
313
1751.4.19 by Andrew Hutchings
Put drizzle and mysql processes in seperate classes/files
314
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */