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