Mon, 01 Dec 2008 02:56:40 +0000
Bugfix: fix connection in DBD.SQLite3.New
Improve handling for booleans accross DB types
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; | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
91 | 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
|
92 | double *num = NULL; |
9 | 93 | int *boolean = NULL; |
1 | 94 | |
95 | switch(type) { | |
96 | case LUA_TNIL: | |
97 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
98 | bind[i].is_null = (my_bool*)1; | |
99 | break; | |
100 | ||
9 | 101 | case LUA_TBOOLEAN: |
102 | boolean = (int *)malloc(sizeof(int)); | |
103 | *boolean = lua_toboolean(L, p); | |
104 | bind[i].buffer_type = MYSQL_TYPE_LONG; | |
105 | bind[i].is_null = (my_bool*)0; | |
106 | bind[i].buffer = (char *)boolean; | |
107 | bind[i].length = 0; | |
108 | break; | |
109 | ||
1 | 110 | 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
|
111 | /* |
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
|
112 | * 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
|
113 | * 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
|
114 | */ |
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
|
115 | num = (double *)malloc(sizeof(double)); |
9 | 116 | *num = lua_tonumber(L, p); |
1 | 117 | |
118 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
119 | 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
|
120 | bind[i].buffer = (char *)num; |
1 | 121 | bind[i].length = 0; |
122 | break; | |
123 | ||
124 | case LUA_TSTRING: | |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
125 | str_len = malloc(sizeof(size_t)); |
9 | 126 | str = lua_tolstring(L, p, str_len); |
1 | 127 | |
128 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
129 | bind[i].is_null = (my_bool*)0; | |
130 | 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
|
131 | bind[i].length = str_len; |
1 | 132 | break; |
133 | ||
134 | default: | |
4 | 135 | error_message = DBI_ERR_BINDING_UNKNOWN; |
1 | 136 | goto cleanup; |
137 | } | |
138 | } | |
139 | ||
140 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 141 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 142 | goto cleanup; |
143 | } | |
144 | ||
145 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 146 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 147 | goto cleanup; |
148 | } | |
149 | ||
150 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
151 | ||
152 | 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
|
153 | 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
|
154 | 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
|
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 | 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
|
157 | /* |
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
|
158 | * Free the memory associated with |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
159 | * the allocation of double and string |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
160 | * bind params. If the interface are |
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
|
161 | * 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
|
162 | * 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
|
163 | */ |
9 | 164 | if (bind[i].buffer_type == MYSQL_TYPE_DOUBLE || bind[i].buffer_type == MYSQL_TYPE_LONG) { |
7
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
165 | if (bind[i].buffer) |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
166 | free(bind[i].buffer); |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
167 | } else if (bind[i].buffer_type == MYSQL_TYPE_STRING) { |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
168 | if (bind[i].length) |
4480ae002881
Bugfix - the size of strings for bind params pointed to the same memory
nrich@ii.net
parents:
6
diff
changeset
|
169 | free(bind[i].length); |
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
|
170 | } |
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
|
171 | } |
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
|
172 | |
1 | 173 | 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
|
174 | } |
1 | 175 | |
176 | if (error_message) { | |
3 | 177 | lua_pushboolean(L, 0); |
178 | lua_pushfstring(L, error_message, mysql_stmt_error(statement->stmt)); | |
179 | return 2; | |
1 | 180 | } |
181 | ||
182 | statement->metadata = metadata; | |
183 | ||
3 | 184 | lua_pushboolean(L, 1); |
1 | 185 | return 1; |
186 | } | |
187 | ||
188 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
189 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
190 | int column_count; | |
191 | MYSQL_BIND *bind = NULL; | |
192 | const char *error_message = NULL; | |
193 | ||
194 | if (!statement->stmt) { | |
4 | 195 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 196 | return 0; |
197 | } | |
198 | ||
199 | if (!statement->metadata) { | |
4 | 200 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 201 | return 0; |
1 | 202 | } |
203 | ||
204 | if (!statement->metadata) { | |
205 | lua_pushnil(L); | |
206 | return 1; | |
207 | } | |
208 | ||
209 | column_count = mysql_num_fields(statement->metadata); | |
210 | ||
211 | if (column_count > 0) { | |
212 | int i; | |
213 | MYSQL_FIELD *fields; | |
214 | ||
215 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
216 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
217 | ||
218 | fields = mysql_fetch_fields(statement->metadata); | |
219 | ||
220 | for (i = 0; i < column_count; i++) { | |
221 | unsigned int length = fields[i].length; | |
222 | ||
223 | char *buffer = (char *)malloc(length); | |
224 | memset(buffer, 0, length); | |
225 | ||
226 | bind[i].buffer_type = fields[i].type; | |
227 | bind[i].buffer = buffer; | |
228 | bind[i].buffer_length = length; | |
229 | } | |
230 | ||
231 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 232 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 233 | goto cleanup; |
234 | } | |
235 | ||
236 | if (!mysql_stmt_fetch(statement->stmt)) { | |
237 | int d = 1; | |
238 | ||
239 | lua_newtable(L); | |
240 | for (i = 0; i < column_count; i++) { | |
241 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
242 | const char *name = fields[i].name; | |
243 | ||
244 | if (lua_push == LUA_PUSH_NIL) { | |
245 | if (named_columns) { | |
246 | LUA_PUSH_ATTRIB_NIL(name); | |
247 | } else { | |
248 | LUA_PUSH_ARRAY_NIL(d); | |
249 | } | |
250 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
251 | if (named_columns) { | |
252 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
253 | } else { | |
254 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
255 | } | |
256 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
257 | if (named_columns) { | |
258 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
259 | } else { | |
260 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
261 | } | |
262 | } else if (lua_push == LUA_PUSH_STRING) { | |
263 | if (named_columns) { | |
264 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
265 | } else { | |
266 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
267 | } | |
268 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
269 | if (named_columns) { | |
270 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
271 | } else { | |
272 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
273 | } | |
274 | } else { | |
4 | 275 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 276 | } |
277 | } | |
278 | } else { | |
279 | lua_pushnil(L); | |
280 | } | |
281 | } | |
282 | ||
283 | cleanup: | |
284 | if (bind) { | |
285 | int i; | |
286 | ||
287 | for (i = 0; i < column_count; i++) { | |
288 | free(bind[i].buffer); | |
289 | } | |
290 | ||
291 | free(bind); | |
292 | } | |
293 | ||
294 | if (error_message) { | |
295 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | return 1; | |
300 | } | |
301 | ||
2 | 302 | /* |
303 | * array = statement:fetch() | |
304 | */ | |
1 | 305 | static int statement_fetch(lua_State *L) { |
306 | return statement_fetch_impl(L, 0); | |
307 | } | |
308 | ||
2 | 309 | /* |
310 | * hashmap = statement:fetchtable() | |
311 | */ | |
1 | 312 | static int statement_fetchtable(lua_State *L) { |
313 | return statement_fetch_impl(L, 1); | |
314 | } | |
315 | ||
2 | 316 | /* |
317 | * __gc | |
318 | */ | |
1 | 319 | static int statement_gc(lua_State *L) { |
320 | /* always free the handle */ | |
321 | statement_close(L); | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
326 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
327 | unsigned long sql_len = strlen(sql_query); | |
328 | ||
329 | statement_t *statement = NULL; | |
330 | ||
331 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
332 | ||
333 | if (!stmt) { | |
3 | 334 | lua_pushnil(L); |
4 | 335 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 336 | return 2; |
1 | 337 | } |
338 | ||
339 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 340 | lua_pushnil(L); |
4 | 341 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 342 | return 2; |
1 | 343 | } |
344 | ||
345 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
346 | statement->mysql = conn->mysql; | |
347 | statement->stmt = stmt; | |
348 | statement->metadata = NULL; | |
349 | ||
350 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
351 | lua_setmetatable(L, -2); | |
352 | ||
353 | return 1; | |
354 | } | |
355 | ||
356 | int dbd_mysql_statement(lua_State *L) { | |
2 | 357 | static const luaL_Reg statement_methods[] = { |
358 | {"close", statement_close}, | |
359 | {"execute", statement_execute}, | |
360 | {"fetch", statement_fetch}, | |
361 | {"fetchtable", statement_fetchtable}, | |
362 | {NULL, NULL} | |
363 | }; | |
364 | ||
365 | static const luaL_Reg statement_class_methods[] = { | |
366 | {NULL, NULL} | |
367 | }; | |
368 | ||
1 | 369 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
370 | luaL_register(L, 0, statement_methods); | |
371 | lua_pushvalue(L,-1); | |
372 | lua_setfield(L, -2, "__index"); | |
373 | ||
374 | lua_pushcfunction(L, statement_gc); | |
375 | lua_setfield(L, -2, "__gc"); | |
376 | ||
377 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
378 | ||
379 | return 1; | |
380 | } |