dbd/sqlite3/statement.c

changeset 3
b61020ca4753
parent 2
c4f02fc67e5a
child 4
c50b0e6f25d6
equal deleted inserted replaced
2:c4f02fc67e5a 3:b61020ca4753
44 } 44 }
45 45
46 /* 46 /*
47 * success = statement:close() 47 * success = statement:close()
48 */ 48 */
49 static int statement_close(lua_State *L) { 49 int statement_close(lua_State *L) {
50 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 50 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
51 int ok = 0; 51 int ok = 0;
52 52
53 if (statement->stmt) { 53 if (statement->stmt) {
54 if (sqlite3_finalize(statement->stmt) == SQLITE_OK) { 54 if (sqlite3_finalize(statement->stmt) == SQLITE_OK) {
55 ok = 1; 55 ok = 1;
56 } 56 }
57
58 statement->stmt = NULL;
57 } 59 }
58 60
59 lua_pushboolean(L, ok); 61 lua_pushboolean(L, ok);
60
61 return 1; 62 return 1;
62 } 63 }
63 64
64 /* 65 /*
65 * success = statement:execute(...) 66 * success,err = statement:execute(...)
66 */ 67 */
67 static int statement_execute(lua_State *L) { 68 int statement_execute(lua_State *L) {
68 int n = lua_gettop(L); 69 int n = lua_gettop(L);
69 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 70 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
70 int p; 71 int p;
72 int err = 0;
73
74
75 if (!statement->stmt) {
76 lua_pushboolean(L, 0);
77 lua_pushstring(L, "execute called on a closed or invalid handle");
78 return 2;
79 }
71 80
72 /* 81 /*
73 * reset the handle before binding params 82 * reset the handle before binding params
74 * this wil be a NOP if the handle has not 83 * this wil be a NOP if the handle has not
75 * been executed 84 * been executed
76 */ 85 */
77 if (sqlite3_reset(statement->stmt) != SQLITE_OK) { 86 if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
78 lua_pushboolean(L, 0); 87 lua_pushboolean(L, 0);
79 return 1; 88 lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite));
89 return 2;
80 } 90 }
81 91
82 for (p = 2; p <= n; p++) { 92 for (p = 2; p <= n; p++) {
83 int i = p - 1; 93 int i = p - 1;
84 94
85 if (lua_isnil(L, p)) { 95 if (lua_isnil(L, p)) {
86 if (sqlite3_bind_null(statement->stmt, i) != SQLITE_OK) { 96 if (sqlite3_bind_null(statement->stmt, i) != SQLITE_OK) {
87 luaL_error(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); 97 err = 1;
88 } 98 }
89 } else if (lua_isnumber(L, p)) { 99 } else if (lua_isnumber(L, p)) {
90 if (sqlite3_bind_double(statement->stmt, i, luaL_checknumber(L, p)) != SQLITE_OK) { 100 if (sqlite3_bind_double(statement->stmt, i, luaL_checknumber(L, p)) != SQLITE_OK) {
91 luaL_error(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); 101 err = 1;
92 } 102 }
93 } else if (lua_isstring(L, p)) { 103 } else if (lua_isstring(L, p)) {
94 if (sqlite3_bind_text(statement->stmt, i, luaL_checkstring(L, p), -1, SQLITE_STATIC) != SQLITE_OK) { 104 if (sqlite3_bind_text(statement->stmt, i, luaL_checkstring(L, p), -1, SQLITE_STATIC) != SQLITE_OK) {
95 luaL_error(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); 105 err = 1;
96 } 106 }
97 } 107 }
108
109 if (err)
110 break;
98 } 111 }
99 112
100 lua_pushboolean(L, step(statement)); 113 if (err || step(statement) == 0) {
114 lua_pushboolean(L, 0);
115 lua_pushfstring(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite));
116 return 2;
117 }
118
119 lua_pushboolean(L, 1);
101 return 1; 120 return 1;
102 } 121 }
103 122
104 /* 123 /*
105 * must be called after an execute 124 * must be called after an execute
106 */ 125 */
107 static int statement_fetch_impl(lua_State *L, int named_columns) { 126 static int statement_fetch_impl(lua_State *L, int named_columns) {
108 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 127 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
109 int num_columns; 128 int num_columns;
129
130 if (!statement->stmt) {
131 luaL_error(L, "fetch called on a closed or invalid handle");
132 return 0;
133 }
110 134
111 if (!statement->more_data) { 135 if (!statement->more_data) {
112 /* 136 /*
113 * Result set is empty, or not result set returned 137 * Result set is empty, or not result set returned
114 */ 138 */
221 statement->sqlite = conn->sqlite; 245 statement->sqlite = conn->sqlite;
222 statement->stmt = NULL; 246 statement->stmt = NULL;
223 statement->more_data = 0; 247 statement->more_data = 0;
224 248
225 if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { 249 if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) {
226 luaL_error(L, "Failed to prepare statement: %s", sqlite3_errmsg(statement->sqlite));
227 lua_pushnil(L); 250 lua_pushnil(L);
228 return 1; 251 lua_pushfstring(L, "Failed to prepare statement: %s", sqlite3_errmsg(statement->sqlite));
252 return 2;
229 } 253 }
230 254
231 luaL_getmetatable(L, DBD_SQLITE_STATEMENT); 255 luaL_getmetatable(L, DBD_SQLITE_STATEMENT);
232 lua_setmetatable(L, -2); 256 lua_setmetatable(L, -2);
233
234 return 1; 257 return 1;
235 } 258 }
236 259
237 int dbd_sqlite3_statement(lua_State *L) { 260 int dbd_sqlite3_statement(lua_State *L) {
238 static const luaL_Reg statement_methods[] = { 261 static const luaL_Reg statement_methods[] = {

mercurial