Skip to content
Snippets Groups Projects
Commit 60ce25c8 authored by Dmitry E. Oboukhov's avatar Dmitry E. Oboukhov
Browse files

:quote methods for Pg

parent ac297107
No related branches found
No related tags found
No related merge requests found
......@@ -169,6 +169,11 @@ box.net.sql = {
-- quote variable
quote = function(self, variable)
return self.raw:quote(variable)
end,
-- quote identifier
quote_ident = function(self, variable)
return self.raw:quote_ident(variable)
end
}
......
......@@ -296,6 +296,54 @@ self_field(struct lua_State *L, const char *name, int index)
}
/**
* quote variable
*/
static int
lua_pg_quote(struct lua_State *L)
{
if (lua_gettop(L) < 2) {
lua_pushnil(L);
return 1;
}
PGconn *conn = lua_check_pgconn(L, 1);
size_t len;
const char *s = lua_tolstring(L, -1, &len);
s = PQescapeLiteral(conn, s, len);
if (!s)
luaL_error(L, "Can't allocate memory");
lua_pushstring(L, s);
free((void *)s);
return 1;
}
/**
* quote identifier
*/
static int
lua_pg_quote_ident(struct lua_State *L)
{
if (lua_gettop(L) < 2) {
lua_pushnil(L);
return 1;
}
PGconn *conn = lua_check_pgconn(L, 1);
size_t len;
const char *s = lua_tolstring(L, -1, &len);
s = PQescapeIdentifier(conn, s, len);
if (!s)
luaL_error(L, "Can't allocate memory");
lua_pushstring(L, s);
free((void *)s);
return 1;
}
/**
* connect to postgresql
*/
......@@ -352,7 +400,9 @@ lbox_net_pg_connect(struct lua_State *L)
lua_newtable(L);
static const struct luaL_reg meta [] = {
{"execute", lua_pg_execute},
{"execute", lua_pg_execute},
{"quote", lua_pg_quote},
{"quote_ident", lua_pg_quote_ident},
{NULL, NULL}
};
luaL_register(L, NULL, meta);
......
......@@ -89,3 +89,7 @@ lua c = box.net.sql.connect('abcd')
---
error: '[string "-- sql.lua (internal file)..."]:61: Unknown driver ''abcd'''
...
lua c:quote('abc"cde"def')
---
- 'abc"cde"def'
...
......@@ -28,3 +28,5 @@ exec admin "lua c:execute('SELEC T')"
exec admin "lua c = box.net.sql.connect('abcd')"
exec admin "lua c:quote('abc\"cde\"def')"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment