Sat, 06 Dec 2008 00:32:37 +0000
Added initial Oracle driver support - functionality is complete, but may be too buggy in its current state for any serious use.
1 | 1 | #include "dbd_postgresql.h" |
2 | ||
3 | static lua_push_type_t postgresql_to_lua_push(unsigned int postgresql_type) { | |
4 | lua_push_type_t lua_type; | |
5 | ||
6 | switch(postgresql_type) { | |
7 | case INT2OID: | |
8 | case INT4OID: | |
9 | lua_type = LUA_PUSH_INTEGER; | |
10 | break; | |
11 | ||
12 | case FLOAT4OID: | |
13 | case FLOAT8OID: | |
14 | lua_type = LUA_PUSH_NUMBER; | |
15 | break; | |
16 | ||
17 | case BOOLOID: | |
18 | lua_type = LUA_PUSH_BOOLEAN; | |
19 | break; | |
20 | ||
21 | default: | |
22 | lua_type = LUA_PUSH_STRING; | |
23 | } | |
24 | ||
25 | return lua_type; | |
26 | } | |
27 | ||
28 | ||
2 | 29 | /* |
30 | * success = statement:close() | |
31 | */ | |
1 | 32 | static int statement_close(lua_State *L) { |
33 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); | |
34 | ||
35 | if (statement->result) { | |
36 | PQclear(statement->result); | |
37 | statement->result = NULL; | |
38 | } | |
39 | ||
40 | return 0; | |
41 | } | |
42 | ||
2 | 43 | /* |
44 | * success = statement:execute(...) | |
45 | */ | |
1 | 46 | static int statement_execute(lua_State *L) { |
47 | int n = lua_gettop(L); | |
48 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); | |
49 | int num_bind_params = n - 1; | |
50 | ExecStatusType status; | |
51 | int p; | |
10 | 52 | const char *errstr = NULL; |
1 | 53 | |
9 | 54 | const char **params; |
1 | 55 | PGresult *result = NULL; |
56 | ||
57 | statement->tuple = 0; | |
58 | ||
59 | params = malloc(num_bind_params * sizeof(params)); | |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
60 | memset(params, 0, num_bind_params * sizeof(params)); |
1 | 61 | |
2 | 62 | /* |
63 | * convert and copy parameters into a string array | |
64 | */ | |
1 | 65 | for (p = 2; p <= n; p++) { |
66 | int i = p - 2; | |
10 | 67 | int type = lua_type(L, p); |
68 | char err[64]; | |
1 | 69 | |
10 | 70 | switch(type) { |
71 | case LUA_TNIL: | |
1 | 72 | params[i] = NULL; |
10 | 73 | break; |
74 | case LUA_TBOOLEAN: | |
75 | /* | |
76 | * boolean values in postgresql can either be | |
77 | * t/f or 1/0. Pass integer values rather than | |
78 | * strings to maintain semantic compatibility | |
79 | * with other DBD drivers that pass booleans | |
80 | * as integers. | |
81 | */ | |
82 | params[i] = lua_toboolean(L, p) ? "1" : "0"; | |
83 | break; | |
84 | case LUA_TNUMBER: | |
85 | case LUA_TSTRING: | |
86 | params[i] = lua_tostring(L, p); | |
87 | break; | |
88 | default: | |
89 | snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type)); | |
90 | errstr = err; | |
91 | goto cleanup; | |
1 | 92 | } |
93 | } | |
94 | ||
95 | result = PQexecPrepared( | |
96 | statement->postgresql, | |
97 | statement->name, | |
98 | num_bind_params, | |
99 | (const char **)params, | |
100 | NULL, | |
101 | NULL, | |
102 | 0 | |
103 | ); | |
104 | ||
10 | 105 | cleanup: |
1 | 106 | free(params); |
107 | ||
10 | 108 | if (errstr) { |
109 | lua_pushboolean(L, 0); | |
110 | lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr); | |
111 | return 2; | |
112 | } | |
113 | ||
1 | 114 | if (!result) { |
115 | lua_pushboolean(L, 0); | |
4 | 116 | lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql)); |
3 | 117 | return 2; |
1 | 118 | } |
119 | ||
120 | status = PQresultStatus(result); | |
121 | if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { | |
122 | lua_pushboolean(L, 0); | |
4 | 123 | lua_pushfstring(L, DBI_ERR_BINDING_EXEC, PQresultErrorMessage(result)); |
3 | 124 | return 2; |
1 | 125 | } |
126 | ||
127 | statement->result = result; | |
128 | ||
129 | lua_pushboolean(L, 1); | |
130 | return 1; | |
131 | } | |
132 | ||
2 | 133 | /* |
134 | * must be called after an execute | |
135 | */ | |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
136 | static int statement_fetch_impl(lua_State *L, statement_t *statement, int named_columns) { |
1 | 137 | int tuple = statement->tuple++; |
138 | int i; | |
139 | int num_columns; | |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
140 | int d = 1; |
1 | 141 | |
3 | 142 | if (!statement->result) { |
4 | 143 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 144 | return 0; |
145 | } | |
146 | ||
1 | 147 | if (PQresultStatus(statement->result) != PGRES_TUPLES_OK) { |
148 | lua_pushnil(L); | |
149 | return 1; | |
150 | } | |
151 | ||
152 | if (tuple >= PQntuples(statement->result)) { | |
153 | lua_pushnil(L); /* no more results */ | |
154 | return 1; | |
155 | } | |
156 | ||
157 | num_columns = PQnfields(statement->result); | |
158 | lua_newtable(L); | |
159 | for (i = 0; i < num_columns; i++) { | |
160 | const char *name = PQfname(statement->result, i); | |
161 | ||
162 | if (PQgetisnull(statement->result, tuple, i)) { | |
163 | if (named_columns) { | |
164 | LUA_PUSH_ATTRIB_NIL(name); | |
165 | } else { | |
166 | LUA_PUSH_ARRAY_NIL(d); | |
167 | } | |
168 | } else { | |
169 | const char *value = PQgetvalue(statement->result, tuple, i); | |
170 | lua_push_type_t lua_push = postgresql_to_lua_push(PQftype(statement->result, i)); | |
171 | ||
2 | 172 | /* |
173 | * data is returned as strings from PSQL | |
9 | 174 | * convert them here into Lua types |
2 | 175 | */ |
176 | ||
1 | 177 | if (lua_push == LUA_PUSH_NIL) { |
178 | if (named_columns) { | |
179 | LUA_PUSH_ATTRIB_NIL(name); | |
180 | } else { | |
181 | LUA_PUSH_ARRAY_NIL(d); | |
182 | } | |
183 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
184 | int val = atoi(value); | |
185 | ||
186 | if (named_columns) { | |
187 | LUA_PUSH_ATTRIB_INT(name, val); | |
188 | } else { | |
189 | LUA_PUSH_ARRAY_INT(d, val); | |
190 | } | |
191 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
192 | double val = strtod(value, NULL); | |
193 | ||
194 | if (named_columns) { | |
195 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
196 | } else { | |
197 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
198 | } | |
199 | } else if (lua_push == LUA_PUSH_STRING) { | |
200 | if (named_columns) { | |
201 | LUA_PUSH_ATTRIB_STRING(name, value); | |
202 | } else { | |
203 | LUA_PUSH_ARRAY_STRING(d, value); | |
204 | } | |
205 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
2 | 206 | /* |
207 | * booleans are returned as a string | |
208 | * either 't' or 'f' | |
209 | */ | |
1 | 210 | int val = value[0] == 't' ? 1 : 0; |
211 | ||
212 | if (named_columns) { | |
213 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
214 | } else { | |
215 | LUA_PUSH_ARRAY_BOOL(d, val); | |
216 | } | |
217 | } else { | |
4 | 218 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 219 | } |
220 | } | |
221 | } | |
222 | ||
223 | return 1; | |
224 | } | |
225 | ||
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
226 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
227 | static int next_iterator(lua_State *L) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
228 | statement_t *statement = (statement_t *)luaL_checkudata(L, lua_upvalueindex(1), DBD_POSTGRESQL_STATEMENT); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
229 | int named_columns = lua_toboolean(L, lua_upvalueindex(2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
230 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
231 | return statement_fetch_impl(L, statement, named_columns); |
1 | 232 | } |
233 | ||
2 | 234 | /* |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
235 | * table = statement:fetch(named_indexes) |
2 | 236 | */ |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
237 | static int statement_fetch(lua_State *L) { |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
238 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
239 | int named_columns = lua_toboolean(L, 2); |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
240 | |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
241 | return statement_fetch_impl(L, statement, named_columns); |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
242 | } |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
243 | |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
244 | /* |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
245 | * iterfunc = statement:rows(named_indexes) |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
246 | */ |
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
247 | static int statement_rows(lua_State *L) { |
11
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
248 | if (lua_gettop(L) == 1) { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
249 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
250 | lua_pushboolean(L, 0); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
251 | } else { |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
252 | lua_pushvalue(L, 1); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
253 | lua_pushboolean(L, lua_toboolean(L, 2)); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
254 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
255 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
256 | lua_pushcclosure(L, next_iterator, 2); |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
257 | return 1; |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
258 | } |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
259 | |
b3e05e361f46
Bugfix: PSQL array returns were not being indexed properly.
nrich@ii.net
parents:
10
diff
changeset
|
260 | /* |
2 | 261 | * __gc |
262 | */ | |
1 | 263 | static int statement_gc(lua_State *L) { |
264 | /* always free the handle */ | |
265 | statement_close(L); | |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
270 | int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
271 | statement_t *statement = NULL; | |
272 | ExecStatusType status; | |
273 | PGresult *result = NULL; | |
274 | char *new_sql; | |
275 | char name[IDLEN]; | |
276 | ||
2 | 277 | /* |
278 | * convert SQL string into a PSQL API compatible SQL statement | |
279 | */ | |
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:
12
diff
changeset
|
280 | new_sql = replace_placeholders(L, '$', sql_query); |
1 | 281 | |
282 | snprintf(name, IDLEN, "%017u", ++conn->statement_id); | |
283 | ||
284 | result = PQprepare(conn->postgresql, name, new_sql, 0, NULL); | |
2 | 285 | |
286 | /* | |
287 | * free converted statement after use | |
288 | */ | |
1 | 289 | free(new_sql); |
290 | ||
291 | if (!result) { | |
3 | 292 | lua_pushnil(L); |
4 | 293 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, PQerrorMessage(statement->postgresql)); |
3 | 294 | return 2; |
1 | 295 | } |
296 | ||
297 | status = PQresultStatus(result); | |
298 | if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { | |
299 | const char *err_string = PQresultErrorMessage(result); | |
300 | PQclear(result); | |
3 | 301 | |
302 | lua_pushnil(L); | |
4 | 303 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, err_string); |
3 | 304 | return 2; |
1 | 305 | } |
306 | ||
307 | PQclear(result); | |
308 | ||
309 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
310 | statement->postgresql = conn->postgresql; | |
311 | statement->result = NULL; | |
312 | statement->tuple = 0; | |
313 | strncpy(statement->name, name, IDLEN-1); | |
314 | statement->name[IDLEN-1] = '\0'; | |
315 | ||
316 | luaL_getmetatable(L, DBD_POSTGRESQL_STATEMENT); | |
317 | lua_setmetatable(L, -2); | |
318 | ||
319 | return 1; | |
320 | } | |
321 | ||
322 | int dbd_postgresql_statement(lua_State *L) { | |
2 | 323 | static const luaL_Reg statement_methods[] = { |
324 | {"close", statement_close}, | |
325 | {"execute", statement_execute}, | |
326 | {"fetch", statement_fetch}, | |
12
014ba3ab3903
Renamed statement:fetch() to statement:rows(), and statement:row() to statement:fetch(). The API reads better this way.
nrich@ii.net
parents:
11
diff
changeset
|
327 | {"rows", statement_rows}, |
2 | 328 | {NULL, NULL} |
329 | }; | |
330 | ||
331 | static const luaL_Reg statement_class_methods[] = { | |
332 | {NULL, NULL} | |
333 | }; | |
334 | ||
1 | 335 | luaL_newmetatable(L, DBD_POSTGRESQL_STATEMENT); |
336 | luaL_register(L, 0, statement_methods); | |
337 | lua_pushvalue(L,-1); | |
338 | lua_setfield(L, -2, "__index"); | |
339 | ||
340 | lua_pushcfunction(L, statement_gc); | |
341 | lua_setfield(L, -2, "__gc"); | |
342 | ||
343 | luaL_register(L, DBD_POSTGRESQL_STATEMENT, statement_class_methods); | |
344 | ||
345 | return 1; | |
346 | } |