/*****************************************************************************/
/* 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);
}