~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzledump_data.h

  • Committer: Mark Atwood
  • Date: 2010-06-24 03:15:21 UTC
  • mto: (1637.2.4 build)
  • mto: This revision was merged to the branch mainline in revision 1639.
  • Revision ID: me@mark.atwood.name-20100624031521-gafmppfbf5afm68w
new syslog module, with plugins for query log, error message, and SYSLOG() function

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 "server_detect.h"
26
 
#include <string>
27
 
#include <iostream>
28
 
#include <iomanip>
29
 
#include <vector>
30
 
#include <sstream>
31
 
#include <algorithm>
32
 
 
33
 
class DrizzleDumpConnection;
34
 
class DrizzleDumpDatabase;
35
 
class DrizzleDumpData;
36
 
 
37
 
class DrizzleDumpForeignKey
38
 
{
39
 
  public:
40
 
    DrizzleDumpConnection *dcon;
41
 
    std::string constraintName;
42
 
 
43
 
    DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
44
 
      dcon(connection),
45
 
      constraintName(name)
46
 
    { }
47
 
 
48
 
    virtual ~DrizzleDumpForeignKey() { }
49
 
 
50
 
    std::string parentColumns;
51
 
    std::string childColumns;
52
 
    std::string childTable;
53
 
    std::string matchOption;
54
 
    std::string deleteRule;
55
 
    std::string updateRule;
56
 
 
57
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpForeignKey &obj);
58
 
};
59
 
 
60
 
class DrizzleDumpIndex
61
 
{
62
 
  public:
63
 
    DrizzleDumpConnection *dcon;
64
 
    std::string indexName;
65
 
 
66
 
    DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
67
 
      dcon(connection),
68
 
      indexName(index),
69
 
      isPrimary(false),
70
 
      isUnique(false),
71
 
      isHash(false)
72
 
    { }
73
 
 
74
 
    virtual ~DrizzleDumpIndex() { }
75
 
 
76
 
    bool isPrimary;
77
 
    bool isUnique;
78
 
    bool isHash;
79
 
    uint32_t length;
80
 
 
81
 
    std::vector<std::string> columns;
82
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpIndex &obj);
83
 
};
84
 
 
85
 
class DrizzleDumpField
86
 
{
87
 
  public:
88
 
    DrizzleDumpField(std::string &field, DrizzleDumpConnection* connection) :
89
 
      dcon(connection),
90
 
      fieldName(field),
91
 
      isNull(false),
92
 
      isUnsigned(false),
93
 
      isAutoIncrement(false),
94
 
      defaultIsNull(false),
95
 
      convertDateTime(false),
96
 
      rangeCheck(false)
97
 
    { }
98
 
 
99
 
    virtual ~DrizzleDumpField() { }
100
 
    DrizzleDumpConnection *dcon;
101
 
 
102
 
    std::stringstream errmsg;
103
 
 
104
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpField &obj);
105
 
    std::string fieldName;
106
 
 
107
 
    std::string type;
108
 
    uint32_t length;
109
 
    bool isNull;
110
 
    bool isUnsigned;
111
 
    bool isAutoIncrement;
112
 
    bool defaultIsNull;
113
 
    bool convertDateTime;
114
 
    bool rangeCheck;
115
 
    std::string defaultValue;
116
 
    std::string collation;
117
 
    std::string comment;
118
 
 
119
 
    /* For enum type */
120
 
    std::string enumValues;
121
 
 
122
 
    /* For decimal/double */
123
 
    uint32_t decimalPrecision;
124
 
    uint32_t decimalScale;
125
 
 
126
 
    virtual void setType(const char*, const char*) { }
127
 
 
128
 
};
129
 
 
130
 
class DrizzleDumpTable
131
 
{
132
 
  public:
133
 
    DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
134
 
      dcon(connection),
135
 
      tableName(table),
136
 
      database(NULL)
137
 
    { }
138
 
 
139
 
    virtual ~DrizzleDumpTable() { }
140
 
    DrizzleDumpConnection *dcon;
141
 
 
142
 
    std::stringstream errmsg;
143
 
 
144
 
    virtual bool populateFields() { return false; }
145
 
    virtual bool populateIndexes() { return false; }
146
 
    virtual bool populateFkeys() { return false; }
147
 
    virtual DrizzleDumpData* getData() { return NULL; }
148
 
    std::vector<DrizzleDumpField*> fields;
149
 
    std::vector<DrizzleDumpIndex*> indexes;
150
 
    std::vector<DrizzleDumpForeignKey*> fkeys;
151
 
 
152
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpTable &obj);
153
 
    std::string tableName;
154
 
    std::string displayName;
155
 
    std::string engineName;
156
 
    std::string collate;
157
 
    std::string comment;
158
 
 
159
 
    // Currently MySQL only, hard to do in Drizzle
160
 
    uint64_t autoIncrement;
161
 
    DrizzleDumpDatabase* database;
162
 
};
163
 
 
164
 
class DrizzleDumpDatabase
165
 
{
166
 
  public:
167
 
    DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
168
 
      dcon(connection),
169
 
      databaseName(database)
170
 
    { }
171
 
    DrizzleDumpConnection *dcon;
172
 
 
173
 
    virtual ~DrizzleDumpDatabase() { }
174
 
 
175
 
    std::stringstream errmsg;
176
 
 
177
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpDatabase &obj);
178
 
 
179
 
    virtual bool populateTables(void) { return false; }
180
 
    virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
181
 
    virtual void setCollate(const char*) { }
182
 
    void cleanTableName(std::string &tableName);
183
 
    bool ignoreTable(std::string tableName);
184
 
    std::vector<DrizzleDumpTable*> tables;
185
 
 
186
 
    const std::string databaseName;
187
 
    std::string collate;
188
 
};
189
 
 
190
 
class DrizzleDumpData
191
 
{
192
 
  public:
193
 
    DrizzleDumpConnection *dcon;
194
 
    std::stringstream errmsg;
195
 
    DrizzleDumpTable *table;
196
 
    drizzle_result_st *result;
197
 
    DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
198
 
      dcon(connection),
199
 
      table(dataTable),
200
 
      result(NULL)
201
 
    { }
202
 
 
203
 
    virtual ~DrizzleDumpData() { }
204
 
    friend std::ostream& operator <<(std::ostream &os, const DrizzleDumpData &obj);
205
 
 
206
 
    virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
207
 
    std::string convertHex(const unsigned char* from, size_t from_size) const;
208
 
    static std::string escape(const char* from, size_t from_size);
209
 
};
210
 
 
211
 
class DrizzleDumpConnection
212
 
{
213
 
  private:
214
 
    drizzle_st drizzle;
215
 
    drizzle_con_st connection;
216
 
    std::string hostName;
217
 
    bool drizzleProtocol;
218
 
    ServerDetect::server_type serverType;
219
 
    std::string serverVersion;
220
 
 
221
 
  public:
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);
242
 
    bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
243
 
    bool getServerType(void) const { return serverType; }
244
 
    std::string getServerVersion(void) const { return serverVersion; }
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
 
 
262
 
    void writeString(std::string &str)
263
 
    {
264
 
      if (not connection->queryNoResult(str))
265
 
        throw std::exception();
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
 
    {
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;
311
 
    }
312
 
};
313
 
 
314
 
#endif /* CLIENT_DRIZZLEDUMP_DATA_H */