]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
MODLANG-174
authorAnthony Minessale <anthm@freeswitch.org>
Tue, 7 Sep 2010 16:21:25 +0000 (11:21 -0500)
committerAnthony Minessale <anthm@freeswitch.org>
Tue, 7 Sep 2010 16:21:25 +0000 (11:21 -0500)
src/mod/languages/mod_lua/freeswitch.i
src/mod/languages/mod_lua/freeswitch_lua.cpp
src/mod/languages/mod_lua/freeswitch_lua.h
src/mod/languages/mod_lua/hack.diff
src/mod/languages/mod_lua/my_swigable_cpp.h

index 3631aca62ffe3d546faa4d5be8ea08c837bce953..383580103f157614439f52aaa87a84f43a8f54aa 100644 (file)
 %}
 
 
+/* 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);
+};
 
+}
 
index c73c88c553afdd8409f0a21fc8eb2f1b5d1f96a9..17d12bc1cbe7e1cc421588ff9c93a9e1e5048f74 100644 (file)
@@ -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;
+}
index a1f4a85ce3019592f1be5739341c177d1d12c367..a0780b1537588b22a01bf08792ddb0bda5dace61 100644 (file)
@@ -8,6 +8,16 @@ extern "C" {
 #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:
@@ -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
index afd37b086dd9a36da76ab6dccd992cf601a6dc8f..fc33ddb9d6e1000f207d67c384a6f9f40e00fbc4 100644 (file)
@@ -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:
index d4d3168a63b8939103a7660cf8c324dd3f6e8775..ebe597260f219bd86dcc25b1957c7115a514e21e 100644 (file)
@@ -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;