Sun, 23 Nov 2008 04:12:04 +0000
Cleanup and commenting
1 | 1 | #include "dbd_sqlite3.h" |
2 | ||
2 | 3 | /* |
4 | * Converts SQLite types to Lua types | |
5 | */ | |
1 | 6 | static lua_push_type_t sqlite_to_lua_push(unsigned int sqlite_type) { |
7 | lua_push_type_t lua_type; | |
8 | ||
9 | switch(sqlite_type) { | |
10 | case SQLITE_NULL: | |
11 | lua_type = LUA_PUSH_NIL; | |
12 | break; | |
13 | ||
14 | case SQLITE_INTEGER: | |
15 | lua_type = LUA_PUSH_INTEGER; | |
16 | break; | |
17 | ||
18 | case SQLITE_FLOAT: | |
19 | lua_type = LUA_PUSH_NUMBER; | |
20 | break; | |
21 | ||
22 | default: | |
23 | lua_type = LUA_PUSH_STRING; | |
24 | } | |
25 | ||
26 | return lua_type; | |
27 | } | |
28 | ||
2 | 29 | /* |
30 | * runs sqlite3_step on a statement handle | |
31 | */ | |
1 | 32 | static int step(statement_t *statement) { |
33 | int res = sqlite3_step(statement->stmt); | |
34 | ||
35 | if (res == SQLITE_DONE) { | |
36 | statement->more_data = 0; | |
37 | return 1; | |
38 | } else if (res == SQLITE_ROW) { | |
39 | statement->more_data = 1; | |
40 | return 1; | |
41 | } | |
42 | ||
43 | return 0; | |
44 | } | |
45 | ||
2 | 46 | /* |
47 | * success = statement:close() | |
48 | */ | |
1 | 49 | static int statement_close(lua_State *L) { |
50 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
51 | int ok = 0; | |
52 | ||
53 | if (statement->stmt) { | |
54 | if (sqlite3_finalize(statement->stmt) == SQLITE_OK) { | |
55 | ok = 1; | |
56 | } | |
57 | } | |
58 | ||
59 | lua_pushboolean(L, ok); | |
60 | ||
61 | return 1; | |
62 | } | |
63 | ||
2 | 64 | /* |
65 | * success = statement:execute(...) | |
66 | */ | |
1 | 67 | static int statement_execute(lua_State *L) { |
68 | int n = lua_gettop(L); | |
69 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
70 | int p; | |
71 | ||
2 | 72 | /* |
73 | * reset the handle before binding params | |
74 | * this wil be a NOP if the handle has not | |
75 | * been executed | |
76 | */ | |
1 | 77 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { |
78 | lua_pushboolean(L, 0); | |
79 | return 1; | |
80 | } | |
81 | ||
82 | for (p = 2; p <= n; p++) { | |
83 | int i = p - 1; | |
84 | ||
85 | if (lua_isnil(L, p)) { | |
86 | if (sqlite3_bind_null(statement->stmt, i) != SQLITE_OK) { | |
87 | luaL_error(L, "Failed to execute statement: %s", sqlite3_errmsg(statement->sqlite)); | |
88 | } | |
89 | } else if (lua_isnumber(L, p)) { | |
90 | 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)); | |
92 | } | |
93 | } else if (lua_isstring(L, p)) { | |
94 | 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)); | |
96 | } | |
97 | } | |
98 | } | |
99 | ||
100 | lua_pushboolean(L, step(statement)); | |
101 | return 1; | |
102 | } | |
103 | ||
2 | 104 | /* |
105 | * must be called after an execute | |
106 | */ | |
1 | 107 | static int statement_fetch_impl(lua_State *L, int named_columns) { |
108 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
109 | int num_columns; | |
110 | ||
111 | if (!statement->more_data) { | |
2 | 112 | /* |
113 | * Result set is empty, or not result set returned | |
114 | */ | |
115 | ||
1 | 116 | lua_pushnil(L); |
117 | return 1; | |
118 | } | |
119 | ||
120 | num_columns = sqlite3_column_count(statement->stmt); | |
121 | ||
122 | if (num_columns) { | |
123 | int i; | |
124 | int d = 1; | |
125 | ||
126 | lua_newtable(L); | |
127 | ||
128 | for (i = 0; i < num_columns; i++) { | |
129 | lua_push_type_t lua_push = sqlite_to_lua_push(sqlite3_column_type(statement->stmt, i)); | |
130 | const char *name = sqlite3_column_name(statement->stmt, i); | |
131 | ||
132 | if (lua_push == LUA_PUSH_NIL) { | |
133 | if (named_columns) { | |
134 | LUA_PUSH_ATTRIB_NIL(name); | |
135 | } else { | |
136 | LUA_PUSH_ARRAY_NIL(d); | |
137 | } | |
138 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
139 | int val = sqlite3_column_int(statement->stmt, i); | |
140 | ||
141 | if (named_columns) { | |
142 | LUA_PUSH_ATTRIB_INT(name, val); | |
143 | } else { | |
144 | LUA_PUSH_ARRAY_INT(d, val); | |
145 | } | |
146 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
147 | double val = sqlite3_column_double(statement->stmt, i); | |
148 | ||
149 | if (named_columns) { | |
150 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
151 | } else { | |
152 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
153 | } | |
154 | } else if (lua_push == LUA_PUSH_STRING) { | |
155 | const char *val = (const char *)sqlite3_column_text(statement->stmt, i); | |
156 | ||
157 | if (named_columns) { | |
158 | LUA_PUSH_ATTRIB_STRING(name, val); | |
159 | } else { | |
160 | LUA_PUSH_ARRAY_STRING(d, val); | |
161 | } | |
162 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
163 | int val = sqlite3_column_int(statement->stmt, i); | |
164 | ||
165 | if (named_columns) { | |
166 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
167 | } else { | |
168 | LUA_PUSH_ARRAY_BOOL(d, val); | |
169 | } | |
170 | } else { | |
171 | luaL_error(L, "Unknown push type in result set"); | |
172 | } | |
173 | } | |
2 | 174 | } else { |
175 | /* | |
176 | * no columns returned by statement? | |
177 | */ | |
178 | lua_pushnil(L); | |
1 | 179 | } |
180 | ||
181 | if (step(statement) == 0) { | |
182 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { | |
2 | 183 | /* |
184 | * reset needs to be called to retrieve the 'real' error message | |
185 | */ | |
1 | 186 | luaL_error(L, "Failed to fetch statement: %s", sqlite3_errmsg(statement->sqlite)); |
187 | } | |
188 | } | |
189 | ||
190 | return 1; | |
191 | } | |
192 | ||
2 | 193 | /* |
194 | * array = statement:fetch() | |
195 | */ | |
1 | 196 | static int statement_fetch(lua_State *L) { |
197 | return statement_fetch_impl(L, 0); | |
198 | } | |
199 | ||
2 | 200 | /* |
201 | * hashmap = statement:fetchtable() | |
202 | */ | |
1 | 203 | static int statement_fetchtable(lua_State *L) { |
204 | return statement_fetch_impl(L, 1); | |
205 | } | |
206 | ||
2 | 207 | /* |
208 | * __gc | |
209 | */ | |
1 | 210 | static int statement_gc(lua_State *L) { |
211 | /* always free the handle */ | |
212 | statement_close(L); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
218 | statement_t *statement = NULL; | |
219 | ||
220 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
221 | statement->sqlite = conn->sqlite; | |
222 | statement->stmt = NULL; | |
223 | statement->more_data = 0; | |
224 | ||
225 | 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); | |
228 | return 1; | |
229 | } | |
230 | ||
231 | luaL_getmetatable(L, DBD_SQLITE_STATEMENT); | |
232 | lua_setmetatable(L, -2); | |
233 | ||
234 | return 1; | |
235 | } | |
236 | ||
237 | int dbd_sqlite3_statement(lua_State *L) { | |
2 | 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 | ||
1 | 250 | luaL_newmetatable(L, DBD_SQLITE_STATEMENT); |
251 | luaL_register(L, 0, statement_methods); | |
252 | lua_pushvalue(L,-1); | |
253 | lua_setfield(L, -2, "__index"); | |
254 | ||
255 | lua_pushcfunction(L, statement_gc); | |
256 | lua_setfield(L, -2, "__gc"); | |
257 | ||
258 | luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods); | |
259 | ||
260 | return 1; | |
261 | } |