Sat, 20 Dec 2008 23:53:07 +0000
Cleanup.
1 | 1 | #include "dbd_mysql.h" |
2 | ||
3 | static lua_push_type_t mysql_to_lua_push(unsigned int mysql_type) { | |
4 | lua_push_type_t lua_type; | |
5 | ||
6 | switch(mysql_type) { | |
7 | case MYSQL_TYPE_NULL: | |
8 | lua_type = LUA_PUSH_NIL; | |
9 | break; | |
10 | ||
11 | case MYSQL_TYPE_TINY: | |
12 | case MYSQL_TYPE_SHORT: | |
13 | case MYSQL_TYPE_LONG: | |
14 | lua_type = LUA_PUSH_INTEGER; | |
15 | break; | |
16 | ||
17 | case MYSQL_TYPE_DOUBLE: | |
18 | case MYSQL_TYPE_LONGLONG: | |
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 | /* |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
30 | * num_affected_rows = statement:affected() |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
31 | */ |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
32 | static int statement_affected(lua_State *L) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
33 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
34 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
35 | if (!statement->stmt) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
36 | luaL_error(L, DBI_ERR_INVALID_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
37 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
38 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
39 | lua_pushinteger(L, mysql_stmt_affected_rows(statement->stmt)); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
40 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
41 | return 1; |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
42 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
43 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
44 | /* |
2 | 45 | * success = statement:close() |
46 | */ | |
1 | 47 | static int statement_close(lua_State *L) { |
48 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
49 | ||
50 | if (statement->metadata) { | |
51 | mysql_free_result(statement->metadata); | |
3 | 52 | statement->metadata = NULL; |
1 | 53 | } |
54 | ||
55 | if (statement->stmt) { | |
3 | 56 | mysql_stmt_close(statement->stmt); |
57 | statement->stmt = NULL; | |
1 | 58 | } |
59 | ||
3 | 60 | lua_pushboolean(L, 1); |
1 | 61 | return 1; |
62 | } | |
63 | ||
2 | 64 | /* |
3 | 65 | * success,err = statement:execute(...) |
2 | 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_MYSQL_STATEMENT); | |
70 | int num_bind_params = n - 1; | |
3 | 71 | int expected_params; |
14 | 72 | |
25 | 73 | unsigned char *buffer = NULL; |
14 | 74 | int offset = 0; |
1 | 75 | |
76 | MYSQL_BIND *bind = NULL; | |
77 | MYSQL_RES *metadata = NULL; | |
78 | ||
79 | char *error_message = NULL; | |
10 | 80 | char *errstr = NULL; |
1 | 81 | |
82 | int p; | |
83 | ||
3 | 84 | if (!statement->stmt) { |
85 | lua_pushboolean(L, 0); | |
4 | 86 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 87 | return 2; |
88 | } | |
89 | ||
90 | expected_params = mysql_stmt_param_count(statement->stmt); | |
91 | ||
92 | if (expected_params != num_bind_params) { | |
93 | /* | |
10 | 94 | * mysql_stmt_bind_param does not handle this condition, |
3 | 95 | * and the client library will segfault if these do no match |
96 | */ | |
97 | lua_pushboolean(L, 0); | |
4 | 98 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); |
3 | 99 | return 2; |
100 | } | |
101 | ||
25 | 102 | if (num_bind_params) { |
103 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); | |
104 | if (bind == NULL) { | |
105 | luaL_error(L, "Could not alloc bind params\n"); | |
106 | } | |
107 | ||
108 | buffer = malloc(num_bind_params * sizeof(size_t)); | |
109 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
23
a4825c3e65e9
Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents:
21
diff
changeset
|
110 | } |
a4825c3e65e9
Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents:
21
diff
changeset
|
111 | |
1 | 112 | for (p = 2; p <= n; p++) { |
113 | int type = lua_type(L, p); | |
114 | int i = p - 2; | |
115 | ||
116 | const char *str = NULL; | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
117 | size_t *str_len = NULL; |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
118 | double *num = NULL; |
9 | 119 | int *boolean = NULL; |
10 | 120 | char err[64]; |
1 | 121 | |
122 | switch(type) { | |
123 | case LUA_TNIL: | |
124 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
125 | bind[i].is_null = (my_bool*)1; | |
126 | break; | |
127 | ||
9 | 128 | case LUA_TBOOLEAN: |
14 | 129 | boolean = (int *)buffer + offset; |
130 | offset += sizeof(int); | |
9 | 131 | *boolean = lua_toboolean(L, p); |
14 | 132 | |
9 | 133 | bind[i].buffer_type = MYSQL_TYPE_LONG; |
134 | bind[i].is_null = (my_bool*)0; | |
135 | bind[i].buffer = (char *)boolean; | |
136 | bind[i].length = 0; | |
137 | break; | |
138 | ||
1 | 139 | case LUA_TNUMBER: |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
140 | /* |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
141 | * num needs to be it's own |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
142 | * memory here |
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
143 | */ |
14 | 144 | num = (double *)buffer + offset; |
145 | offset += sizeof(double); | |
9 | 146 | *num = lua_tonumber(L, p); |
1 | 147 | |
148 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
149 | bind[i].is_null = (my_bool*)0; | |
6
22046b996150
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
nrich@ii.net
parents:
4
diff
changeset
|
150 | bind[i].buffer = (char *)num; |
1 | 151 | bind[i].length = 0; |
152 | break; | |
153 | ||
154 | case LUA_TSTRING: | |
14 | 155 | str_len = (size_t *)buffer + offset; |
156 | offset += sizeof(size_t); | |
9 | 157 | str = lua_tolstring(L, p, str_len); |
1 | 158 | |
159 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
160 | bind[i].is_null = (my_bool*)0; | |
161 | bind[i].buffer = (char *)str; | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
162 | bind[i].length = str_len; |
1 | 163 | break; |
164 | ||
165 | default: | |
10 | 166 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); |
167 | errstr = err; | |
168 | error_message = DBI_ERR_BINDING_PARAMS; | |
1 | 169 | goto cleanup; |
170 | } | |
171 | } | |
172 | ||
173 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 174 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 175 | goto cleanup; |
176 | } | |
177 | ||
178 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 179 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 180 | goto cleanup; |
181 | } | |
182 | ||
183 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
184 | ||
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
185 | if (metadata) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
186 | mysql_stmt_store_result(statement->stmt); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
187 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
188 | |
1 | 189 | cleanup: |
23
a4825c3e65e9
Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents:
21
diff
changeset
|
190 | if (bind) { |
1 | 191 | free(bind); |
23
a4825c3e65e9
Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents:
21
diff
changeset
|
192 | } |
1 | 193 | |
25 | 194 | if (buffer) { |
195 | free(buffer); | |
196 | } | |
197 | ||
1 | 198 | if (error_message) { |
3 | 199 | lua_pushboolean(L, 0); |
10 | 200 | lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt)); |
3 | 201 | return 2; |
1 | 202 | } |
203 | ||
204 | statement->metadata = metadata; | |
205 | ||
3 | 206 | lua_pushboolean(L, 1); |
1 | 207 | return 1; |
208 | } | |
209 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
210 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
1 | 211 | int column_count; |
212 | MYSQL_BIND *bind = NULL; | |
213 | const char *error_message = NULL; | |
214 | ||
215 | if (!statement->stmt) { | |
4 | 216 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 217 | return 0; |
218 | } | |
219 | ||
220 | if (!statement->metadata) { | |
4 | 221 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 222 | return 0; |
1 | 223 | } |
224 | ||
225 | if (!statement->metadata) { | |
226 | lua_pushnil(L); | |
227 | return 1; | |
228 | } | |
229 | ||
23
a4825c3e65e9
Bugfix: memory corruption possible after reallocs. Using a static buffer instead. Will need to check for overflows on static buffer.
nrich@ii.net
parents:
21
diff
changeset
|
230 | |
1 | 231 | column_count = mysql_num_fields(statement->metadata); |
232 | ||
233 | if (column_count > 0) { | |
234 | int i; | |
235 | MYSQL_FIELD *fields; | |
236 | ||
237 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
238 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
239 | ||
240 | fields = mysql_fetch_fields(statement->metadata); | |
241 | ||
242 | for (i = 0; i < column_count; i++) { | |
243 | unsigned int length = fields[i].length; | |
244 | ||
245 | char *buffer = (char *)malloc(length); | |
246 | memset(buffer, 0, length); | |
247 | ||
248 | bind[i].buffer_type = fields[i].type; | |
249 | bind[i].buffer = buffer; | |
250 | bind[i].buffer_length = length; | |
251 | } | |
252 | ||
253 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 254 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 255 | goto cleanup; |
256 | } | |
257 | ||
258 | if (!mysql_stmt_fetch(statement->stmt)) { | |
259 | int d = 1; | |
260 | ||
261 | lua_newtable(L); | |
262 | for (i = 0; i < column_count; i++) { | |
263 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
264 | const char *name = fields[i].name; | |
265 | ||
266 | if (lua_push == LUA_PUSH_NIL) { | |
267 | if (named_columns) { | |
268 | LUA_PUSH_ATTRIB_NIL(name); | |
269 | } else { | |
270 | LUA_PUSH_ARRAY_NIL(d); | |
271 | } | |
272 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
273 | if (named_columns) { | |
274 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
275 | } else { | |
276 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
277 | } | |
278 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
279 | if (named_columns) { | |
280 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
281 | } else { | |
282 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
283 | } | |
284 | } else if (lua_push == LUA_PUSH_STRING) { | |
285 | if (named_columns) { | |
286 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
287 | } else { | |
288 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
289 | } | |
290 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
291 | if (named_columns) { | |
292 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
293 | } else { | |
294 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
295 | } | |
296 | } else { | |
4 | 297 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 298 | } |
299 | } | |
300 | } else { | |
301 | lua_pushnil(L); | |
302 | } | |
303 | } | |
304 | ||
305 | cleanup: | |
306 | if (bind) { | |
307 | int i; | |
308 | ||
309 | for (i = 0; i < column_count; i++) { | |
310 | free(bind[i].buffer); | |
311 | } | |
312 | ||
313 | free(bind); | |
314 | } | |
315 | ||
316 | if (error_message) { | |
317 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | return 1; | |
322 | } | |
323 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
324 | static int next_iterator(lua_State *L) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
325 | statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_MYSQL_STATEMENT); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
326 | 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
|
327 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
328 | return statement_fetch_impl(L, statement, named_columns); |
1 | 329 | } |
330 | ||
2 | 331 | /* |
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
|
332 | * table = statement:fetch(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
|
333 | */ |
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
|
334 | 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
|
335 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_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
|
336 | int named_columns = lua_toboolean(L, 2); |
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
|
337 | |
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
|
338 | 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
|
339 | } |
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
|
340 | |
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
|
341 | /* |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
342 | * num_rows = statement:rowcount() |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
343 | */ |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
344 | static int statement_rowcount(lua_State *L) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
345 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
346 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
347 | if (!statement->stmt) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
348 | luaL_error(L, DBI_ERR_INVALID_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
349 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
350 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
351 | lua_pushinteger(L, mysql_stmt_num_rows(statement->stmt)); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
352 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
353 | return 1; |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
354 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
355 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
356 | /* |
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
|
357 | * iterfunc = statement:rows(named_indexes) |
2 | 358 | */ |
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
|
359 | 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
|
360 | if (lua_gettop(L) == 1) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
361 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
362 | lua_pushboolean(L, 0); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
363 | } else { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
364 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
365 | lua_pushboolean(L, lua_toboolean(L, 2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
366 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
367 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
368 | lua_pushcclosure(L, next_iterator, 2); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
369 | return 1; |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
370 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
371 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
372 | /* |
2 | 373 | * __gc |
374 | */ | |
1 | 375 | static int statement_gc(lua_State *L) { |
376 | /* always free the handle */ | |
377 | statement_close(L); | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
383 | unsigned long sql_len = strlen(sql_query); | |
384 | ||
385 | statement_t *statement = NULL; | |
386 | ||
387 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
388 | ||
389 | if (!stmt) { | |
3 | 390 | lua_pushnil(L); |
4 | 391 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 392 | return 2; |
1 | 393 | } |
394 | ||
395 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 396 | lua_pushnil(L); |
4 | 397 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 398 | return 2; |
1 | 399 | } |
400 | ||
401 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
402 | statement->mysql = conn->mysql; | |
403 | statement->stmt = stmt; | |
404 | statement->metadata = NULL; | |
405 | ||
406 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
407 | lua_setmetatable(L, -2); | |
408 | ||
409 | return 1; | |
410 | } | |
411 | ||
412 | int dbd_mysql_statement(lua_State *L) { | |
2 | 413 | static const luaL_Reg statement_methods[] = { |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
414 | {"affected", statement_affected}, |
2 | 415 | {"close", statement_close}, |
416 | {"execute", statement_execute}, | |
417 | {"fetch", statement_fetch}, | |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
418 | {"rowcount", statement_rowcount}, |
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
|
419 | {"rows", statement_rows}, |
2 | 420 | {NULL, NULL} |
421 | }; | |
422 | ||
423 | static const luaL_Reg statement_class_methods[] = { | |
424 | {NULL, NULL} | |
425 | }; | |
426 | ||
1 | 427 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
428 | luaL_register(L, 0, statement_methods); | |
429 | lua_pushvalue(L,-1); | |
430 | lua_setfield(L, -2, "__index"); | |
431 | ||
432 | lua_pushcfunction(L, statement_gc); | |
433 | lua_setfield(L, -2, "__gc"); | |
434 | ||
435 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
436 | ||
437 | return 1; | |
438 | } |