From c9ad4f578310a5242548a691739adf53f59445df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Fri, 23 Aug 2013 09:41:12 +0000 Subject: Add support for nil/NULL in out of band query parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- lem/postgres.c | 37 +++++++++++++++++++++++++------------ 1 file 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; -- cgit v1.2.1