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