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