217 */ |
217 */ |
218 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
218 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
219 int tuple = statement->tuple++; |
219 int tuple = statement->tuple++; |
220 int i; |
220 int i; |
221 int num_columns; |
221 int num_columns; |
|
222 int d = 1; |
222 |
223 |
223 if (!statement->result) { |
224 if (!statement->result) { |
224 luaL_error(L, DBI_ERR_FETCH_INVALID); |
225 luaL_error(L, DBI_ERR_FETCH_INVALID); |
225 return 0; |
226 return 0; |
226 } |
227 } |
235 return 1; |
236 return 1; |
236 } |
237 } |
237 |
238 |
238 num_columns = PQnfields(statement->result); |
239 num_columns = PQnfields(statement->result); |
239 lua_newtable(L); |
240 lua_newtable(L); |
240 int d = 1; |
|
241 for (i = 0; i < num_columns; i++) { |
241 for (i = 0; i < num_columns; i++) { |
242 const char *name = PQfname(statement->result, i); |
242 const char *name = PQfname(statement->result, i); |
243 |
243 |
244 if (PQgetisnull(statement->result, tuple, i)) { |
244 if (PQgetisnull(statement->result, tuple, i)) { |
245 if (named_columns) { |
245 if (named_columns) { |
312 |
312 |
313 return statement_fetch_impl(L, statement, named_columns); |
313 return statement_fetch_impl(L, statement, named_columns); |
314 } |
314 } |
315 |
315 |
316 /* |
316 /* |
317 * iterfunc = statement:fetch(named_indexes) |
317 * table = statement:fetch(named_indexes) |
318 */ |
318 */ |
319 |
|
320 static int statement_fetch(lua_State *L) { |
319 static int statement_fetch(lua_State *L) { |
|
320 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
|
321 int named_columns = lua_toboolean(L, 2); |
|
322 |
|
323 return statement_fetch_impl(L, statement, named_columns); |
|
324 } |
|
325 |
|
326 /* |
|
327 * iterfunc = statement:rows(named_indexes) |
|
328 */ |
|
329 static int statement_rows(lua_State *L) { |
321 if (lua_gettop(L) == 1) { |
330 if (lua_gettop(L) == 1) { |
322 lua_pushvalue(L, 1); |
331 lua_pushvalue(L, 1); |
323 lua_pushboolean(L, 0); |
332 lua_pushboolean(L, 0); |
324 } else { |
333 } else { |
325 lua_pushvalue(L, 1); |
334 lua_pushvalue(L, 1); |
329 lua_pushcclosure(L, next_iterator, 2); |
338 lua_pushcclosure(L, next_iterator, 2); |
330 return 1; |
339 return 1; |
331 } |
340 } |
332 |
341 |
333 /* |
342 /* |
334 * table = statement:row(named_indexes) |
|
335 */ |
|
336 static int statement_row(lua_State *L) { |
|
337 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
|
338 int named_columns = lua_toboolean(L, 2); |
|
339 |
|
340 return statement_fetch_impl(L, statement, named_columns); |
|
341 } |
|
342 |
|
343 /* |
|
344 * __gc |
343 * __gc |
345 */ |
344 */ |
346 static int statement_gc(lua_State *L) { |
345 static int statement_gc(lua_State *L) { |
347 /* always free the handle */ |
346 /* always free the handle */ |
348 statement_close(L); |
347 statement_close(L); |
405 int dbd_postgresql_statement(lua_State *L) { |
404 int dbd_postgresql_statement(lua_State *L) { |
406 static const luaL_Reg statement_methods[] = { |
405 static const luaL_Reg statement_methods[] = { |
407 {"close", statement_close}, |
406 {"close", statement_close}, |
408 {"execute", statement_execute}, |
407 {"execute", statement_execute}, |
409 {"fetch", statement_fetch}, |
408 {"fetch", statement_fetch}, |
410 {"row", statement_row}, |
409 {"rows", statement_rows}, |
411 {NULL, NULL} |
410 {NULL, NULL} |
412 }; |
411 }; |
413 |
412 |
414 static const luaL_Reg statement_class_methods[] = { |
413 static const luaL_Reg statement_class_methods[] = { |
415 {NULL, NULL} |
414 {NULL, NULL} |