-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.h
212 lines (181 loc) · 4.56 KB
/
sql.h
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
#ifndef CALENDARI__UTIL__SQL_H
#define CALENDARI__UTIL__SQL_H
#include "err.h"
#include <cstdio>
#include <cstdarg>
#include <sqlite3.h>
#include <string>
namespace calendari {
namespace sql {
typedef void(*MemoryStatus)(void*);
inline void
error(const util::Here& here, sqlite3* sdb)
{
int errcode = ::sqlite3_errcode(sdb);
const char* errmsg = ::sqlite3_errmsg(sdb);
util::error(here,0,0,"sqlite error %i: %s",errcode,errmsg);
}
inline void
check_error(const util::Here& here, sqlite3* sdb, int return_code)
{
if( SQLITE_OK != return_code )
sql::error(here,sdb);
}
#define CALI_SQLCHK(DB,RET) \
do{calendari::sql::check_error( \
calendari::util::Here(__FILE__,__LINE__),DB,RET); \
}while(0)
/** Escape single-quote characters for Sqlite strings. */
inline std::string
quote(const std::string& s)
{
std::string result = "";
std::string::size_type cur = 0;
while(true)
{
std::string::size_type pos = s.find_first_of("'",cur);
if(pos==s.npos)
{
result += s.substr(cur, pos);
break;
}
result += s.substr(cur, pos-cur) + "''";
cur = pos+1;
}
return result;
}
/** Scoped class that wraps sqlite3_prepare_v2()..sqlite3_finalize().
* The constructor takes the same parameters as sqlite3_prepare_v2().
* The object behaves as a sqlite3_stmt*. sqlite3_finalize() is called
* upon destruction. */
class Statement
{
public:
Statement(const util::Here& here,
sqlite3* db, const char* zSql, int nByte=-1, const char** pzTail=NULL);
~Statement(void);
operator sqlite3_stmt* (void) const;
sqlite3* db(void) const;
private:
sqlite3* _db;
sqlite3_stmt* _stmt;
};
inline void
bind_int(
const util::Here& here,
sqlite3* sdb,
sqlite3_stmt* stmt,
int idx,
int val)
{
int ret;
ret= ::sqlite3_bind_int(stmt,idx,val);
sql::check_error(here,sdb,ret);
}
inline void
bind_int64(
const util::Here& here,
sqlite3* sdb,
sqlite3_stmt* stmt,
int idx,
sqlite3_int64 val)
{
int ret;
ret= ::sqlite3_bind_int64(stmt,idx,val);
sql::check_error(here,sdb,ret);
}
inline void
bind_text(
const util::Here& here,
sqlite3* sdb,
sqlite3_stmt* stmt,
int idx,
const char* s,
int n = -1,
MemoryStatus mem = SQLITE_TRANSIENT
)
{
int ret;
ret= ::sqlite3_bind_text(stmt,idx,s,n,mem);
sql::check_error(here,sdb,ret);
}
inline void
step_reset(const util::Here& here, sqlite3* sdb, sqlite3_stmt* stmt)
{
int return_code = ::sqlite3_step(stmt);
switch(return_code)
{
case SQLITE_ROW:
util::error(here,0,0,"Executed INSERT, but got rows!");
break;
case SQLITE_DONE: // OK
case SQLITE_CONSTRAINT: // Probably primary key violation. ??
break;
default:
sql::error(here,sdb);
} // end switch
::sqlite3_reset(stmt);
}
/** Execute arbitrary SQL. Discards any resulting columns. */
inline void
exec(const util::Here& here, sqlite3* sdb, const char* sql)
{
char* errmsg;
int ret = ::sqlite3_exec(sdb,sql,NULL,NULL,&errmsg);
if(SQLITE_OK != ret)
{
util::error(here,1,0,"sqlite error %i: %s in SQL \"%s\"",ret,errmsg,sql);
::sqlite3_free(errmsg);
}
}
/** Execute arbitrary SQL. Discards any resulting columns. */
inline void
execf(const util::Here& here, sqlite3* sdb, const char* format, ...)
{
va_list args;
va_start(args,format);
char buf[256];
int ret = vsnprintf(buf,sizeof(buf),format,args);
if(ret >= static_cast<int>(sizeof(buf)))
CALI_ERRO(1,0,"SQL too large for buffer."); //??
sql::exec(here,sdb,buf);
va_end(args);
}
/** Query a single value, returns TRUE if the value was found. */
inline bool
query_val(
const util::Here& here,
sqlite3* sdb,
int& val, // output
const char* format, ...
)
{
bool result = false;
// Format the SQL.
va_list args;
va_start(args,format);
char sql[256];
int ret = vsnprintf(sql,sizeof(sql),format,args);
if(ret >= static_cast<int>(sizeof(sql)))
util::error(here,1,0,"SQL too large for buffer."); //??
va_end(args);
// Query
Statement select_stmt(here,sdb,sql);
int return_code = ::sqlite3_step(select_stmt);
switch(return_code)
{
case SQLITE_ROW:
result = true;
val = ::sqlite3_column_int(select_stmt,0);
break;
case SQLITE_DONE:
// No value.
break;
default:
// Error
sql::error(here,sdb);
}
return result;
}
} } // end namespace calendari::sql
#endif // CALENDARI__UTIL__SQL_H