67 static void *sqlite3_open_conn(
const DBConf *db_conf)
77 error(0,
"SQLite3: can not open or create database file `%s'!",
79 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
89 info(0,
"SQLite3: library version %s.", sqlite3_version);
98 static void sqlite3_close_conn(
void *conn)
106 rc = sqlite3_close((sqlite3*) conn);
107 }
while (rc == SQLITE_BUSY);
109 if (rc == SQLITE_ERROR) {
110 error(0,
"SQLite3: error while closing database file.");
115 static int sqlite3_check_conn(
void *conn)
126 static void sqlite3_conf_destroy(
DBConf *db_conf)
136 static int sqlite3_select(
void *theconn,
const Octstr *sql,
List *binds,
List **res)
138 sqlite3 *db = theconn;
145 int binds_len = (binds ?
gwlist_len(binds) : 0);
150 #if SQLITE_VERSION_NUMBER >= 3003009 155 if (SQLITE_OK !=
status) {
156 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
161 for (i = 0; i < binds_len; i++) {
164 if (SQLITE_OK !=
status) {
165 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
166 sqlite3_finalize(stmt);
173 while ((
status = sqlite3_step(stmt)) == SQLITE_ROW) {
174 columns = sqlite3_data_count(stmt);
175 debug(
"dbpool.sqlite3",0,
"SQL has %d columns", columns);
177 for (i = 0; i < columns; i++) {
178 if (sqlite3_column_type(stmt, i) == SQLITE_NULL) {
189 if (SQLITE_DONE !=
status) {
190 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
195 sqlite3_finalize(stmt);
199 sqlite3_finalize(stmt);
205 static int sqlite3_update(
void *theconn,
const Octstr *sql,
List *binds)
207 sqlite3 *db = theconn;
213 int binds_len = (binds ?
gwlist_len(binds) : 0);
216 #if SQLITE_VERSION_NUMBER >= 3003009 221 if (SQLITE_OK !=
status) {
222 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
225 debug(
"dbpool.sqlite3",0,
"sqlite3_prepare done");
228 for (i = 0; i < binds_len; i++) {
231 if (SQLITE_OK !=
status) {
232 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
233 sqlite3_finalize(stmt);
239 if ((
status = sqlite3_step(stmt)) != SQLITE_DONE) {
240 error(0,
"SQLite3: %s", sqlite3_errmsg(db));
241 sqlite3_finalize(stmt);
244 debug(
"dbpool.sqlite3",0,
"sqlite3_step done");
246 rows = sqlite3_changes(db);
247 debug(
"dbpool.sqlite3",0,
"rows processed = %d", rows);
249 sqlite3_finalize(stmt);
254 static struct db_ops sqlite3_ops = {
255 .
open = sqlite3_open_conn,
256 .close = sqlite3_close_conn,
257 .check = sqlite3_check_conn,
258 .conf_destroy = sqlite3_conf_destroy,
259 .select = sqlite3_select,
260 .update = sqlite3_update
void error(int err, const char *fmt,...)
void info(int err, const char *fmt,...)
void gwlist_append(List *list, void *item)
long gwlist_len(List *list)
void * gwlist_get(List *list, long pos)
#define octstr_get_cstr(ostr)
void * gwlist_extract_first(List *list)
void octstr_destroy(Octstr *ostr)
#define octstr_create(cstr)
void octstr_destroy_item(void *os)
void gwlist_insert(List *list, long pos, void *item)
long octstr_len(const Octstr *ostr)
void debug(const char *place, int err, const char *fmt,...)
void *(* open)(const DBConf *conf)
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)