Sun, 23 Nov 2008 04:12:04 +0000
Cleanup and commenting
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); | |
37 | } | |
38 | ||
39 | if (statement->stmt) { | |
40 | mysql_stmt_close(statement->stmt); | |
41 | } | |
42 | ||
43 | return 1; | |
44 | } | |
45 | ||
2 | 46 | /* |
47 | * success = statement:execute(...) | |
48 | */ | |
1 | 49 | static int statement_execute(lua_State *L) { |
50 | int n = lua_gettop(L); | |
51 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
52 | int num_bind_params = n - 1; | |
53 | ||
54 | MYSQL_BIND *bind = NULL; | |
55 | MYSQL_RES *metadata = NULL; | |
56 | ||
57 | char *error_message = NULL; | |
58 | ||
59 | int p; | |
60 | ||
61 | bind = malloc(sizeof(MYSQL_BIND) * num_bind_params); | |
62 | memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params); | |
63 | ||
64 | for (p = 2; p <= n; p++) { | |
65 | int type = lua_type(L, p); | |
66 | int i = p - 2; | |
67 | ||
68 | const char *str = NULL; | |
69 | size_t str_len; | |
70 | ||
71 | double num; | |
72 | ||
73 | switch(type) { | |
74 | case LUA_TNIL: | |
75 | bind[i].buffer_type = MYSQL_TYPE_NULL; | |
76 | bind[i].is_null = (my_bool*)1; | |
77 | break; | |
78 | ||
79 | case LUA_TNUMBER: | |
80 | num = luaL_checknumber(L, p); | |
81 | ||
82 | bind[i].buffer_type = MYSQL_TYPE_DOUBLE; | |
83 | bind[i].is_null = (my_bool*)0; | |
84 | bind[i].buffer = (char *)# | |
85 | bind[i].length = 0; | |
86 | break; | |
87 | ||
88 | case LUA_TSTRING: | |
89 | str = luaL_checklstring(L, p, &str_len); | |
90 | ||
91 | bind[i].buffer_type = MYSQL_TYPE_STRING; | |
92 | bind[i].is_null = (my_bool*)0; | |
93 | bind[i].buffer = (char *)str; | |
94 | bind[i].length = &str_len; | |
95 | break; | |
96 | ||
97 | default: | |
98 | error_message = "Binding unknown or unsupported type"; | |
99 | goto cleanup; | |
100 | } | |
101 | } | |
102 | ||
103 | if (mysql_stmt_bind_param(statement->stmt, bind)) { | |
104 | error_message = "Error binding statement parameters: %s"; | |
105 | goto cleanup; | |
106 | } | |
107 | ||
108 | if (mysql_stmt_execute(statement->stmt)) { | |
109 | error_message = "Error executing statement: %s"; | |
110 | goto cleanup; | |
111 | } | |
112 | ||
113 | metadata = mysql_stmt_result_metadata(statement->stmt); | |
114 | ||
115 | cleanup: | |
116 | if (bind) | |
117 | free(bind); | |
118 | ||
119 | if (error_message) { | |
120 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
121 | return 0; | |
122 | } | |
123 | ||
124 | statement->metadata = metadata; | |
125 | ||
126 | return 1; | |
127 | } | |
128 | ||
129 | static int statement_fetch_impl(lua_State *L, int named_columns) { | |
130 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_MYSQL_STATEMENT); | |
131 | int column_count; | |
132 | MYSQL_BIND *bind = NULL; | |
133 | const char *error_message = NULL; | |
134 | ||
135 | if (!statement->stmt) { | |
136 | luaL_error(L, "fetch called before execute"); | |
137 | lua_pushnil(L); | |
138 | return 1; | |
139 | } | |
140 | ||
141 | if (!statement->metadata) { | |
142 | lua_pushnil(L); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | column_count = mysql_num_fields(statement->metadata); | |
147 | ||
148 | if (column_count > 0) { | |
149 | int i; | |
150 | MYSQL_FIELD *fields; | |
151 | ||
152 | bind = malloc(sizeof(MYSQL_BIND) * column_count); | |
153 | memset(bind, 0, sizeof(MYSQL_BIND) * column_count); | |
154 | ||
155 | fields = mysql_fetch_fields(statement->metadata); | |
156 | ||
157 | for (i = 0; i < column_count; i++) { | |
158 | unsigned int length = fields[i].length; | |
159 | ||
160 | char *buffer = (char *)malloc(length); | |
161 | memset(buffer, 0, length); | |
162 | ||
163 | bind[i].buffer_type = fields[i].type; | |
164 | bind[i].buffer = buffer; | |
165 | bind[i].buffer_length = length; | |
166 | } | |
167 | ||
168 | if (mysql_stmt_bind_result(statement->stmt, bind)) { | |
169 | error_message = "Error binding results: %s"; | |
170 | goto cleanup; | |
171 | } | |
172 | ||
173 | if (!mysql_stmt_fetch(statement->stmt)) { | |
174 | int d = 1; | |
175 | ||
176 | lua_newtable(L); | |
177 | for (i = 0; i < column_count; i++) { | |
178 | lua_push_type_t lua_push = mysql_to_lua_push(fields[i].type); | |
179 | const char *name = fields[i].name; | |
180 | ||
181 | if (lua_push == LUA_PUSH_NIL) { | |
182 | if (named_columns) { | |
183 | LUA_PUSH_ATTRIB_NIL(name); | |
184 | } else { | |
185 | LUA_PUSH_ARRAY_NIL(d); | |
186 | } | |
187 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
188 | if (named_columns) { | |
189 | LUA_PUSH_ATTRIB_INT(name, *(int *)(bind[i].buffer)); | |
190 | } else { | |
191 | LUA_PUSH_ARRAY_INT(d, *(int *)(bind[i].buffer)); | |
192 | } | |
193 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
194 | if (named_columns) { | |
195 | LUA_PUSH_ATTRIB_FLOAT(name, *(double *)(bind[i].buffer)); | |
196 | } else { | |
197 | LUA_PUSH_ARRAY_FLOAT(d, *(double *)(bind[i].buffer)); | |
198 | } | |
199 | } else if (lua_push == LUA_PUSH_STRING) { | |
200 | if (named_columns) { | |
201 | LUA_PUSH_ATTRIB_STRING(name, bind[i].buffer); | |
202 | } else { | |
203 | LUA_PUSH_ARRAY_STRING(d, bind[i].buffer); | |
204 | } | |
205 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
206 | if (named_columns) { | |
207 | LUA_PUSH_ATTRIB_BOOL(name, *(int *)(bind[i].buffer)); | |
208 | } else { | |
209 | LUA_PUSH_ARRAY_BOOL(d, *(int *)(bind[i].buffer)); | |
210 | } | |
211 | } else { | |
212 | luaL_error(L, "Unknown push type in result set"); | |
213 | } | |
214 | } | |
215 | } else { | |
216 | lua_pushnil(L); | |
217 | } | |
218 | } | |
219 | ||
220 | cleanup: | |
221 | if (bind) { | |
222 | int i; | |
223 | ||
224 | for (i = 0; i < column_count; i++) { | |
225 | free(bind[i].buffer); | |
226 | } | |
227 | ||
228 | free(bind); | |
229 | } | |
230 | ||
231 | if (error_message) { | |
232 | luaL_error(L, error_message, mysql_stmt_error(statement->stmt)); | |
233 | return 0; | |
234 | } | |
235 | ||
236 | return 1; | |
237 | } | |
238 | ||
2 | 239 | /* |
240 | * array = statement:fetch() | |
241 | */ | |
1 | 242 | static int statement_fetch(lua_State *L) { |
243 | return statement_fetch_impl(L, 0); | |
244 | } | |
245 | ||
2 | 246 | /* |
247 | * hashmap = statement:fetchtable() | |
248 | */ | |
1 | 249 | static int statement_fetchtable(lua_State *L) { |
250 | return statement_fetch_impl(L, 1); | |
251 | } | |
252 | ||
2 | 253 | /* |
254 | * __gc | |
255 | */ | |
1 | 256 | static int statement_gc(lua_State *L) { |
257 | /* always free the handle */ | |
258 | statement_close(L); | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | int dbd_mysql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
264 | unsigned long sql_len = strlen(sql_query); | |
265 | ||
266 | statement_t *statement = NULL; | |
267 | ||
268 | MYSQL_STMT *stmt = mysql_stmt_init(conn->mysql); | |
269 | ||
270 | if (!stmt) { | |
271 | luaL_error(L, "Error allocating statement handle: %s", mysql_error(conn->mysql)); | |
272 | return 0; | |
273 | } | |
274 | ||
275 | if (mysql_stmt_prepare(stmt, sql_query, sql_len)) { | |
276 | luaL_error(L, "Error preparing statement handle: %s", mysql_stmt_error(stmt)); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
281 | statement->mysql = conn->mysql; | |
282 | statement->stmt = stmt; | |
283 | statement->metadata = NULL; | |
284 | ||
285 | luaL_getmetatable(L, DBD_MYSQL_STATEMENT); | |
286 | lua_setmetatable(L, -2); | |
287 | ||
288 | return 1; | |
289 | } | |
290 | ||
291 | int dbd_mysql_statement(lua_State *L) { | |
2 | 292 | static const luaL_Reg statement_methods[] = { |
293 | {"close", statement_close}, | |
294 | {"execute", statement_execute}, | |
295 | {"fetch", statement_fetch}, | |
296 | {"fetchtable", statement_fetchtable}, | |
297 | {NULL, NULL} | |
298 | }; | |
299 | ||
300 | static const luaL_Reg statement_class_methods[] = { | |
301 | {NULL, NULL} | |
302 | }; | |
303 | ||
1 | 304 | luaL_newmetatable(L, DBD_MYSQL_STATEMENT); |
305 | luaL_register(L, 0, statement_methods); | |
306 | lua_pushvalue(L,-1); | |
307 | lua_setfield(L, -2, "__index"); | |
308 | ||
309 | lua_pushcfunction(L, statement_gc); | |
310 | lua_setfield(L, -2, "__gc"); | |
311 | ||
312 | luaL_register(L, DBD_MYSQL_STATEMENT, statement_class_methods); | |
313 | ||
314 | return 1; | |
315 | } |