137 PGresult *result = NULL; |
137 PGresult *result = NULL; |
138 |
138 |
139 statement->tuple = 0; |
139 statement->tuple = 0; |
140 |
140 |
141 params = malloc(num_bind_params * sizeof(params)); |
141 params = malloc(num_bind_params * sizeof(params)); |
|
142 memset(params, 0, num_bind_params * sizeof(params)); |
142 |
143 |
143 /* |
144 /* |
144 * convert and copy parameters into a string array |
145 * convert and copy parameters into a string array |
145 */ |
146 */ |
146 for (p = 2; p <= n; p++) { |
147 for (p = 2; p <= n; p++) { |
212 } |
213 } |
213 |
214 |
214 /* |
215 /* |
215 * must be called after an execute |
216 * must be called after an execute |
216 */ |
217 */ |
217 static int statement_fetch_impl(lua_State *L, int named_columns) { |
218 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
218 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
|
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 |
222 |
223 if (!statement->result) { |
223 if (!statement->result) { |
235 return 1; |
235 return 1; |
236 } |
236 } |
237 |
237 |
238 num_columns = PQnfields(statement->result); |
238 num_columns = PQnfields(statement->result); |
239 lua_newtable(L); |
239 lua_newtable(L); |
|
240 int d = 1; |
240 for (i = 0; i < num_columns; i++) { |
241 for (i = 0; i < num_columns; i++) { |
241 int d = 1; |
|
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) { |
246 LUA_PUSH_ATTRIB_NIL(name); |
246 LUA_PUSH_ATTRIB_NIL(name); |
303 } |
303 } |
304 |
304 |
305 return 1; |
305 return 1; |
306 } |
306 } |
307 |
307 |
308 /* |
308 |
309 * array = statement:fetch() |
309 static int next_iterator(lua_State *L) { |
310 */ |
310 statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_POSTGRESQL_STATEMENT); |
|
311 int named_columns = lua_toboolean(L, lua_upvalueindex(2)); |
|
312 |
|
313 return statement_fetch_impl(L, statement, named_columns); |
|
314 } |
|
315 |
|
316 /* |
|
317 * iterfunc = statement:fetch(named_indexes) |
|
318 */ |
|
319 |
311 static int statement_fetch(lua_State *L) { |
320 static int statement_fetch(lua_State *L) { |
312 return statement_fetch_impl(L, 0); |
321 if (lua_gettop(L) == 1) { |
313 } |
322 lua_pushvalue(L, 1); |
314 |
323 lua_pushboolean(L, 0); |
315 /* |
324 } else { |
316 * hashmap = statement:fetchtable() |
325 lua_pushvalue(L, 1); |
317 */ |
326 lua_pushboolean(L, lua_toboolean(L, 2)); |
318 static int statement_fetchtable(lua_State *L) { |
327 } |
319 return statement_fetch_impl(L, 1); |
328 |
|
329 lua_pushcclosure(L, next_iterator, 2); |
|
330 return 1; |
|
331 } |
|
332 |
|
333 /* |
|
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); |
320 } |
341 } |
321 |
342 |
322 /* |
343 /* |
323 * __gc |
344 * __gc |
324 */ |
345 */ |
384 int dbd_postgresql_statement(lua_State *L) { |
405 int dbd_postgresql_statement(lua_State *L) { |
385 static const luaL_Reg statement_methods[] = { |
406 static const luaL_Reg statement_methods[] = { |
386 {"close", statement_close}, |
407 {"close", statement_close}, |
387 {"execute", statement_execute}, |
408 {"execute", statement_execute}, |
388 {"fetch", statement_fetch}, |
409 {"fetch", statement_fetch}, |
389 {"fetchtable", statement_fetchtable}, |
410 {"row", statement_row}, |
390 {NULL, NULL} |
411 {NULL, NULL} |
391 }; |
412 }; |
392 |
413 |
393 static const luaL_Reg statement_class_methods[] = { |
414 static const luaL_Reg statement_class_methods[] = { |
394 {NULL, NULL} |
415 {NULL, NULL} |