Menu

[r34]: / framework / trunk / db_wrapper.h  Maximize  Restore  History

Download this file

121 lines (100 with data), 2.5 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
#ifndef _MYSQL_DB_H
#define _MYSQL_DB_H
#include <stdlib.h>
#include <string.h>
#include <string>
#include <stdarg.h>
#include <boost/scoped_array.hpp>
#include <mysql/mysql.h>
#include <vector>
#include <iostream>
namespace db_wrapper {
using namespace std;
#define DB_ERR_MAX_LEN 128
typedef char **DB_Row;
class DB_Escape {
typedef enum { LONG, DOUBLE, STRING } types;
struct element {
types type;
int length;
union {
char const *str_val;
long long_val;
double double_val;
} data;
};
vector<element> parameters;
char *query;
char const *format;
int calc_len();
void escape_str(MYSQL *conn);
public:
DB_Escape &operator<<(char const *str);
DB_Escape &operator<<(string const &str)
{
return *this<<str.c_str();
};
DB_Escape &operator<<(int v)
{
return *this<<(long)v;
};
DB_Escape &operator<<(long);
DB_Escape &operator<<(double);
char *get(MYSQL *conn);
DB_Escape(char const *format);
~DB_Escape() { delete [] query; };
};
typedef DB_Escape escape;
class DB_Err {
char message[DB_ERR_MAX_LEN];
public:
char const *get() { return message; };
DB_Err(char const *text)
{
strncpy(message,text,DB_ERR_MAX_LEN);
message[DB_ERR_MAX_LEN-1]=0;
};
DB_Err(string const &str)
{
strncpy(message,str.c_str(),DB_ERR_MAX_LEN);
message[DB_ERR_MAX_LEN-1]=0;
};
};
class DB_Res {
MYSQL_RES *res;
public:
DB_Res() { res=NULL; };
DB_Res(MYSQL_RES *result) {
res=result;
};
void free() { if(res) mysql_free_result(res); };
~DB_Res() { free(); };
DB_Row next() { return mysql_fetch_row(res); };
void operator=(MYSQL_RES *result) { free(); res=result; };
int cols() { return mysql_num_fields(res); };
int rows() { return mysql_num_rows(res); };
};
class Data_Base {
bool setup;
MYSQL *conn;
string host;
string username;
string password;
string database;
void connect();
void exec_query(char const *q);
public:
MYSQL_RES *query(char const *);
MYSQL_RES *query(DB_Escape &e) { return query(e.get(conn)); };
MYSQL_RES *query(string const &s) { return query(s.c_str()); };
void exec(char const *);
void exec(DB_Escape &e) { exec(e.get(conn)); };
void exec(string const &s) { exec(s.c_str()); };
void close() { if(conn) mysql_close(conn); conn=NULL; };
void open(string const &h,string const &u,string const &p,string const &d);
void open();
Data_Base() { conn=NULL; setup=false; };
~Data_Base() { if(conn) mysql_close(conn); };
};
}
#endif /* _MYSQL_DB_H */
MongoDB Logo MongoDB