dbd/oracle/statement.c

changeset 31
999ef93f0dbc
parent 30
8599f34c139b
child 32
03ed0ca09837
equal deleted inserted replaced
30:8599f34c139b 31:999ef93f0dbc
21 lua_type = LUA_PUSH_STRING; 21 lua_type = LUA_PUSH_STRING;
22 } 22 }
23 23
24 return lua_type; 24 return lua_type;
25 } 25 }
26
27 /*
28 * Fetch metadata from the database
29 */
30
31 static void statement_fetch_metadata(lua_State *L, statement_t *statement) {
32 bindparams_t *bind;
33 int i;
34
35 char errbuf[100];
36 int errcode;
37 int rc;
38
39 if (statement->metadata)
40 return;
41
42 statement->bind = (bindparams_t *)malloc(sizeof(bindparams_t) * statement->num_columns);
43 memset(statement->bind, 0, sizeof(bindparams_t) * statement->num_columns);
44 bind = statement->bind;
45
46 for (i = 0; i < statement->num_columns; i++) {
47 rc = OCIParamGet(statement->stmt, OCI_HTYPE_STMT, statement->conn->err, (dvoid **)&bind[i].param, i+1);
48 if (rc) {
49 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
50 luaL_error(L, "param get %s", errbuf);
51 }
52
53 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].name), (ub4 *)&(bind[i].name_len), OCI_ATTR_NAME, statement->conn->err);
54 if (rc) {
55 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
56 luaL_error(L, "name get %s", errbuf);
57 }
58
59 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].data_type), (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->conn->err);
60 if (rc) {
61 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
62 luaL_error(L, "datatype get %s", errbuf);
63 }
64
65 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].max_len), 0, OCI_ATTR_DATA_SIZE, statement->conn->err);
66 if (rc) {
67 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
68 luaL_error(L, "datasize get %s", errbuf);
69 }
70
71 bind[i].data = calloc(bind[i].max_len+1, sizeof(char));
72 rc = OCIDefineByPos(statement->stmt, &bind[i].define, statement->conn->err, (ub4)i+1, bind[i].data, bind[i].max_len, SQLT_STR, (dvoid *)&(bind[i].null), (ub2 *)0, (ub2 *)0, (ub4)OCI_DEFAULT);
73 if (rc) {
74 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
75 luaL_error(L, "define by pos %s", errbuf);
76 }
77 }
78
79 statement->metadata = 1;
80 }
81
26 82
27 /* 83 /*
28 * num_affected_rows = statement:affected() 84 * num_affected_rows = statement:affected()
29 */ 85 */
30 static int statement_affected(lua_State *L) { 86 static int statement_affected(lua_State *L) {
78 } 134 }
79 135
80 /* 136 /*
81 * column_names = statement:columns() 137 * column_names = statement:columns()
82 */ 138 */
83 static int statement_rowcount(lua_State *L) { 139 static int statement_columns(lua_State *L) {
84 luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_ORACLE_STATEMENT, "columns"); 140 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
85 141 int rc;
86 return 0; 142
143 bindparams_t *bind;
144
145 char errbuf[100];
146 int errcode;
147
148 int i;
149 int d = 1;
150
151 if (!statement->stmt) {
152 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
153 return 0;
154 }
155
156 statement_fetch_metadata(L, statement);
157
158 lua_newtable(L);
159 for (i = 0; i < statement->num_columns; i++) {
160 const char *name = strlower(statement->bind[i].name);
161
162 LUA_PUSH_ARRAY_STRING(d, name);
163 }
164
165 return 1;
87 } 166 }
88 167
89 168
90 /* 169 /*
91 * success,err = statement:execute(...) 170 * success,err = statement:execute(...)
267 if (!statement->stmt) { 346 if (!statement->stmt) {
268 luaL_error(L, DBI_ERR_FETCH_INVALID); 347 luaL_error(L, DBI_ERR_FETCH_INVALID);
269 return 0; 348 return 0;
270 } 349 }
271 350
272 statement->bind = (bindparams_t *)malloc(sizeof(bindparams_t) * statement->num_columns); 351 statement_fetch_metadata(L, statement);
273 memset(statement->bind, 0, sizeof(bindparams_t) * statement->num_columns);
274 bind = statement->bind; 352 bind = statement->bind;
275
276 for (i = 0; i < statement->num_columns; i++) {
277 rc = OCIParamGet(statement->stmt, OCI_HTYPE_STMT, statement->conn->err, (dvoid **)&bind[i].param, i+1);
278 if (rc) {
279 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
280 luaL_error(L, "param get %s", errbuf);
281 }
282
283 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].name), (ub4 *)&(bind[i].name_len), OCI_ATTR_NAME, statement->conn->err);
284 if (rc) {
285 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
286 luaL_error(L, "name get %s", errbuf);
287 }
288
289 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].data_type), (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->conn->err);
290 if (rc) {
291 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
292 luaL_error(L, "datatype get %s", errbuf);
293 }
294
295 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].max_len), 0, OCI_ATTR_DATA_SIZE, statement->conn->err);
296 if (rc) {
297 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
298 luaL_error(L, "datasize get %s", errbuf);
299 }
300
301 bind[i].data = calloc(bind[i].max_len+1, sizeof(char));
302 rc = OCIDefineByPos(statement->stmt, &bind[i].define, statement->conn->err, (ub4)i+1, bind[i].data, bind[i].max_len, SQLT_STR, (dvoid *)&(bind[i].null), (ub2 *)0, (ub2 *)0, (ub4)OCI_DEFAULT);
303 if (rc) {
304 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
305 luaL_error(L, "define by pos %s", errbuf);
306 }
307 }
308 353
309 status = OCIStmtFetch(statement->stmt, statement->conn->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT); 354 status = OCIStmtFetch(statement->stmt, statement->conn->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
310 355
311 if (status == OCI_NO_DATA) { 356 if (status == OCI_NO_DATA) {
312 /* No more rows */ 357 /* No more rows */
449 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); 494 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
450 statement->conn = conn; 495 statement->conn = conn;
451 statement->stmt = stmt; 496 statement->stmt = stmt;
452 statement->num_columns = 0; 497 statement->num_columns = 0;
453 statement->bind = NULL; 498 statement->bind = NULL;
499 statement->metadata = 0;
454 500
455 luaL_getmetatable(L, DBD_ORACLE_STATEMENT); 501 luaL_getmetatable(L, DBD_ORACLE_STATEMENT);
456 lua_setmetatable(L, -2); 502 lua_setmetatable(L, -2);
457 503
458 return 1; 504 return 1;

mercurial