dbd/sqlite3/statement.c

changeset 11
b3e05e361f46
parent 10
3aa8a37a3dd8
child 12
014ba3ab3903
equal deleted inserted replaced
10:3aa8a37a3dd8 11:b3e05e361f46
155 } 155 }
156 156
157 /* 157 /*
158 * must be called after an execute 158 * must be called after an execute
159 */ 159 */
160 static int statement_fetch_impl(lua_State *L, int named_columns) { 160 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) {
161 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
162 int num_columns; 161 int num_columns;
163 162
164 if (!statement->stmt) { 163 if (!statement->stmt) {
165 luaL_error(L, DBI_ERR_FETCH_INVALID); 164 luaL_error(L, DBI_ERR_FETCH_INVALID);
166 return 0; 165 return 0;
246 } 245 }
247 246
248 return 1; 247 return 1;
249 } 248 }
250 249
251 /* 250 static int next_iterator(lua_State *L) {
252 * array = statement:fetch() 251 statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_SQLITE_STATEMENT);
253 */ 252 int named_columns = lua_toboolean(L, lua_upvalueindex(2));
253
254 return statement_fetch_impl(L, statement, named_columns);
255 }
256
257 /*
258 * iterfunc = statement:fetch(named_indexes)
259 */
260
254 static int statement_fetch(lua_State *L) { 261 static int statement_fetch(lua_State *L) {
255 return statement_fetch_impl(L, 0); 262 if (lua_gettop(L) == 1) {
256 } 263 lua_pushvalue(L, 1);
257 264 lua_pushboolean(L, 0);
258 /* 265 } else {
259 * hashmap = statement:fetchtable() 266 lua_pushvalue(L, 1);
260 */ 267 lua_pushboolean(L, lua_toboolean(L, 2));
261 static int statement_fetchtable(lua_State *L) { 268 }
262 return statement_fetch_impl(L, 1); 269
270 lua_pushcclosure(L, next_iterator, 2);
271 return 1;
272 }
273
274 /*
275 * table = statement:row(named_indexes)
276 */
277 static int statement_row(lua_State *L) {
278 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
279 int named_columns = lua_toboolean(L, 2);
280
281 return statement_fetch_impl(L, statement, named_columns);
263 } 282 }
264 283
265 /* 284 /*
266 * __gc 285 * __gc
267 */ 286 */
294 int dbd_sqlite3_statement(lua_State *L) { 313 int dbd_sqlite3_statement(lua_State *L) {
295 static const luaL_Reg statement_methods[] = { 314 static const luaL_Reg statement_methods[] = {
296 {"close", statement_close}, 315 {"close", statement_close},
297 {"execute", statement_execute}, 316 {"execute", statement_execute},
298 {"fetch", statement_fetch}, 317 {"fetch", statement_fetch},
299 {"fetchtable", statement_fetchtable}, 318 {"row", statement_row},
300 {NULL, NULL} 319 {NULL, NULL}
301 }; 320 };
302 321
303 static const luaL_Reg statement_class_methods[] = { 322 static const luaL_Reg statement_class_methods[] = {
304 {NULL, NULL} 323 {NULL, NULL}

mercurial