Drizzled Public API Documentation

drizzledump_data.h
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 #pragma once
21 
22 #define DRIZZLE_MAX_LINE_LENGTH 1024*1024L-1025
23 #include "client/client_priv.h"
24 #include "client/server_detect.h"
25 #include <cstdio>
26 #include <string>
27 #include <iosfwd>
28 #include <iomanip>
29 #include <vector>
30 #include <sstream>
31 #include <algorithm>
32 
35 class DrizzleDumpData;
36 
38 {
39  public:
41  std::string constraintName;
42 
43  DrizzleDumpForeignKey(std::string name, DrizzleDumpConnection* connection) :
44  dcon(connection),
45  constraintName(name)
46  { }
47 
48  std::string parentColumns;
49  std::string childColumns;
50  std::string childTable;
51  std::string matchOption;
52  std::string deleteRule;
53  std::string updateRule;
54 
55  friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpForeignKey &obj);
56 };
57 
59 {
60  public:
62  std::string indexName;
63 
64  DrizzleDumpIndex(std::string &index, DrizzleDumpConnection* connection) :
65  dcon(connection),
66  indexName(index),
67  isPrimary(false),
68  isUnique(false),
69  isHash(false)
70  { }
71 
72  virtual ~DrizzleDumpIndex() { }
73 
74  bool isPrimary;
75  bool isUnique;
76  bool isHash;
77 
78  /* Stores the column name and the length of the index part */
79  typedef std::pair<std::string,uint32_t> columnData;
80  std::vector<columnData> columns;
81  friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpIndex &obj);
82 };
83 
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() { }
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 
130 {
131  public:
132  DrizzleDumpTable(std::string &table, DrizzleDumpConnection* connection) :
133  dcon(connection),
134  tableName(table),
135  replicate(true),
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  bool replicate;
159 
160  // Currently MySQL only, hard to do in Drizzle
161  uint64_t autoIncrement;
162  DrizzleDumpDatabase* database;
163 };
164 
166 {
167  public:
168  DrizzleDumpDatabase(const std::string &database, DrizzleDumpConnection* connection) :
169  dcon(connection),
170  databaseName(database)
171  { }
172  DrizzleDumpConnection *dcon;
173 
174  virtual ~DrizzleDumpDatabase() { }
175 
176  std::stringstream errmsg;
177 
178  friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpDatabase &obj);
179 
180  virtual bool populateTables(void) { return false; }
181  virtual bool populateTables(const std::vector<std::string> &table_names) { return table_names.empty(); }
182  virtual void setCollate(const char*) { }
183  void cleanTableName(std::string &tableName);
184  bool ignoreTable(std::string tableName);
185  std::vector<DrizzleDumpTable*> tables;
186 
187  const std::string databaseName;
188  std::string collate;
189 };
190 
192 {
193  public:
194  DrizzleDumpConnection *dcon;
195  std::stringstream errmsg;
196  DrizzleDumpTable *table;
197  drizzle_result_st *result;
198  DrizzleDumpData(DrizzleDumpTable *dataTable, DrizzleDumpConnection *connection) :
199  dcon(connection),
200  table(dataTable),
201  result(NULL)
202  { }
203 
204  virtual ~DrizzleDumpData() { }
205  friend std::ostream& operator<<(std::ostream &os, const DrizzleDumpData &obj);
206 
207  virtual std::string checkDateTime(const char*, uint32_t) const { return std::string(""); }
208  std::string convertHex(const unsigned char* from, size_t from_size) const;
209  static std::string escape(const char* from, size_t from_size);
210 };
211 
213 {
214  private:
215  drizzle_st *drizzle;
216  drizzle_con_st *connection;
217  std::string hostName;
218  bool drizzleProtocol;
219  ServerDetect::server_type serverType;
220  std::string serverVersion;
221 
222  public:
223  DrizzleDumpConnection(std::string &host,
224  uint16_t port,
225  std::string &username,
226  std::string &password,
227  bool drizzle_protocol);
228 
230 
231  void errorHandler(drizzle_result_st *res, drizzle_return_t ret, const char *when);
232  drizzle_result_st* query(std::string &str_query);
233  bool queryNoResult(std::string &str_query);
234 
235  drizzle_result_st* query(const char* ch_query)
236  {
237  std::string str_query(ch_query);
238  return query(str_query);
239  }
240 
241  bool queryNoResult(const char* ch_query)
242  {
243  std::string str_query(ch_query);
244  return queryNoResult(str_query);
245  }
246 
247  void freeResult(drizzle_result_st* result);
248  bool setDB(std::string databaseName);
249  bool usingDrizzleProtocol(void) const { return drizzleProtocol; }
250  bool getServerType(void) const { return serverType; }
251  std::string getServerVersion(void) const { return serverVersion; }
252 };
253 
254 class DrizzleStringBuf : public std::streambuf
255 {
256  public:
257  DrizzleStringBuf(int size) :
258  buffSize(size)
259  {
260  resize= 1;
261  ptr.resize(buffSize);
262  setp(&ptr[0], &ptr.back());
263  }
264  virtual ~DrizzleStringBuf()
265  {
266  sync();
267  }
268 
269  void writeString(std::string &str)
270  {
271  if (not connection->queryNoResult(str))
272  {
273  throw std::exception();
274  }
275  }
276 
277  void setConnection(DrizzleDumpConnection *conn) { connection= conn; }
278 
279  private:
280  DrizzleDumpConnection *connection;
281  size_t buffSize;
282  uint32_t resize;
283  std::vector<char> ptr;
284 
285  int overflow(int c)
286  {
287  if (c != EOF)
288  {
289  size_t len = size_t(pptr() - pbase());
290  resize++;
291  ptr.resize(buffSize*resize);
292  setp(&ptr[0], &ptr.back());
293  /* setp resets current pointer, put it back */
294  pbump(len);
295  sputc(c);
296  }
297 
298  return 0;
299  }
300 
301  int sync()
302  {
303  size_t len = size_t(pptr() - pbase());
304  std::string temp(pbase(), len);
305 
306  /* Drop newlines */
307  temp.erase(std::remove(temp.begin(), temp.end(), '\n'), temp.end());
308 
309  if (temp.compare(0, 2, "--") == 0)
310  {
311  /* Drop comments */
312  setp(pbase(), epptr());
313  }
314  if (temp.find(";") != std::string::npos)
315  {
316  writeString(temp);
317  setp(pbase(), epptr());
318  }
319  return 0;
320  }
321 };