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