Fri, 28 Nov 2008 22:32:14 +0000
Fixed bug: double bind params were using the same variables memory space so they were all being set to the same number.
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 | /* |
30 | * success = statement:close() | |
31 | */ | |
1 | 32 | static int statement_close(lua_State *L) { |
33 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
34 | ||
35 | if (statement->metadata) { | |
36 | mysql_free_result(statement->metadata); | |
3 | 37 | statement->metadata = NULL; |
1 | 38 | } |
39 | ||
40 | if (statement->stmt) { | |
3 | 41 | mysql_stmt_close(statement->stmt); |
42 | statement->stmt = NULL; | |
1 | 43 | } |
44 | ||
3 | 45 | lua_pushboolean(L, 1); |
1 | 46 | return 1; |
47 | } | |
48 | ||
2 | 49 | /* |
3 | 50 | * success,err = statement:execute(...) |
2 | 51 | */ |
1 | 52 | static int statement_execute(lua_State *L) { |
53 | int n = lua_gettop(L); | |
54 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
55 | int num_bind_params = n - 1; | |
3 | 56 | int expected_params; |
1 | 57 | |
58 | MYSQL_BIND *bind = NULL; | |
59 | MYSQL_RES *metadata = NULL; | |
60 | ||
61 | char *error_message = NULL; | |
62 | ||
63 | int p; | |
64 | ||
3 | 65 | if (!statement->stmt) { |
66 | lua_pushboolean(L, 0); | |
4 | 67 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 68 | return 2; |
69 | } | |
70 | ||
71 | expected_params = mysql_stmt_param_count(statement->stmt); | |
72 | ||
73 | if (expected_params != num_bind_params) { | |
74 | /* | |
75 | * mysql_stmt_bind_param does not handle this conndition, | |
76 | * and the client library will segfault if these do no match | |
77 | */ | |
78 | lua_pushboolean(L, 0); | |
4 | 79 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); |
3 | 80 | return 2; |
81 | } | |
82 | ||
1 | 83 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); |
84 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
85 | ||
86 | for (p = 2; p <= n; p++) { | |
87 | int type = lua_type(L, p); | |
88 | int i = p - 2; | |
89 | ||
90 | const char *str = NULL; | |
91 | size_t str_len; | |
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
|
92 | double *num = NULL; |
1 | 93 | |
94 | switch(type) { | |
95 | case LUA_TNIL: | |
96 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
97 | bind[i].is_null = (my_bool*)1; | |
98 | break; | |
99 | ||
100 | 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
|
101 | /* |
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
|
102 | * 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
|
103 | * 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
|
104 | */ |
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
|
105 | num = (double *)malloc(sizeof(double)); |
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
|
106 | *num = luaL_checknumber(L, p); |
1 | 107 | |
108 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
109 | 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
|
110 | bind[i].buffer = (char *)num; |
1 | 111 | bind[i].length = 0; |
112 | break; | |
113 | ||
114 | case LUA_TSTRING: | |
115 | str = luaL_checklstring(L, p, &str_len); | |
116 | ||
117 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
118 | bind[i].is_null = (my_bool*)0; | |
119 | bind[i].buffer = (char *)str; | |
120 | bind[i].length = &str_len; | |
121 | break; | |
122 | ||
123 | default: | |
4 | 124 | error_message = DBI_ERR_BINDING_UNKNOWN; |
1 | 125 | goto cleanup; |
126 | } | |
127 | } | |
128 | ||
129 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 130 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 131 | goto cleanup; |
132 | } | |
133 | ||
134 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 135 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 136 | goto cleanup; |
137 | } | |
138 | ||
139 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
140 | ||
141 | cleanup: | |
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
|
142 | if (bind) { |
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 | int i; |
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
|
144 | |
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 | for (i = 0; i < num_bind_params; i++) { |
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
|
146 | /* |
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
|
147 | * Free the memory associated with |
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
|
148 | * the allocation of double bind |
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
|
149 | * params. If the interface is |
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 | * extended with other types they |
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
|
151 | * will need to be added 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
|
152 | */ |
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
|
153 | if (bind[i].buffer_type == MYSQL_TYPE_DOUBLE && bind[i].buffer) { |
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
|
154 | free(bind[i].buffer); |
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
|
155 | } |
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
|
156 | } |
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
|
157 | |
1 | 158 | free(bind); |
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
|
159 | } |
1 | 160 | |
161 | if (error_message) { | |
3 | 162 | lua_pushboolean(L, 0); |
163 | lua_pushfstring(L, error_message, mysql_stmt_error(statement->stmt)); | |
164 | return 2; | |
1 | 165 | } |
166 | ||
167 | statement->metadata = metadata; | |
168 | ||
3 | 169 | lua_pushboolean(L, 1); |
1 | 170 | return 1; |
171 | } | |
172 | ||
173 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
174 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
175 | int column_count; | |
176 | MYSQL_BIND *bind = NULL; | |
177 | const char *error_message = NULL; | |
178 | ||
179 | if (!statement->stmt) { | |
4 | 180 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 181 | return 0; |
182 | } | |
183 | ||
184 | if (!statement->metadata) { | |
4 | 185 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 186 | return 0; |
1 | 187 | } |
188 | ||
189 | if (!statement->metadata) { | |
190 | lua_pushnil(L); | |
191 | return 1; | |
192 | } | |
193 | ||
194 | column_count = mysql_num_fields(statement->metadata); | |
195 | ||
196 | if (column_count > 0) { | |
197 | int i; | |
198 | MYSQL_FIELD *fields; | |
199 | ||
200 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
201 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
202 | ||
203 | fields = mysql_fetch_fields(statement->metadata); | |
204 | ||
205 | for (i = 0; i < column_count; i++) { | |
206 | unsigned int length = fields[i].length; | |
207 | ||
208 | char *buffer = (char *)malloc(length); | |
209 | memset(buffer, 0, length); | |
210 | ||
211 | bind[i].buffer_type = fields[i].type; | |
212 | bind[i].buffer = buffer; | |
213 | bind[i].buffer_length = length; | |
214 | } | |
215 | ||
216 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 217 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 218 | goto cleanup; |
219 | } | |
220 | ||
221 | if (!mysql_stmt_fetch(statement->stmt)) { | |
222 | int d = 1; | |
223 | ||
224 | lua_newtable(L); | |
225 | for (i = 0; i < column_count; i++) { | |
226 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
227 | const char *name = fields[i].name; | |
228 | ||
229 | if (lua_push == LUA_PUSH_NIL) { | |
230 | if (named_columns) { | |
231 | LUA_PUSH_ATTRIB_NIL(name); | |
232 | } else { | |
233 | LUA_PUSH_ARRAY_NIL(d); | |
234 | } | |
235 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
236 | if (named_columns) { | |
237 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
238 | } else { | |
239 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
240 | } | |
241 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
242 | if (named_columns) { | |
243 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
244 | } else { | |
245 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
246 | } | |
247 | } else if (lua_push == LUA_PUSH_STRING) { | |
248 | if (named_columns) { | |
249 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
250 | } else { | |
251 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
252 | } | |
253 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
254 | if (named_columns) { | |
255 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
256 | } else { | |
257 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
258 | } | |
259 | } else { | |
4 | 260 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 261 | } |
262 | } | |
263 | } else { | |
264 | lua_pushnil(L); | |
265 | } | |
266 | } | |
267 | ||
268 | cleanup: | |
269 | if (bind) { | |
270 | int i; | |
271 | ||
272 | for (i = 0; i < column_count; i++) { | |
273 | free(bind[i].buffer); | |
274 | } | |
275 | ||
276 | free(bind); | |
277 | } | |
278 | ||
279 | if (error_message) { | |
280 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | return 1; | |
285 | } | |
286 | ||
2 | 287 | /* |
288 | * array = statement:fetch() | |
289 | */ | |
1 | 290 | static int statement_fetch(lua_State *L) { |
291 | return statement_fetch_impl(L, 0); | |
292 | } | |
293 | ||
2 | 294 | /* |
295 | * hashmap = statement:fetchtable() | |
296 | */ | |
1 | 297 | static int statement_fetchtable(lua_State *L) { |
298 | return statement_fetch_impl(L, 1); | |
299 | } | |
300 | ||
2 | 301 | /* |
302 | * __gc | |
303 | */ | |
1 | 304 | static int statement_gc(lua_State *L) { |
305 | /* always free the handle */ | |
306 | statement_close(L); | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
312 | unsigned long sql_len = strlen(sql_query); | |
313 | ||
314 | statement_t *statement = NULL; | |
315 | ||
316 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
317 | ||
318 | if (!stmt) { | |
3 | 319 | lua_pushnil(L); |
4 | 320 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 321 | return 2; |
1 | 322 | } |
323 | ||
324 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 325 | lua_pushnil(L); |
4 | 326 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 327 | return 2; |
1 | 328 | } |
329 | ||
330 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
331 | statement->mysql = conn->mysql; | |
332 | statement->stmt = stmt; | |
333 | statement->metadata = NULL; | |
334 | ||
335 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
336 | lua_setmetatable(L, -2); | |
337 | ||
338 | return 1; | |
339 | } | |
340 | ||
341 | int dbd_mysql_statement(lua_State *L) { | |
2 | 342 | static const luaL_Reg statement_methods[] = { |
343 | {"close", statement_close}, | |
344 | {"execute", statement_execute}, | |
345 | {"fetch", statement_fetch}, | |
346 | {"fetchtable", statement_fetchtable}, | |
347 | {NULL, NULL} | |
348 | }; | |
349 | ||
350 | static const luaL_Reg statement_class_methods[] = { | |
351 | {NULL, NULL} | |
352 | }; | |
353 | ||
1 | 354 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
355 | luaL_register(L, 0, statement_methods); | |
356 | lua_pushvalue(L,-1); | |
357 | lua_setfield(L, -2, "__index"); | |
358 | ||
359 | lua_pushcfunction(L, statement_gc); | |
360 | lua_setfield(L, -2, "__gc"); | |
361 | ||
362 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
363 | ||
364 | return 1; | |
365 | } |