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