44 /* |
44 /* |
45 * success = statement:close() |
45 * success = statement:close() |
46 */ |
46 */ |
47 static int statement_close(lua_State *L) { |
47 static int statement_close(lua_State *L) { |
48 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT); |
48 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT); |
49 SQLRETURN rc = SQL_SUCCESS; |
|
50 |
49 |
51 if (statement->stmt) { |
50 if (statement->stmt) { |
52 rc = SQLFreeHandle(SQL_HANDLE_STMT, statement->stmt); |
51 SQLFreeStmt(statement->stmt, SQL_CLOSE); |
53 |
52 |
54 if (statement->resultset) |
53 if (statement->resultset) |
55 free(statement->resultset); |
54 free(statement->resultset); |
56 |
55 |
57 if (statement->bind) { |
56 if (statement->bind) { |
79 int p; |
78 int p; |
80 int i; |
79 int i; |
81 int errflag = 0; |
80 int errflag = 0; |
82 const char *errstr = NULL; |
81 const char *errstr = NULL; |
83 SQLRETURN rc = SQL_SUCCESS; |
82 SQLRETURN rc = SQL_SUCCESS; |
84 unsigned char *buffer = NULL; |
83 unsigned char b[1024]; |
|
84 unsigned char *buffer = &b[0]; |
85 int offset = 0; |
85 int offset = 0; |
86 resultset_t *resultset = NULL; |
86 resultset_t *resultset = NULL; |
87 bindparams_t *bind; /* variable to read the results */ |
87 bindparams_t *bind; /* variable to read the results */ |
88 SQLSMALLINT num_params; |
88 SQLSMALLINT num_params; |
89 |
89 |
132 case LUA_TNIL: |
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); |
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; |
134 errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; |
135 break; |
135 break; |
136 case LUA_TNUMBER: |
136 case LUA_TNUMBER: |
137 buffer = realloc(buffer, offset + sizeof(double)); |
|
138 num = (double *)buffer + offset; |
137 num = (double *)buffer + offset; |
139 *num = lua_tonumber(L, p); |
138 *num = lua_tonumber(L, p); |
140 offset += sizeof(double); |
139 offset += sizeof(double); |
141 rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL, 10, 0, (SQLPOINTER)num, 0, NULL); |
140 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; |
141 errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; |
145 str = lua_tolstring(L, p, &len); |
144 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); |
145 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; |
146 errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; |
148 break; |
147 break; |
149 case LUA_TBOOLEAN: |
148 case LUA_TBOOLEAN: |
150 buffer = realloc(buffer, offset + sizeof(int)); |
|
151 boolean = (int *)buffer + offset; |
149 boolean = (int *)buffer + offset; |
152 *boolean = lua_toboolean(L, p); |
150 *boolean = lua_toboolean(L, p); |
153 offset += sizeof(int); |
151 offset += sizeof(int); |
154 rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, (SQLPOINTER)boolean, len, NULL); |
152 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; |
153 errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO; |
250 |
247 |
251 statement->resultset = resultset; |
248 statement->resultset = resultset; |
252 statement->bind = bind; |
249 statement->bind = bind; |
253 } |
250 } |
254 |
251 |
255 /* |
|
256 * free the buffer with a resize to 0 |
|
257 */ |
|
258 realloc(buffer, 0); |
|
259 |
|
260 lua_pushboolean(L, 1); |
252 lua_pushboolean(L, 1); |
261 return 1; |
253 return 1; |
262 } |
254 } |
263 |
255 |
264 /* |
256 /* |
265 * must be called after an execute |
257 * must be called after an execute |
266 */ |
258 */ |
267 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
259 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
268 int i; |
260 int i; |
269 int d; |
261 int d; |
|
262 |
|
263 SQLCHAR message[SQL_MAX_MESSAGE_LENGTH + 1]; |
|
264 SQLCHAR sqlstate[SQL_SQLSTATE_SIZE + 1]; |
|
265 SQLINTEGER sqlcode; |
|
266 SQLSMALLINT length; |
270 |
267 |
271 SQLRETURN rc = SQL_SUCCESS; |
268 SQLRETURN rc = SQL_SUCCESS; |
272 |
269 |
273 if (!statement->resultset || !statement->bind) { |
270 if (!statement->resultset || !statement->bind) { |
274 lua_pushnil(L); |
271 lua_pushnil(L); |
276 } |
273 } |
277 |
274 |
278 /* fetch each row, and display */ |
275 /* fetch each row, and display */ |
279 rc = SQLFetch(statement->stmt); |
276 rc = SQLFetch(statement->stmt); |
280 if (rc == SQL_NO_DATA_FOUND) { |
277 if (rc == SQL_NO_DATA_FOUND) { |
|
278 SQLFreeStmt(statement->stmt, SQL_RESET_PARAMS); |
281 lua_pushnil(L); |
279 lua_pushnil(L); |
282 return 1; |
280 return 1; |
|
281 } |
|
282 |
|
283 if (rc != SQL_SUCCESS) { |
|
284 SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length); |
|
285 |
|
286 luaL_error(L, DBI_ERR_FETCH_FAILED, message); |
283 } |
287 } |
284 |
288 |
285 d = 1; |
289 d = 1; |
286 lua_newtable(L); |
290 lua_newtable(L); |
287 for (i = 0; i < statement->num_result_columns; i++) { |
291 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); |
292 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); |
293 const char *name = strlower(statement->resultset[i].name); |
290 double val; |
294 double val; |
291 char *value = (char *)statement->bind[i].buffer; |
295 char *value = (char *)statement->bind[i].buffer; |
292 |
296 |
293 switch (lua_push) { |
297 switch (lua_push) { |
294 case LUA_PUSH_NIL: |
298 case LUA_PUSH_NIL: |