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