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