Fri, 17 Apr 2009 23:46:12 +0000
Add 'columns' method to statement handles to retrieve column names from a result set
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 | /* |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
47 | * num_affected_rows = statement:affected() |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
48 | */ |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
49 | static int statement_affected(lua_State *L) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
50 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
51 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
52 | if (!statement->stmt) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
53 | luaL_error(L, DBI_ERR_INVALID_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
54 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
55 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
56 | lua_pushinteger(L, statement->affected); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
57 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
58 | return 1; |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
59 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
60 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
61 | /* |
2 | 62 | * success = statement:close() |
63 | */ | |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
64 | static int statement_close(lua_State *L) { |
1 | 65 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); |
66 | int ok = 0; | |
67 | ||
68 | if (statement->stmt) { | |
69 | if (sqlite3_finalize(statement->stmt) == SQLITE_OK) { | |
70 | ok = 1; | |
71 | } | |
3 | 72 | |
73 | statement->stmt = NULL; | |
1 | 74 | } |
75 | ||
76 | lua_pushboolean(L, ok); | |
77 | return 1; | |
78 | } | |
79 | ||
2 | 80 | /* |
30
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
81 | * column_names = statement:columns() |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
82 | */ |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
83 | static int statement_columns(lua_State *L) { |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
84 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
85 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
86 | int i; |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
87 | int num_columns; |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
88 | int d = 1; |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
89 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
90 | if (!statement->stmt) { |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
91 | luaL_error(L, DBI_ERR_INVALID_STATEMENT); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
92 | return 0; |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
93 | } |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
94 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
95 | num_columns = sqlite3_column_count(statement->stmt); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
96 | lua_newtable(L); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
97 | for (i = 0; i < num_columns; i++) { |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
98 | const char *name = sqlite3_column_name(statement->stmt, i); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
99 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
100 | LUA_PUSH_ARRAY_STRING(d, name); |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
101 | } |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
102 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
103 | return 1; |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
104 | } |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
105 | |
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
106 | /* |
3 | 107 | * success,err = statement:execute(...) |
2 | 108 | */ |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
109 | static int statement_execute(lua_State *L) { |
1 | 110 | int n = lua_gettop(L); |
111 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); | |
112 | int p; | |
10 | 113 | int errflag = 0; |
114 | const char *errstr = NULL; | |
115 | int expected_params; | |
116 | int num_bind_params = n - 1; | |
3 | 117 | |
118 | if (!statement->stmt) { | |
119 | lua_pushboolean(L, 0); | |
4 | 120 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 121 | return 2; |
122 | } | |
1 | 123 | |
2 | 124 | /* |
125 | * reset the handle before binding params | |
126 | * this wil be a NOP if the handle has not | |
127 | * been executed | |
128 | */ | |
1 | 129 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { |
130 | lua_pushboolean(L, 0); | |
4 | 131 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); |
3 | 132 | return 2; |
1 | 133 | } |
134 | ||
10 | 135 | expected_params = sqlite3_bind_parameter_count(statement->stmt); |
136 | if (expected_params != num_bind_params) { | |
137 | /* | |
138 | * sqlite3_reset does not handle this condition, | |
139 | * and the client library will fill unset params | |
140 | * with NULLs | |
141 | */ | |
142 | lua_pushboolean(L, 0); | |
143 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); | |
144 | return 2; | |
145 | } | |
146 | ||
1 | 147 | for (p = 2; p <= n; p++) { |
148 | int i = p - 1; | |
9 | 149 | int type = lua_type(L, p); |
10 | 150 | char err[64]; |
9 | 151 | |
152 | switch(type) { | |
153 | case LUA_TNIL: | |
10 | 154 | errflag = sqlite3_bind_null(statement->stmt, i) != SQLITE_OK; |
9 | 155 | break; |
156 | case LUA_TNUMBER: | |
10 | 157 | errflag = sqlite3_bind_double(statement->stmt, i, lua_tonumber(L, p)) != SQLITE_OK; |
9 | 158 | break; |
159 | case LUA_TSTRING: | |
10 | 160 | errflag = sqlite3_bind_text(statement->stmt, i, lua_tostring(L, p), -1, SQLITE_STATIC) != SQLITE_OK; |
9 | 161 | break; |
162 | case LUA_TBOOLEAN: | |
10 | 163 | errflag = sqlite3_bind_int(statement->stmt, i, lua_toboolean(L, p)) != SQLITE_OK; |
9 | 164 | break; |
165 | default: | |
166 | /* | |
167 | * Unknown/unsupported value type | |
168 | */ | |
10 | 169 | errflag = 1; |
170 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); | |
171 | errstr = err; | |
1 | 172 | } |
3 | 173 | |
10 | 174 | if (errflag) |
3 | 175 | break; |
1 | 176 | } |
177 | ||
10 | 178 | if (errflag) { |
3 | 179 | lua_pushboolean(L, 0); |
10 | 180 | if (errstr) |
181 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr); | |
182 | else | |
183 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, sqlite3_errmsg(statement->sqlite)); | |
184 | ||
3 | 185 | return 2; |
186 | } | |
187 | ||
4 | 188 | if (!step(statement)) { |
189 | lua_pushboolean(L, 0); | |
190 | lua_pushfstring(L, DBI_ERR_EXECUTE_FAILED, sqlite3_errmsg(statement->sqlite)); | |
5 | 191 | return 2; |
4 | 192 | } |
193 | ||
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
194 | statement->affected = sqlite3_changes(statement->sqlite); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
195 | |
3 | 196 | lua_pushboolean(L, 1); |
1 | 197 | return 1; |
198 | } | |
199 | ||
2 | 200 | /* |
201 | * must be called after an execute | |
202 | */ | |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
203 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
1 | 204 | int num_columns; |
205 | ||
3 | 206 | if (!statement->stmt) { |
4 | 207 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 208 | return 0; |
209 | } | |
210 | ||
1 | 211 | if (!statement->more_data) { |
2 | 212 | /* |
213 | * Result set is empty, or not result set returned | |
214 | */ | |
215 | ||
1 | 216 | lua_pushnil(L); |
217 | return 1; | |
218 | } | |
219 | ||
220 | num_columns = sqlite3_column_count(statement->stmt); | |
221 | ||
222 | if (num_columns) { | |
223 | int i; | |
224 | int d = 1; | |
225 | ||
226 | lua_newtable(L); | |
227 | ||
228 | for (i = 0; i < num_columns; i++) { | |
229 | lua_push_type_t lua_push = sqlite_to_lua_push(sqlite3_column_type(statement->stmt, i)); | |
230 | const char *name = sqlite3_column_name(statement->stmt, i); | |
231 | ||
232 | if (lua_push == LUA_PUSH_NIL) { | |
233 | if (named_columns) { | |
234 | LUA_PUSH_ATTRIB_NIL(name); | |
235 | } else { | |
236 | LUA_PUSH_ARRAY_NIL(d); | |
237 | } | |
238 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
239 | int val = sqlite3_column_int(statement->stmt, i); | |
240 | ||
241 | if (named_columns) { | |
242 | LUA_PUSH_ATTRIB_INT(name, val); | |
243 | } else { | |
244 | LUA_PUSH_ARRAY_INT(d, val); | |
245 | } | |
246 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
247 | double val = sqlite3_column_double(statement->stmt, i); | |
248 | ||
249 | if (named_columns) { | |
250 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
251 | } else { | |
252 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
253 | } | |
254 | } else if (lua_push == LUA_PUSH_STRING) { | |
255 | const char *val = (const char *)sqlite3_column_text(statement->stmt, i); | |
256 | ||
257 | if (named_columns) { | |
258 | LUA_PUSH_ATTRIB_STRING(name, val); | |
259 | } else { | |
260 | LUA_PUSH_ARRAY_STRING(d, val); | |
261 | } | |
262 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
263 | int val = sqlite3_column_int(statement->stmt, i); | |
264 | ||
265 | if (named_columns) { | |
266 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
267 | } else { | |
268 | LUA_PUSH_ARRAY_BOOL(d, val); | |
269 | } | |
270 | } else { | |
4 | 271 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 272 | } |
273 | } | |
2 | 274 | } else { |
275 | /* | |
276 | * no columns returned by statement? | |
277 | */ | |
278 | lua_pushnil(L); | |
1 | 279 | } |
280 | ||
281 | if (step(statement) == 0) { | |
282 | if (sqlite3_reset(statement->stmt) != SQLITE_OK) { | |
2 | 283 | /* |
284 | * reset needs to be called to retrieve the 'real' error message | |
285 | */ | |
4 | 286 | luaL_error(L, DBI_ERR_FETCH_FAILED, sqlite3_errmsg(statement->sqlite)); |
1 | 287 | } |
288 | } | |
289 | ||
290 | return 1; | |
291 | } | |
292 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
293 | static int next_iterator(lua_State *L) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
294 | statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_SQLITE_STATEMENT); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
295 | int named_columns = lua_toboolean(L, lua_upvalueindex(2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
296 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
297 | return statement_fetch_impl(L, statement, named_columns); |
1 | 298 | } |
299 | ||
2 | 300 | /* |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
301 | * table = statement:fetch(named_indexes) |
2 | 302 | */ |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
303 | static int statement_fetch(lua_State *L) { |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
304 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_SQLITE_STATEMENT); |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
305 | int named_columns = lua_toboolean(L, 2); |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
306 | |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
307 | return statement_fetch_impl(L, statement, named_columns); |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
308 | } |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
309 | |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
310 | /* |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
311 | * iterfunc = statement:rows(named_indexes) |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
312 | */ |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
313 | static int statement_rows(lua_State *L) { |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
314 | if (lua_gettop(L) == 1) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
315 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
316 | lua_pushboolean(L, 0); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
317 | } else { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
318 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
319 | lua_pushboolean(L, lua_toboolean(L, 2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
320 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
321 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
322 | lua_pushcclosure(L, next_iterator, 2); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
323 | return 1; |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
324 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
325 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
326 | /* |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
327 | * num_rows = statement:rowcount() |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
328 | */ |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
329 | static int statement_rowcount(lua_State *L) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
330 | luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_SQLITE_STATEMENT, "rowcount"); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
331 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
332 | return 0; |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
333 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
334 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
335 | /* |
2 | 336 | * __gc |
337 | */ | |
1 | 338 | static int statement_gc(lua_State *L) { |
339 | /* always free the handle */ | |
340 | statement_close(L); | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | int dbd_sqlite3_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
346 | statement_t *statement = NULL; | |
347 | ||
348 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
349 | statement->sqlite = conn->sqlite; | |
350 | statement->stmt = NULL; | |
351 | statement->more_data = 0; | |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
352 | statement->affected = 0; |
1 | 353 | |
354 | if (sqlite3_prepare_v2(statement->sqlite, sql_query, strlen(sql_query), &statement->stmt, NULL) != SQLITE_OK) { | |
355 | lua_pushnil(L); | |
4 | 356 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, sqlite3_errmsg(statement->sqlite)); |
3 | 357 | return 2; |
1 | 358 | } |
359 | ||
360 | luaL_getmetatable(L, DBD_SQLITE_STATEMENT); | |
361 | lua_setmetatable(L, -2); | |
362 | return 1; | |
363 | } | |
364 | ||
365 | int dbd_sqlite3_statement(lua_State *L) { | |
2 | 366 | static const luaL_Reg statement_methods[] = { |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
367 | {"affected", statement_affected}, |
2 | 368 | {"close", statement_close}, |
30
8599f34c139b
Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents:
21
diff
changeset
|
369 | {"columns", statement_columns}, |
2 | 370 | {"execute", statement_execute}, |
371 | {"fetch", statement_fetch}, | |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
372 | {"rows", statement_rows}, |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
12
diff
changeset
|
373 | {"rowcount", statement_rowcount}, |
2 | 374 | {NULL, NULL} |
375 | }; | |
376 | ||
377 | static const luaL_Reg statement_class_methods[] = { | |
378 | {NULL, NULL} | |
379 | }; | |
380 | ||
1 | 381 | luaL_newmetatable(L, DBD_SQLITE_STATEMENT); |
382 | luaL_register(L, 0, statement_methods); | |
383 | lua_pushvalue(L,-1); | |
384 | lua_setfield(L, -2, "__index"); | |
385 | ||
386 | lua_pushcfunction(L, statement_gc); | |
387 | lua_setfield(L, -2, "__gc"); | |
388 | ||
389 | luaL_register(L, DBD_SQLITE_STATEMENT, statement_class_methods); | |
390 | ||
391 | return 1; | |
392 | } |