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