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