Fri, 19 Dec 2008 09:17:16 +0000
Added the statement:affected() and statement:rowcount() methods.
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 | |
73 | unsigned char *buffer = NULL; | |
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 | ||
1 | 102 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); |
103 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
104 | ||
105 | for (p = 2; p <= n; p++) { | |
106 | int type = lua_type(L, p); | |
107 | int i = p - 2; | |
108 | ||
109 | 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
|
110 | 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
|
111 | double *num = NULL; |
9 | 112 | int *boolean = NULL; |
10 | 113 | char err[64]; |
1 | 114 | |
115 | switch(type) { | |
116 | case LUA_TNIL: | |
117 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
118 | bind[i].is_null = (my_bool*)1; | |
119 | break; | |
120 | ||
9 | 121 | case LUA_TBOOLEAN: |
14 | 122 | buffer = realloc(buffer, offset + sizeof(int)); |
123 | boolean = (int *)buffer + offset; | |
124 | offset += sizeof(int); | |
9 | 125 | *boolean = lua_toboolean(L, p); |
14 | 126 | |
9 | 127 | bind[i].buffer_type = MYSQL_TYPE_LONG; |
128 | bind[i].is_null = (my_bool*)0; | |
129 | bind[i].buffer = (char *)boolean; | |
130 | bind[i].length = 0; | |
131 | break; | |
132 | ||
1 | 133 | 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
|
134 | /* |
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
|
135 | * 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
|
136 | * 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
|
137 | */ |
14 | 138 | buffer = realloc(buffer, offset + sizeof(double)); |
139 | num = (double *)buffer + offset; | |
140 | offset += sizeof(double); | |
9 | 141 | *num = lua_tonumber(L, p); |
1 | 142 | |
143 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
144 | 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
|
145 | bind[i].buffer = (char *)num; |
1 | 146 | bind[i].length = 0; |
147 | break; | |
148 | ||
149 | case LUA_TSTRING: | |
14 | 150 | buffer = realloc(buffer, offset + sizeof(size_t)); |
151 | str_len = (size_t *)buffer + offset; | |
152 | offset += sizeof(size_t); | |
9 | 153 | str = lua_tolstring(L, p, str_len); |
1 | 154 | |
155 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
156 | bind[i].is_null = (my_bool*)0; | |
157 | 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
|
158 | bind[i].length = str_len; |
1 | 159 | break; |
160 | ||
161 | default: | |
10 | 162 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); |
163 | errstr = err; | |
164 | error_message = DBI_ERR_BINDING_PARAMS; | |
1 | 165 | goto cleanup; |
166 | } | |
167 | } | |
168 | ||
169 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 170 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 171 | goto cleanup; |
172 | } | |
173 | ||
174 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 175 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 176 | goto cleanup; |
177 | } | |
178 | ||
179 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
180 | ||
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
181 | if (metadata) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
182 | mysql_stmt_store_result(statement->stmt); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
183 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
184 | |
1 | 185 | cleanup: |
14 | 186 | /* |
187 | * free the buffer with a resize to 0 | |
188 | */ | |
189 | realloc(buffer, 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
|
190 | |
14 | 191 | if (bind) |
1 | 192 | free(bind); |
193 | ||
194 | if (error_message) { | |
3 | 195 | lua_pushboolean(L, 0); |
10 | 196 | lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt)); |
3 | 197 | return 2; |
1 | 198 | } |
199 | ||
200 | statement->metadata = metadata; | |
201 | ||
3 | 202 | lua_pushboolean(L, 1); |
1 | 203 | return 1; |
204 | } | |
205 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
206 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
1 | 207 | int column_count; |
208 | MYSQL_BIND *bind = NULL; | |
209 | const char *error_message = NULL; | |
210 | ||
211 | if (!statement->stmt) { | |
4 | 212 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 213 | return 0; |
214 | } | |
215 | ||
216 | if (!statement->metadata) { | |
4 | 217 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 218 | return 0; |
1 | 219 | } |
220 | ||
221 | if (!statement->metadata) { | |
222 | lua_pushnil(L); | |
223 | return 1; | |
224 | } | |
225 | ||
226 | column_count = mysql_num_fields(statement->metadata); | |
227 | ||
228 | if (column_count > 0) { | |
229 | int i; | |
230 | MYSQL_FIELD *fields; | |
231 | ||
232 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
233 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
234 | ||
235 | fields = mysql_fetch_fields(statement->metadata); | |
236 | ||
237 | for (i = 0; i < column_count; i++) { | |
238 | unsigned int length = fields[i].length; | |
239 | ||
240 | char *buffer = (char *)malloc(length); | |
241 | memset(buffer, 0, length); | |
242 | ||
243 | bind[i].buffer_type = fields[i].type; | |
244 | bind[i].buffer = buffer; | |
245 | bind[i].buffer_length = length; | |
246 | } | |
247 | ||
248 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 249 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 250 | goto cleanup; |
251 | } | |
252 | ||
253 | if (!mysql_stmt_fetch(statement->stmt)) { | |
254 | int d = 1; | |
255 | ||
256 | lua_newtable(L); | |
257 | for (i = 0; i < column_count; i++) { | |
258 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
259 | const char *name = fields[i].name; | |
260 | ||
261 | if (lua_push == LUA_PUSH_NIL) { | |
262 | if (named_columns) { | |
263 | LUA_PUSH_ATTRIB_NIL(name); | |
264 | } else { | |
265 | LUA_PUSH_ARRAY_NIL(d); | |
266 | } | |
267 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
268 | if (named_columns) { | |
269 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
270 | } else { | |
271 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
272 | } | |
273 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
274 | if (named_columns) { | |
275 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
276 | } else { | |
277 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
278 | } | |
279 | } else if (lua_push == LUA_PUSH_STRING) { | |
280 | if (named_columns) { | |
281 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
282 | } else { | |
283 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
284 | } | |
285 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
286 | if (named_columns) { | |
287 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
288 | } else { | |
289 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
290 | } | |
291 | } else { | |
4 | 292 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 293 | } |
294 | } | |
295 | } else { | |
296 | lua_pushnil(L); | |
297 | } | |
298 | } | |
299 | ||
300 | cleanup: | |
301 | if (bind) { | |
302 | int i; | |
303 | ||
304 | for (i = 0; i < column_count; i++) { | |
305 | free(bind[i].buffer); | |
306 | } | |
307 | ||
308 | free(bind); | |
309 | } | |
310 | ||
311 | if (error_message) { | |
312 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
313 | return 0; | |
314 | } | |
315 | ||
316 | return 1; | |
317 | } | |
318 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
319 | static int next_iterator(lua_State *L) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
320 | 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
|
321 | 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
|
322 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
323 | return statement_fetch_impl(L, statement, named_columns); |
1 | 324 | } |
325 | ||
2 | 326 | /* |
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
|
327 | * 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
|
328 | */ |
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
|
329 | 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
|
330 | 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
|
331 | 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
|
332 | |
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 | 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
|
334 | } |
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 | |
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 | /* |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
337 | * num_rows = statement:rowcount() |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
338 | */ |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
339 | static int statement_rowcount(lua_State *L) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
340 | 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
|
341 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
342 | if (!statement->stmt) { |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
343 | luaL_error(L, DBI_ERR_INVALID_STATEMENT); |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
344 | } |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
345 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
346 | 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
|
347 | |
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
348 | return 1; |
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 | /* |
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
|
352 | * iterfunc = statement:rows(named_indexes) |
2 | 353 | */ |
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
|
354 | 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
|
355 | if (lua_gettop(L) == 1) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
356 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
357 | lua_pushboolean(L, 0); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
358 | } else { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
359 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
360 | lua_pushboolean(L, lua_toboolean(L, 2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
361 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
362 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
363 | lua_pushcclosure(L, next_iterator, 2); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
364 | return 1; |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
365 | } |
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 | /* |
2 | 368 | * __gc |
369 | */ | |
1 | 370 | static int statement_gc(lua_State *L) { |
371 | /* always free the handle */ | |
372 | statement_close(L); | |
373 | ||
374 | return 0; | |
375 | } | |
376 | ||
377 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
378 | unsigned long sql_len = strlen(sql_query); | |
379 | ||
380 | statement_t *statement = NULL; | |
381 | ||
382 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
383 | ||
384 | if (!stmt) { | |
3 | 385 | lua_pushnil(L); |
4 | 386 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 387 | return 2; |
1 | 388 | } |
389 | ||
390 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 391 | lua_pushnil(L); |
4 | 392 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 393 | return 2; |
1 | 394 | } |
395 | ||
396 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
397 | statement->mysql = conn->mysql; | |
398 | statement->stmt = stmt; | |
399 | statement->metadata = NULL; | |
400 | ||
401 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
402 | lua_setmetatable(L, -2); | |
403 | ||
404 | return 1; | |
405 | } | |
406 | ||
407 | int dbd_mysql_statement(lua_State *L) { | |
2 | 408 | static const luaL_Reg statement_methods[] = { |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
409 | {"affected", statement_affected}, |
2 | 410 | {"close", statement_close}, |
411 | {"execute", statement_execute}, | |
412 | {"fetch", statement_fetch}, | |
21
7956401a0c5e
Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents:
14
diff
changeset
|
413 | {"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
|
414 | {"rows", statement_rows}, |
2 | 415 | {NULL, NULL} |
416 | }; | |
417 | ||
418 | static const luaL_Reg statement_class_methods[] = { | |
419 | {NULL, NULL} | |
420 | }; | |
421 | ||
1 | 422 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
423 | luaL_register(L, 0, statement_methods); | |
424 | lua_pushvalue(L,-1); | |
425 | lua_setfield(L, -2, "__index"); | |
426 | ||
427 | lua_pushcfunction(L, statement_gc); | |
428 | lua_setfield(L, -2, "__gc"); | |
429 | ||
430 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
431 | ||
432 | return 1; | |
433 | } |