dbd/postgresql/statement.c

changeset 9
06eb2850703f
parent 8
0b805fda1c91
child 10
3aa8a37a3dd8
equal deleted inserted replaced
8:0b805fda1c91 9:06eb2850703f
130 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT); 130 statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_STATEMENT);
131 int num_bind_params = n - 1; 131 int num_bind_params = n - 1;
132 ExecStatusType status; 132 ExecStatusType status;
133 int p; 133 int p;
134 134
135 char **params; 135 const char **params;
136 PGresult *result = NULL; 136 PGresult *result = NULL;
137 137
138 statement->tuple = 0; 138 statement->tuple = 0;
139 139
140 params = malloc(num_bind_params * sizeof(params)); 140 params = malloc(num_bind_params * sizeof(params));
146 int i = p - 2; 146 int i = p - 2;
147 147
148 if (lua_isnil(L, p)) { 148 if (lua_isnil(L, p)) {
149 params[i] = NULL; 149 params[i] = NULL;
150 } else { 150 } else {
151 const char *param = lua_tostring(L, p); 151 if (lua_isboolean(L, p))
152 size_t len = strlen(param) + 1; 152 /*
153 153 * boolean values in postgresql can either be
154 params[i] = malloc(len * sizeof(char)); 154 * t/f or 1/0. Pass integer values rather than
155 memset(params[i], 0, len); 155 * strings to maintain semantic compatibility
156 156 * with other DBD drivers that pass booleans
157 strncpy(params[i], param, len); 157 * as integers.
158 params[i][len] = '\0'; 158 */
159 params[i] = lua_toboolean(L, p) ? "1" : "0";
160 else
161 params[i] = lua_tostring(L, p);
159 } 162 }
160 } 163 }
161 164
162 result = PQexecPrepared( 165 result = PQexecPrepared(
163 statement->postgresql, 166 statement->postgresql,
167 NULL, 170 NULL,
168 NULL, 171 NULL,
169 0 172 0
170 ); 173 );
171 174
172 /*
173 * free string array
174 */
175 for (p = 0; p < num_bind_params; p++) {
176 if (params[p]) {
177 free(params[p]);
178 }
179 }
180 free(params); 175 free(params);
181 176
182 if (!result) { 177 if (!result) {
183 lua_pushboolean(L, 0); 178 lua_pushboolean(L, 0);
184 lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql)); 179 lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql));
238 const char *value = PQgetvalue(statement->result, tuple, i); 233 const char *value = PQgetvalue(statement->result, tuple, i);
239 lua_push_type_t lua_push = postgresql_to_lua_push(PQftype(statement->result, i)); 234 lua_push_type_t lua_push = postgresql_to_lua_push(PQftype(statement->result, i));
240 235
241 /* 236 /*
242 * data is returned as strings from PSQL 237 * data is returned as strings from PSQL
238 * convert them here into Lua types
243 */ 239 */
244 240
245 if (lua_push == LUA_PUSH_NIL) { 241 if (lua_push == LUA_PUSH_NIL) {
246 if (named_columns) { 242 if (named_columns) {
247 LUA_PUSH_ATTRIB_NIL(name); 243 LUA_PUSH_ATTRIB_NIL(name);

mercurial