Skinny: switch to new cache_db abstraction

This commit is contained in:
Mathieu Parent 2010-08-24 23:35:52 +02:00
parent 2aac00ef81
commit 8921675668
2 changed files with 58 additions and 48 deletions

View File

@ -298,73 +298,82 @@ switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, l
/*****************************************************************************/ /*****************************************************************************/
/* SQL FUNCTIONS */ /* 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) { if (!zstr(profile->odbc_dsn)) {
switch_mutex_lock(mutex); 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) { if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
switch_odbc_statement_handle_t stmt; dbh = NULL;
if (switch_odbc_handle_exec(profile->master_odbc, sql, &stmt, NULL) != SWITCH_ODBC_SUCCESS) { return dbh;
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 { } else {
if (!(db = switch_core_db_open_file(profile->dbname))) { options.core_db_options.db_path = profile->dbname;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname); if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS)
goto end; dbh = NULL;
} return dbh;
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);
} }
} }
switch_bool_t skinny_execute_sql_callback(skinny_profile_t *profile, switch_status_t skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex)
switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
{ {
switch_bool_t ret = SWITCH_FALSE; switch_cache_db_handle_t *dbh = NULL;
switch_core_db_t *db; switch_status_t status = SWITCH_STATUS_FALSE;
char *errmsg = NULL;
if (mutex) { if (mutex) {
switch_mutex_lock(mutex); switch_mutex_lock(mutex);
} }
if (switch_odbc_available() && profile->odbc_dsn) { if (!(dbh = skinny_get_db_handle(profile))) {
switch_odbc_handle_callback_exec(profile->master_odbc, sql, callback, pdata, NULL); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
} else { goto end;
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 (errmsg) { status = switch_cache_db_execute_sql(dbh, sql, NULL);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
switch_core_db_free(errmsg);
}
if (db) { end:
switch_core_db_close(db);
} 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: end:
switch_cache_db_release_db_handle(&dbh);
if (mutex) { if (mutex) {
switch_mutex_unlock(mutex); switch_mutex_unlock(mutex);
} }

View File

@ -215,7 +215,8 @@ switch_status_t dump_device(skinny_profile_t *profile, const char *device_name,
/*****************************************************************************/ /*****************************************************************************/
/* SQL FUNCTIONS */ /* 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_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_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata);