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