Fri, 05 Dec 2008 07:41:15 +0000
Add DB2 driver module.
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; |
14 | 57 | |
58 | unsigned char *buffer = NULL; | |
59 | int offset = 0; | |
1 | 60 | |
61 | MYSQL_BIND *bind = NULL; | |
62 | MYSQL_RES *metadata = NULL; | |
63 | ||
64 | char *error_message = NULL; | |
10 | 65 | char *errstr = NULL; |
1 | 66 | |
67 | int p; | |
68 | ||
3 | 69 | if (!statement->stmt) { |
70 | lua_pushboolean(L, 0); | |
4 | 71 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); |
3 | 72 | return 2; |
73 | } | |
74 | ||
75 | expected_params = mysql_stmt_param_count(statement->stmt); | |
76 | ||
77 | if (expected_params != num_bind_params) { | |
78 | /* | |
10 | 79 | * mysql_stmt_bind_param does not handle this condition, |
3 | 80 | * and the client library will segfault if these do no match |
81 | */ | |
82 | lua_pushboolean(L, 0); | |
4 | 83 | lua_pushfstring(L, DBI_ERR_PARAM_MISCOUNT, expected_params, num_bind_params); |
3 | 84 | return 2; |
85 | } | |
86 | ||
1 | 87 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); |
88 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
89 | ||
90 | for (p = 2; p <= n; p++) { | |
91 | int type = lua_type(L, p); | |
92 | int i = p - 2; | |
93 | ||
94 | 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
|
95 | 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
|
96 | double *num = NULL; |
9 | 97 | int *boolean = NULL; |
10 | 98 | char err[64]; |
1 | 99 | |
100 | switch(type) { | |
101 | case LUA_TNIL: | |
102 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
103 | bind[i].is_null = (my_bool*)1; | |
104 | break; | |
105 | ||
9 | 106 | case LUA_TBOOLEAN: |
14 | 107 | buffer = realloc(buffer, offset + sizeof(int)); |
108 | boolean = (int *)buffer + offset; | |
109 | offset += sizeof(int); | |
9 | 110 | *boolean = lua_toboolean(L, p); |
14 | 111 | |
9 | 112 | bind[i].buffer_type = MYSQL_TYPE_LONG; |
113 | bind[i].is_null = (my_bool*)0; | |
114 | bind[i].buffer = (char *)boolean; | |
115 | bind[i].length = 0; | |
116 | break; | |
117 | ||
1 | 118 | 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
|
119 | /* |
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 | * 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
|
121 | * 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
|
122 | */ |
14 | 123 | buffer = realloc(buffer, offset + sizeof(double)); |
124 | num = (double *)buffer + offset; | |
125 | offset += sizeof(double); | |
9 | 126 | *num = lua_tonumber(L, p); |
1 | 127 | |
128 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
129 | 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
|
130 | bind[i].buffer = (char *)num; |
1 | 131 | bind[i].length = 0; |
132 | break; | |
133 | ||
134 | case LUA_TSTRING: | |
14 | 135 | buffer = realloc(buffer, offset + sizeof(size_t)); |
136 | str_len = (size_t *)buffer + offset; | |
137 | offset += sizeof(size_t); | |
9 | 138 | str = lua_tolstring(L, p, str_len); |
1 | 139 | |
140 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
141 | bind[i].is_null = (my_bool*)0; | |
142 | 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
|
143 | bind[i].length = str_len; |
1 | 144 | break; |
145 | ||
146 | default: | |
10 | 147 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); |
148 | errstr = err; | |
149 | error_message = DBI_ERR_BINDING_PARAMS; | |
1 | 150 | goto cleanup; |
151 | } | |
152 | } | |
153 | ||
154 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 155 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 156 | goto cleanup; |
157 | } | |
158 | ||
159 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 160 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 161 | goto cleanup; |
162 | } | |
163 | ||
164 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
165 | ||
166 | cleanup: | |
14 | 167 | /* |
168 | * free the buffer with a resize to 0 | |
169 | */ | |
170 | 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
|
171 | |
14 | 172 | if (bind) |
1 | 173 | free(bind); |
174 | ||
175 | if (error_message) { | |
3 | 176 | lua_pushboolean(L, 0); |
10 | 177 | lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt)); |
3 | 178 | return 2; |
1 | 179 | } |
180 | ||
181 | statement->metadata = metadata; | |
182 | ||
3 | 183 | lua_pushboolean(L, 1); |
1 | 184 | return 1; |
185 | } | |
186 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
187 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
1 | 188 | int column_count; |
189 | MYSQL_BIND *bind = NULL; | |
190 | const char *error_message = NULL; | |
191 | ||
192 | if (!statement->stmt) { | |
4 | 193 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 194 | return 0; |
195 | } | |
196 | ||
197 | if (!statement->metadata) { | |
4 | 198 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 199 | return 0; |
1 | 200 | } |
201 | ||
202 | if (!statement->metadata) { | |
203 | lua_pushnil(L); | |
204 | return 1; | |
205 | } | |
206 | ||
207 | column_count = mysql_num_fields(statement->metadata); | |
208 | ||
209 | if (column_count > 0) { | |
210 | int i; | |
211 | MYSQL_FIELD *fields; | |
212 | ||
213 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
214 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
215 | ||
216 | fields = mysql_fetch_fields(statement->metadata); | |
217 | ||
218 | for (i = 0; i < column_count; i++) { | |
219 | unsigned int length = fields[i].length; | |
220 | ||
221 | char *buffer = (char *)malloc(length); | |
222 | memset(buffer, 0, length); | |
223 | ||
224 | bind[i].buffer_type = fields[i].type; | |
225 | bind[i].buffer = buffer; | |
226 | bind[i].buffer_length = length; | |
227 | } | |
228 | ||
229 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 230 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 231 | goto cleanup; |
232 | } | |
233 | ||
234 | if (!mysql_stmt_fetch(statement->stmt)) { | |
235 | int d = 1; | |
236 | ||
237 | lua_newtable(L); | |
238 | for (i = 0; i < column_count; i++) { | |
239 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
240 | const char *name = fields[i].name; | |
241 | ||
242 | if (lua_push == LUA_PUSH_NIL) { | |
243 | if (named_columns) { | |
244 | LUA_PUSH_ATTRIB_NIL(name); | |
245 | } else { | |
246 | LUA_PUSH_ARRAY_NIL(d); | |
247 | } | |
248 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
249 | if (named_columns) { | |
250 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
251 | } else { | |
252 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
253 | } | |
254 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
255 | if (named_columns) { | |
256 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
257 | } else { | |
258 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
259 | } | |
260 | } else if (lua_push == LUA_PUSH_STRING) { | |
261 | if (named_columns) { | |
262 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
263 | } else { | |
264 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
265 | } | |
266 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
267 | if (named_columns) { | |
268 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
269 | } else { | |
270 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
271 | } | |
272 | } else { | |
4 | 273 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 274 | } |
275 | } | |
276 | } else { | |
277 | lua_pushnil(L); | |
278 | } | |
279 | } | |
280 | ||
281 | cleanup: | |
282 | if (bind) { | |
283 | int i; | |
284 | ||
285 | for (i = 0; i < column_count; i++) { | |
286 | free(bind[i].buffer); | |
287 | } | |
288 | ||
289 | free(bind); | |
290 | } | |
291 | ||
292 | if (error_message) { | |
293 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
294 | return 0; | |
295 | } | |
296 | ||
297 | return 1; | |
298 | } | |
299 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
300 | static int next_iterator(lua_State *L) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
301 | 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
|
302 | 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
|
303 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
304 | return statement_fetch_impl(L, statement, named_columns); |
1 | 305 | } |
306 | ||
2 | 307 | /* |
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
|
308 | * 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
|
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 | 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
|
311 | 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
|
312 | 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
|
313 | |
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
|
314 | 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
|
315 | } |
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
|
316 | |
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
|
317 | /* |
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
|
318 | * iterfunc = statement:rows(named_indexes) |
2 | 319 | */ |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
320 | |
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
|
321 | 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
|
322 | if (lua_gettop(L) == 1) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
323 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
324 | lua_pushboolean(L, 0); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
325 | } else { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
326 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
327 | lua_pushboolean(L, lua_toboolean(L, 2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
328 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
329 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
330 | lua_pushcclosure(L, next_iterator, 2); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
331 | return 1; |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
332 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
333 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
334 | /* |
2 | 335 | * __gc |
336 | */ | |
1 | 337 | static int statement_gc(lua_State *L) { |
338 | /* always free the handle */ | |
339 | statement_close(L); | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
345 | unsigned long sql_len = strlen(sql_query); | |
346 | ||
347 | statement_t *statement = NULL; | |
348 | ||
349 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
350 | ||
351 | if (!stmt) { | |
3 | 352 | lua_pushnil(L); |
4 | 353 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 354 | return 2; |
1 | 355 | } |
356 | ||
357 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 358 | lua_pushnil(L); |
4 | 359 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 360 | return 2; |
1 | 361 | } |
362 | ||
363 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
364 | statement->mysql = conn->mysql; | |
365 | statement->stmt = stmt; | |
366 | statement->metadata = NULL; | |
367 | ||
368 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
369 | lua_setmetatable(L, -2); | |
370 | ||
371 | return 1; | |
372 | } | |
373 | ||
374 | int dbd_mysql_statement(lua_State *L) { | |
2 | 375 | static const luaL_Reg statement_methods[] = { |
376 | {"close", statement_close}, | |
377 | {"execute", statement_execute}, | |
378 | {"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
|
379 | {"rows", statement_rows}, |
2 | 380 | {NULL, NULL} |
381 | }; | |
382 | ||
383 | static const luaL_Reg statement_class_methods[] = { | |
384 | {NULL, NULL} | |
385 | }; | |
386 | ||
1 | 387 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
388 | luaL_register(L, 0, statement_methods); | |
389 | lua_pushvalue(L,-1); | |
390 | lua_setfield(L, -2, "__index"); | |
391 | ||
392 | lua_pushcfunction(L, statement_gc); | |
393 | lua_setfield(L, -2, "__gc"); | |
394 | ||
395 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
396 | ||
397 | return 1; | |
398 | } |