Wed, 26 Nov 2008 10:43:45 +0000
Clean up error messages for consistency.
1 | 1 | #include "dbd_postgresql.h" |
2 | ||
2 | 3 | #define MAX_PLACEHOLDERS 9999 |
1 | 4 | #define MAX_PLACEHOLDER_SIZE (1+4) /* $\d{4} */ |
5 | ||
6 | static lua_push_type_t postgresql_to_lua_push(unsigned int postgresql_type) { | |
7 | lua_push_type_t lua_type; | |
8 | ||
9 | switch(postgresql_type) { | |
10 | case INT2OID: | |
11 | case INT4OID: | |
12 | lua_type = LUA_PUSH_INTEGER; | |
13 | break; | |
14 | ||
15 | case FLOAT4OID: | |
16 | case FLOAT8OID: | |
17 | lua_type = LUA_PUSH_NUMBER; | |
18 | break; | |
19 | ||
20 | case BOOLOID: | |
21 | lua_type = LUA_PUSH_BOOLEAN; | |
22 | break; | |
23 | ||
24 | default: | |
25 | lua_type = LUA_PUSH_STRING; | |
26 | } | |
27 | ||
28 | return lua_type; | |
29 | } | |
30 | ||
2 | 31 | /* |
32 | * replace '?' placeholders with $\d+ placeholders | |
33 | * to be compatible with PSQL API | |
34 | */ | |
1 | 35 | static char *replace_placeholders(lua_State *L, const char *sql) { |
36 | size_t len = strlen(sql); | |
37 | int num_placeholders = 0; | |
38 | int extra_space = 0; | |
39 | int i; | |
40 | char *newsql; | |
41 | int newpos = 1; | |
42 | int ph_num = 1; | |
43 | int in_quote = 0; | |
44 | ||
2 | 45 | /* |
46 | * dumb count of all '?' | |
47 | * this will match more placeholders than necessesary | |
48 | * but it's safer to allocate more placeholders at the | |
49 | * cost of a few bytes than risk a buffer overflow | |
50 | */ | |
1 | 51 | for (i = 1; i < len; i++) { |
52 | if (sql[i] == '?') { | |
53 | num_placeholders++; | |
54 | } | |
55 | } | |
56 | ||
2 | 57 | /* |
58 | * this is MAX_PLACEHOLDER_SIZE-1 because the '?' is | |
59 | * replaced with '$' | |
60 | */ | |
1 | 61 | extra_space = num_placeholders * (MAX_PLACEHOLDER_SIZE-1); |
62 | ||
2 | 63 | /* |
64 | * allocate a new string for the converted SQL statement | |
65 | */ | |
1 | 66 | newsql = malloc(sizeof(char) * (len+extra_space+1)); |
67 | memset(newsql, 0, sizeof(char) * (len+extra_space+1)); | |
68 | ||
2 | 69 | /* |
70 | * copy first char. In valid SQL this cannot be a placeholder | |
71 | */ | |
1 | 72 | newsql[0] = sql[0]; |
2 | 73 | |
74 | /* | |
75 | * only replace '?' not in a single quoted string | |
76 | */ | |
1 | 77 | for (i = 1; i < len; i++) { |
2 | 78 | /* |
79 | * don't change the quote flag if the ''' is preceded | |
80 | * bt a '\' to account for escaping | |
81 | */ | |
1 | 82 | if (sql[i] == '\'' && sql[i-1] != '\\') { |
83 | in_quote = !in_quote; | |
84 | } | |
85 | ||
86 | if (sql[i] == '?' && !in_quote) { | |
87 | size_t n; | |
88 | ||
89 | if (ph_num > MAX_PLACEHOLDERS) { | |
90 | luaL_error(L, "Sorry, you are using more than %d placeholders. Use ${num} format instead", MAX_PLACEHOLDERS); | |
91 | } | |
92 | ||
93 | n = snprintf(&newsql[newpos], MAX_PLACEHOLDER_SIZE, "$%u", ph_num++); | |
94 | ||
95 | newpos += n; | |
96 | } else { | |
97 | newsql[newpos] = sql[i]; | |
98 | newpos++; | |
99 | } | |
100 | } | |
101 | ||
2 | 102 | /* |
103 | * terminate string on the last position | |
104 | */ | |
1 | 105 | newsql[newpos] = '\0'; |
106 | ||
107 | /* fprintf(stderr, "[%s]\n", newsql); */ | |
108 | return newsql; | |
109 | } | |
110 | ||
2 | 111 | /* |
112 | * success = statement:close() | |
113 | */ | |
1 | 114 | static int statement_close(lua_State *L) { |
115 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); | |
116 | ||
117 | if (statement->result) { | |
118 | PQclear(statement->result); | |
119 | statement->result = NULL; | |
120 | } | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
2 | 125 | /* |
126 | * success = statement:execute(...) | |
127 | */ | |
1 | 128 | static int statement_execute(lua_State *L) { |
129 | int n = lua_gettop(L); | |
130 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); | |
131 | int num_bind_params = n - 1; | |
132 | ExecStatusType status; | |
133 | int p; | |
134 | ||
135 | char **params; | |
136 | PGresult *result = NULL; | |
137 | ||
138 | statement->tuple = 0; | |
139 | ||
140 | params = malloc(num_bind_params * sizeof(params)); | |
141 | ||
2 | 142 | /* |
143 | * convert and copy parameters into a string array | |
144 | */ | |
1 | 145 | for (p = 2; p <= n; p++) { |
146 | int i = p - 2; | |
147 | ||
148 | if (lua_isnil(L, p)) { | |
149 | params[i] = NULL; | |
150 | } else { | |
151 | const char *param = lua_tostring(L, p); | |
152 | size_t len = strlen(param) + 1; | |
153 | ||
154 | params[i] = malloc(len * sizeof(char)); | |
155 | memset(params[i], 0, len); | |
156 | ||
157 | strncpy(params[i], param, len); | |
158 | params[i][len] = '\0'; | |
159 | } | |
160 | } | |
161 | ||
162 | result = PQexecPrepared( | |
163 | statement->postgresql, | |
164 | statement->name, | |
165 | num_bind_params, | |
166 | (const char **)params, | |
167 | NULL, | |
168 | NULL, | |
169 | 0 | |
170 | ); | |
171 | ||
2 | 172 | /* |
173 | * free string array | |
174 | */ | |
1 | 175 | for (p = 0; p < num_bind_params; p++) { |
176 | if (params[p]) { | |
177 | free(params[p]); | |
178 | } | |
179 | } | |
180 | free(params); | |
181 | ||
182 | if (!result) { | |
183 | lua_pushboolean(L, 0); | |
4 | 184 | lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql)); |
3 | 185 | return 2; |
1 | 186 | } |
187 | ||
188 | status = PQresultStatus(result); | |
189 | if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { | |
190 | lua_pushboolean(L, 0); | |
4 | 191 | lua_pushfstring(L, DBI_ERR_BINDING_EXEC, PQresultErrorMessage(result)); |
3 | 192 | return 2; |
1 | 193 | } |
194 | ||
195 | statement->result = result; | |
196 | ||
197 | lua_pushboolean(L, 1); | |
3 | 198 | lua_pushnil(L); |
1 | 199 | return 1; |
200 | } | |
201 | ||
2 | 202 | /* |
203 | * must be called after an execute | |
204 | */ | |
1 | 205 | static int statement_fetch_impl(lua_State *L, int named_columns) { |
206 | statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); | |
207 | int tuple = statement->tuple++; | |
208 | int i; | |
209 | int num_columns; | |
210 | ||
3 | 211 | if (!statement->result) { |
4 | 212 | luaL_error(L, DBI_ERR_FETCH_INVALID); |
3 | 213 | return 0; |
214 | } | |
215 | ||
1 | 216 | if (PQresultStatus(statement->result) != PGRES_TUPLES_OK) { |
217 | lua_pushnil(L); | |
218 | return 1; | |
219 | } | |
220 | ||
221 | if (tuple >= PQntuples(statement->result)) { | |
222 | lua_pushnil(L); /* no more results */ | |
223 | return 1; | |
224 | } | |
225 | ||
226 | num_columns = PQnfields(statement->result); | |
227 | lua_newtable(L); | |
228 | for (i = 0; i < num_columns; i++) { | |
229 | int d = 1; | |
230 | const char *name = PQfname(statement->result, i); | |
231 | ||
232 | if (PQgetisnull(statement->result, tuple, i)) { | |
233 | if (named_columns) { | |
234 | LUA_PUSH_ATTRIB_NIL(name); | |
235 | } else { | |
236 | LUA_PUSH_ARRAY_NIL(d); | |
237 | } | |
238 | } else { | |
239 | const char *value = PQgetvalue(statement->result, tuple, i); | |
240 | lua_push_type_t lua_push = postgresql_to_lua_push(PQftype(statement->result, i)); | |
241 | ||
2 | 242 | /* |
243 | * data is returned as strings from PSQL | |
244 | */ | |
245 | ||
1 | 246 | if (lua_push == LUA_PUSH_NIL) { |
247 | if (named_columns) { | |
248 | LUA_PUSH_ATTRIB_NIL(name); | |
249 | } else { | |
250 | LUA_PUSH_ARRAY_NIL(d); | |
251 | } | |
252 | } else if (lua_push == LUA_PUSH_INTEGER) { | |
253 | int val = atoi(value); | |
254 | ||
255 | if (named_columns) { | |
256 | LUA_PUSH_ATTRIB_INT(name, val); | |
257 | } else { | |
258 | LUA_PUSH_ARRAY_INT(d, val); | |
259 | } | |
260 | } else if (lua_push == LUA_PUSH_NUMBER) { | |
261 | double val = strtod(value, NULL); | |
262 | ||
263 | if (named_columns) { | |
264 | LUA_PUSH_ATTRIB_FLOAT(name, val); | |
265 | } else { | |
266 | LUA_PUSH_ARRAY_FLOAT(d, val); | |
267 | } | |
268 | } else if (lua_push == LUA_PUSH_STRING) { | |
269 | if (named_columns) { | |
270 | LUA_PUSH_ATTRIB_STRING(name, value); | |
271 | } else { | |
272 | LUA_PUSH_ARRAY_STRING(d, value); | |
273 | } | |
274 | } else if (lua_push == LUA_PUSH_BOOLEAN) { | |
2 | 275 | /* |
276 | * booleans are returned as a string | |
277 | * either 't' or 'f' | |
278 | */ | |
1 | 279 | int val = value[0] == 't' ? 1 : 0; |
280 | ||
281 | if (named_columns) { | |
282 | LUA_PUSH_ATTRIB_BOOL(name, val); | |
283 | } else { | |
284 | LUA_PUSH_ARRAY_BOOL(d, val); | |
285 | } | |
286 | } else { | |
4 | 287 | luaL_error(L, DBI_ERR_UNKNOWN_PUSH); |
1 | 288 | } |
289 | } | |
290 | } | |
291 | ||
292 | return 1; | |
293 | } | |
294 | ||
2 | 295 | /* |
296 | * array = statement:fetch() | |
297 | */ | |
1 | 298 | static int statement_fetch(lua_State *L) { |
299 | return statement_fetch_impl(L, 0); | |
300 | } | |
301 | ||
2 | 302 | /* |
303 | * hashmap = statement:fetchtable() | |
304 | */ | |
1 | 305 | static int statement_fetchtable(lua_State *L) { |
306 | return statement_fetch_impl(L, 1); | |
307 | } | |
308 | ||
2 | 309 | /* |
310 | * __gc | |
311 | */ | |
1 | 312 | static int statement_gc(lua_State *L) { |
313 | /* always free the handle */ | |
314 | statement_close(L); | |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | int dbd_postgresql_statement_create(lua_State *L, connection_t *conn, const char *sql_query) { | |
320 | statement_t *statement = NULL; | |
321 | ExecStatusType status; | |
322 | PGresult *result = NULL; | |
323 | char *new_sql; | |
324 | char name[IDLEN]; | |
325 | ||
2 | 326 | /* |
327 | * convert SQL string into a PSQL API compatible SQL statement | |
328 | */ | |
1 | 329 | new_sql = replace_placeholders(L, sql_query); |
330 | ||
331 | snprintf(name, IDLEN, "%017u", ++conn->statement_id); | |
332 | ||
333 | result = PQprepare(conn->postgresql, name, new_sql, 0, NULL); | |
2 | 334 | |
335 | /* | |
336 | * free converted statement after use | |
337 | */ | |
1 | 338 | free(new_sql); |
339 | ||
340 | if (!result) { | |
3 | 341 | lua_pushnil(L); |
4 | 342 | lua_pushfstring(L, DBI_ERR_ALLOC_STATEMENT, PQerrorMessage(statement->postgresql)); |
3 | 343 | return 2; |
1 | 344 | } |
345 | ||
346 | status = PQresultStatus(result); | |
347 | if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) { | |
348 | const char *err_string = PQresultErrorMessage(result); | |
349 | PQclear(result); | |
3 | 350 | |
351 | lua_pushnil(L); | |
4 | 352 | lua_pushfstring(L, DBI_ERR_PREP_STATEMENT, err_string); |
3 | 353 | return 2; |
1 | 354 | } |
355 | ||
356 | PQclear(result); | |
357 | ||
358 | statement = (statement_t *)lua_newuserdata(L, sizeof(statement_t)); | |
359 | statement->postgresql = conn->postgresql; | |
360 | statement->result = NULL; | |
361 | statement->tuple = 0; | |
362 | strncpy(statement->name, name, IDLEN-1); | |
363 | statement->name[IDLEN-1] = '\0'; | |
364 | ||
365 | luaL_getmetatable(L, DBD_POSTGRESQL_STATEMENT); | |
366 | lua_setmetatable(L, -2); | |
367 | ||
368 | return 1; | |
369 | } | |
370 | ||
371 | int dbd_postgresql_statement(lua_State *L) { | |
2 | 372 | static const luaL_Reg statement_methods[] = { |
373 | {"close", statement_close}, | |
374 | {"execute", statement_execute}, | |
375 | {"fetch", statement_fetch}, | |
376 | {"fetchtable", statement_fetchtable}, | |
377 | {NULL, NULL} | |
378 | }; | |
379 | ||
380 | static const luaL_Reg statement_class_methods[] = { | |
381 | {NULL, NULL} | |
382 | }; | |
383 | ||
1 | 384 | luaL_newmetatable(L, DBD_POSTGRESQL_STATEMENT); |
385 | luaL_register(L, 0, statement_methods); | |
386 | lua_pushvalue(L,-1); | |
387 | lua_setfield(L, -2, "__index"); | |
388 | ||
389 | lua_pushcfunction(L, statement_gc); | |
390 | lua_setfield(L, -2, "__gc"); | |
391 | ||
392 | luaL_register(L, DBD_POSTGRESQL_STATEMENT, statement_class_methods); | |
393 | ||
394 | return 1; | |
395 | } |