dbd/sqlite3/statement.c

changeset 21
7956401a0c5e
parent 12
014ba3ab3903
child 30
8599f34c139b
equal deleted inserted replaced
20:5ab0b30f8fbd 21:7956401a0c5e
42 42
43 return 0; 43 return 0;
44 } 44 }
45 45
46 /* 46 /*
47 * num_affected_rows = statement:affected()
48 */
49 static int statement_affected(lua_State *L) {
50 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
51
52 if (!statement->stmt) {
53 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
54 }
55
56 lua_pushinteger(L, statement->affected);
57
58 return 1;
59 }
60
61 /*
47 * success = statement:close() 62 * success = statement:close()
48 */ 63 */
49 int statement_close(lua_State *L) { 64 static int statement_close(lua_State *L) {
50 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 65 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
51 int ok = 0; 66 int ok = 0;
52 67
53 if (statement->stmt) { 68 if (statement->stmt) {
54 if (sqlite3_finalize(statement->stmt) == SQLITE_OK) { 69 if (sqlite3_finalize(statement->stmt) == SQLITE_OK) {
63 } 78 }
64 79
65 /* 80 /*
66 * success,err = statement:execute(...) 81 * success,err = statement:execute(...)
67 */ 82 */
68 int statement_execute(lua_State *L) { 83 static int statement_execute(lua_State *L) {
69 int n = lua_gettop(L); 84 int n = lua_gettop(L);
70 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 85 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
71 int p; 86 int p;
72 int errflag = 0; 87 int errflag = 0;
73 const char *errstr = NULL; 88 const char *errstr = NULL;
147 if (!step(statement)) { 162 if (!step(statement)) {
148 lua_pushboolean(L, 0); 163 lua_pushboolean(L, 0);
149 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); 164 lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite));
150 return 2; 165 return 2;
151 } 166 }
167
168 statement->affected = sqlite3_changes(statement->sqlite);
152 169
153 lua_pushboolean(L, 1); 170 lua_pushboolean(L, 1);
154 return 1; 171 return 1;
155 } 172 }
156 173
279 lua_pushcclosure(L, next_iterator, 2); 296 lua_pushcclosure(L, next_iterator, 2);
280 return 1; 297 return 1;
281 } 298 }
282 299
283 /* 300 /*
301 * num_rows = statement:rowcount()
302 */
303 static int statement_rowcount(lua_State *L) {
304 luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_SQLITE_STATEMENT, "rowcount");
305
306 return 0;
307 }
308
309 /*
284 * __gc 310 * __gc
285 */ 311 */
286 static int statement_gc(lua_State *L) { 312 static int statement_gc(lua_State *L) {
287 /* always free the handle */ 313 /* always free the handle */
288 statement_close(L); 314 statement_close(L);
295 321
296 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 322 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
297 statement->sqlite = conn->sqlite; 323 statement->sqlite = conn->sqlite;
298 statement->stmt = NULL; 324 statement->stmt = NULL;
299 statement->more_data = 0; 325 statement->more_data = 0;
326 statement->affected = 0;
300 327
301 if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { 328 if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) {
302 lua_pushnil(L); 329 lua_pushnil(L);
303 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite)); 330 lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite));
304 return 2; 331 return 2;
309 return 1; 336 return 1;
310 } 337 }
311 338
312 int dbd_sqlite3_statement(lua_State *L) { 339 int dbd_sqlite3_statement(lua_State *L) {
313 static const luaL_Reg statement_methods[] = { 340 static const luaL_Reg statement_methods[] = {
341 {"affected", statement_affected},
314 {"close", statement_close}, 342 {"close", statement_close},
315 {"execute", statement_execute}, 343 {"execute", statement_execute},
316 {"fetch", statement_fetch}, 344 {"fetch", statement_fetch},
317 {"rows", statement_rows}, 345 {"rows", statement_rows},
346 {"rowcount", statement_rowcount},
318 {NULL, NULL} 347 {NULL, NULL}
319 }; 348 };
320 349
321 static const luaL_Reg statement_class_methods[] = { 350 static const luaL_Reg statement_class_methods[] = {
322 {NULL, NULL} 351 {NULL, NULL}

mercurial