annotate src/lib-sql/driver-sqlite.c @ 4444:d0d04db8e7a6 HEAD

Escape ' with '' instead of with \'.
author Timo Sirainen <tss@iki.fi>
date Tue, 27 Jun 2006 12:25:55 +0300
parents 1a98cb709395
children 3e196acd60b7
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
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
7 #ifdef BUILD_SQLITE
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include <sqlite3.h>
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 /* retry time if db is busy (in ms) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 const int sqlite_busy_timeout = 1000;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
13 struct sqlite_db {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
14 struct sql_db api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 pool_t pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 const char *dbfile;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18 sqlite3 *sqlite;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 unsigned int connected:1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
20 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 struct sqlite_result {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 struct sql_result api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 sqlite3_stmt *stmt;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 unsigned int cols;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 const char **row;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 struct sqlite_transaction_context {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31 struct sql_transaction_context ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 unsigned int failed:1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
35 extern struct sql_db driver_sqlite_db;
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 extern struct sql_result driver_sqlite_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 extern struct sql_result driver_sqlite_error_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39 static int driver_sqlite_connect(struct sql_db *_db)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
41 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43 if (db->connected)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
46 db->rc = sqlite3_open(db->dbfile, &db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
47
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
48 if (db->rc == SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49 db->connected = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 sqlite3_busy_timeout(db->sqlite, sqlite_busy_timeout);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53 i_error("sqlite: open(%s) failed: %s", db->dbfile,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 sqlite3_errmsg(db->sqlite));
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 sqlite3_close(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 }
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
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
60 static struct sql_db *_driver_sqlite_init(const char *connect_string)
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 struct sqlite_db *db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
63 pool_t pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
64
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
65 i_assert(connect_string != NULL);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
66
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
67 pool = pool_alloconly_create("sqlite driver", 512);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
68 db = p_new(pool, struct sqlite_db, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
69 db->pool = pool;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
70 db->api = driver_sqlite_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
71 db->dbfile = p_strdup(db->pool, connect_string);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
72 db->connected = FALSE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
74 return &db->api;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
75 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
76
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
77 static void _driver_sqlite_deinit(struct sql_db *_db)
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
80
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
81 sqlite3_close(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
82 pool_unref(db->pool);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
83 }
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 static enum sql_db_flags
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
86 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
87 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
88 return SQL_DB_FLAG_BLOCKING;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
89 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
90
4294
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
91 static char *driver_sqlite_escape_string(struct sql_db *_db __attr_unused__,
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
92 const char *string)
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
93 {
4444
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
94 const char *p;
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
95 char *dest, *destbegin;
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
96
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
97 /* find the first ' */
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
98 for (p = string; *p != '\''; p++) {
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
99 if (*p == '\0')
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
100 return t_strdup_noconst(string);
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
101 }
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
102
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
103 /* @UNSAFE: escape ' with '' */
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
104 dest = destbegin = t_buffer_get((p - string) + strlen(string) * 2 + 1);
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
105
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
106 memcpy(dest, string, p - string);
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
107 dest += p - string;
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
108
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
109 for (; *p != '\0'; p++) {
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
110 *dest++ = *p;
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
111 if (*p == '\'')
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
112 *dest++ = *p;
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
113 }
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
114 *dest++ = '\0';
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
115 t_buffer_alloc(dest - destbegin);
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
116
d0d04db8e7a6 Escape ' with '' instead of with \'.
Timo Sirainen <tss@iki.fi>
parents: 4294
diff changeset
117 return destbegin;
4294
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
118 }
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
119
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 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
121 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
122 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 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
125 if (db->rc != SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 i_error("sqlite: exec(%s) failed: %s (%d)",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 query, sqlite3_errmsg(db->sqlite), db->rc);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
130
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
131 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
132 sql_query_callback_t *callback, void *context)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
133 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 struct sql_result *result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
135
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136 result = sql_query_s(db, query);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 result->callback = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
138 callback(result, context);
3946
d7051c7a0200 Fixed memory leaks
Timo Sirainen <tss@iki.fi>
parents: 3943
diff changeset
139 result->callback = FALSE;
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 sql_result_free(result);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
143 static struct sql_result *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 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
145 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 struct sqlite_result *result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150 result = i_new(struct sqlite_result, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152 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
153 if (rc == SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 result->api = driver_sqlite_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
155 result->cols = sqlite3_column_count(result->stmt);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 result->row = i_new(const char *, result->cols);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 result->api = driver_sqlite_error_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 result->stmt = NULL;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 result->cols = 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 result->api.db = _db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 return &result->api;
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 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
168 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
169 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170 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
171 int rc;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172
3947
5b21fb41d14d Cleanups.
Timo Sirainen <tss@iki.fi>
parents: 3946
diff changeset
173 if (_result->callback)
5b21fb41d14d Cleanups.
Timo Sirainen <tss@iki.fi>
parents: 3946
diff changeset
174 return;
5b21fb41d14d Cleanups.
Timo Sirainen <tss@iki.fi>
parents: 3946
diff changeset
175
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
176 if (result->stmt != NULL) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177 if ((rc = sqlite3_finalize(result->stmt)) != SQLITE_OK) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 i_warning("sqlite: finalize failed: %s (%d)",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 sqlite3_errmsg(db->sqlite), rc);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 i_free(result->row);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183 i_free(result);
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
186 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
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 switch (sqlite3_step(result->stmt)) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 case SQLITE_ROW:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 return 1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 case SQLITE_DONE:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 return 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 default:
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 return -1;
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 }
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 static unsigned int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 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
202 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 struct sqlite_result *result = (struct sqlite_result *)_result;
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 return result->cols;
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 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
210 unsigned int idx)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
212 struct sqlite_result *result = (struct sqlite_result *)_result;
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 return sqlite3_column_name(result->stmt, 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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 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
218 const char *field_name)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
219 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
220 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
221 unsigned int i;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
222
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
223 for (i = 0; i < result->cols; ++i) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
224 const char *col = sqlite3_column_name(result->stmt, i);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
225
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 if (strcmp(col, field_name) == 0)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 return i;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
228 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
230 return -1;
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 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 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
235 unsigned int idx)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
236 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238
3936
8e827b05047b Compiler warning fix. Patch by Marcus Rueckert
Timo Sirainen <tss@iki.fi>
parents: 3923
diff changeset
239 return (const char*)sqlite3_column_text(result->stmt, idx);
3923
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 static const char *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243 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
244 const char *field_name)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
246 int idx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
248 idx = driver_sqlite_result_find_field(result, field_name);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
249 if (idx < 0)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250 return NULL;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 return driver_sqlite_result_get_field_value(result, idx);
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
254 static const char *const *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 driver_sqlite_result_get_values(struct sql_result *_result)
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 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 unsigned int i;
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 for (i = 0; i < result->cols; ++i) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
261 result->row[i] =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
262 driver_sqlite_result_get_field_value(_result, i);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
264
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
265 return (const char *const *)result->row;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
267
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
268 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
269 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
270 struct sqlite_result *result = (struct sqlite_result *)_result;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
271 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
272
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
273 return sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 static struct sql_transaction_context *
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 driver_sqlite_transaction_begin(struct sql_db *_db)
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 struct sqlite_transaction_context *ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280 struct sqlite_db *db = (struct sqlite_db *)_db;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
281
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 ctx = i_new(struct sqlite_transaction_context, 1);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
283 ctx->ctx.db = _db;
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 sql_exec(_db, "BEGIN TRANSACTION");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
286 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
287 ctx->failed = TRUE;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
288
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
289 return &ctx->ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 driver_sqlite_transaction_rollback(struct sql_transaction_context *_ctx)
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 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298 sql_exec(_ctx->db, "ROLLBACK");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299 i_free(ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
301
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 driver_sqlite_transaction_commit(struct sql_transaction_context *_ctx,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
304 sql_commit_callback_t *callback, void *context)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
305 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
306 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
307 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
308 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
309 const char *errmsg;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
310
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
311 if (!ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
312 sql_exec(_ctx->db, "COMMIT");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
313 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
314 ctx->failed = TRUE;
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
317 if (ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
318 errmsg = sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
319 callback(errmsg, context);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
320 /* also does i_free(ctx) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
321 driver_sqlite_transaction_rollback(_ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
322 } else {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
323 callback(NULL, context);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
324 i_free(ctx);
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 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
327
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
328 static int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
329 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
330 const char **error_r)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
332 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
333 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
334 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
335
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
336 if (ctx->failed) {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 /* also does i_free(ctx) */
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
338 driver_sqlite_transaction_rollback(_ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340 }
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 sql_exec(_ctx->db, "COMMIT");
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
343 *error_r = sqlite3_errmsg(db->sqlite);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
344 i_free(ctx);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
345 return 0;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
346 }
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 static void
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
349 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
350 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
351 struct sqlite_transaction_context *ctx =
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
352 (struct sqlite_transaction_context *)_ctx;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
353 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
354
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
355 if (ctx->failed)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
356 return;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
357
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 sql_exec(_ctx->db, query);
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 if (db->rc != SQLITE_OK)
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
360 ctx->failed = TRUE;
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
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
363 struct sql_db driver_sqlite_db = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
364 "sqlite",
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
365
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
366 _driver_sqlite_init,
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
367 _driver_sqlite_deinit,
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
368 driver_sqlite_get_flags,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
369 driver_sqlite_connect,
4294
1a98cb709395 Added sql_escape_string()
Timo Sirainen <tss@iki.fi>
parents: 3947
diff changeset
370 driver_sqlite_escape_string,
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 driver_sqlite_exec,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372 driver_sqlite_query,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373 driver_sqlite_query_s,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 driver_sqlite_transaction_begin,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
376 driver_sqlite_transaction_commit,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 driver_sqlite_transaction_commit_s,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378 driver_sqlite_transaction_rollback,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 driver_sqlite_update
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 struct sql_result driver_sqlite_result = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 driver_sqlite_result_free,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 driver_sqlite_result_next_row,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387 driver_sqlite_result_get_fields_count,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
388 driver_sqlite_result_get_field_name,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
389 driver_sqlite_result_find_field,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
390 driver_sqlite_result_get_field_value,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
391 driver_sqlite_result_find_field_value,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
392 driver_sqlite_result_get_values,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
393 driver_sqlite_result_get_error,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
394
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
395 FALSE
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
396 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
397
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
398 static int
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
399 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
400 {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
401 return -1;
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
402 }
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
403
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
404 struct sql_result driver_sqlite_error_result = {
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
405 NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
406
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
407 driver_sqlite_result_free,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
408 driver_sqlite_result_error_next_row,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
409 NULL, NULL, NULL, NULL, NULL, NULL,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410 driver_sqlite_result_get_error,
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
411
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
412 FALSE
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
413 };
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414
3943
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
415 void driver_sqlite_init(void);
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
416 void driver_sqlite_deinit(void);
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
417
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
418 void driver_sqlite_init(void)
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
419 {
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
420 sql_driver_register(&driver_sqlite_db);
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
421 }
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
422
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
423 void driver_sqlite_deinit(void)
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
424 {
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
425 sql_driver_unregister(&driver_sqlite_db);
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
426 }
cbe5c6772e0d Added support for dynamically building SQL drivers.
Timo Sirainen <tss@iki.fi>
parents: 3936
diff changeset
427
3923
e9d45ec624cb Forgot to add in sqlite patch.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
428 #endif