Skip to content

Commit 615a031

Browse files
committed
new connection.dec_cast option ('s' - string, 'n' - number, default is 'n')
1 parent d462bbd commit 615a031

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

pg/driver.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ lua_push_error(struct lua_State *L)
9797
* Parse pg values to lua
9898
*/
9999
static int
100-
parse_pg_value(struct lua_State *L, PGresult *res, int row, int col)
100+
parse_pg_value(struct lua_State *L, char dec_cast, PGresult *res, int row, int col)
101101
{
102102
if (PQgetisnull(res, row, col))
103103
return false;
@@ -107,6 +107,14 @@ parse_pg_value(struct lua_State *L, PGresult *res, int row, int col)
107107
int len = PQgetlength(res, row, col);
108108

109109
switch (PQftype(res, col)) {
110+
case NUMERICOID: {
111+
if (dec_cast == 's')
112+
{
113+
lua_pushlstring(L, val, len);
114+
break;
115+
}
116+
// else fallthrough
117+
}
110118
case INT2OID:
111119
case INT4OID: {
112120
lua_pushlstring(L, val, len);
@@ -126,7 +134,6 @@ parse_pg_value(struct lua_State *L, PGresult *res, int row, int col)
126134
else
127135
lua_pushboolean(L, 0);
128136
break;
129-
//case NUMERICOID: // leave it as is (otherwise cast to decimal)
130137
default:
131138
lua_pushlstring(L, val, len);
132139
}
@@ -141,14 +148,15 @@ static int
141148
safe_pg_parsetuples(struct lua_State *L)
142149
{
143150
PGresult *res = (PGresult *)lua_topointer(L, 1);
151+
const char dec_cast = (char)lua_tointeger(L, 2);
144152
int row, rows = PQntuples(res);
145153
int col, cols = PQnfields(res);
146154
lua_newtable(L);
147155
for (row = 0; row < rows; ++row) {
148156
lua_pushnumber(L, row + 1);
149157
lua_newtable(L);
150158
for (col = 0; col < cols; ++col)
151-
parse_pg_value(L, res, row, col);
159+
parse_pg_value(L, dec_cast, res, row, col);
152160
lua_settable(L, -3);
153161
}
154162
return 1;
@@ -205,7 +213,7 @@ pg_wait_for_result(PGconn *conn)
205213
* Appends result fom postgres to lua table
206214
*/
207215
static int
208-
pg_resultget(struct lua_State *L, PGconn *conn, int *res_no, int status_ok)
216+
pg_resultget(struct lua_State *L, const char dec_cast, PGconn *conn, int *res_no, int status_ok)
209217
{
210218
int wait_res = pg_wait_for_result(conn);
211219
if (wait_res != 1)
@@ -235,7 +243,8 @@ pg_resultget(struct lua_State *L, PGconn *conn, int *res_no, int status_ok)
235243
lua_pushinteger(L, (*res_no)++);
236244
lua_pushcfunction(L, safe_pg_parsetuples);
237245
lua_pushlightuserdata(L, pg_res);
238-
fail = lua_pcall(L, 1, 1, 0);
246+
lua_pushinteger(L, dec_cast);
247+
fail = lua_pcall(L, 2, 1, 0);
239248
if (!fail)
240249
lua_settable(L, -3);
241250
case PGRES_COMMAND_OK:
@@ -328,12 +337,20 @@ static int
328337
lua_pg_execute(struct lua_State *L)
329338
{
330339
PGconn *conn = lua_check_pgconn(L, 1);
331-
if (!lua_isstring(L, 2)) {
340+
341+
char dec_cast = 'n';
342+
if (lua_isstring(L, 2)) {
343+
const char *tmp = lua_tostring(L, 2);
344+
if (*tmp == 'n' || *tmp == 's') // TODO 'd' - decimal
345+
dec_cast = *tmp;
346+
}
347+
348+
if (!lua_isstring(L, 3)) {
332349
safe_pushstring(L, "Second param should be a sql command");
333350
return lua_push_error(L);
334351
}
335-
const char *sql = lua_tostring(L, 2);
336-
int paramCount = lua_gettop(L) - 2;
352+
const char *sql = lua_tostring(L, 3);
353+
int paramCount = lua_gettop(L) - 3;
337354

338355
const char **paramValues = NULL;
339356
int *paramLengths = NULL;
@@ -354,7 +371,7 @@ lua_pg_execute(struct lua_State *L)
354371

355372
int idx;
356373
for (idx = 0; idx < paramCount; ++idx) {
357-
lua_parse_param(L, idx + 3, paramValues + idx,
374+
lua_parse_param(L, idx + 4, paramValues + idx,
358375
paramLengths + idx, paramTypes + idx);
359376
}
360377
res = PQsendQueryParams(conn, sql, paramCount, paramTypes,
@@ -373,7 +390,7 @@ lua_pg_execute(struct lua_State *L)
373390

374391
int res_no = 1;
375392
int status_ok = 1;
376-
while ((status_ok = pg_resultget(L, conn, &res_no, status_ok)));
393+
while ((status_ok = pg_resultget(L, dec_cast, conn, &res_no, status_ok)));
377394

378395
return 2;
379396
}

pg/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local function conn_create(pg_conn)
1515
usable = true,
1616
conn = pg_conn,
1717
queue = queue,
18+
dec_cast = 'n' -- 'n' - number, 's' - string
1819
}, conn_mt)
1920

2021
return conn
@@ -60,7 +61,7 @@ conn_mt = {
6061
self.queue:put(false)
6162
return get_error(self.raise.pool, 'Connection is broken')
6263
end
63-
local status, datas = self.conn:execute(sql, ...)
64+
local status, datas = self.conn:execute(self.dec_cast, sql, ...)
6465
if status ~= 0 then
6566
self.queue:put(status > 0)
6667
return error(datas)

0 commit comments

Comments
 (0)