提交 89216756 authored 作者: Mathieu Parent's avatar Mathieu Parent

Skinny: switch to new cache_db abstraction

上级 2aac00ef
...@@ -298,73 +298,82 @@ switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, l ...@@ -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 (!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) { 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_statement_handle_t stmt; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
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; goto end;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
switch_core_db_persistant_execute(db, sql, 1); status = switch_cache_db_execute_sql(dbh, sql, NULL);
switch_core_db_close(db);
}
end: end:
switch_cache_db_release_db_handle(&dbh);
if (mutex) { if (mutex) {
switch_mutex_unlock(mutex); switch_mutex_unlock(mutex);
} }
}
return status;
}
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,
switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata) void *pdata)
{ {
switch_bool_t ret = SWITCH_FALSE; switch_bool_t ret = SWITCH_FALSE;
switch_core_db_t *db;
char *errmsg = NULL; char *errmsg = NULL;
switch_cache_db_handle_t *dbh = 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 {
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; goto end;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SQL: %s\n", sql);
switch_core_db_exec(db, sql, callback, pdata, &errmsg); switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
if (errmsg) { if (errmsg) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
switch_core_db_free(errmsg); free(errmsg);
}
if (db) {
switch_core_db_close(db);
}
} }
end: end:
switch_cache_db_release_db_handle(&dbh);
if (mutex) { if (mutex) {
switch_mutex_unlock(mutex); switch_mutex_unlock(mutex);
} }
......
...@@ -215,7 +215,8 @@ switch_status_t dump_device(skinny_profile_t *profile, const char *device_name, ...@@ -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);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论