%}
+/* Lua function typemap */
+%typemap(in,checkfn="lua_isfunction") SWIGLUA_FN
+%{ $1.L=L; $1.idx=$input; %}
+
%ignore SwitchToMempool;
%newobject EventConsumer::pop;
%newobject CoreSession;
%newobject Event;
%newobject Stream;
+%newobject Dbh;
/**
* tell swig to grok everything defined in these header files and
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);
+};
+}
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;
+}
#include "mod_lua_extra.h"
}
#include <switch_cpp.h>
+
+
+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:
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
---- 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:
};
+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;