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