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