summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-23 10:43:58 +0000
committerAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-23 11:14:11 +0000
commitb2a3f5508a52b87b9e556b30c26ab19fc0e4d591 (patch)
tree155885e532ed8b97fbf08f32a886acf6cdc943d5
parentc9ad4f578310a5242548a691739adf53f59445df (diff)
downloadlem-postgres-b2a3f5508a52b87b9e556b30c26ab19fc0e4d591.tar.gz
lem-postgres-b2a3f5508a52b87b9e556b30c26ab19fc0e4d591.tar.xz
lem-postgres-b2a3f5508a52b87b9e556b30c26ab19fc0e4d591.zip
Dedup out of band query parameter handlingfix-null-handling
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
-rw-r--r--lem/postgres.c69
1 files changed, 29 insertions, 40 deletions
diff --git a/lem/postgres.c b/lem/postgres.c
index e944508..7d1ec37 100644
--- a/lem/postgres.c
+++ b/lem/postgres.c
@@ -467,6 +467,33 @@ error:
}
}
+void
+prepare_params(lua_State *T, int n, const char **values, int *lengths)
+{
+ int i;
+
+ for (i = 0; i < n; i++) {
+ size_t len;
+ const char *val;
+
+ if (lua_isnil(T, i+3)) {
+ val = NULL;
+ /* len is ignored by libpq */
+ } else {
+ val = lua_tolstring(T, i+3, &len);
+ if (val == NULL) {
+ free(values);
+ free(lengths);
+ luaL_argerror(T, i+3, "expected nil or string");
+ /* unreachable */
+ }
+ }
+
+ values[i] = val;
+ lengths[i] = len;
+ }
+}
+
static int
db_exec(lua_State *T)
{
@@ -487,27 +514,8 @@ db_exec(lua_State *T)
if (n > 0) {
const char **values = lem_xmalloc(n * sizeof(char *));
int *lengths = lem_xmalloc(n * sizeof(int));
- int i;
-
- for (i = 0; i < n; i++) {
- size_t len;
- const char *val;
-
- if (lua_isnil(T, i+3)) {
- val = NULL;
- /* len is ignored by libpq */
- } else {
- val = lua_tolstring(T, i+3, &len);
- if (val == NULL) {
- free(values);
- free(lengths);
- return luaL_argerror(T, i+3, "expected nil or string");
- }
- }
- values[i] = val;
- lengths[i] = len;
- }
+ prepare_params(T, n, values, lengths);
n = PQsendQueryParams(d->conn, command, n,
NULL, values, lengths, NULL, 0);
@@ -578,27 +586,8 @@ db_run(lua_State *T)
if (n > 0) {
const char **values = lem_xmalloc(n * sizeof(char *));
int *lengths = lem_xmalloc(n * sizeof(int));
- int i;
-
- for (i = 0; i < n; i++) {
- size_t len;
- const char *val;
-
- if (lua_isnil(T, i+3)) {
- val = NULL;
- /* len is ignored by libpq */
- } else {
- val = lua_tolstring(T, i+3, &len);
- if (val == NULL) {
- free(values);
- free(lengths);
- return luaL_argerror(T, i+3, "expected nil or string");
- }
- }
- values[i] = val;
- lengths[i] = len;
- }
+ prepare_params(T, n, values, lengths);
n = PQsendQueryPrepared(d->conn, name, n,
values, lengths, NULL, 0);