dbd/postgresql/statement.c

changeset 10
3aa8a37a3dd8
parent 9
06eb2850703f
child 11
b3e05e361f46
equal deleted inserted replaced
9:06eb2850703f 10:3aa8a37a3dd8
129 int n = lua_gettop(L); 129 int n = lua_gettop(L);
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 const char *errstr = NULL;
134 135
135 const char **params; 136 const char **params;
136 PGresult *result = NULL; 137 PGresult *result = NULL;
137 138
138 statement->tuple = 0; 139 statement->tuple = 0;
142 /* 143 /*
143 * convert and copy parameters into a string array 144 * convert and copy parameters into a string array
144 */ 145 */
145 for (p = 2; p <= n; p++) { 146 for (p = 2; p <= n; p++) {
146 int i = p - 2; 147 int i = p - 2;
147 148 int type = lua_type(L, p);
148 if (lua_isnil(L, p)) { 149 char err[64];
150
151 switch(type) {
152 case LUA_TNIL:
149 params[i] = NULL; 153 params[i] = NULL;
150 } else { 154 break;
151 if (lua_isboolean(L, p)) 155 case LUA_TBOOLEAN:
152 /* 156 /*
153 * boolean values in postgresql can either be 157 * boolean values in postgresql can either be
154 * t/f or 1/0. Pass integer values rather than 158 * t/f or 1/0. Pass integer values rather than
155 * strings to maintain semantic compatibility 159 * strings to maintain semantic compatibility
156 * with other DBD drivers that pass booleans 160 * with other DBD drivers that pass booleans
157 * as integers. 161 * as integers.
158 */ 162 */
159 params[i] = lua_toboolean(L, p) ? "1" : "0"; 163 params[i] = lua_toboolean(L, p) ? "1" : "0";
160 else 164 break;
161 params[i] = lua_tostring(L, p); 165 case LUA_TNUMBER:
166 case LUA_TSTRING:
167 params[i] = lua_tostring(L, p);
168 break;
169 default:
170 snprintf(err, sizeof(err)-1, DBI_ERR_BINDING_TYPE_ERR, lua_typename(L, type));
171 errstr = err;
172 goto cleanup;
162 } 173 }
163 } 174 }
164 175
165 result = PQexecPrepared( 176 result = PQexecPrepared(
166 statement->postgresql, 177 statement->postgresql,
170 NULL, 181 NULL,
171 NULL, 182 NULL,
172 0 183 0
173 ); 184 );
174 185
186 cleanup:
175 free(params); 187 free(params);
188
189 if (errstr) {
190 lua_pushboolean(L, 0);
191 lua_pushfstring(L, DBI_ERR_BINDING_PARAMS, errstr);
192 return 2;
193 }
176 194
177 if (!result) { 195 if (!result) {
178 lua_pushboolean(L, 0); 196 lua_pushboolean(L, 0);
179 lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql)); 197 lua_pushfstring(L, DBI_ERR_ALLOC_RESULT, PQerrorMessage(statement->postgresql));
180 return 2; 198 return 2;

mercurial