annotate src/lib-sql/driver-sqlite.c @ 3923:e9d45ec624cb HEAD

Forgot to add in sqlite patch.
author Timo Sirainen <tss@iki.fi>
date Sun, 22 Jan 2006 14:57:03 +0200
parents
children 8e827b05047b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2006 Jakob Hirsch */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "str.h"
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "sql-api-private.h"
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #ifdef HAVE_SQLITE
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include <stdlib.h>
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 #include <time.h>
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include <sqlite3.h>
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 /* retry time if db is busy (in ms) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 const int sqlite_busy_timeout = 1000;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 struct sqlite_db {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 struct sql_db api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 pool_t pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 const char *dbfile;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 sqlite3 *sqlite;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 unsigned int connected:1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 struct sqlite_result {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 struct sql_result api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 sqlite3_stmt *stmt;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 unsigned int cols;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 const char **row;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 struct sqlite_transaction_context {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 struct sql_transaction_context ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 unsigned int failed:1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 extern struct sql_result driver_sqlite_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 extern struct sql_result driver_sqlite_error_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 static int driver_sqlite_connect(struct sql_db *_db)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 if (db->connected)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47 db->rc = sqlite3_open(db->dbfile, &db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 if (db->rc == SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 db->connected = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 sqlite3_busy_timeout(db->sqlite, sqlite_busy_timeout);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 i_error("sqlite: open(%s) failed: %s", db->dbfile,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 sqlite3_errmsg(db->sqlite));
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 sqlite3_close(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
59 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 static struct sql_db *driver_sqlite_init(const char *connect_string)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 struct sqlite_db *db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64 pool_t pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66 i_assert(connect_string != NULL);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 pool = pool_alloconly_create("sqlite driver", 512);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 db = p_new(pool, struct sqlite_db, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 db->pool = pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 db->api = driver_sqlite_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 db->dbfile = p_strdup(db->pool, connect_string);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73 db->connected = FALSE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 return &db->api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 static void driver_sqlite_deinit(struct sql_db *_db)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 sqlite3_close(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 pool_unref(db->pool);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
84 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 static enum sql_db_flags
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
87 driver_sqlite_get_flags(struct sql_db *db __attr_unused__)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 return SQL_DB_FLAG_BLOCKING;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
91
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
92 static void driver_sqlite_exec(struct sql_db *_db, const char *query)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
93 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
94 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
95
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
96 db->rc = sqlite3_exec(db->sqlite, query, NULL, 0, NULL);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
97 if (db->rc != SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
98 i_error("sqlite: exec(%s) failed: %s (%d)",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
99 query, sqlite3_errmsg(db->sqlite), db->rc);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
100 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
101 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
102
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
103 static void driver_sqlite_query(struct sql_db *db, const char *query,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
104 sql_query_callback_t *callback, void *context)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
105 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 struct sql_result *result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 result = sql_query_s(db, query);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
109 result->callback = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 callback(result, context);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 sql_result_free(result);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 static struct sql_result *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115 driver_sqlite_query_s(struct sql_db *_db, const char *query)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 struct sqlite_result *result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 result = i_new(struct sqlite_result, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 rc = sqlite3_prepare(db->sqlite, query, -1, &result->stmt, NULL);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 if (rc == SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125 result->api = driver_sqlite_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 result->cols = sqlite3_column_count(result->stmt);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 result->row = i_new(const char *, result->cols);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 result->api = driver_sqlite_error_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130 result->stmt = NULL;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 result->cols = 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
132 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 result->api.db = _db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135 return &result->api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 static void driver_sqlite_result_free(struct sql_result *_result)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 struct sqlite_db *db = (struct sqlite_db *) result->api.db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 if (result->stmt != NULL) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 if ((rc = sqlite3_finalize(result->stmt)) != SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 i_warning("sqlite: finalize failed: %s (%d)",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 sqlite3_errmsg(db->sqlite), rc);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 i_free(result->row);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 i_free(result);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 static int driver_sqlite_result_next_row(struct sql_result *_result)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 switch (sqlite3_step(result->stmt)) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 case SQLITE_ROW:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 case SQLITE_DONE:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 return 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 default:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
168 static unsigned int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 driver_sqlite_result_get_fields_count(struct sql_result *_result)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 return result->cols;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 driver_sqlite_result_get_field_name(struct sql_result *_result,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 unsigned int idx)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 return sqlite3_column_name(result->stmt, idx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
184
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
185 static int driver_sqlite_result_find_field(struct sql_result *_result,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 const char *field_name)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
187 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
188 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189 unsigned int i;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 for (i = 0; i < result->cols; ++i) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 const char *col = sqlite3_column_name(result->stmt, i);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 if (strcmp(col, field_name) == 0)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 return i;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
198 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 driver_sqlite_result_get_field_value(struct sql_result *_result,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 unsigned int idx)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
205 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
206
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207 return sqlite3_column_text(result->stmt, idx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 driver_sqlite_result_find_field_value(struct sql_result *result,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 const char *field_name)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
213 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
214 int idx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 idx = driver_sqlite_result_find_field(result, field_name);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 if (idx < 0)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 return NULL;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 return driver_sqlite_result_get_field_value(result, idx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222 static const char *const *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 driver_sqlite_result_get_values(struct sql_result *_result)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 unsigned int i;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 for (i = 0; i < result->cols; ++i) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 result->row[i] =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 driver_sqlite_result_get_field_value(_result, i);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 return (const char *const *)result->row;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236 static const char *driver_sqlite_result_get_error(struct sql_result *_result)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239 struct sqlite_db *db = (struct sqlite_db *)result->api.db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241 return sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244 static struct sql_transaction_context *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245 driver_sqlite_transaction_begin(struct sql_db *_db)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 struct sqlite_transaction_context *ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 ctx = i_new(struct sqlite_transaction_context, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 ctx->ctx.db = _db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
252
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 sql_exec(_db, "BEGIN TRANSACTION");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 ctx->failed = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257 return &ctx->ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
259
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 driver_sqlite_transaction_rollback(struct sql_transaction_context *_ctx)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 sql_exec(_ctx->db, "ROLLBACK");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267 i_free(ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
269
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 driver_sqlite_transaction_commit(struct sql_transaction_context *_ctx,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
272 sql_commit_callback_t *callback, void *context)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 struct sqlite_db *db = (struct sqlite_db *)ctx->ctx.db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 const char *errmsg;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279 if (!ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 sql_exec(_ctx->db, "COMMIT");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 ctx->failed = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
284
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
285 if (ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 errmsg = sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 callback(errmsg, context);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288 /* also does i_free(ctx) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289 driver_sqlite_transaction_rollback(_ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 callback(NULL, context);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 i_free(ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 static int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 driver_sqlite_transaction_commit_s(struct sql_transaction_context *_ctx,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 const char **error_r)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 struct sqlite_db *db = (struct sqlite_db *) ctx->ctx.db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 if (ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 /* also does i_free(ctx) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 driver_sqlite_transaction_rollback(_ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
309
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310 sql_exec(_ctx->db, "COMMIT");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311 *error_r = sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 i_free(ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 return 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
315
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
316 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 driver_sqlite_update(struct sql_transaction_context *_ctx, const char *query)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321 struct sqlite_db *db = (struct sqlite_db *)ctx->ctx.db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323 if (ctx->failed)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 return;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
325
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
326 sql_exec(_ctx->db, query);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328 ctx->failed = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
330
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 struct sql_db driver_sqlite_db = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 "sqlite",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 driver_sqlite_init,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
335 driver_sqlite_deinit,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 driver_sqlite_get_flags,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 driver_sqlite_connect,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 driver_sqlite_exec,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 driver_sqlite_query,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 driver_sqlite_query_s,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
341
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
342 driver_sqlite_transaction_begin,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 driver_sqlite_transaction_commit,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 driver_sqlite_transaction_commit_s,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
345 driver_sqlite_transaction_rollback,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
346 driver_sqlite_update
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
347 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
348
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 struct sql_result driver_sqlite_result = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
350 NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 driver_sqlite_result_free,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 driver_sqlite_result_next_row,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
354 driver_sqlite_result_get_fields_count,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355 driver_sqlite_result_get_field_name,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 driver_sqlite_result_find_field,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357 driver_sqlite_result_get_field_value,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 driver_sqlite_result_find_field_value,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 driver_sqlite_result_get_values,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 driver_sqlite_result_get_error,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
362 FALSE
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365 static int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
366 driver_sqlite_result_error_next_row(struct sql_result *result __attr_unused__)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
367 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
370
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 struct sql_result driver_sqlite_error_result = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 driver_sqlite_result_free,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 driver_sqlite_result_error_next_row,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
376 NULL, NULL, NULL, NULL, NULL, NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 driver_sqlite_result_get_error,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 FALSE
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
381
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382 #endif