]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Skinny: switch to new cache_db abstraction
authorMathieu Parent <math.parent@gmail.com>
Tue, 24 Aug 2010 21:35:52 +0000 (23:35 +0200)
committerMathieu Parent <math.parent@gmail.com>
Tue, 24 Aug 2010 21:35:52 +0000 (23:35 +0200)
src/mod/endpoints/mod_skinny/mod_skinny.c
src/mod/endpoints/mod_skinny/mod_skinny.h

index 0c955884f1455d2658ee7ee3e3741f3cd3ac4a1c..64b354dc36fd255c31a316bf38c808ff756461f3 100644 (file)
@@ -298,73 +298,82 @@ switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, l
 /*****************************************************************************/
 /* SQL FUNCTIONS */
 /*****************************************************************************/
-void skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex)
+switch_cache_db_handle_t *skinny_get_db_handle(skinny_profile_t *profile)
 {
-       switch_core_db_t *db;
+       switch_cache_db_connection_options_t options = { {0} };
+       switch_cache_db_handle_t *dbh = NULL;
+
+       if (!zstr(profile->odbc_dsn)) {
+               options.odbc_options.dsn = profile->odbc_dsn;
+               options.odbc_options.user = profile->odbc_user;
+               options.odbc_options.pass = profile->odbc_pass;
+
+               if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
+                       dbh = NULL;
+               return dbh;
+       } else {
+               options.core_db_options.db_path = profile->dbname;
+               if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS)
+                       dbh = NULL;
+               return dbh;
+       }
+}
+
+
+switch_status_t skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex)
+{
+       switch_cache_db_handle_t *dbh = NULL;
+       switch_status_t status = SWITCH_STATUS_FALSE;
 
        if (mutex) {
                switch_mutex_lock(mutex);
        }
 
-       if (switch_odbc_available() && profile->odbc_dsn) {
-               switch_odbc_statement_handle_t stmt;
-               if (switch_odbc_handle_exec(profile->master_odbc, sql, &stmt, NULL) != SWITCH_ODBC_SUCCESS) {
-                       char *err_str;
-                       err_str = switch_odbc_handle_get_error(profile->master_odbc, stmt);
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, switch_str_nil(err_str));
-                       switch_safe_free(err_str);
-               }
-               switch_odbc_statement_handle_free(&stmt);
-       } else {
-               if (!(db = switch_core_db_open_file(profile->dbname))) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                       goto end;
-               }
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
-               switch_core_db_persistant_execute(db, sql, 1);
-               switch_core_db_close(db);
+       if (!(dbh = skinny_get_db_handle(profile))) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+               goto end;
        }
 
+       status = switch_cache_db_execute_sql(dbh, sql, NULL);
+
   end:
+
+       switch_cache_db_release_db_handle(&dbh);
+
        if (mutex) {
                switch_mutex_unlock(mutex);
        }
-}
 
+       return status;
+}
 
-switch_bool_t skinny_execute_sql_callback(skinny_profile_t *profile,
-                                                                                         switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
+switch_bool_t skinny_execute_sql_callback(skinny_profile_t *profile, switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback,
+                                                                                        void *pdata)
 {
        switch_bool_t ret = SWITCH_FALSE;
-       switch_core_db_t *db;
        char *errmsg = NULL;
+       switch_cache_db_handle_t *dbh = NULL;
 
        if (mutex) {
                switch_mutex_lock(mutex);
        }
 
-       if (switch_odbc_available() && profile->odbc_dsn) {
-               switch_odbc_handle_callback_exec(profile->master_odbc, sql, callback, pdata, NULL);
-       } else {
-               if (!(db = switch_core_db_open_file(profile->dbname))) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
-                       goto end;
-               }
-               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
-               switch_core_db_exec(db, sql, callback, pdata, &errmsg);
+       if (!(dbh = skinny_get_db_handle(profile))) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+               goto end;
+       }
 
-               if (errmsg) {
-                       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
-                       switch_core_db_free(errmsg);
-               }
+       switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
 
-               if (db) {
-                       switch_core_db_close(db);
-               }
+       if (errmsg) {
+               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
+               free(errmsg);
        }
 
   end:
 
+       switch_cache_db_release_db_handle(&dbh);
+
        if (mutex) {
                switch_mutex_unlock(mutex);
        }
index 68f3fddf87477906be7cf9ffd5e0542e4edbfe05..41b9b72a6e9bde5a9a084f9f6697ed24ca37221d 100644 (file)
@@ -215,7 +215,8 @@ switch_status_t dump_device(skinny_profile_t *profile, const char *device_name,
 /*****************************************************************************/
 /* SQL FUNCTIONS */
 /*****************************************************************************/
-void skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex);
+switch_cache_db_handle_t *skinny_get_db_handle(skinny_profile_t *profile);
+switch_status_t skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex);
 switch_bool_t skinny_execute_sql_callback(skinny_profile_t *profile,
                                                                                      switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata);