Mon, 01 Dec 2008 02:56:40 +0000
Bugfix: fix connection in DBD.SQLite3.New
Improve handling for booleans accross DB types
1 | 1 | #include "dbd_sqlite3.h" |
2 | ||
2 | 3 | /* |
4 | * Converts SQLite types to Lua types | |
5 | */ | |
1 | 6 | static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) { |
7 | lua_push_type_t lua_type; | |
8 | ||
9 | switch(sqlite_type) { | |
10 | case SQLITE_NULL: | |
11 | lua_type = LUA_PUSH_NIL; | |
12 | break; | |
13 | ||
14 | case SQLITE_INTEGER: | |
15 | lua_type = LUA_PUSH_INTEGER; | |
16 | break; | |
17 | ||
18 | case SQLITE_FLOAT: | |
19 | lua_type = LUA_PUSH_NUMBER; | |
20 | break; | |
21 | ||
22 | default: | |
23 | lua_type = LUA_PUSH_STRING; | |
24 | } | |
25 | ||
26 | return lua_type; | |
27 | } | |
28 | ||
2 | 29 | /* |
30 | * runs sqlite3_step on a statement handle | |
31 | */ | |
1 | 32 | static int step(statement_t *statement) { |
33 | int res = sqlite3_step(statement->stmt); | |
34 | ||
35 | if (res == SQLITE_DONE) { | |
36 | statement->more_data = 0; | |
37 | return 1; | |
38 | } else if (res == SQLITE_ROW) { | |
39 | statement->more_data = 1; | |
40 | return 1; | |
41 | } | |
42 | ||
43 | return 0; | |
44 | } | |
45 | ||
2 | 46 | /* |
47 | * success = statement:close() | |
48 | */ | |
3 | 49 | int statement_close(lua_State *L) { |
1 | 50 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); |
51 | int ok = 0; | |
52 | ||
53 | if (statement->stmt) { | |
54 | if (sqlite3_finalize(statement->stmt) == SQLITE_OK) { | |
55 | ok = 1; | |
56 | } | |
3 | 57 | |
58 | statement->stmt = NULL; | |
1 | 59 | } |
60 | ||
61 | lua_pushboolean(L, ok); | |
62 | return 1; | |
63 | } | |
64 | ||
2 | 65 | /* |
3 | 66 | * success,err = statement:execute(...) |
2 | 67 | */ |
3 | 68 | int statement_execute(lua_State *L) { |
1 | 69 | int n = lua_gettop(L); |
70 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
71 | int p; | |
3 | 72 | int err = 0; |
73 | ||
74 | ||
75 | if (!statement->stmt) { | |
76 | lua_pushboolean(L, 0); | |
4 | 77 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 78 | return 2; |
79 | } | |
1 | 80 | |
2 | 81 | /* |
82 | * reset the handle before binding params | |
83 | * this wil be a NOP if the handle has not | |
84 | * been executed | |
85 | */ | |
1 | 86 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { |
87 | lua_pushboolean(L, 0); | |
4 | 88 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); |
3 | 89 | return 2; |
1 | 90 | } |
91 | ||
92 | for (p = 2; p <= n; p++) { | |
93 | int i = p - 1; | |
94 | ||
9 | 95 | int type = lua_type(L, p); |
96 | ||
97 | switch(type) { | |
98 | case LUA_TNIL: | |
99 | err = sqlite3_bind_null(statement->stmt, i) != SQLITE_OK; | |
100 | break; | |
101 | case LUA_TNUMBER: | |
102 | err = sqlite3_bind_double(statement->stmt, i, lua_tonumber(L, p)) != SQLITE_OK; | |
103 | break; | |
104 | case LUA_TSTRING: | |
105 | err = sqlite3_bind_text(statement->stmt, i, lua_tostring(L, p), -1, SQLITE_STATIC) != SQLITE_OK; | |
106 | break; | |
107 | case LUA_TBOOLEAN: | |
108 | err = sqlite3_bind_int(statement->stmt, i, lua_toboolean(L, p)) != SQLITE_OK; | |
109 | break; | |
110 | default: | |
111 | /* | |
112 | * Unknown/unsupported value type | |
113 | */ | |
114 | err = 1; | |
1 | 115 | } |
3 | 116 | |
9 | 117 | |
3 | 118 | if (err) |
119 | break; | |
1 | 120 | } |
121 | ||
4 | 122 | if (err) { |
3 | 123 | lua_pushboolean(L, 0); |
4 | 124 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite)); |
3 | 125 | return 2; |
126 | } | |
127 | ||
4 | 128 | if (!step(statement)) { |
129 | lua_pushboolean(L, 0); | |
130 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); | |
5 | 131 | return 2; |
4 | 132 | } |
133 | ||
3 | 134 | lua_pushboolean(L, 1); |
1 | 135 | return 1; |
136 | } | |
137 | ||
2 | 138 | /* |
139 | * must be called after an execute | |
140 | */ | |
1 | 141 | static int statement_fetch_impl(lua_State *L, int named_columns) { |
142 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
143 | int num_columns; | |
144 | ||
3 | 145 | if (!statement->stmt) { |
4 | 146 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 147 | return 0; |
148 | } | |
149 | ||
1 | 150 | if (!statement->more_data) { |
2 | 151 | /* |
152 | * Result set is empty, or not result set returned | |
153 | */ | |
154 | ||
1 | 155 | lua_pushnil(L); |
156 | return 1; | |
157 | } | |
158 | ||
159 | num_columns = sqlite3_column_count(statement->stmt); | |
160 | ||
161 | if (num_columns) { | |
162 | int i; | |
163 | int d = 1; | |
164 | ||
165 | lua_newtable(L); | |
166 | ||
167 | for (i = 0; i < num_columns; i++) { | |
168 | lua_push_type_t lua_push = sqlite_to_lua_push(sqlite3_column_type(statement->stmt, i)); | |
169 | const char *name = sqlite3_column_name(statement->stmt, i); | |
170 | ||
171 | if (lua_push == LUA_PUSH_NIL) { | |
172 | if (named_columns) { | |
173 | LUA_PUSH_ATTRIB_NIL(name); | |
174 | } else { | |
175 | LUA_PUSH_ARRAY_NIL(d); | |
176 | } | |
177 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
178 | int val = sqlite3_column_int(statement->stmt, i); | |
179 | ||
180 | if (named_columns) { | |
181 | LUA_PUSH_ATTRIB_INT(name, val); | |
182 | } else { | |
183 | LUA_PUSH_ARRAY_INT(d, val); | |
184 | } | |
185 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
186 | double val = sqlite3_column_double(statement->stmt, i); | |
187 | ||
188 | if (named_columns) { | |
189 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
190 | } else { | |
191 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
192 | } | |
193 | } else if (lua_push == LUA_PUSH_STRING) { | |
194 | const char *val = (const char *)sqlite3_column_text(statement->stmt, i); | |
195 | ||
196 | if (named_columns) { | |
197 | LUA_PUSH_ATTRIB_STRING(name, val); | |
198 | } else { | |
199 | LUA_PUSH_ARRAY_STRING(d, val); | |
200 | } | |
201 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
202 | int val = sqlite3_column_int(statement->stmt, i); | |
203 | ||
204 | if (named_columns) { | |
205 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
206 | } else { | |
207 | LUA_PUSH_ARRAY_BOOL(d, val); | |
208 | } | |
209 | } else { | |
4 | 210 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 211 | } |
212 | } | |
2 | 213 | } else { |
214 | /* | |
215 | * no columns returned by statement? | |
216 | */ | |
217 | lua_pushnil(L); | |
1 | 218 | } |
219 | ||
220 | if (step(statement) == 0) { | |
221 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { | |
2 | 222 | /* |
223 | * reset needs to be called to retrieve the 'real' error message | |
224 | */ | |
4 | 225 | luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->sqlite)); |
1 | 226 | } |
227 | } | |
228 | ||
229 | return 1; | |
230 | } | |
231 | ||
2 | 232 | /* |
233 | * array = statement:fetch() | |
234 | */ | |
1 | 235 | static int statement_fetch(lua_State *L) { |
236 | return statement_fetch_impl(L, 0); | |
237 | } | |
238 | ||
2 | 239 | /* |
240 | * hashmap = statement:fetchtable() | |
241 | */ | |
1 | 242 | static int statement_fetchtable(lua_State *L) { |
243 | return statement_fetch_impl(L, 1); | |
244 | } | |
245 | ||
2 | 246 | /* |
247 | * __gc | |
248 | */ | |
1 | 249 | static int statement_gc(lua_State *L) { |
250 | /* always free the handle */ | |
251 | statement_close(L); | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
257 | statement_t *statement = NULL; | |
258 | ||
259 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
260 | statement->sqlite = conn->sqlite; | |
261 | statement->stmt = NULL; | |
262 | statement->more_data = 0; | |
263 | ||
264 | if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { | |
265 | lua_pushnil(L); | |
4 | 266 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite)); |
3 | 267 | return 2; |
1 | 268 | } |
269 | ||
270 | luaL_getmetatable(L, DBD_SQLITE_STATEMENT); | |
271 | lua_setmetatable(L, -2); | |
272 | return 1; | |
273 | } | |
274 | ||
275 | int dbd_sqlite3_statement(lua_State *L) { | |
2 | 276 | static const luaL_Reg statement_methods[] = { |
277 | {"close", statement_close}, | |
278 | {"execute", statement_execute}, | |
279 | {"fetch", statement_fetch}, | |
280 | {"fetchtable", statement_fetchtable}, | |
281 | {NULL, NULL} | |
282 | }; | |
283 | ||
284 | static const luaL_Reg statement_class_methods[] = { | |
285 | {NULL, NULL} | |
286 | }; | |
287 | ||
1 | 288 | luaL_newmetatable(L, DBD_SQLITE_STATEMENT); |
289 | luaL_register(L, 0, statement_methods); | |
290 | lua_pushvalue(L,-1); | |
291 | lua_setfield(L, -2, "__index"); | |
292 | ||
293 | lua_pushcfunction(L, statement_gc); | |
294 | lua_setfield(L, -2, "__gc"); | |
295 | ||
296 | luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods); | |
297 | ||
298 | return 1; | |
299 | } |