Wed, 26 Nov 2008 10:43:45 +0000
Clean up error messages for consistency.
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; | |
92 | ||
93 | double num; | |
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 | ||
101 | case LUA_TNUMBER: | |
102 | num = luaL_checknumber(L, p); | |
103 | ||
104 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
105 | bind[i].is_null = (my_bool*)0; | |
106 | bind[i].buffer = (char *)# | |
107 | bind[i].length = 0; | |
108 | break; | |
109 | ||
110 | case LUA_TSTRING: | |
111 | str = luaL_checklstring(L, p, &str_len); | |
112 | ||
113 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
114 | bind[i].is_null = (my_bool*)0; | |
115 | bind[i].buffer = (char *)str; | |
116 | bind[i].length = &str_len; | |
117 | break; | |
118 | ||
119 | default: | |
4 | 120 | error_message = DBI_ERR_BINDING_UNKNOWN; |
1 | 121 | goto cleanup; |
122 | } | |
123 | } | |
124 | ||
125 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
4 | 126 | error_message = DBI_ERR_BINDING_PARAMS; |
1 | 127 | goto cleanup; |
128 | } | |
129 | ||
130 | if (mysql_stmt_execute(statement->stmt)) { | |
4 | 131 | error_message = DBI_ERR_BINDING_EXEC; |
1 | 132 | goto cleanup; |
133 | } | |
134 | ||
135 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
136 | ||
137 | cleanup: | |
138 | if (bind) | |
139 | free(bind); | |
140 | ||
141 | if (error_message) { | |
3 | 142 | lua_pushboolean(L, 0); |
143 | lua_pushfstring(L, error_message, mysql_stmt_error(statement->stmt)); | |
144 | return 2; | |
1 | 145 | } |
146 | ||
147 | statement->metadata = metadata; | |
148 | ||
3 | 149 | lua_pushboolean(L, 1); |
1 | 150 | return 1; |
151 | } | |
152 | ||
153 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
154 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
155 | int column_count; | |
156 | MYSQL_BIND *bind = NULL; | |
157 | const char *error_message = NULL; | |
158 | ||
159 | if (!statement->stmt) { | |
4 | 160 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 161 | return 0; |
162 | } | |
163 | ||
164 | if (!statement->metadata) { | |
4 | 165 | luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE); |
3 | 166 | return 0; |
1 | 167 | } |
168 | ||
169 | if (!statement->metadata) { | |
170 | lua_pushnil(L); | |
171 | return 1; | |
172 | } | |
173 | ||
174 | column_count = mysql_num_fields(statement->metadata); | |
175 | ||
176 | if (column_count > 0) { | |
177 | int i; | |
178 | MYSQL_FIELD *fields; | |
179 | ||
180 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
181 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
182 | ||
183 | fields = mysql_fetch_fields(statement->metadata); | |
184 | ||
185 | for (i = 0; i < column_count; i++) { | |
186 | unsigned int length = fields[i].length; | |
187 | ||
188 | char *buffer = (char *)malloc(length); | |
189 | memset(buffer, 0, length); | |
190 | ||
191 | bind[i].buffer_type = fields[i].type; | |
192 | bind[i].buffer = buffer; | |
193 | bind[i].buffer_length = length; | |
194 | } | |
195 | ||
196 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
4 | 197 | error_message = DBI_ERR_BINDING_RESULTS; |
1 | 198 | goto cleanup; |
199 | } | |
200 | ||
201 | if (!mysql_stmt_fetch(statement->stmt)) { | |
202 | int d = 1; | |
203 | ||
204 | lua_newtable(L); | |
205 | for (i = 0; i < column_count; i++) { | |
206 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
207 | const char *name = fields[i].name; | |
208 | ||
209 | if (lua_push == LUA_PUSH_NIL) { | |
210 | if (named_columns) { | |
211 | LUA_PUSH_ATTRIB_NIL(name); | |
212 | } else { | |
213 | LUA_PUSH_ARRAY_NIL(d); | |
214 | } | |
215 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
216 | if (named_columns) { | |
217 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
218 | } else { | |
219 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
220 | } | |
221 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
222 | if (named_columns) { | |
223 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
224 | } else { | |
225 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
226 | } | |
227 | } else if (lua_push == LUA_PUSH_STRING) { | |
228 | if (named_columns) { | |
229 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
230 | } else { | |
231 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
232 | } | |
233 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
234 | if (named_columns) { | |
235 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
236 | } else { | |
237 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
238 | } | |
239 | } else { | |
4 | 240 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 241 | } |
242 | } | |
243 | } else { | |
244 | lua_pushnil(L); | |
245 | } | |
246 | } | |
247 | ||
248 | cleanup: | |
249 | if (bind) { | |
250 | int i; | |
251 | ||
252 | for (i = 0; i < column_count; i++) { | |
253 | free(bind[i].buffer); | |
254 | } | |
255 | ||
256 | free(bind); | |
257 | } | |
258 | ||
259 | if (error_message) { | |
260 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
261 | return 0; | |
262 | } | |
263 | ||
264 | return 1; | |
265 | } | |
266 | ||
2 | 267 | /* |
268 | * array = statement:fetch() | |
269 | */ | |
1 | 270 | static int statement_fetch(lua_State *L) { |
271 | return statement_fetch_impl(L, 0); | |
272 | } | |
273 | ||
2 | 274 | /* |
275 | * hashmap = statement:fetchtable() | |
276 | */ | |
1 | 277 | static int statement_fetchtable(lua_State *L) { |
278 | return statement_fetch_impl(L, 1); | |
279 | } | |
280 | ||
2 | 281 | /* |
282 | * __gc | |
283 | */ | |
1 | 284 | static int statement_gc(lua_State *L) { |
285 | /* always free the handle */ | |
286 | statement_close(L); | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
292 | unsigned long sql_len = strlen(sql_query); | |
293 | ||
294 | statement_t *statement = NULL; | |
295 | ||
296 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
297 | ||
298 | if (!stmt) { | |
3 | 299 | lua_pushnil(L); |
4 | 300 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, mysql_error(conn->mysql)); |
3 | 301 | return 2; |
1 | 302 | } |
303 | ||
304 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
3 | 305 | lua_pushnil(L); |
4 | 306 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, mysql_stmt_error(stmt)); |
3 | 307 | return 2; |
1 | 308 | } |
309 | ||
310 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
311 | statement->mysql = conn->mysql; | |
312 | statement->stmt = stmt; | |
313 | statement->metadata = NULL; | |
314 | ||
315 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
316 | lua_setmetatable(L, -2); | |
317 | ||
318 | return 1; | |
319 | } | |
320 | ||
321 | int dbd_mysql_statement(lua_State *L) { | |
2 | 322 | static const luaL_Reg statement_methods[] = { |
323 | {"close", statement_close}, | |
324 | {"execute", statement_execute}, | |
325 | {"fetch", statement_fetch}, | |
326 | {"fetchtable", statement_fetchtable}, | |
327 | {NULL, NULL} | |
328 | }; | |
329 | ||
330 | static const luaL_Reg statement_class_methods[] = { | |
331 | {NULL, NULL} | |
332 | }; | |
333 | ||
1 | 334 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
335 | luaL_register(L, 0, statement_methods); | |
336 | lua_pushvalue(L,-1); | |
337 | lua_setfield(L, -2, "__index"); | |
338 | ||
339 | lua_pushcfunction(L, statement_gc); | |
340 | lua_setfield(L, -2, "__gc"); | |
341 | ||
342 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
343 | ||
344 | return 1; | |
345 | } |