dbd/sqlite3/statement.c

changeset 2
c4f02fc67e5a
parent 1
408291a6eb3e
child 3
b61020ca4753
equal deleted inserted replaced
1:408291a6eb3e 2:c4f02fc67e5a
1 #include "dbd_sqlite3.h" 1 #include "dbd_sqlite3.h"
2 2
3 /*
4 * Converts SQLite types to Lua types
5 */
3 static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) { 6 static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) {
4 lua_push_type_t lua_type; 7 lua_push_type_t lua_type;
5 8
6 switch(sqlite_type) { 9 switch(sqlite_type) {
7 case SQLITE_NULL: 10 case SQLITE_NULL:
21 } 24 }
22 25
23 return lua_type; 26 return lua_type;
24 } 27 }
25 28
29 /*
30 * runs sqlite3_step on a statement handle
31 */
26 static int step(statement_t *statement) { 32 static int step(statement_t *statement) {
27 int res = sqlite3_step(statement->stmt); 33 int res = sqlite3_step(statement->stmt);
28 34
29 if (res == SQLITE_DONE) { 35 if (res == SQLITE_DONE) {
30 statement->more_data = 0; 36 statement->more_data = 0;
35 } 41 }
36 42
37 return 0; 43 return 0;
38 } 44 }
39 45
46 /*
47 * success = statement:close()
48 */
40 static int statement_close(lua_State *L) { 49 static int statement_close(lua_State *L) {
41 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);
42 int ok = 0; 51 int ok = 0;
43 52
44 if (statement->stmt) { 53 if (statement->stmt) {
50 lua_pushboolean(L, ok); 59 lua_pushboolean(L, ok);
51 60
52 return 1; 61 return 1;
53 } 62 }
54 63
64 /*
65 * success = statement:execute(...)
66 */
55 static int statement_execute(lua_State *L) { 67 static int statement_execute(lua_State *L) {
56 int n = lua_gettop(L); 68 int n = lua_gettop(L);
57 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 69 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
58 int p; 70 int p;
59 71
72 /*
73 * reset the handle before binding params
74 * this wil be a NOP if the handle has not
75 * been executed
76 */
60 if (sqlite3_reset(statement->stmt) != SQLITE_OK) { 77 if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
61 lua_pushboolean(L, 0); 78 lua_pushboolean(L, 0);
62 return 1; 79 return 1;
63 } 80 }
64 81
82 99
83 lua_pushboolean(L, step(statement)); 100 lua_pushboolean(L, step(statement));
84 return 1; 101 return 1;
85 } 102 }
86 103
104 /*
105 * must be called after an execute
106 */
87 static int statement_fetch_impl(lua_State *L, int named_columns) { 107 static int statement_fetch_impl(lua_State *L, int named_columns) {
88 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); 108 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT);
89 int num_columns; 109 int num_columns;
90 110
91 if (!statement->more_data) { 111 if (!statement->more_data) {
112 /*
113 * Result set is empty, or not result set returned
114 */
115
92 lua_pushnil(L); 116 lua_pushnil(L);
93 return 1; 117 return 1;
94 } 118 }
95 119
96 num_columns = sqlite3_column_count(statement->stmt); 120 num_columns = sqlite3_column_count(statement->stmt);
145 } 169 }
146 } else { 170 } else {
147 luaL_error(L, "Unknown push type in result set"); 171 luaL_error(L, "Unknown push type in result set");
148 } 172 }
149 } 173 }
174 } else {
175 /*
176 * no columns returned by statement?
177 */
178 lua_pushnil(L);
150 } 179 }
151 180
152 if (step(statement) == 0) { 181 if (step(statement) == 0) {
153 if (sqlite3_reset(statement->stmt) != SQLITE_OK) { 182 if (sqlite3_reset(statement->stmt) != SQLITE_OK) {
183 /*
184 * reset needs to be called to retrieve the 'real' error message
185 */
154 luaL_error(L, "Failed to fetch statement: %s", sqlite3_errmsg(statement->sqlite)); 186 luaL_error(L, "Failed to fetch statement: %s", sqlite3_errmsg(statement->sqlite));
155 } 187 }
156 } 188 }
157 189
158 return 1; 190 return 1;
159 } 191 }
160 192
161 193 /*
194 * array = statement:fetch()
195 */
162 static int statement_fetch(lua_State *L) { 196 static int statement_fetch(lua_State *L) {
163 return statement_fetch_impl(L, 0); 197 return statement_fetch_impl(L, 0);
164 } 198 }
165 199
200 /*
201 * hashmap = statement:fetchtable()
202 */
166 static int statement_fetchtable(lua_State *L) { 203 static int statement_fetchtable(lua_State *L) {
167 return statement_fetch_impl(L, 1); 204 return statement_fetch_impl(L, 1);
168 } 205 }
169 206
207 /*
208 * __gc
209 */
170 static int statement_gc(lua_State *L) { 210 static int statement_gc(lua_State *L) {
171 /* always free the handle */ 211 /* always free the handle */
172 statement_close(L); 212 statement_close(L);
173 213
174 return 0; 214 return 0;
175 } 215 }
176
177
178 static const luaL_Reg statement_methods[] = {
179 {"close", statement_close},
180 {"execute", statement_execute},
181 {"fetch", statement_fetch},
182 {"fetchtable", statement_fetchtable},
183 {NULL, NULL}
184 };
185
186 static const luaL_Reg statement_class_methods[] = {
187 {NULL, NULL}
188 };
189 216
190 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { 217 int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) {
191 statement_t *statement = NULL; 218 statement_t *statement = NULL;
192 219
193 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 220 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
206 233
207 return 1; 234 return 1;
208 } 235 }
209 236
210 int dbd_sqlite3_statement(lua_State *L) { 237 int dbd_sqlite3_statement(lua_State *L) {
238 static const luaL_Reg statement_methods[] = {
239 {"close", statement_close},
240 {"execute", statement_execute},
241 {"fetch", statement_fetch},
242 {"fetchtable", statement_fetchtable},
243 {NULL, NULL}
244 };
245
246 static const luaL_Reg statement_class_methods[] = {
247 {NULL, NULL}
248 };
249
211 luaL_newmetatable(L, DBD_SQLITE_STATEMENT); 250 luaL_newmetatable(L, DBD_SQLITE_STATEMENT);
212 luaL_register(L, 0, statement_methods); 251 luaL_register(L, 0, statement_methods);
213 lua_pushvalue(L,-1); 252 lua_pushvalue(L,-1);
214 lua_setfield(L, -2, "__index"); 253 lua_setfield(L, -2, "__index");
215 254

mercurial