~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_data.h

  • Committer: Brian Aker
  • Date: 2010-06-08 03:51:35 UTC
  • Revision ID: brian@gaz-20100608035135-l66xrjt7xk9d502n
Remove current_session from myisam.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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>
30
 
#include <algorithm>
31
 
 
32
 
class DrizzleDumpConnection;
33
 
class DrizzleDumpDatabase;
34
 
class DrizzleDumpData;
35
 
 
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
 
 
59
 
class DrizzleDumpIndex
60
 
{
61
 
  public:
62
 
    DrizzleDumpConnection *dcon;
63
 
    std::string indexName;
64
 
 
65
 
    DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
66
 
      dcon(connection),
67
 
      indexName(index),
68
 
      isPrimary(false),
69
 
      isUnique(false),
70
 
      isHash(false)
71
 
    { }
72
 
 
73
 
    virtual ~DrizzleDumpIndex() { }
74
 
 
75
 
    bool isPrimary;
76
 
    bool isUnique;
77
 
    bool isHash;
78
 
    uint32_t length;
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:
87
 
    DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
88
 
      dcon(connection),
89
 
      fieldName(field),
90
 
      isNull(false),
91
 
      isUnsigned(false),
92
 
      isAutoIncrement(false),
93
 
      defaultIsNull(false),
94
 
      convertDateTime(false),
95
 
      rangeCheck(false)
96
 
    { }
97
 
 
98
 
    virtual ~DrizzleDumpField() { }
99
 
    DrizzleDumpConnection *dcon;
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;
113
 
    bool rangeCheck;
114
 
    std::string defaultValue;
115
 
    std::string collation;
116
 
    std::string comment;
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:
132
 
    DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
133
 
      dcon(connection),
134
 
      tableName(table),
135
 
      database(NULL)
136
 
    { }
137
 
 
138
 
    virtual ~DrizzleDumpTable() { }
139
 
    DrizzleDumpConnection *dcon;
140
 
 
141
 
    std::stringstream errmsg;
142
 
 
143
 
    virtual bool populateFields() { return false; }
144
 
    virtual bool populateIndexes() { return false; }
145
 
    virtual bool populateFkeys() { return false; }
146
 
    virtual DrizzleDumpData* getData() { return NULL; }
147
 
    std::vector<DrizzleDumpField*> fields;
148
 
    std::vector<DrizzleDumpIndex*> indexes;
149
 
    std::vector<DrizzleDumpForeignKey*> fkeys;
150
 
 
151
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
152
 
    std::string tableName;
153
 
    std::string displayName;
154
 
    std::string engineName;
155
 
    std::string collate;
156
 
    std::string comment;
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:
166
 
    DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
167
 
      dcon(connection),
168
 
      databaseName(database)
169
 
    { }
170
 
    DrizzleDumpConnection *dcon;
171
 
 
172
 
    virtual ~DrizzleDumpDatabase() { }
173
 
 
174
 
    std::stringstream errmsg;
175
 
 
176
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
177
 
 
178
 
    virtual bool populateTables(void) { return false; }
179
 
    virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
180
 
    virtual void setCollate(const char*) { }
181
 
    void cleanTableName(std::string &tableName);
182
 
    bool ignoreTable(std::string tableName);
183
 
    std::vector<DrizzleDumpTable*> tables;
184
 
 
185
 
    const std::string databaseName;
186
 
    std::string collate;
187
 
};
188
 
 
189
 
class DrizzleDumpData
190
 
{
191
 
  public:
192
 
    DrizzleDumpConnection *dcon;
193
 
    std::stringstream errmsg;
194
 
    DrizzleDumpTable *table;
195
 
    drizzle_result_st *result;
196
 
    DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
197
 
      dcon(connection),
198
 
      table(dataTable),
199
 
      result(NULL)
200
 
    { }
201
 
 
202
 
    virtual ~DrizzleDumpData() { }
203
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
204
 
 
205
 
    virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
206
 
    std::string convertHex(const unsigned char* from, size_t from_size) const;
207
 
    static std::string escape(const char* from, size_t from_size);
208
 
};
209
 
 
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);
245
 
    bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
246
 
    bool getServerType(void) const { return serverType; }
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
 
 
265
 
    void writeString(std::string &str)
266
 
    {
267
 
      if (not connection->queryNoResult(str))
268
 
        throw std::exception();
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
 
    {
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;
314
 
    }
315
 
};
316
 
 
317
 
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */