Wed, 26 Nov 2008 10:01:03 +0000
Cleanup and 'assert' error handling.
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); | |
77 | lua_pushstring(L, "execute called on a closed or invalid handle"); | |
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); | |
3 | 88 | lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); |
89 | return 2; | |
1 | 90 | } |
91 | ||
92 | for (p = 2; p <= n; p++) { | |
93 | int i = p - 1; | |
94 | ||
95 | if (lua_isnil(L, p)) { | |
96 | if (sqlite3_bind_null(statement->stmt, i) != SQLITE_OK) { | |
3 | 97 | err = 1; |
1 | 98 | } |
99 | } else if (lua_isnumber(L, p)) { | |
100 | if (sqlite3_bind_double(statement->stmt, i, luaL_checknumber(L, p)) != SQLITE_OK) { | |
3 | 101 | err = 1; |
1 | 102 | } |
103 | } else if (lua_isstring(L, p)) { | |
104 | if (sqlite3_bind_text(statement->stmt, i, luaL_checkstring(L, p), -1, SQLITE_STATIC) != SQLITE_OK) { | |
3 | 105 | err = 1; |
1 | 106 | } |
107 | } | |
3 | 108 | |
109 | if (err) | |
110 | break; | |
1 | 111 | } |
112 | ||
3 | 113 | if (err || step(statement) == 0) { |
114 | lua_pushboolean(L, 0); | |
115 | lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); | |
116 | return 2; | |
117 | } | |
118 | ||
119 | lua_pushboolean(L, 1); | |
1 | 120 | return 1; |
121 | } | |
122 | ||
2 | 123 | /* |
124 | * must be called after an execute | |
125 | */ | |
1 | 126 | static int statement_fetch_impl(lua_State *L, int named_columns) { |
127 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
128 | int num_columns; | |
129 | ||
3 | 130 | if (!statement->stmt) { |
131 | luaL_error(L, "fetch called on a closed or invalid handle"); | |
132 | return 0; | |
133 | } | |
134 | ||
1 | 135 | if (!statement->more_data) { |
2 | 136 | /* |
137 | * Result set is empty, or not result set returned | |
138 | */ | |
139 | ||
1 | 140 | lua_pushnil(L); |
141 | return 1; | |
142 | } | |
143 | ||
144 | num_columns = sqlite3_column_count(statement->stmt); | |
145 | ||
146 | if (num_columns) { | |
147 | int i; | |
148 | int d = 1; | |
149 | ||
150 | lua_newtable(L); | |
151 | ||
152 | for (i = 0; i < num_columns; i++) { | |
153 | lua_push_type_t lua_push = sqlite_to_lua_push(sqlite3_column_type(statement->stmt, i)); | |
154 | const char *name = sqlite3_column_name(statement->stmt, i); | |
155 | ||
156 | if (lua_push == LUA_PUSH_NIL) { | |
157 | if (named_columns) { | |
158 | LUA_PUSH_ATTRIB_NIL(name); | |
159 | } else { | |
160 | LUA_PUSH_ARRAY_NIL(d); | |
161 | } | |
162 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
163 | int val = sqlite3_column_int(statement->stmt, i); | |
164 | ||
165 | if (named_columns) { | |
166 | LUA_PUSH_ATTRIB_INT(name, val); | |
167 | } else { | |
168 | LUA_PUSH_ARRAY_INT(d, val); | |
169 | } | |
170 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
171 | double val = sqlite3_column_double(statement->stmt, i); | |
172 | ||
173 | if (named_columns) { | |
174 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
175 | } else { | |
176 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
177 | } | |
178 | } else if (lua_push == LUA_PUSH_STRING) { | |
179 | const char *val = (const char *)sqlite3_column_text(statement->stmt, i); | |
180 | ||
181 | if (named_columns) { | |
182 | LUA_PUSH_ATTRIB_STRING(name, val); | |
183 | } else { | |
184 | LUA_PUSH_ARRAY_STRING(d, val); | |
185 | } | |
186 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
187 | int val = sqlite3_column_int(statement->stmt, i); | |
188 | ||
189 | if (named_columns) { | |
190 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
191 | } else { | |
192 | LUA_PUSH_ARRAY_BOOL(d, val); | |
193 | } | |
194 | } else { | |
195 | luaL_error(L, "Unknown push type in result set"); | |
196 | } | |
197 | } | |
2 | 198 | } else { |
199 | /* | |
200 | * no columns returned by statement? | |
201 | */ | |
202 | lua_pushnil(L); | |
1 | 203 | } |
204 | ||
205 | if (step(statement) == 0) { | |
206 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { | |
2 | 207 | /* |
208 | * reset needs to be called to retrieve the 'real' error message | |
209 | */ | |
1 | 210 | luaL_error(L, "Failed to fetch statement: %s", sqlite3_errmsg(statement->sqlite)); |
211 | } | |
212 | } | |
213 | ||
214 | return 1; | |
215 | } | |
216 | ||
2 | 217 | /* |
218 | * array = statement:fetch() | |
219 | */ | |
1 | 220 | static int statement_fetch(lua_State *L) { |
221 | return statement_fetch_impl(L, 0); | |
222 | } | |
223 | ||
2 | 224 | /* |
225 | * hashmap = statement:fetchtable() | |
226 | */ | |
1 | 227 | static int statement_fetchtable(lua_State *L) { |
228 | return statement_fetch_impl(L, 1); | |
229 | } | |
230 | ||
2 | 231 | /* |
232 | * __gc | |
233 | */ | |
1 | 234 | static int statement_gc(lua_State *L) { |
235 | /* always free the handle */ | |
236 | statement_close(L); | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
242 | statement_t *statement = NULL; | |
243 | ||
244 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
245 | statement->sqlite = conn->sqlite; | |
246 | statement->stmt = NULL; | |
247 | statement->more_data = 0; | |
248 | ||
249 | if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { | |
250 | lua_pushnil(L); | |
3 | 251 | lua_pushfstring(L, "Failed to prepare statement: %s", sqlite3_errmsg(statement->sqlite)); |
252 | return 2; | |
1 | 253 | } |
254 | ||
255 | luaL_getmetatable(L, DBD_SQLITE_STATEMENT); | |
256 | lua_setmetatable(L, -2); | |
257 | return 1; | |
258 | } | |
259 | ||
260 | int dbd_sqlite3_statement(lua_State *L) { | |
2 | 261 | static const luaL_Reg statement_methods[] = { |
262 | {"close", statement_close}, | |
263 | {"execute", statement_execute}, | |
264 | {"fetch", statement_fetch}, | |
265 | {"fetchtable", statement_fetchtable}, | |
266 | {NULL, NULL} | |
267 | }; | |
268 | ||
269 | static const luaL_Reg statement_class_methods[] = { | |
270 | {NULL, NULL} | |
271 | }; | |
272 | ||
1 | 273 | luaL_newmetatable(L, DBD_SQLITE_STATEMENT); |
274 | luaL_register(L, 0, statement_methods); | |
275 | lua_pushvalue(L,-1); | |
276 | lua_setfield(L, -2, "__index"); | |
277 | ||
278 | lua_pushcfunction(L, statement_gc); | |
279 | lua_setfield(L, -2, "__gc"); | |
280 | ||
281 | luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods); | |
282 | ||
283 | return 1; | |
284 | } |