]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-9741: lua expose db err str in freeeswitch.dbh
authorAron Podrigal <aronp@guaranteedplus.com>
Mon, 21 Nov 2016 17:03:31 +0000 (17:03 +0000)
committerAron Podrigal <aronp@guaranteedplus.com>
Mon, 21 Nov 2016 21:11:03 +0000 (21:11 +0000)
Added 2 methods to lua freeswitch.Dbh
`last_error()` Returns the error string from the last query or nil.
`clear_error()` clears the error string.

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/mod_lua_wrap.cpp

index a5c9d75a14f75563bb414615f5294843c3a0bc3f..4933bfefbcffe28fd5f14f7e8e603f3299eeae58 100644 (file)
@@ -90,6 +90,7 @@ class Session : public CoreSession {
 class Dbh {
   private:
     switch_cache_db_handle_t *dbh;
+    char *err;
     bool m_connected;
     static int query_callback(void *pArg, int argc, char **argv, char **cargv);
   public:
@@ -100,6 +101,8 @@ class Dbh {
     bool test_reactive(char *test_sql, char *drop_sql = NULL, char *reactive_sql = NULL);
     bool query(char *sql, SWIGLUA_FN lua_fun);
     int affected_rows();
+    char *last_error();
+    void clear_error();
     int load_extension(const char *extension);
 };
 
index e9196654019f63aff0fa630e52a7169e18ae1c6d..cc5705f90c163273dee65487c062e758503c8663 100644 (file)
@@ -354,6 +354,7 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp
 Dbh::Dbh(char *dsn, char *user, char *pass)
 {
        dbh = NULL;
+       err = NULL;
        char *tmp = NULL;
        
        if (!zstr(user) || !zstr(pass)) {
@@ -380,6 +381,18 @@ Dbh::Dbh(char *dsn, char *user, char *pass)
 Dbh::~Dbh()
 {
        if (dbh) release();
+
+       clear_error();
+}
+
+void Dbh::clear_error()
+{
+       switch_safe_free(err);
+}
+
+char *Dbh::last_error()
+{
+       return err;
 }
 
 bool Dbh::release()
@@ -446,6 +459,8 @@ int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
 
 bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
 {
+  clear_error();
+
   if (zstr(sql)) {
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing SQL query.\n");
     return false;
@@ -453,17 +468,17 @@ bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
 
   if (dbh) {
     if (lua_fun.L) {
-      if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) {
+      if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, &err) == SWITCH_STATUS_SUCCESS) {
         return true;
       }
     } else { /* if no lua_fun arg is passed from Lua, an empty initialized struct will be sent - see freeswitch.i */
-      if (switch_cache_db_execute_sql(dbh, sql, NULL) == SWITCH_STATUS_SUCCESS) {
+      if (switch_cache_db_execute_sql(dbh, sql, &err) == SWITCH_STATUS_SUCCESS) {
         return true;
       }
     }
+  } else {
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
   }
-
-  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
   return false;
 }
 
index 780fc2e545144f22e3a764d8b3e02bc0bd7f7b7a..d205f6b46e9cb82bdc7c834fb4876ec1a7aef793 100644 (file)
@@ -58,6 +58,7 @@ namespace LUA {
   class Dbh {
     protected:
       switch_cache_db_handle_t *dbh;
+      char *err;
       bool m_connected;
       static int query_callback(void *pArg, int argc, char **argv, char **cargv);
     public:
@@ -68,6 +69,8 @@ namespace LUA {
       bool test_reactive(char *test_sql, char *drop_sql = NULL, char *reactive_sql = NULL);
       bool query(char *sql, SWIGLUA_FN lua_fun);
       int affected_rows();
+      char *last_error();
+      void clear_error();
       int load_extension(const char *extension);
   };
 }
index 705b72eb44f2e890b9ce0586361143dd055409fc..fc6640809313d17d575d6494562d3ea090a7e35d 100644 (file)
@@ -8263,6 +8263,53 @@ fail:
 }
 
 
+static int _wrap_Dbh_last_error(lua_State* L) {
+  int SWIG_arg = 0;
+  LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
+  char *result = 0 ;
+  
+  SWIG_check_num_args("LUA::Dbh::last_error",1,1)
+  if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("LUA::Dbh::last_error",1,"LUA::Dbh *");
+  
+  if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
+    SWIG_fail_ptr("Dbh_last_error",1,SWIGTYPE_p_LUA__Dbh);
+  }
+  
+  result = (char *)(arg1)->last_error();
+  lua_pushstring(L,(const char *)result); SWIG_arg++;
+  return SWIG_arg;
+  
+  if(0) SWIG_fail;
+  
+fail:
+  lua_error(L);
+  return SWIG_arg;
+}
+
+
+static int _wrap_Dbh_clear_error(lua_State* L) {
+  int SWIG_arg = 0;
+  LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
+  
+  SWIG_check_num_args("LUA::Dbh::clear_error",1,1)
+  if(!SWIG_isptrtype(L,1)) SWIG_fail_arg("LUA::Dbh::clear_error",1,"LUA::Dbh *");
+  
+  if (!SWIG_IsOK(SWIG_ConvertPtr(L,1,(void**)&arg1,SWIGTYPE_p_LUA__Dbh,0))){
+    SWIG_fail_ptr("Dbh_clear_error",1,SWIGTYPE_p_LUA__Dbh);
+  }
+  
+  (arg1)->clear_error();
+  
+  return SWIG_arg;
+  
+  if(0) SWIG_fail;
+  
+fail:
+  lua_error(L);
+  return SWIG_arg;
+}
+
+
 static int _wrap_Dbh_load_extension(lua_State* L) {
   int SWIG_arg = 0;
   LUA::Dbh *arg1 = (LUA::Dbh *) 0 ;
@@ -8300,6 +8347,8 @@ static swig_lua_method swig_LUA_Dbh_methods[] = {
     {"test_reactive", _wrap_Dbh_test_reactive}, 
     {"query", _wrap_Dbh_query}, 
     {"affected_rows", _wrap_Dbh_affected_rows}, 
+    {"last_error", _wrap_Dbh_last_error}, 
+    {"clear_error", _wrap_Dbh_clear_error}, 
     {"load_extension", _wrap_Dbh_load_extension}, 
     {0,0}
 };