~mordred/ubuntu/maverick/drizzle/prerelease

« back to all changes in this revision

Viewing changes to client/drizzledump_data.h

  • Committer: Monty Taylor
  • Date: 2010-09-26 16:09:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1383.
  • Revision ID: mordred@inaugust.com-20100926160902-r30v5hegk16cjk22
Tags: upstream-2010.09.1794
ImportĀ upstreamĀ versionĀ 2010.09.1794

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