dbd/oracle/statement.c

Sat, 13 Jun 2009 08:55:44 +0000

author
nrich@ii.net
date
Sat, 13 Jun 2009 08:55:44 +0000
changeset 31
999ef93f0dbc
parent 30
8599f34c139b
child 32
03ed0ca09837
permissions
-rw-r--r--

* Add `columns' method for DB2 and Oracle statment handles
* Misc cleanup

17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
1 #include "dbd_oracle.h"
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
2
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
3 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
4 * Converts SQLite types to Lua types
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
5 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
6 static lua_push_type_t oracle_to_lua_push(unsigned int oracle_type, int null) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
7 lua_push_type_t lua_type;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
8
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
9 if (null)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
10 return LUA_PUSH_NIL;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
11
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
12 switch(oracle_type) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
13 case SQLT_NUM:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
14 case SQLT_FLT:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
15 lua_type = LUA_PUSH_NUMBER;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
16 break;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
17 case SQLT_INT:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
18 lua_type = LUA_PUSH_INTEGER;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
19 break;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
20 default:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
21 lua_type = LUA_PUSH_STRING;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
22 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
23
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
24 return lua_type;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
25 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
26
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
27 /*
31
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
28 * Fetch metadata from the database
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
29 */
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
30
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
31 static void statement_fetch_metadata(lua_State *L, statement_t *statement) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
32 bindparams_t *bind;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
33 int i;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
34
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
35 char errbuf[100];
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
36 int errcode;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
37 int rc;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
38
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
39 if (statement->metadata)
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
40 return;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
41
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
42 statement->bind = (bindparams_t *)malloc(sizeof(bindparams_t) * statement->num_columns);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
43 memset(statement->bind, 0, sizeof(bindparams_t) * statement->num_columns);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
44 bind = statement->bind;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
45
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
46 for (i = 0; i < statement->num_columns; i++) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
47 rc = OCIParamGet(statement->stmt, OCI_HTYPE_STMT, statement->conn->err, (dvoid **)&bind[i].param, i+1);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
48 if (rc) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
49 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
50 luaL_error(L, "param get %s", errbuf);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
51 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
52
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
53 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].name), (ub4 *)&(bind[i].name_len), OCI_ATTR_NAME, statement->conn->err);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
54 if (rc) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
55 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
56 luaL_error(L, "name get %s", errbuf);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
57 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
58
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
59 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].data_type), (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->conn->err);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
60 if (rc) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
61 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
62 luaL_error(L, "datatype get %s", errbuf);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
63 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
64
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
65 rc = OCIAttrGet(bind[i].param, OCI_DTYPE_PARAM, (dvoid *)&(bind[i].max_len), 0, OCI_ATTR_DATA_SIZE, statement->conn->err);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
66 if (rc) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
67 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
68 luaL_error(L, "datasize get %s", errbuf);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
69 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
70
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
71 bind[i].data = calloc(bind[i].max_len+1, sizeof(char));
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
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);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
73 if (rc) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
74 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
75 luaL_error(L, "define by pos %s", errbuf);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
76 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
77 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
78
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
79 statement->metadata = 1;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
80 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
81
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 /*
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
84 * num_affected_rows = statement:affected()
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
85 */
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
86 static int statement_affected(lua_State *L) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
87 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
88 int affected;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
89 int rc;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
90
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
91 if (!statement->stmt) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
92 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
93 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
94
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
95 /*
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
96 * get number of affected rows
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
97 */
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
98 rc = OCIAttrGet(
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
99 (dvoid *)statement->stmt,
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
100 (ub4)OCI_HTYPE_STMT,
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
101 (dvoid *)&affected,
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
102 (ub4 *)0,
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
103 (ub4)OCI_ATTR_ROW_COUNT,
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
104 statement->conn->err
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
105 );
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
106
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
107 lua_pushinteger(L, affected);
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
108
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
109 return 1;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
110 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
111
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
112 /*
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
113 * success = statement:close()
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
114 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
115 int statement_close(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
116 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
117 int ok = 0;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
118
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
119 if (statement->stmt) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
120 int rc;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
121
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
122 rc = OCIHandleFree((dvoid *)statement->stmt, OCI_HTYPE_STMT); /* Free handles */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
123
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
124 statement->stmt = NULL;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
125 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
126
27
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
127 if (statement->bind) {
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
128 free(statement->bind);
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
129 statement->bind = NULL;
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
130 }
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
131
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
132 lua_pushboolean(L, ok);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
133 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
134 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
135
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
136 /*
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
137 * column_names = statement:columns()
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
138 */
31
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
139 static int statement_columns(lua_State *L) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
140 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
141 int rc;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
142
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
143 bindparams_t *bind;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
144
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
145 char errbuf[100];
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
146 int errcode;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
147
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
148 int i;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
149 int d = 1;
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
150
31
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
151 if (!statement->stmt) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
152 luaL_error(L, DBI_ERR_INVALID_STATEMENT);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
153 return 0;
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
154 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
155
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
156 statement_fetch_metadata(L, statement);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
157
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
158 lua_newtable(L);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
159 for (i = 0; i < statement->num_columns; i++) {
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
160 const char *name = strlower(statement->bind[i].name);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
161
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
162 LUA_PUSH_ARRAY_STRING(d, name);
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
163 }
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
164
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
165 return 1;
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
166 }
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
167
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
168
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
169 /*
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
170 * success,err = statement:execute(...)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
171 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
172 int statement_execute(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
173 int n = lua_gettop(L);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
174 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
175 int p;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
176 int errflag = 0;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
177 const char *errstr = NULL;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
178 int num_columns;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
179 int rc;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
180
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
181 char errbuf[100];
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
182 int errcode;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
183
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
184 ub2 type;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
185
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
186 if (!statement->stmt) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
187 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
188 lua_pushstring(L, DBI_ERR_EXECUTE_INVALID);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
189 return 2;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
190 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
191
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
192 for (p = 2; p <= n; p++) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
193 int i = p - 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
194 int type = lua_type(L, p);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
195 char err[64];
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
196 const char *value;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
197
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
198 OCIBind *bnd = (OCIBind *)0;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
199
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
200 switch(type) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
201 case LUA_TNIL:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
202 errflag = OCIBindByPos(
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
203 statement->stmt,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
204 &bnd,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
205 statement->conn->err,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
206 i,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
207 NULL,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
208 0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
209 SQLT_CHR,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
210 (dvoid *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
211 (ub2 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
212 (ub2 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
213 (ub4)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
214 (ub4 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
215 OCI_DEFAULT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
216 break;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
217 case LUA_TNUMBER:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
218 case LUA_TSTRING:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
219 case LUA_TBOOLEAN:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
220 value = lua_tostring(L, p);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
221
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
222 errflag = OCIBindByPos(
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
223 statement->stmt,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
224 &bnd,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
225 statement->conn->err,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
226 i,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
227 value,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
228 strlen(value),
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
229 SQLT_CHR,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
230 (dvoid *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
231 (ub2 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
232 (ub2 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
233 (ub4)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
234 (ub4 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
235 (ub4)OCI_DEFAULT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
236 break;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
237 default:
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
238 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
239 * Unknown/unsupported value type
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
240 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
241 errflag = 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
242 snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type));
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
243 errstr = err;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
244 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
245
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
246 if (errflag)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
247 break;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
248 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
249
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
250 if (errflag) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
251 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
252 if (errstr)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
253 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
254 else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
255 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
256
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
257 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errbuf);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
258 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
259
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
260 return 2;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
261 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
262
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
263 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
264 * statement type
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
265 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
266 rc = OCIAttrGet(
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
267 (dvoid *)statement->stmt,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
268 (ub4)OCI_HTYPE_STMT,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
269 (dvoid *)&type,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
270 (ub4 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
271 (ub4)OCI_ATTR_STMT_TYPE,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
272 statement->conn->err
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
273 );
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
274
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
275 if (rc) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
276 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
277
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
278 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
279 lua_pushfstring(L, "Error getting type: %s", errbuf);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
280
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
281 return 2;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
282 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
283
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
284 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
285 * execute statement
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
286 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
287 rc = OCIStmtExecute(
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
288 statement->conn->svc,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
289 statement->stmt,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
290 statement->conn->err,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
291 type == OCI_STMT_SELECT ? 0 : 1,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
292 (ub4)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
293 (CONST OCISnapshot *)NULL,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
294 (OCISnapshot *)NULL,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
295 statement->conn->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
296 );
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
297
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
298 if (rc) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
299 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
300
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
301 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
302 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errbuf);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
303
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
304 return 2;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
305 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
306
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
307 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
308 * get number of columns
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
309 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
310 rc = OCIAttrGet(
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
311 (dvoid *)statement->stmt,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
312 (ub4)OCI_HTYPE_STMT,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
313 (dvoid *)&num_columns,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
314 (ub4 *)0,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
315 (ub4)OCI_ATTR_PARAM_COUNT,
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
316 statement->conn->err
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
317 );
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
318
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
319 if (rc) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
320 OCIErrorGet((dvoid *)statement->conn->err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
321
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
322 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
323 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errbuf);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
324
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
325 return 2;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
326 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
327
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
328 statement->num_columns = num_columns;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
329
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
330 lua_pushboolean(L, 1);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
331 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
332 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
333
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
334 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
335 * must be called after an execute
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
336 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
337 static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
338 int rc;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
339 sword status;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
340 int i;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
341 bindparams_t *bind;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
342
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
343 char errbuf[100];
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
344 int errcode;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
345
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
346 if (!statement->stmt) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
347 luaL_error(L, DBI_ERR_FETCH_INVALID);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
348 return 0;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
349 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
350
31
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
351 statement_fetch_metadata(L, statement);
27
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
352 bind = statement->bind;
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
353
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
354 status = OCIStmtFetch(statement->stmt, statement->conn->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
355
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
356 if (status == OCI_NO_DATA) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
357 /* No more rows */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
358 lua_pushnil(L);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
359 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
360 } else if (status != OCI_SUCCESS) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
361 OCIErrorGet((dvoid *)statement->conn->err, (ub4)1, (text *)NULL, &errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
362 luaL_error(L, DBI_ERR_FETCH_FAILED, errbuf);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
363 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
364
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
365 if (statement->num_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
366 int i;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
367 int d = 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
368
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
369 lua_newtable(L);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
370
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
371 for (i = 0; i < statement->num_columns; i++) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
372 lua_push_type_t lua_push = oracle_to_lua_push(bind[i].data_type, bind[i].null);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
373 const char *name = strlower(bind[i].name);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
374 const char *data = bind[i].data;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
375
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
376 if (lua_push == LUA_PUSH_NIL) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
377 if (named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
378 LUA_PUSH_ATTRIB_NIL(name);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
379 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
380 LUA_PUSH_ARRAY_NIL(d);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
381 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
382 } else if (lua_push == LUA_PUSH_INTEGER) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
383 int val = atoi(data);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
384
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
385 if (named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
386 LUA_PUSH_ATTRIB_INT(name, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
387 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
388 LUA_PUSH_ARRAY_INT(d, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
389 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
390 } else if (lua_push == LUA_PUSH_NUMBER) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
391 double val = strtod(data, NULL);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
392
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
393 if (named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
394 LUA_PUSH_ATTRIB_FLOAT(name, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
395 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
396 LUA_PUSH_ARRAY_FLOAT(d, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
397 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
398 } else if (lua_push == LUA_PUSH_STRING) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
399 if (named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
400 LUA_PUSH_ATTRIB_STRING(name, data);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
401 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
402 LUA_PUSH_ARRAY_STRING(d, data);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
403 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
404 } else if (lua_push == LUA_PUSH_BOOLEAN) {
18
b705ba343e94 Misc changes.
nrich@ii.net
parents: 17
diff changeset
405 int val = atoi(data);
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
406
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
407 if (named_columns) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
408 LUA_PUSH_ATTRIB_BOOL(name, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
409 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
410 LUA_PUSH_ARRAY_BOOL(d, val);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
411 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
412 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
413 luaL_error(L, DBI_ERR_UNKNOWN_PUSH);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
414 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
415 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
416 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
417 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
418 * no columns returned by statement?
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
419 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
420 lua_pushnil(L);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
421 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
422
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
423 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
424 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
425
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
426 static int next_iterator(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
427 statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
428 int named_columns = lua_toboolean(L, lua_upvalueindex(2));
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
429
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
430 return statement_fetch_impl(L, statement, named_columns);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
431 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
432
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
433 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
434 * table = statement:fetch(named_indexes)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
435 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
436 static int statement_fetch(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
437 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
438 int named_columns = lua_toboolean(L, 2);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
439
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
440 return statement_fetch_impl(L, statement, named_columns);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
441 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
442
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
443 /*
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
444 * num_rows = statement:rowcount()
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
445 */
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
446 static int statement_rowcount(lua_State *L) {
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
447 luaL_error(L, DBI_ERR_NOT_IMPLEMENTED, DBD_ORACLE_STATEMENT, "rowcount");
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
448
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
449 return 0;
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
450 }
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
451
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
452 /*
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
453 * iterfunc = statement:rows(named_indexes)
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
454 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
455 static int statement_rows(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
456 if (lua_gettop(L) == 1) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
457 lua_pushvalue(L, 1);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
458 lua_pushboolean(L, 0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
459 } else {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
460 lua_pushvalue(L, 1);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
461 lua_pushboolean(L, lua_toboolean(L, 2));
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
462 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
463
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
464 lua_pushcclosure(L, next_iterator, 2);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
465 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
466 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
467
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
468 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
469 * __gc
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
470 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
471 static int statement_gc(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
472 /* always free the handle */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
473 statement_close(L);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
474
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
475 return 0;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
476 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
477
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
478 int dbd_oracle_statement_create(lua_State *L, connection_t *conn, const char *sql_query) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
479 int rc;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
480 statement_t *statement = NULL;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
481 OCIStmt *stmt;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
482 char *new_sql;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
483
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
484 /*
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
485 * convert SQL string into a Oracle API compatible SQL statement
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
486 */
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
487 new_sql = replace_placeholders(L, ':', sql_query);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
488
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
489 rc = OCIHandleAlloc((dvoid *)conn->oracle, (dvoid **)&stmt, OCI_HTYPE_STMT, 0, (dvoid **)0);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
490 rc = OCIStmtPrepare(stmt, conn->err, new_sql, strlen(new_sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
491
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
492 free(new_sql);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
493
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
494 statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t));
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
495 statement->conn = conn;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
496 statement->stmt = stmt;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
497 statement->num_columns = 0;
27
1d2369ebee21 Cleanup oracle bind allocations.
nrich@ii.net
parents: 21
diff changeset
498 statement->bind = NULL;
31
999ef93f0dbc * Add `columns' method for DB2 and Oracle statment handles
nrich@ii.net
parents: 30
diff changeset
499 statement->metadata = 0;
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
500
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
501 luaL_getmetatable(L, DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
502 lua_setmetatable(L, -2);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
503
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
504 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
505 }
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
506
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
507 int dbd_oracle_statement(lua_State *L) {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
508 static const luaL_Reg statement_methods[] = {
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
509 {"affected", statement_affected},
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
510 {"close", statement_close},
30
8599f34c139b Add 'columns' method to statement handles to retrieve column names from a result set
nrich@ii.net
parents: 27
diff changeset
511 {"columns", statement_columns},
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
512 {"execute", statement_execute},
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
513 {"fetch", statement_fetch},
21
7956401a0c5e Added the statement:affected() and statement:rowcount() methods.
nrich@ii.net
parents: 19
diff changeset
514 {"rowcount", statement_rowcount},
17
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
515 {"rows", statement_rows},
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
516 {NULL, NULL}
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
517 };
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
518
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
519 static const luaL_Reg statement_class_methods[] = {
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
520 {NULL, NULL}
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
521 };
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
522
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
523 luaL_newmetatable(L, DBD_ORACLE_STATEMENT);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
524 luaL_register(L, 0, statement_methods);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
525 lua_pushvalue(L,-1);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
526 lua_setfield(L, -2, "__index");
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
527
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
528 lua_pushcfunction(L, statement_gc);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
529 lua_setfield(L, -2, "__gc");
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
530
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
531 luaL_register(L, DBD_ORACLE_STATEMENT, statement_class_methods);
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
532
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
533 return 1;
21c4feaeafe7 Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
nrich@ii.net
parents:
diff changeset
534 }

mercurial