178 } |
178 } |
179 } |
179 } |
180 free(params); |
180 free(params); |
181 |
181 |
182 if (!result) { |
182 if (!result) { |
183 luaL_error(L, "Unable to allocate result handle"); |
|
184 lua_pushboolean(L, 0); |
183 lua_pushboolean(L, 0); |
185 return 1; |
184 lua_pushstring(L, "Unable to allocate result handle"); |
|
185 return 2; |
186 } |
186 } |
187 |
187 |
188 status = PQresultStatus(result); |
188 status = PQresultStatus(result); |
189 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { |
189 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { |
190 luaL_error(L, "Unable to execute statment: %s", PQresultErrorMessage(result)); |
|
191 lua_pushboolean(L, 0); |
190 lua_pushboolean(L, 0); |
192 return 1; |
191 lua_pushfstring(L, "Unable to execute statment: %s", PQresultErrorMessage(result)); |
|
192 return 2; |
193 } |
193 } |
194 |
194 |
195 statement->result = result; |
195 statement->result = result; |
196 |
196 |
197 lua_pushboolean(L, 1); |
197 lua_pushboolean(L, 1); |
|
198 lua_pushnil(L); |
198 return 1; |
199 return 1; |
199 } |
200 } |
200 |
201 |
201 /* |
202 /* |
202 * must be called after an execute |
203 * must be called after an execute |
204 static int statement_fetch_impl(lua_State *L, int named_columns) { |
205 static int statement_fetch_impl(lua_State *L, int named_columns) { |
205 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
206 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
206 int tuple = statement->tuple++; |
207 int tuple = statement->tuple++; |
207 int i; |
208 int i; |
208 int num_columns; |
209 int num_columns; |
|
210 |
|
211 if (!statement->result) { |
|
212 luaL_error(L, "fetch called on a closed or invalid statement"); |
|
213 return 0; |
|
214 } |
209 |
215 |
210 if (PQresultStatus(statement->result) != PGRES_TUPLES_OK) { |
216 if (PQresultStatus(statement->result) != PGRES_TUPLES_OK) { |
211 lua_pushnil(L); |
217 lua_pushnil(L); |
212 return 1; |
218 return 1; |
213 } |
219 } |
330 * free converted statement after use |
336 * free converted statement after use |
331 */ |
337 */ |
332 free(new_sql); |
338 free(new_sql); |
333 |
339 |
334 if (!result) { |
340 if (!result) { |
335 luaL_error(L, "Unable to allocate prepare result handle"); |
341 lua_pushnil(L); |
|
342 lua_pushstring(L, "Unable to allocate prepare result handle"); |
|
343 return 2; |
336 } |
344 } |
337 |
345 |
338 status = PQresultStatus(result); |
346 status = PQresultStatus(result); |
339 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { |
347 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { |
340 const char *err_string = PQresultErrorMessage(result); |
348 const char *err_string = PQresultErrorMessage(result); |
341 PQclear(result); |
349 PQclear(result); |
342 luaL_error(L, "Unable to prepare statment: %s", err_string); |
350 |
343 return 0; |
351 lua_pushnil(L); |
|
352 lua_pushfstring(L, "Unable to prepare statment: %s", err_string); |
|
353 return 2; |
344 } |
354 } |
345 |
355 |
346 PQclear(result); |
356 PQclear(result); |
347 |
357 |
348 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); |
358 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); |