Menu

[r1560]: / cppdb / trunk / cppdb / frontend.h  Maximize  Restore  History

Download this file

370 lines (300 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef CPPDB_FRONTEND_H
#define CPPDB_FRONEND_H
#include <cppdb/defs.h>
#include <cppdb/errors.h>
#include <cppdb/ref_ptr.h>
#include <iosfwd>
#include <ctime>
#include <string>
#include <memory>
///
/// The namespace of all data related to the cppdb api
///
namespace cppdb {
class result;
class statement;
class session;
namespace backend {
class result;
class statement;
class connection;
}
///
/// Null value marker
///
typedef enum {
null_value, ///< The value is null value
not_null_value ///< The valus is not a null value
} null_tag_type;
/// \cond INTERNAL
namespace tags {
template<typename T>
struct into_tag {
T &value;
null_tag_type &tag;
into_tag(T &v, null_tag_type &t) : value(v),tag(t) {}
};
template<typename T>
struct use_tag {
T value;
null_tag_type tag;
use_tag(T v,null_tag_type t) : value(v),tag(t) {}
};
} // tags
/// \endcond
///
/// Create a pair of value and tag for fetching a value from row, the fetched
/// value will be stored in \a value if the column is not null and the flag
/// if the value is null or not saved in \a tag
///
template<typename T>
tags::into_tag<T> into(T &value,null_tag_type &tag)
{
return tags::into_tag<T>(value,tag);
}
inline tags::use_tag<std::string const &> use(std::string const &v,null_tag_type tag)
{
return tags::use_tag<std::string const &>(v,tag);
}
inline tags::use_tag<char const *> use(char const *v,null_tag_type tag)
{
return tags::use_tag<char const *>(v,tag);
}
template<typename T>
tags::use_tag<T> use(T value,null_tag_type tag)
{
return tags::use_tag<T>(value,tag);
}
class CPPDB_API result {
public:
result();
~result();
result(result const &);
result const &operator=(result const &);
int cols();
bool next();
int index(std::string const &n);
int find_column(std::string const &name);
std::string name(int col);
bool is_null(int col);
bool is_null(std::string const &n);
void clear();
void rewind_column();
bool empty();
bool fetch(int col,short &v);
bool fetch(int col,unsigned short &v);
bool fetch(int col,int &v);
bool fetch(int col,unsigned &v);
bool fetch(int col,long &v);
bool fetch(int col,unsigned long &v);
bool fetch(int col,long long &v);
bool fetch(int col,unsigned long long &v);
bool fetch(int col,float &v);
bool fetch(int col,double &v);
bool fetch(int col,long double &v);
bool fetch(int col,std::string &v);
bool fetch(int col,std::tm &v);
bool fetch(int col,std::ostream &v);
bool fetch(std::string const &n,short &v);
bool fetch(std::string const &n,unsigned short &v);
bool fetch(std::string const &n,int &v);
bool fetch(std::string const &n,unsigned &v);
bool fetch(std::string const &n,long &v);
bool fetch(std::string const &n,unsigned long &v);
bool fetch(std::string const &n,long long &v);
bool fetch(std::string const &n,unsigned long long &v);
bool fetch(std::string const &n,float &v);
bool fetch(std::string const &n,double &v);
bool fetch(std::string const &n,long double &v);
bool fetch(std::string const &n,std::string &v);
bool fetch(std::string const &n,std::tm &v);
bool fetch(std::string const &n,std::ostream &v);
bool fetch(short &v);
bool fetch(unsigned short &v);
bool fetch(int &v);
bool fetch(unsigned &v);
bool fetch(long &v);
bool fetch(unsigned long &v);
bool fetch(long long &v);
bool fetch(unsigned long long &v);
bool fetch(float &v);
bool fetch(double &v);
bool fetch(long double &v);
bool fetch(std::string &v);
bool fetch(std::tm &v);
bool fetch(std::ostream &v);
template<typename T>
T get(std::string const &name)
{
T v=T();
if(!fetch(name,v))
throw null_value_fetch();
return v;
}
template<typename T>
T get(int col)
{
T v=T();
if(!fetch(col,v))
throw null_value_fetch();
return v;
}
template<typename T>
result &operator>>(tags::into_tag<T> ref)
{
if(fetch(ref.value))
ref.tag = not_null_value;
else
ref.tag = null_value;
return *this;
}
template<typename T>
result &operator>>(T &value)
{
fetch(value);
return *this;
}
private:
result( ref_ptr<backend::result> res,
ref_ptr<backend::statement> stat,
ref_ptr<backend::connection> conn);
void check();
friend class statement;
struct data;
std::auto_ptr<data> d;
bool eof_;
bool fetched_;
int current_col_;
ref_ptr<backend::result> res_;
ref_ptr<backend::statement> stat_;
ref_ptr<backend::connection> conn_;
};
class CPPDB_API statement {
public:
statement();
~statement();
statement(statement const &);
statement const &operator=(statement const &);
void reset();
statement &bind(int v);
statement &bind(unsigned v);
statement &bind(long v);
statement &bind(unsigned long v);
statement &bind(long long v);
statement &bind(unsigned long long v);
statement &bind(double v);
statement &bind(long double v);
statement &bind(std::string const &v);
statement &bind(char const *s);
statement &bind(char const *b,char const *e);
statement &bind(std::tm const &v);
statement &bind(std::istream &v);
statement &bind_null();
void bind(int col,int v);
void bind(int col,unsigned v);
void bind(int col,long v);
void bind(int col,unsigned long v);
void bind(int col,long long v);
void bind(int col,unsigned long long v);
void bind(int col,double v);
void bind(int col,long double v);
void bind(int col,std::string const &v);
void bind(int col,char const *s);
void bind(int col,char const *b,char const *e);
void bind(int col,std::tm const &v);
void bind(int col,std::istream &v);
void bind_null(int col);
long long last_insert_id();
long long sequence_last(std::string const &seq);
unsigned long long affected();
result row();
result query();
operator result();
void exec();
statement &operator<<(std::string const &v);
statement &operator<<(char const *s);
statement &operator<<(std::tm const &v);
statement &operator<<(std::istream &v);
statement &operator<<(void (*manipulator)(statement &st));
result operator<<(result (*manipulator)(statement &st));
template<typename T>
statement &operator<<(tags::use_tag<T> const &val)
{
if(val.tag == null_value)
return bind_null();
else
return bind(val.value);
}
template<typename T>
statement &operator<<(T v)
{
return bind(v);
}
private:
statement(ref_ptr<backend::statement> stat,ref_ptr<backend::connection> conn);
friend class session;
int placeholder_;
ref_ptr<backend::statement> stat_;
ref_ptr<backend::connection> conn_;
struct data;
std::auto_ptr<data> d;
};
inline void exec(statement &st)
{
st.exec();
}
inline void null(statement &st)
{
st.bind_null();
}
inline result row(statement &st)
{
return st.row();
}
class CPPDB_API session {
public:
session();
session(session const &);
session const &operator=(session const &);
~session();
session(std::string const &cs);
session(ref_ptr<backend::connection> conn);
void open(std::string const &cs);
void close();
bool is_open();
statement prepare(std::string const &query);
statement operator<<(std::string const &q);
statement operator<<(char const *s);
statement create_statement(std::string const &q);
statement create_prepared_statement(std::string const &q);
statement create_prepared_uncached_statement(std::string const &q);
void clear_cache();
void begin();
void commit();
void rollback();
std::string escape(char const *b,char const *e);
std::string escape(char const *s);
std::string escape(std::string const &s);
std::string driver();
std::string engine();
private:
struct data;
std::auto_ptr<data> d;
ref_ptr<backend::connection> conn_;
};
class CPPDB_API transaction {
transaction(transaction const &);
void operator=(transaction const &);
public:
transaction(session &s);
~transaction();
void commit();
void rollback();
private:
struct data;
session *s_;
bool commited_;
std::auto_ptr<data> d;
};
} // cppdb
#endif
MongoDB Logo MongoDB