It will be very usefull if you would add support binary data to retrieve, for instance, number values.
I found the code below useful for me.
static int
conn_exec_binary(lua_State *L)
{
PGresult **res;
res = lua_newuserdata(L, sizeof(PGresult *));
*res = PQexecParams(pgsql_conn(L, 1), luaL_checkstring(L, 2),
0, /* one param */
NULL, /* int8[] OID */
NULL,
NULL,
NULL,
1); /* ask for binary results */
luaL_getmetatable(L, RES_METATABLE);
lua_setmetatable(L, -2);
return 1;
}
{ "exec", conn_exec },
{ "exec_binary", conn_exec_binary },
{ "getvalue_binary", res_getvalue_binary },
uint64_t
ntoh64(const uint64_t *input)
{
uint64_t rval;
uint8_t *data = (uint8_t *)&rval;
data[0] = *input >> 56;
data[1] = *input >> 48;
data[2] = *input >> 40;
data[3] = *input >> 32;
data[4] = *input >> 24;
data[5] = *input >> 16;
data[6] = *input >> 8;
data[7] = *input >> 0;
return rval;
}
uint64_t
hton64(const uint64_t *input)
{
return (ntoh64(input));
}
static int
res_getvalue_binary(lua_State *L)
{
lua_pushnumber(L, ntoh64(((uint64_t *)
PQgetvalue(*(PGresult **)luaL_checkudata(L, 1, RES_METATABLE),
luaL_checkinteger(L, 2) - 1, luaL_checkinteger(L, 3) - 1))));
return 1;
}
local = sql = string.format(" SELECT d1, d2, type FROM ddates WHERE d1 < %s ORDER BY id DESC LIMIT 1 " , tostring(os.time() * 1000))
res = conn:exec_binary(sql);
local t = res:getvalue_binary(1,1);
For small amount of rows it doesn't matter to use binary of text data. But for a lot of tons of content it can give emprovment.