Skip to content

Commit 53abb53

Browse files
authored
Merge pull request #2375 from ar45/mod_lua_query_rows
[mod_lua] Add Dbh:query_rows
2 parents ad5fe8b + 7d2befa commit 53abb53

File tree

5 files changed

+128
-23
lines changed

5 files changed

+128
-23
lines changed

src/mod/languages/mod_lua/freeswitch.i

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
%include "typemaps.i"
6262
%apply int *OUTPUT { int *len };
6363

64+
%typemap(out) DbhQueryRowsReturn {
65+
SWIG_arg += result;
66+
}
67+
68+
6469
/**
6570
* tell swig to grok everything defined in these header files and
6671
* build all sorts of c wrappers and lua shadows of the c wrappers.
@@ -115,6 +120,7 @@ class Dbh {
115120
bool connected();
116121
bool test_reactive(char *test_sql, char *drop_sql = NULL, char *reactive_sql = NULL);
117122
bool query(char *sql, SWIGLUA_FN lua_fun);
123+
DbhQueryRowsReturn query_rows(lua_State* L, char *sql);
118124
int affected_rows();
119125
char *last_error();
120126
void clear_error();

src/mod/languages/mod_lua/freeswitch_lua.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,62 @@ bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
482482
return false;
483483
}
484484

485+
struct query_callback_data {
486+
lua_State *L;
487+
int stack_index;
488+
int *row_num;
489+
};
490+
491+
int query2_callback(void *pArg, int argc, char **argv, char **columnNames)
492+
{
493+
struct query_callback_data *data = (struct query_callback_data *) pArg;
494+
lua_State *tL = data->L;
495+
lua_createtable(tL, 0, argc);
496+
for (int i = 0; i < argc; i++) {
497+
lua_pushstring(tL, argv[i]);
498+
lua_setfield(tL, -2, switch_str_nil(columnNames[i]));
499+
}
500+
lua_rawseti(tL, data->stack_index + 2, (*data->row_num)++);
501+
return 0;
502+
}
503+
504+
DbhQueryRowsReturn Dbh::query_rows(lua_State* L, char *sql)
505+
{
506+
int stack_index = lua_gettop(L);
507+
clear_error();
508+
lua_pushboolean(L, 0); // result success error: stack_index + 1
509+
lua_newtable(L); // the rows: stack_index + 2
510+
lua_pushnil(L); // error message if any: stack_index + 3
511+
512+
if (zstr(sql)) {
513+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing SQL query.\n");
514+
lua_pushstring(L, "Missing SQL query.");
515+
lua_replace(L, stack_index + 3);
516+
return 3;
517+
}
518+
519+
if (dbh) {
520+
int index = 1;
521+
struct query_callback_data pData = {L, stack_index, &index};
522+
523+
if (switch_cache_db_execute_sql_callback(dbh, sql, query2_callback, &pData, &err) == SWITCH_STATUS_SUCCESS) {
524+
// no errors
525+
lua_pushboolean(L, 1);
526+
lua_replace(L, stack_index + 1);
527+
} else {
528+
lua_pushstring(L, !zstr(err) ? err : "Failed to execute sql query");
529+
lua_replace(L, stack_index + 3);
530+
}
531+
} else {
532+
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
533+
lua_pushstring(L, "DBH NOT Connected.");
534+
lua_replace(L, stack_index + 3);
535+
}
536+
537+
return 3;
538+
}
539+
540+
485541
int Dbh::affected_rows()
486542
{
487543
if (dbh) {

src/mod/languages/mod_lua/freeswitch_lua.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct{
2828

2929
#define SWIGLUA_TABLE_GET(fn) {lua_pushvalue(fn.L,fn.idx);}
3030

31+
typedef int DbhQueryRowsReturn;
3132

3233
namespace LUA {
3334
class Session:public CoreSession {
@@ -76,6 +77,7 @@ namespace LUA {
7677
bool connected();
7778
bool test_reactive(char *test_sql, char *drop_sql = NULL, char *reactive_sql = NULL);
7879
bool query(char *sql, SWIGLUA_FN lua_fun);
80+
DbhQueryRowsReturn query_rows(lua_State* L, char *sql);
7981
int affected_rows();
8082
char *last_error();
8183
void clear_error();

src/mod/languages/mod_lua/hack.diff

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
--- mod_lua_wrap.cpp.old 2015-06-16 12:27:19.024000000 -0500
2-
+++ mod_lua_wrap.cpp 2015-06-16 12:34:51.540000000 -0500
3-
@@ -4242,7 +4242,7 @@ static int _wrap_Stream_read(lua_State* L) {
1+
--- mod_lua_wrap.cpp.old 2025-01-15 13:22:48.705853645 +0000
2+
+++ mod_lua_wrap.cpp 2025-01-15 13:23:33.161847705 +0000
3+
@@ -4242,7 +4242,7 @@
44
}
55

66
result = (char *)(arg1)->read(arg2);
@@ -9,7 +9,7 @@
99
lua_pushnumber(L, (lua_Number) *arg2); SWIG_arg++;
1010
return SWIG_arg;
1111

12-
@@ -8304,7 +8304,7 @@ static int _wrap_new_Session__SWIG_0(lua_State* L) {
12+
@@ -8336,7 +8336,7 @@
1313

1414
SWIG_check_num_args("LUA::Session::Session",0,0)
1515
result = (LUA::Session *)new LUA::Session();
@@ -18,7 +18,7 @@
1818
return SWIG_arg;
1919

2020
if(0) SWIG_fail;
21-
@@ -8331,7 +8331,7 @@ static int _wrap_new_Session__SWIG_1(lua_State* L) {
21+
@@ -8363,7 +8363,7 @@
2222
}
2323

2424
result = (LUA::Session *)new LUA::Session(arg1,arg2);
@@ -27,7 +27,7 @@
2727
return SWIG_arg;
2828

2929
if(0) SWIG_fail;
30-
@@ -8351,7 +8351,7 @@ static int _wrap_new_Session__SWIG_2(lua_State* L) {
30+
@@ -8383,7 +8383,7 @@
3131
if(!SWIG_lua_isnilstring(L,1)) SWIG_fail_arg("LUA::Session::Session",1,"char *");
3232
arg1 = (char *)lua_tostring(L, 1);
3333
result = (LUA::Session *)new LUA::Session(arg1);
@@ -36,7 +36,7 @@
3636
return SWIG_arg;
3737

3838
if(0) SWIG_fail;
39-
@@ -8375,7 +8375,7 @@ static int _wrap_new_Session__SWIG_3(lua_State* L) {
39+
@@ -8407,7 +8407,7 @@
4040
}
4141

4242
result = (LUA::Session *)new LUA::Session(arg1);
@@ -45,39 +45,47 @@
4545
return SWIG_arg;
4646

4747
if(0) SWIG_fail;
48-
@@ -9485,6 +9485,7 @@ static int _wrap_Dbh_test_reactive__SWIG_0(lua_State* L) {
48+
@@ -9517,6 +9517,7 @@
4949
arg2 = (char *)lua_tostring(L, 2);
5050
arg3 = (char *)lua_tostring(L, 3);
5151
arg4 = (char *)lua_tostring(L, 4);
5252
+ switch_assert(arg1);
5353
result = (bool)(arg1)->test_reactive(arg2,arg3,arg4);
5454
lua_pushboolean(L,(int)(result!=0)); SWIG_arg++;
5555
return SWIG_arg;
56-
@@ -9516,6 +9516,7 @@ static int _wrap_Dbh_test_reactive__SWIG_1(lua_State* L) {
56+
@@ -9547,6 +9548,7 @@
5757

5858
arg2 = (char *)lua_tostring(L, 2);
5959
arg3 = (char *)lua_tostring(L, 3);
6060
+ switch_assert(arg1);
6161
result = (bool)(arg1)->test_reactive(arg2,arg3);
6262
lua_pushboolean(L,(int)(result!=0)); SWIG_arg++;
6363
return SWIG_arg;
64-
@@ -9543,6 +9544,7 @@ static int _wrap_Dbh_test_reactive__SWIG_2(lua_State* L) {
64+
@@ -9574,6 +9576,7 @@
6565
}
6666

6767
arg2 = (char *)lua_tostring(L, 2);
6868
+ switch_assert(arg1);
6969
result = (bool)(arg1)->test_reactive(arg2);
7070
lua_pushboolean(L,(int)(result!=0)); SWIG_arg++;
7171
return SWIG_arg;
72-
@@ -9672,6 +9673,7 @@ static int _wrap_Dbh_query(lua_State* L) {
72+
@@ -9704,6 +9707,7 @@
7373
(&arg3)->idx = 3;
7474
}
7575
}
7676
+ switch_assert(arg1);
7777
result = (bool)(arg1)->query(arg2,arg3);
7878
lua_pushboolean(L,(int)(result!=0)); SWIG_arg++;
7979
return SWIG_arg;
80-
@@ -9695,7 +9697,7 @@ static int _wrap_Dbh_affected_rows(lua_State* L) {
80+
@@ -9733,6 +9737,7 @@
81+
}
82+
83+
arg3 = (char *)lua_tostring(L, 2);
84+
+ switch_assert(arg1);
85+
result = (arg1)->query_rows(arg2,arg3);
86+
{
87+
SWIG_arg += result;
88+
@@ -9758,7 +9763,7 @@
8189
if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
8290
SWIG_fail_ptr("Dbh_affected_rows",1,SWIGTYPE_p_LUA__Dbh);
8391
}
@@ -86,7 +94,7 @@
8694
result = (int)(arg1)->affected_rows();
8795
lua_pushnumber(L, (lua_Number) result); SWIG_arg++;
8896
return SWIG_arg;
89-
@@ -9719,7 +9721,7 @@ static int _wrap_Dbh_last_error(lua_State* L) {
97+
@@ -9782,7 +9787,7 @@
9098
if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
9199
SWIG_fail_ptr("Dbh_last_error",1,SWIGTYPE_p_LUA__Dbh);
92100
}
@@ -95,7 +103,7 @@
95103
result = (char *)(arg1)->last_error();
96104
lua_pushstring(L,(const char *)result); SWIG_arg++;
97105
return SWIG_arg;
98-
@@ -9742,7 +9744,7 @@ static int _wrap_Dbh_clear_error(lua_State* L) {
106+
@@ -9805,7 +9810,7 @@
99107
if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
100108
SWIG_fail_ptr("Dbh_clear_error",1,SWIGTYPE_p_LUA__Dbh);
101109
}
@@ -104,71 +112,71 @@
104112
(arg1)->clear_error();
105113

106114
return SWIG_arg;
107-
@@ -9770,6 +9772,7 @@ static int _wrap_Dbh_load_extension(lua_State* L) {
115+
@@ -9833,6 +9838,7 @@
108116
}
109117

110118
arg2 = (char *)lua_tostring(L, 2);
111119
+ switch_assert(arg1);
112120
result = (int)(arg1)->load_extension((char const *)arg2);
113121
lua_pushnumber(L, (lua_Number) result); SWIG_arg++;
114122
return SWIG_arg;
115-
@@ -9869,6 +9872,7 @@ static int _wrap_JSON_decode(lua_State* L) {
123+
@@ -9933,6 +9939,7 @@
116124
}
117125

118126
arg2 = (char *)lua_tostring(L, 2);
119127
+ switch_assert(arg1);
120128
result = (cJSON *)(arg1)->decode((char const *)arg2);
121129
{
122130
SWIG_arg += LUA::JSON::cJSON2LuaTable(L, result);
123-
@@ -9902,6 +9906,7 @@ static int _wrap_JSON_encode(lua_State* L) {
131+
@@ -9966,6 +9973,7 @@
124132
(&arg2)->L = L;
125133
(&arg2)->idx = 2;
126134
}
127135
+ switch_assert(arg1);
128136
result = (arg1)->encode(arg2);
129137
lua_pushlstring(L,(&result)->data(),(&result)->size()); SWIG_arg++;
130138
return SWIG_arg;
131-
@@ -9929,6 +9934,7 @@ static int _wrap_JSON_execute__SWIG_0(lua_State* L) {
139+
@@ -9993,6 +10001,7 @@
132140
}
133141

134142
arg2 = (char *)lua_tostring(L, 2);
135143
+ switch_assert(arg1);
136144
result = (cJSON *)(arg1)->execute((char const *)arg2);
137145
{
138146
SWIG_arg += LUA::JSON::cJSON2LuaTable(L, result);
139-
@@ -9962,6 +9968,7 @@ static int _wrap_JSON_execute__SWIG_1(lua_State* L) {
147+
@@ -10026,6 +10035,7 @@
140148
(&arg2)->L = L;
141149
(&arg2)->idx = 2;
142150
}
143151
+ switch_assert(arg1);
144152
result = (cJSON *)(arg1)->execute(arg2);
145153
{
146154
SWIG_arg += LUA::JSON::cJSON2LuaTable(L, result);
147-
@@ -10046,6 +10053,7 @@ static int _wrap_JSON_execute2__SWIG_0(lua_State* L) {
155+
@@ -10110,6 +10120,7 @@
148156
}
149157

150158
arg2 = (char *)lua_tostring(L, 2);
151159
+ switch_assert(arg1);
152160
result = (arg1)->execute2((char const *)arg2);
153161
lua_pushlstring(L,(&result)->data(),(&result)->size()); SWIG_arg++;
154162
return SWIG_arg;
155-
@@ -10076,6 +10084,7 @@ static int _wrap_JSON_execute2__SWIG_1(lua_State* L) {
163+
@@ -10140,6 +10151,7 @@
156164
(&arg2)->L = L;
157165
(&arg2)->idx = 2;
158166
}
159167
+ switch_assert(arg1);
160168
result = (arg1)->execute2(arg2);
161169
lua_pushlstring(L,(&result)->data(),(&result)->size()); SWIG_arg++;
162170
return SWIG_arg;
163-
@@ -10156,6 +10165,7 @@ static int _wrap_JSON_encode_empty_table_as_object(lua_State* L) {
171+
@@ -10220,6 +10232,7 @@
164172
}
165173

166174
arg2 = (lua_toboolean(L, 2)!=0);
167175
+ switch_assert(arg1);
168176
(arg1)->encode_empty_table_as_object(arg2);
169177

170178
return SWIG_arg;
171-
@@ -10182,6 +10192,7 @@ static int _wrap_JSON_return_unformatted_json(lua_State* L) {
179+
@@ -10246,6 +10259,7 @@
172180
}
173181

174182
arg2 = (lua_toboolean(L, 2)!=0);

src/mod/languages/mod_lua/mod_lua_wrap.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9720,6 +9720,38 @@ static int _wrap_Dbh_query(lua_State* L) {
97209720
}
97219721

97229722

9723+
static int _wrap_Dbh_query_rows(lua_State* L) {
9724+
int SWIG_arg = 0;
9725+
LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
9726+
lua_State *arg2 = (lua_State *) 0 ;
9727+
char *arg3 = (char *) 0 ;
9728+
DbhQueryRowsReturn result;
9729+
9730+
arg2 = L;
9731+
SWIG_check_num_args("LUA::Dbh::query_rows",2,2)
9732+
if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("LUA::Dbh::query_rows",1,"LUA::Dbh *");
9733+
if(!SWIG_lua_isnilstring(L,2)) SWIG_fail_arg("LUA::Dbh::query_rows",2,"char *");
9734+
9735+
if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
9736+
SWIG_fail_ptr("Dbh_query_rows",1,SWIGTYPE_p_LUA__Dbh);
9737+
}
9738+
9739+
arg3 = (char *)lua_tostring(L, 2);
9740+
switch_assert(arg1);
9741+
result = (arg1)->query_rows(arg2,arg3);
9742+
{
9743+
SWIG_arg += result;
9744+
}
9745+
return SWIG_arg;
9746+
9747+
if(0) SWIG_fail;
9748+
9749+
fail:
9750+
lua_error(L);
9751+
return SWIG_arg;
9752+
}
9753+
9754+
97239755
static int _wrap_Dbh_affected_rows(lua_State* L) {
97249756
int SWIG_arg = 0;
97259757
LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
@@ -9839,6 +9871,7 @@ static swig_lua_method swig_Dbh_methods[]= {
98399871
{ "connected", _wrap_Dbh_connected},
98409872
{ "test_reactive", _wrap_Dbh_test_reactive},
98419873
{ "query", _wrap_Dbh_query},
9874+
{ "query_rows", _wrap_Dbh_query_rows},
98429875
{ "affected_rows", _wrap_Dbh_affected_rows},
98439876
{ "last_error", _wrap_Dbh_last_error},
98449877
{ "clear_error", _wrap_Dbh_clear_error},

0 commit comments

Comments
 (0)