@@ -97,7 +97,7 @@ lua_push_error(struct lua_State *L)
97
97
* Parse pg values to lua
98
98
*/
99
99
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 )
101
101
{
102
102
if (PQgetisnull (res , row , col ))
103
103
return false;
@@ -107,6 +107,14 @@ parse_pg_value(struct lua_State *L, PGresult *res, int row, int col)
107
107
int len = PQgetlength (res , row , col );
108
108
109
109
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
+ }
110
118
case INT2OID :
111
119
case INT4OID : {
112
120
lua_pushlstring (L , val , len );
@@ -126,7 +134,6 @@ parse_pg_value(struct lua_State *L, PGresult *res, int row, int col)
126
134
else
127
135
lua_pushboolean (L , 0 );
128
136
break ;
129
- //case NUMERICOID: // leave it as is (otherwise cast to decimal)
130
137
default :
131
138
lua_pushlstring (L , val , len );
132
139
}
@@ -141,14 +148,15 @@ static int
141
148
safe_pg_parsetuples (struct lua_State * L )
142
149
{
143
150
PGresult * res = (PGresult * )lua_topointer (L , 1 );
151
+ const char dec_cast = (char )lua_tointeger (L , 2 );
144
152
int row , rows = PQntuples (res );
145
153
int col , cols = PQnfields (res );
146
154
lua_newtable (L );
147
155
for (row = 0 ; row < rows ; ++ row ) {
148
156
lua_pushnumber (L , row + 1 );
149
157
lua_newtable (L );
150
158
for (col = 0 ; col < cols ; ++ col )
151
- parse_pg_value (L , res , row , col );
159
+ parse_pg_value (L , dec_cast , res , row , col );
152
160
lua_settable (L , -3 );
153
161
}
154
162
return 1 ;
@@ -205,7 +213,7 @@ pg_wait_for_result(PGconn *conn)
205
213
* Appends result fom postgres to lua table
206
214
*/
207
215
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 )
209
217
{
210
218
int wait_res = pg_wait_for_result (conn );
211
219
if (wait_res != 1 )
@@ -235,7 +243,8 @@ pg_resultget(struct lua_State *L, PGconn *conn, int *res_no, int status_ok)
235
243
lua_pushinteger (L , (* res_no )++ );
236
244
lua_pushcfunction (L , safe_pg_parsetuples );
237
245
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 );
239
248
if (!fail )
240
249
lua_settable (L , -3 );
241
250
case PGRES_COMMAND_OK :
@@ -328,12 +337,20 @@ static int
328
337
lua_pg_execute (struct lua_State * L )
329
338
{
330
339
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 )) {
332
349
safe_pushstring (L , "Second param should be a sql command" );
333
350
return lua_push_error (L );
334
351
}
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 ;
337
354
338
355
const char * * paramValues = NULL ;
339
356
int * paramLengths = NULL ;
@@ -354,7 +371,7 @@ lua_pg_execute(struct lua_State *L)
354
371
355
372
int idx ;
356
373
for (idx = 0 ; idx < paramCount ; ++ idx ) {
357
- lua_parse_param (L , idx + 3 , paramValues + idx ,
374
+ lua_parse_param (L , idx + 4 , paramValues + idx ,
358
375
paramLengths + idx , paramTypes + idx );
359
376
}
360
377
res = PQsendQueryParams (conn , sql , paramCount , paramTypes ,
@@ -373,7 +390,7 @@ lua_pg_execute(struct lua_State *L)
373
390
374
391
int res_no = 1 ;
375
392
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 )));
377
394
378
395
return 2 ;
379
396
}
0 commit comments