Menu

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

Download this file

208 lines (191 with data), 3.8 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
#include "db_wrapper.h"
#include <boost/scoped_array.hpp>
#include <iostream>
#include "global_config.h"
namespace db_wrapper {
DB_Escape::DB_Escape(char const *format)
{
query=NULL;
this->format=format;
parameters.reserve(16);
}
DB_Escape &DB_Escape::operator<<(long val)
{
element e;
e.data.long_val=val;
e.type=LONG;
e.length=snprintf(NULL,0,"%ld",val);
parameters.push_back(e);
return *this;
}
DB_Escape &DB_Escape::operator<<(double val)
{
element e;
e.data.double_val=val;
e.type=DOUBLE;
e.length=snprintf(NULL,0,"%e",val);
parameters.push_back(e);
return *this;
}
DB_Escape &DB_Escape::operator<<(char const *str)
{
element e;
e.data.str_val=str;
e.type=STRING;
e.length=2*strlen(str)+1;
parameters.push_back(e);
return *this;
}
char *DB_Escape::get(MYSQL *conn)
{
int overall_len=calc_len();
query=new char [overall_len+1];
escape_str(conn);
return query;
}
int DB_Escape::calc_len()
{
int id,len=0,i,j;
for(i=0;format[i];i++) {
if(format[i]=='%' && format[i+1]!='%') {
id=0;
for(j=i+1;format[j]!='%';j++) {
if('0'<format[j] && format[j]<'9') {
id=id*10+format[j]-'0';
}
else {
throw DB_Err("Invalid format");
}
}
i=j;
id--;
if(id>=0 && id<(int)parameters.size()) {
len+=parameters[id].length;
}
}
else if(format[i]=='%' && format[i+1]=='%') {
i++;
}
}
return i+len;
}
void DB_Escape::escape_str(MYSQL *conn)
{
char *q=query;
int i,j,id;
for(i=0;format[i];i++) {
if(format[i]=='%' && format[i+1]!='%') {
id=0;
for(j=i+1;format[j]!='%';j++) {
if('0'<format[j] && format[j]<'9') {
id=id*10+format[j]-'0';
}
else {
throw DB_Err("Invalid format");
}
}
i=j;
id--;
if(id>=0 && id<(int)parameters.size()) {
char const *ptr;
switch(parameters[id].type) {
case LONG:
q+=sprintf(q,"%ld",parameters[id].data.long_val);
break;
case DOUBLE:
q+=sprintf(q,"%e",parameters[id].data.double_val);
break;
case STRING:
ptr=parameters[id].data.str_val;
q+=mysql_real_escape_string(conn,
q,
ptr,
strlen(ptr));
}
}
}
else if(format[i]=='%' && format[i+1]=='%') {
*q='%';
q++;
i++;
}
else {
*q=format[i];
q++;
}
}
*q=0;
}
void Data_Base::connect()
{
conn=mysql_init(NULL);
if(!conn){
throw DB_Err("No memory");
}
if(!mysql_real_connect( conn,
host.c_str(),
username.c_str(),
password.c_str(),
database.c_str(),
0,NULL,0))
{
close();
throw DB_Err("Failed to connect to database");
}
}
void Data_Base::open(string const &h,string const &u,string const &p,string const &d)
{
host=h;
username=u;
password=p;
database=d;
setup=true;
connect();
}
void Data_Base::open()
{
open( global_config.sval("mysql.host"),
global_config.sval("mysql.username"),
global_config.sval("mysql.password"),
global_config.sval("mysql.database"));
}
void Data_Base::exec_query(char const *q)
{
bool not_try_once_more=false;
if(!setup) throw DB_Err("Date base must be open first");
if(!conn){
connect();
not_try_once_more=true;
}
if(mysql_query(conn,q) && !not_try_once_more) {
// MayBe we lost presistent connection
close();
connect();
if(mysql_query(conn,q)) {
throw DB_Err(string("Failed to exectue the query:") + q);
}
}
}
void Data_Base::exec(char const *q)
{
exec_query(q);
MYSQL_RES *res=mysql_store_result(conn);
if(res) {
mysql_free_result(res);
throw DB_Err("You must not use query for operation that "
"returns result");
}
if(mysql_errno(conn)) {
throw DB_Err(string("Error executing query:")+q);
}
}
MYSQL_RES *Data_Base::query(char const *q)
{
exec_query(q);
MYSQL_RES *res=mysql_store_result(conn);
if(!res || mysql_errno(conn)) {
throw DB_Err(string("Error executing query:")+q);
}
return res;
}
}
MongoDB Logo MongoDB