dbd/sqlite3/statement.c

changeset 41
e490414a391d
parent 33
6c64c45e7d8f
equal deleted inserted replaced
40:71c4b5dd82bb 41:e490414a391d
1 #include "dbd_sqlite3.h" 1 #include "dbd_sqlite3.h"
2
3 extern int try_begin_transaction(connection_t *conn);
4 extern int try_end_transaction(connection_t *conn);
2 5
3 /* 6 /*
4 * Converts SQLite types to Lua types 7 * Converts SQLite types to Lua types
5 */ 8 */
6 static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) { 9 static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) {
126 * this will be a NOP if the handle has not 129 * this will be a NOP if the handle has not
127 * been executed 130 * been executed
128 */ 131 */
129 if (sqlite3_reset(statement->stmt) != SQLITE_OK) { 132 if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
130 lua_pushboolean(L, 0); 133 lua_pushboolean(L, 0);
131 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); 134 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->conn->sqlite));
132 return 2; 135 return 2;
133 } 136 }
137
138 sqlite3_clear_bindings(statement->stmt);
134 139
135 expected_params = sqlite3_bind_parameter_count(statement->stmt); 140 expected_params = sqlite3_bind_parameter_count(statement->stmt);
136 if (expected_params != num_bind_params) { 141 if (expected_params != num_bind_params) {
137 /* 142 /*
138 * sqlite3_reset does not handle this condition, 143 * sqlite3_reset does not handle this condition,
178 if (errflag) { 183 if (errflag) {
179 lua_pushboolean(L, 0); 184 lua_pushboolean(L, 0);
180 if (errstr) 185 if (errstr)
181 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr); 186 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr);
182 else 187 else
183 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite)); 188 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->conn->sqlite));
184 189
185 return 2; 190 return 2;
186 } 191 }
192
193 try_begin_transaction(statement->conn);
187 194
188 if (!step(statement)) { 195 if (!step(statement)) {
189 lua_pushboolean(L, 0); 196 lua_pushboolean(L, 0);
190 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); 197 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->conn->sqlite));
191 return 2; 198 return 2;
192 } 199 }
193 200
194 statement->affected = sqlite3_changes(statement->sqlite); 201 statement->affected = sqlite3_changes(statement->conn->sqlite);
195 202
196 lua_pushboolean(L, 1); 203 lua_pushboolean(L, 1);
197 return 1; 204 return 1;
198 } 205 }
199 206
281 if (step(statement) == 0) { 288 if (step(statement) == 0) {
282 if (sqlite3_reset(statement->stmt) != SQLITE_OK) { 289 if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
283 /* 290 /*
284 * reset needs to be called to retrieve the 'real' error message 291 * reset needs to be called to retrieve the 'real' error message
285 */ 292 */
286 luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->sqlite)); 293 luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->conn->sqlite));
287 } 294 }
288 } 295 }
289 296
290 return 1; 297 return 1;
291 } 298 }
355 362
356 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 363 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) {
357 statement_t *statement = NULL; 364 statement_t *statement = NULL;
358 365
359 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 366 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
360 statement->sqlite = conn->sqlite; 367 statement->conn = conn;
361 statement->stmt = NULL; 368 statement->stmt = NULL;
362 statement->more_data = 0; 369 statement->more_data = 0;
363 statement->affected = 0; 370 statement->affected = 0;
364 371
365 if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { 372 if (sqlite3_prepare_v2(statement->conn->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) {
366 lua_pushnil(L); 373 lua_pushnil(L);
367 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite)); 374 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->conn->sqlite));
368 return 2; 375 return 2;
369 } 376 }
370 377
371 luaL_getmetatable(L, DBD_SQLITE_STATEMENT); 378 luaL_getmetatable(L, DBD_SQLITE_STATEMENT);
372 lua_setmetatable(L, -2); 379 lua_setmetatable(L, -2);

mercurial