From 26f2e095efbc9b5aee04906dd5983858e8c29eb8 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 7 Sep 2010 11:21:25 -0500 Subject: [PATCH] MODLANG-174 --- src/mod/languages/mod_lua/freeswitch.i | 22 +++++-- src/mod/languages/mod_lua/freeswitch_lua.cpp | 66 ++++++++++++++++++++ src/mod/languages/mod_lua/freeswitch_lua.h | 22 +++++++ src/mod/languages/mod_lua/hack.diff | 28 ++++----- src/mod/languages/mod_lua/my_swigable_cpp.h | 13 ++++ 5 files changed, 133 insertions(+), 18 deletions(-) diff --git a/src/mod/languages/mod_lua/freeswitch.i b/src/mod/languages/mod_lua/freeswitch.i index 3631aca62f..383580103f 100644 --- a/src/mod/languages/mod_lua/freeswitch.i +++ b/src/mod/languages/mod_lua/freeswitch.i @@ -18,6 +18,10 @@ %} +/* Lua function typemap */ +%typemap(in,checkfn="lua_isfunction") SWIGLUA_FN +%{ $1.L=L; $1.idx=$input; %} + %ignore SwitchToMempool; %newobject EventConsumer::pop; @@ -25,6 +29,7 @@ %newobject CoreSession; %newobject Event; %newobject Stream; +%newobject Dbh; /** * tell swig to grok everything defined in these header files and @@ -66,9 +71,18 @@ class Session : public CoreSession { void setLUA(lua_State *state); }; + +class Dbh { + private: + switch_cache_db_handle_t *dbh; + bool connected; + static int query_callback(void *pArg, int argc, char **argv, char **cargv); + public: + Dbh(char *dsn, char *user = NULL, char *pass = NULL); + ~Dbh(); + bool release(); + bool query(char *sql, SWIGLUA_FN lua_fun); +}; + } - - - - diff --git a/src/mod/languages/mod_lua/freeswitch_lua.cpp b/src/mod/languages/mod_lua/freeswitch_lua.cpp index c73c88c553..17d12bc1cb 100644 --- a/src/mod/languages/mod_lua/freeswitch_lua.cpp +++ b/src/mod/languages/mod_lua/freeswitch_lua.cpp @@ -308,3 +308,69 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp return SWITCH_STATUS_SUCCESS; } + +Dbh::Dbh(char *dsn, char *user, char *pass) +{ + switch_cache_db_connection_options_t options = { {0} }; + + options.odbc_options.dsn = dsn; + options.odbc_options.user = user; + options.odbc_options.pass = pass; + + if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) == SWITCH_STATUS_SUCCESS) { + connected = true; + } else { + connected = false; + } +} + +Dbh::~Dbh() +{ + release(); +} + +bool Dbh::release() +{ + if (connected) { + switch_cache_db_release_db_handle(&dbh); + connected = false; + return true; + } + return false; +} + +int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv) +{ + SWIGLUA_FN *lua_fun = (SWIGLUA_FN *)pArg; + + lua_pushvalue(lua_fun->L, lua_fun->idx); /* get the lua callback function onto the stack */ + + lua_newtable(lua_fun->L); /* push a row (table) */ + + for (int i = 0; i < argc; i++) { + lua_pushstring(lua_fun->L, switch_str_nil(cargv[i])); + lua_pushstring(lua_fun->L, switch_str_nil(argv[i])); + lua_settable(lua_fun->L, -3); + } + + lua_call(lua_fun->L, 1, 1); /* 1 in, 1 out */ + + if (lua_isnumber(lua_fun->L, -1)) { + if (lua_tonumber(lua_fun->L, -1) != 0) { + return 1; + } + } + + return 0; /* 0 to continue with next row */ +} + +bool Dbh::query(char *sql, SWIGLUA_FN lua_fun) +{ + if (connected) { + if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) { + return true; + } + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "no workie workie :(\n"); + return false; +} diff --git a/src/mod/languages/mod_lua/freeswitch_lua.h b/src/mod/languages/mod_lua/freeswitch_lua.h index a1f4a85ce3..a0780b1537 100644 --- a/src/mod/languages/mod_lua/freeswitch_lua.h +++ b/src/mod/languages/mod_lua/freeswitch_lua.h @@ -8,6 +8,16 @@ extern "C" { #include "mod_lua_extra.h" } #include + + +typedef struct{ + lua_State* L; + int idx; +}SWIGLUA_FN; + +#define SWIGLUA_FN_GET(fn) {lua_pushvalue(fn.L,fn.idx);} + + namespace LUA { class Session:public CoreSession { private: @@ -41,5 +51,17 @@ namespace LUA { void setLUA(lua_State * state); }; + + class Dbh { + protected: + switch_cache_db_handle_t *dbh; + bool connected; + static int query_callback(void *pArg, int argc, char **argv, char **cargv); + public: + Dbh(char *dsn, char *user = NULL, char *pass = NULL); + ~Dbh(); + bool release(); + bool query(char *sql, SWIGLUA_FN lua_fun); + }; } #endif diff --git a/src/mod/languages/mod_lua/hack.diff b/src/mod/languages/mod_lua/hack.diff index afd37b086d..fc33ddb9d6 100644 --- a/src/mod/languages/mod_lua/hack.diff +++ b/src/mod/languages/mod_lua/hack.diff @@ -1,38 +1,38 @@ ---- mod_lua_wrap.cpp 2008-07-16 16:58:58.000000000 -0400 -+++ old.cpp 2008-07-16 16:58:42.000000000 -0400 -@@ -6731,7 +6731,7 @@ - SWIG_check_num_args("LUA::Session",0,0) +--- mod_lua_wrap.cpp.orig 2010-09-05 16:39:26.000000000 +0200 ++++ mod_lua_wrap.cpp 2010-09-05 16:39:44.000000000 +0200 +@@ -4913,7 +4913,7 @@ + result = (LUA::Session *)new LUA::Session(); SWIG_arg=0; - SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; + SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; result->setLUA(L); return SWIG_arg; - if(0) SWIG_fail; -@@ -6759,7 +6759,7 @@ - + fail: +@@ -4934,7 +4934,7 @@ + arg2=(CoreSession *)SWIG_MustGetPtr(L,2,SWIGTYPE_p_CoreSession,0,2,"new_Session"); result = (LUA::Session *)new LUA::Session(arg1,arg2); SWIG_arg=0; - SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; + SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; result->setLUA(L); return SWIG_arg; - if(0) SWIG_fail; -@@ -6780,7 +6780,7 @@ - arg1 = (char *)lua_tostring(L, 1); + fail: +@@ -4952,7 +4952,7 @@ + arg1 = (char*)lua_tostring(L, 1); result = (LUA::Session *)new LUA::Session(arg1); SWIG_arg=0; - SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; + SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; result->setLUA(L); return SWIG_arg; - if(0) SWIG_fail; -@@ -6805,7 +6805,7 @@ - + fail: +@@ -4970,7 +4970,7 @@ + arg1=(switch_core_session_t *)SWIG_MustGetPtr(L,1,SWIGTYPE_p_switch_core_session_t,0,1,"new_Session"); result = (LUA::Session *)new LUA::Session(arg1); SWIG_arg=0; - SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; + SWIG_NewPointerObj(L,result,SWIGTYPE_p_LUA__Session,1); SWIG_arg++; result->setLUA(L); return SWIG_arg; - if(0) SWIG_fail; + fail: diff --git a/src/mod/languages/mod_lua/my_swigable_cpp.h b/src/mod/languages/mod_lua/my_swigable_cpp.h index d4d3168a63..ebe597260f 100644 --- a/src/mod/languages/mod_lua/my_swigable_cpp.h +++ b/src/mod/languages/mod_lua/my_swigable_cpp.h @@ -49,6 +49,19 @@ class Event { }; +class Dbh { + protected: + switch_cache_db_handle_t *dbh; + bool connected; + static int query_callback(void *pArg, int argc, char **argv, char **cargv); + public: + Dbh(char *dsn, char *user = NULL, char *pass = NULL); + ~Dbh(); + bool release(); + bool query(char *sql, SWIGLUA_FN lua_fun); +}; + + class CoreSession { protected: switch_input_args_t args;