dbd/postgresql/connection.c

changeset 22
fd78e9cdc6e9
parent 14
98192b7d4e89
child 32
03ed0ca09837
equal deleted inserted replaced
21:7956401a0c5e 22:fd78e9cdc6e9
197 lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE); 197 lua_pushstring(L, DBI_ERR_DB_UNAVAILABLE);
198 return 2; 198 return 2;
199 } 199 }
200 200
201 /* 201 /*
202 * quoted = connection:quote(str)
203 */
204 static int connection_quote(lua_State *L) {
205 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION);
206 size_t len;
207 const char *from = luaL_checklstring(L, 2, &len);
208 char *to = (char *)calloc(len*2+1, sizeof(char));
209 int err = 0;
210 int quoted_len;
211
212 if (!conn->postgresql) {
213 luaL_error(L, DBI_ERR_DB_UNAVAILABLE);
214 }
215
216 quoted_len = PQescapeStringConn(conn->postgresql, to, from, len, &err);
217
218 if (err) {
219 free(to);
220
221 luaL_error(L, DBI_ERR_QUOTING_STR, PQerrorMessage(conn->postgresql));
222 }
223
224 lua_pushlstring(L, to, quoted_len);
225 free(to);
226
227 return 1;
228 }
229
230 /*
202 * success = connection:rollback() 231 * success = connection:rollback()
203 */ 232 */
204 static int connection_rollback(lua_State *L) { 233 static int connection_rollback(lua_State *L) {
205 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION); 234 connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_POSTGRESQL_CONNECTION);
206 int err = 0; 235 int err = 0;
233 {"autocommit", connection_autocommit}, 262 {"autocommit", connection_autocommit},
234 {"close", connection_close}, 263 {"close", connection_close},
235 {"commit", connection_commit}, 264 {"commit", connection_commit},
236 {"ping", connection_ping}, 265 {"ping", connection_ping},
237 {"prepare", connection_prepare}, 266 {"prepare", connection_prepare},
267 {"quote", connection_quote},
238 {"rollback", connection_rollback}, 268 {"rollback", connection_rollback},
239 {NULL, NULL} 269 {NULL, NULL}
240 }; 270 };
241 271
242 static const luaL_Reg connection_class_methods[] = { 272 static const luaL_Reg connection_class_methods[] = {

mercurial