Mon, 01 Dec 2008 07:22:44 +0000
Added new typechecks and errors
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; | |
10 | 72 | int errflag = 0; |
73 | const char *errstr = NULL; | |
74 | int expected_params; | |
75 | int num_bind_params = n - 1; | |
3 | 76 | |
77 | if (!statement->stmt) { | |
78 | lua_pushboolean(L, 0); | |
4 | 79 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 80 | return 2; |
81 | } | |
1 | 82 | |
2 | 83 | /* |
84 | * reset the handle before binding params | |
85 | * this wil be a NOP if the handle has not | |
86 | * been executed | |
87 | */ | |
1 | 88 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { |
89 | lua_pushboolean(L, 0); | |
4 | 90 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); |
3 | 91 | return 2; |
1 | 92 | } |
93 | ||
10 | 94 | expected_params = sqlite3_bind_parameter_count(statement->stmt); |
95 | if (expected_params != num_bind_params) { | |
96 | /* | |
97 | * sqlite3_reset does not handle this condition, | |
98 | * and the client library will fill unset params | |
99 | * with NULLs | |
100 | */ | |
101 | lua_pushboolean(L, 0); | |
102 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); | |
103 | return 2; | |
104 | } | |
105 | ||
1 | 106 | for (p = 2; p <= n; p++) { |
107 | int i = p - 1; | |
9 | 108 | int type = lua_type(L, p); |
10 | 109 | char err[64]; |
9 | 110 | |
111 | switch(type) { | |
112 | case LUA_TNIL: | |
10 | 113 | errflag = sqlite3_bind_null(statement->stmt, i) != SQLITE_OK; |
9 | 114 | break; |
115 | case LUA_TNUMBER: | |
10 | 116 | errflag = sqlite3_bind_double(statement->stmt, i, lua_tonumber(L, p)) != SQLITE_OK; |
9 | 117 | break; |
118 | case LUA_TSTRING: | |
10 | 119 | errflag = sqlite3_bind_text(statement->stmt, i, lua_tostring(L, p), -1, SQLITE_STATIC) != SQLITE_OK; |
9 | 120 | break; |
121 | case LUA_TBOOLEAN: | |
10 | 122 | errflag = sqlite3_bind_int(statement->stmt, i, lua_toboolean(L, p)) != SQLITE_OK; |
9 | 123 | break; |
124 | default: | |
125 | /* | |
126 | * Unknown/unsupported value type | |
127 | */ | |
10 | 128 | errflag = 1; |
129 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); | |
130 | errstr = err; | |
1 | 131 | } |
3 | 132 | |
10 | 133 | if (errflag) |
3 | 134 | break; |
1 | 135 | } |
136 | ||
10 | 137 | if (errflag) { |
3 | 138 | lua_pushboolean(L, 0); |
10 | 139 | if (errstr) |
140 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr); | |
141 | else | |
142 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite)); | |
143 | ||
3 | 144 | return 2; |
145 | } | |
146 | ||
4 | 147 | if (!step(statement)) { |
148 | lua_pushboolean(L, 0); | |
149 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); | |
5 | 150 | return 2; |
4 | 151 | } |
152 | ||
3 | 153 | lua_pushboolean(L, 1); |
1 | 154 | return 1; |
155 | } | |
156 | ||
2 | 157 | /* |
158 | * must be called after an execute | |
159 | */ | |
1 | 160 | static int statement_fetch_impl(lua_State *L, int named_columns) { |
161 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
162 | int num_columns; | |
163 | ||
3 | 164 | if (!statement->stmt) { |
4 | 165 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 166 | return 0; |
167 | } | |
168 | ||
1 | 169 | if (!statement->more_data) { |
2 | 170 | /* |
171 | * Result set is empty, or not result set returned | |
172 | */ | |
173 | ||
1 | 174 | lua_pushnil(L); |
175 | return 1; | |
176 | } | |
177 | ||
178 | num_columns = sqlite3_column_count(statement->stmt); | |
179 | ||
180 | if (num_columns) { | |
181 | int i; | |
182 | int d = 1; | |
183 | ||
184 | lua_newtable(L); | |
185 | ||
186 | for (i = 0; i < num_columns; i++) { | |
187 | lua_push_type_t lua_push = sqlite_to_lua_push(sqlite3_column_type(statement->stmt, i)); | |
188 | const char *name = sqlite3_column_name(statement->stmt, i); | |
189 | ||
190 | if (lua_push == LUA_PUSH_NIL) { | |
191 | if (named_columns) { | |
192 | LUA_PUSH_ATTRIB_NIL(name); | |
193 | } else { | |
194 | LUA_PUSH_ARRAY_NIL(d); | |
195 | } | |
196 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
197 | int val = sqlite3_column_int(statement->stmt, i); | |
198 | ||
199 | if (named_columns) { | |
200 | LUA_PUSH_ATTRIB_INT(name, val); | |
201 | } else { | |
202 | LUA_PUSH_ARRAY_INT(d, val); | |
203 | } | |
204 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
205 | double val = sqlite3_column_double(statement->stmt, i); | |
206 | ||
207 | if (named_columns) { | |
208 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
209 | } else { | |
210 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
211 | } | |
212 | } else if (lua_push == LUA_PUSH_STRING) { | |
213 | const char *val = (const char *)sqlite3_column_text(statement->stmt, i); | |
214 | ||
215 | if (named_columns) { | |
216 | LUA_PUSH_ATTRIB_STRING(name, val); | |
217 | } else { | |
218 | LUA_PUSH_ARRAY_STRING(d, val); | |
219 | } | |
220 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
221 | int val = sqlite3_column_int(statement->stmt, i); | |
222 | ||
223 | if (named_columns) { | |
224 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
225 | } else { | |
226 | LUA_PUSH_ARRAY_BOOL(d, val); | |
227 | } | |
228 | } else { | |
4 | 229 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 230 | } |
231 | } | |
2 | 232 | } else { |
233 | /* | |
234 | * no columns returned by statement? | |
235 | */ | |
236 | lua_pushnil(L); | |
1 | 237 | } |
238 | ||
239 | if (step(statement) == 0) { | |
240 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { | |
2 | 241 | /* |
242 | * reset needs to be called to retrieve the 'real' error message | |
243 | */ | |
4 | 244 | luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->sqlite)); |
1 | 245 | } |
246 | } | |
247 | ||
248 | return 1; | |
249 | } | |
250 | ||
2 | 251 | /* |
252 | * array = statement:fetch() | |
253 | */ | |
1 | 254 | static int statement_fetch(lua_State *L) { |
255 | return statement_fetch_impl(L, 0); | |
256 | } | |
257 | ||
2 | 258 | /* |
259 | * hashmap = statement:fetchtable() | |
260 | */ | |
1 | 261 | static int statement_fetchtable(lua_State *L) { |
262 | return statement_fetch_impl(L, 1); | |
263 | } | |
264 | ||
2 | 265 | /* |
266 | * __gc | |
267 | */ | |
1 | 268 | static int statement_gc(lua_State *L) { |
269 | /* always free the handle */ | |
270 | statement_close(L); | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
275 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
276 | statement_t *statement = NULL; | |
277 | ||
278 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
279 | statement->sqlite = conn->sqlite; | |
280 | statement->stmt = NULL; | |
281 | statement->more_data = 0; | |
282 | ||
283 | if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { | |
284 | lua_pushnil(L); | |
4 | 285 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite)); |
3 | 286 | return 2; |
1 | 287 | } |
288 | ||
289 | luaL_getmetatable(L, DBD_SQLITE_STATEMENT); | |
290 | lua_setmetatable(L, -2); | |
291 | return 1; | |
292 | } | |
293 | ||
294 | int dbd_sqlite3_statement(lua_State *L) { | |
2 | 295 | static const luaL_Reg statement_methods[] = { |
296 | {"close", statement_close}, | |
297 | {"execute", statement_execute}, | |
298 | {"fetch", statement_fetch}, | |
299 | {"fetchtable", statement_fetchtable}, | |
300 | {NULL, NULL} | |
301 | }; | |
302 | ||
303 | static const luaL_Reg statement_class_methods[] = { | |
304 | {NULL, NULL} | |
305 | }; | |
306 | ||
1 | 307 | luaL_newmetatable(L, DBD_SQLITE_STATEMENT); |
308 | luaL_register(L, 0, statement_methods); | |
309 | lua_pushvalue(L,-1); | |
310 | lua_setfield(L, -2, "__index"); | |
311 | ||
312 | lua_pushcfunction(L, statement_gc); | |
313 | lua_setfield(L, -2, "__gc"); | |
314 | ||
315 | luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods); | |
316 | ||
317 | return 1; | |
318 | } |