From 89216756680a56196771dd3139d6f5e38908c0eb Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Tue, 24 Aug 2010 23:35:52 +0200 Subject: [PATCH] Skinny: switch to new cache_db abstraction --- src/mod/endpoints/mod_skinny/mod_skinny.c | 103 ++++++++++++---------- src/mod/endpoints/mod_skinny/mod_skinny.h | 3 +- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/mod/endpoints/mod_skinny/mod_skinny.c b/src/mod/endpoints/mod_skinny/mod_skinny.c index 0c955884f1..64b354dc36 100644 --- a/src/mod/endpoints/mod_skinny/mod_skinny.c +++ b/src/mod/endpoints/mod_skinny/mod_skinny.c @@ -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 (mutex) { - switch_mutex_lock(mutex); - } + 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_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); + if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) + dbh = NULL; + return dbh; } 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); - } - - end: - if (mutex) { - switch_mutex_unlock(mutex); + 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_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_status_t skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex) { - switch_bool_t ret = SWITCH_FALSE; - switch_core_db_t *db; - char *errmsg = NULL; + 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_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); - } + status = switch_cache_db_execute_sql(dbh, sql, NULL); - if (db) { - switch_core_db_close(db); - } + 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 ret = SWITCH_FALSE; + char *errmsg = NULL; + switch_cache_db_handle_t *dbh = NULL; + + if (mutex) { + switch_mutex_lock(mutex); + } + + if (!(dbh = skinny_get_db_handle(profile))) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n"); + goto end; + } + + switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg); + + 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); } diff --git a/src/mod/endpoints/mod_skinny/mod_skinny.h b/src/mod/endpoints/mod_skinny/mod_skinny.h index 68f3fddf87..41b9b72a6e 100644 --- a/src/mod/endpoints/mod_skinny/mod_skinny.h +++ b/src/mod/endpoints/mod_skinny/mod_skinny.h @@ -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);