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