summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-23 09:41:12 +0000
committerAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2013-08-23 11:14:06 +0000
commitc9ad4f578310a5242548a691739adf53f59445df (patch)
treea2c909968f833b10350dc81947f4c5aedcede800
parentfc821cda70e3bd2677ad68a62b0b2b0698c189d4 (diff)
downloadlem-postgres-c9ad4f578310a5242548a691739adf53f59445df.tar.gz
lem-postgres-c9ad4f578310a5242548a691739adf53f59445df.tar.xz
lem-postgres-c9ad4f578310a5242548a691739adf53f59445df.zip
Add support for nil/NULL in out of band query parameters
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
-rw-r--r--lem/postgres.c37
1 files changed, 25 insertions, 12 deletions
diff --git a/lem/postgres.c b/lem/postgres.c
index da31a85..e944508 100644
--- a/lem/postgres.c
+++ b/lem/postgres.c
@@ -1,6 +1,7 @@
/*
* This file is part of lem-postgres.
* Copyright 2011 Emil Renner Berthing
+ * Copyright 2013 Asbjørn Sloth Tønnesen
*
* lem-postgres is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@@ -490,12 +491,18 @@ db_exec(lua_State *T)
for (i = 0; i < n; i++) {
size_t len;
- const char *val = lua_tolstring(T, i+3, &len);
-
- if (val == NULL) {
- free(values);
- free(lengths);
- return luaL_argerror(T, i+3, "expected string");
+ 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;
@@ -575,12 +582,18 @@ db_run(lua_State *T)
for (i = 0; i < n; i++) {
size_t len;
- const char *val = lua_tolstring(T, i+3, &len);
-
- if (val == NULL) {
- free(values);
- free(lengths);
- return luaL_argerror(T, i+3, "expected string");
+ 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;