Sat, 06 Dec 2008 00:32:37 +0000
Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
14 | 1 | #include "dbd_db2.h" |
2 | ||
3 | static lua_push_type_t db2_to_lua_push(unsigned int db2_type, int len) { | |
4 | lua_push_type_t lua_type; | |
5 | ||
6 | if (len == SQL_NULL_DATA) | |
7 | return LUA_PUSH_NIL; | |
8 | ||
9 | switch(db2_type) { | |
10 | case SQL_SMALLINT: | |
11 | case SQL_INTEGER: | |
12 | lua_type = LUA_PUSH_INTEGER; | |
13 | break; | |
14 | case SQL_DECIMAL: | |
15 | lua_type = LUA_PUSH_NUMBER; | |
16 | break; | |
17 | default: | |
18 | lua_type = LUA_PUSH_STRING; | |
19 | } | |
20 | ||
21 | return lua_type; | |
22 | } | |
23 | ||
24 | /* | |
25 | * success = statement:close() | |
26 | */ | |
27 | static int statement_close(lua_State *L) { | |
28 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT); | |
29 | SQLRETURN rc = SQL_SUCCESS; | |
30 | ||
31 | if (statement->stmt) { | |
32 | rc = SQLFreeHandle(SQL_HANDLE_STMT, statement->stmt); | |
33 | ||
34 | if (statement->resultset) | |
35 | free(statement->resultset); | |
36 | ||
37 | if (statement->bind) { | |
38 | int i; | |
39 | ||
40 | for (i = 0; i < statement->num_result_columns; i++) { | |
41 | free(statement->bind[i].buffer); | |
42 | } | |
43 | ||
44 | free(statement->bind); | |
45 | } | |
46 | ||
47 | statement->num_result_columns = 0; | |
48 | } | |
49 | ||
50 | return 0; | |
51 | } | |
52 | ||
53 | /* | |
54 | * success = statement:execute(...) | |
55 | */ | |
56 | static int statement_execute(lua_State *L) { | |
57 | int n = lua_gettop(L); | |
58 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT); | |
59 | int p; | |
60 | int i; | |
61 | int errflag = 0; | |
62 | const char *errstr = NULL; | |
63 | SQLRETURN rc = SQL_SUCCESS; | |
64 | unsigned char *buffer = NULL; | |
65 | int offset = 0; | |
66 | resultset_t *resultset = NULL; | |
67 | bindparams_t *bind; /* variable to read the results */ | |
68 | ||
69 | SQLCHAR message[SQL_MAX_MESSAGE_LENGTH + 1]; | |
70 | SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; | |
71 | SQLINTEGER sqlcode; | |
72 | SQLSMALLINT length; | |
73 | ||
74 | if (!statement->stmt) { | |
75 | lua_pushboolean(L, 0); | |
76 | lua_pushstring(L, DBI_ERR_EXECUTE_INVALID); | |
77 | return 2; | |
78 | } | |
79 | ||
80 | for (p = 2; p <= n; p++) { | |
81 | int i = p - 1; | |
82 | int type = lua_type(L, p); | |
83 | char err[64]; | |
84 | const char *str = NULL; | |
85 | size_t len = 0; | |
86 | double *num; | |
87 | int *boolean; | |
88 | const static SQLLEN nullvalue = SQL_NULL_DATA; | |
89 | ||
90 | switch(type) { | |
91 | case LUA_TNIL: | |
92 | rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, (SQLPOINTER)0, 0, (SQLPOINTER)&nullvalue); | |
93 | errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; | |
94 | break; | |
95 | case LUA_TNUMBER: | |
96 | buffer = realloc(buffer, offset + sizeof(double)); | |
97 | num = (double *)buffer + offset; | |
98 | *num = lua_tonumber(L, p); | |
99 | offset += sizeof(double); | |
100 | rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL, 10, 0, (SQLPOINTER)num, 0, NULL); | |
101 | errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; | |
102 | break; | |
103 | case LUA_TSTRING: | |
104 | str = lua_tolstring(L, p, &len); | |
105 | rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 0, 0, (SQLPOINTER)str, len, NULL); | |
106 | errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; | |
107 | break; | |
108 | case LUA_TBOOLEAN: | |
109 | buffer = realloc(buffer, offset + sizeof(int)); | |
110 | boolean = (int *)buffer + offset; | |
111 | *boolean = lua_toboolean(L, p); | |
112 | offset += sizeof(int); | |
113 | rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, (SQLPOINTER)boolean, len, NULL); | |
114 | errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; | |
115 | break; | |
116 | default: | |
117 | /* | |
118 | * Unknown/unsupported value type | |
119 | */ | |
120 | errflag = 1; | |
121 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); | |
122 | errstr = err; | |
123 | } | |
124 | ||
125 | if (errflag) | |
126 | break; | |
127 | } | |
128 | ||
129 | if (errflag) { | |
130 | realloc(buffer, 0); | |
131 | lua_pushboolean(L, 0); | |
132 | ||
133 | if (errstr) { | |
134 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr); | |
135 | } else { | |
136 | SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
137 | ||
138 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, message); | |
139 | } | |
140 | ||
141 | return 2; | |
142 | } | |
143 | ||
144 | rc = SQLExecute(statement->stmt); | |
145 | if (rc != SQL_SUCCESS) { | |
146 | SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
147 | ||
148 | lua_pushnil(L); | |
149 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, message); | |
150 | return 2; | |
151 | } | |
152 | ||
153 | /* | |
154 | * identify the number of output columns | |
155 | */ | |
156 | rc = SQLNumResultCols(statement->stmt, &statement->num_result_columns); | |
157 | ||
158 | if (statement->num_result_columns > 0) { | |
159 | resultset = (resultset_t *)malloc(sizeof(resultset_t) * statement->num_result_columns); | |
160 | memset(resultset, 0, sizeof(resultset_t) * statement->num_result_columns); | |
161 | ||
162 | bind = (bindparams_t *)malloc(sizeof(bindparams_t) * statement->num_result_columns); | |
163 | memset(bind, 0, sizeof(bindparams_t) * statement->num_result_columns); | |
164 | ||
165 | for (i = 0; i < statement->num_result_columns; i++) { | |
166 | /* | |
167 | * return a set of attributes for a column | |
168 | */ | |
169 | rc = SQLDescribeCol(statement->stmt, | |
170 | (SQLSMALLINT)(i + 1), | |
171 | resultset[i].name, | |
172 | sizeof(resultset[i].name), | |
173 | &resultset[i].name_len, | |
174 | &resultset[i].type, | |
175 | &resultset[i].size, | |
176 | &resultset[i].scale, | |
177 | NULL); | |
178 | ||
179 | if (rc != SQL_SUCCESS) { | |
180 | SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
181 | ||
182 | lua_pushnil(L); | |
183 | lua_pushfstring(L, DBI_ERR_DESC_RESULT, message); | |
184 | return 2; | |
185 | } | |
186 | ||
187 | bind[i].buffer_len = resultset[i].size+1; | |
188 | ||
189 | /* | |
190 | *allocate memory to bind a column | |
191 | */ | |
192 | bind[i].buffer = (SQLCHAR *)malloc((int)bind[i].buffer_len); | |
193 | ||
194 | rc = SQLBindCol(statement->stmt, | |
195 | (SQLSMALLINT)(i + 1), | |
196 | SQL_C_CHAR, | |
197 | bind[i].buffer, | |
198 | bind[i].buffer_len, | |
199 | &bind[i].len); | |
200 | ||
201 | if (rc != SQL_SUCCESS) { | |
202 | SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
203 | ||
204 | lua_pushnil(L); | |
205 | lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, message); | |
206 | return 2; | |
207 | } | |
208 | } | |
209 | ||
210 | statement->resultset = resultset; | |
211 | statement->bind = bind; | |
212 | } | |
213 | ||
214 | /* | |
215 | * free the buffer with a resize to 0 | |
216 | */ | |
217 | realloc(buffer, 0); | |
218 | ||
219 | lua_pushboolean(L, 1); | |
220 | return 1; | |
221 | } | |
222 | ||
223 | /* | |
224 | * must be called after an execute | |
225 | */ | |
226 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { | |
227 | int i; | |
228 | int d; | |
229 | ||
230 | SQLRETURN rc = SQL_SUCCESS; | |
231 | ||
232 | if (!statement->resultset || !statement->bind) { | |
233 | lua_pushnil(L); | |
234 | return 1; | |
235 | } | |
236 | ||
237 | /* fetch each row, and display */ | |
238 | rc = SQLFetch(statement->stmt); | |
239 | if (rc == SQL_NO_DATA_FOUND) { | |
240 | lua_pushnil(L); | |
241 | return 1; | |
242 | } | |
243 | ||
244 | d = 1; | |
245 | lua_newtable(L); | |
246 | for (i = 0; i < statement->num_result_columns; i++) { | |
247 | lua_push_type_t lua_push = db2_to_lua_push(statement->resultset[i].type, statement->bind[i].len); | |
248 | const char *name = strlower((char *)statement->resultset[i].name); | |
249 | double val; | |
250 | char *value = (char *)statement->bind[i].buffer; | |
251 | ||
252 | switch (lua_push) { | |
253 | case LUA_PUSH_NIL: | |
254 | if (named_columns) { | |
255 | LUA_PUSH_ATTRIB_NIL(name); | |
256 | } else { | |
257 | LUA_PUSH_ARRAY_NIL(d); | |
258 | } | |
259 | break; | |
260 | case LUA_PUSH_INTEGER: | |
261 | if (named_columns) { | |
262 | LUA_PUSH_ATTRIB_INT(name, atoi(value)); | |
263 | } else { | |
264 | LUA_PUSH_ARRAY_INT(d, atoi(value)); | |
265 | } | |
266 | break; | |
267 | case LUA_PUSH_NUMBER: | |
268 | val = strtod(value, NULL); | |
269 | ||
270 | if (named_columns) { | |
271 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
272 | } else { | |
273 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
274 | } | |
275 | break; | |
276 | case LUA_PUSH_BOOLEAN: | |
277 | if (named_columns) { | |
278 | LUA_PUSH_ATTRIB_BOOL(name, atoi(value)); | |
279 | } else { | |
280 | LUA_PUSH_ARRAY_BOOL(d, atoi(value)); | |
281 | } | |
282 | break; | |
283 | case LUA_PUSH_STRING: | |
284 | if (named_columns) { | |
285 | LUA_PUSH_ATTRIB_STRING(name, value); | |
286 | } else { | |
287 | LUA_PUSH_ARRAY_STRING(d, value); | |
288 | } | |
289 | break; | |
290 | default: | |
291 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); | |
292 | } | |
293 | } | |
294 | ||
295 | return 1; | |
296 | } | |
297 | ||
298 | ||
299 | static int next_iterator(lua_State *L) { | |
300 | statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_DB2_STATEMENT); | |
301 | int named_columns = lua_toboolean(L, lua_upvalueindex(2)); | |
302 | ||
303 | return statement_fetch_impl(L, statement, named_columns); | |
304 | } | |
305 | ||
306 | /* | |
307 | * table = statement:fetch(named_indexes) | |
308 | */ | |
309 | static int statement_fetch(lua_State *L) { | |
310 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT); | |
311 | int named_columns = lua_toboolean(L, 2); | |
312 | ||
313 | return statement_fetch_impl(L, statement, named_columns); | |
314 | } | |
315 | ||
316 | /* | |
317 | * iterfunc = statement:rows(named_indexes) | |
318 | */ | |
319 | static int statement_rows(lua_State *L) { | |
320 | if (lua_gettop(L) == 1) { | |
321 | lua_pushvalue(L, 1); | |
322 | lua_pushboolean(L, 0); | |
323 | } else { | |
324 | lua_pushvalue(L, 1); | |
325 | lua_pushboolean(L, lua_toboolean(L, 2)); | |
326 | } | |
327 | ||
328 | lua_pushcclosure(L, next_iterator, 2); | |
329 | return 1; | |
330 | } | |
331 | ||
332 | /* | |
333 | * __gc | |
334 | */ | |
335 | static int statement_gc(lua_State *L) { | |
336 | /* always free the handle */ | |
337 | statement_close(L); | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | int dbd_db2_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
343 | SQLRETURN rc = SQL_SUCCESS; | |
344 | statement_t *statement = NULL; | |
345 | SQLHANDLE stmt; | |
346 | ||
347 | SQLCHAR message[SQL_MAX_MESSAGE_LENGTH + 1]; | |
348 | SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; | |
349 | SQLINTEGER sqlcode; | |
350 | SQLSMALLINT length; | |
351 | ||
352 | rc = SQLAllocHandle(SQL_HANDLE_STMT, conn->db2, &stmt); | |
353 | if (rc != SQL_SUCCESS) { | |
354 | SQLGetDiagRec(SQL_HANDLE_DBC, conn->db2, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
355 | ||
356 | lua_pushnil(L); | |
357 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, message); | |
358 | return 2; | |
359 | } | |
360 | ||
361 | /* | |
362 | * turn off deferred prepare | |
363 | * statements will be sent to the server at prepare timr, | |
364 | * and therefor we can catch errors then rather | |
365 | * than at execute time | |
366 | */ | |
367 | rc = SQLSetStmtAttr(stmt,SQL_ATTR_DEFERRED_PREPARE,(SQLPOINTER)SQL_DEFERRED_PREPARE_OFF,0); | |
368 | ||
369 | rc = SQLPrepare(stmt, (SQLCHAR *)sql_query, SQL_NTS); | |
370 | if (rc != SQL_SUCCESS) { | |
371 | SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); | |
372 | ||
373 | lua_pushnil(L); | |
374 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, message); | |
375 | return 2; | |
376 | } | |
377 | ||
378 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
379 | statement->stmt = stmt; | |
380 | statement->db2 = conn->db2; | |
381 | statement->resultset = NULL; | |
382 | statement->bind = NULL; | |
383 | ||
384 | luaL_getmetatable(L, DBD_DB2_STATEMENT); | |
385 | lua_setmetatable(L, -2); | |
386 | ||
387 | return 1; | |
388 | } | |
389 | ||
390 | int dbd_db2_statement(lua_State *L) { | |
391 | static const luaL_Reg statement_methods[] = { | |
392 | {"close", statement_close}, | |
393 | {"execute", statement_execute}, | |
394 | {"fetch", statement_fetch}, | |
395 | {"rows", statement_rows}, | |
396 | {NULL, NULL} | |
397 | }; | |
398 | ||
399 | static const luaL_Reg statement_class_methods[] = { | |
400 | {NULL, NULL} | |
401 | }; | |
402 | ||
403 | luaL_newmetatable(L, DBD_DB2_STATEMENT); | |
404 | luaL_register(L, 0, statement_methods); | |
405 | lua_pushvalue(L,-1); | |
406 | lua_setfield(L, -2, "__index"); | |
407 | ||
408 | lua_pushcfunction(L, statement_gc); | |
409 | lua_setfield(L, -2, "__gc"); | |
410 | ||
411 | luaL_register(L, DBD_DB2_STATEMENT, statement_class_methods); | |
412 | ||
413 | return 1; | |
414 | } |