switch_mutex_lock(profile->ireg_mutex);
- sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
+ //sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
char *sql = (char *) pop;
}
//printf("TRANS:\n%s\n", sqlbuf);
- sofia_glue_actually_execute_sql(profile, sqlbuf, NULL);
- sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
+ sofia_glue_actually_execute_sql_trans(profile, sqlbuf, NULL);
+ //sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
switch_mutex_unlock(profile->ireg_mutex);
if (++ireg_loops >= IREG_SECONDS) {
time_t now = switch_epoch_time_now(NULL);
sofia_reg_check_expire(profile, now, 0);
- sofia_glue_sql_close(profile, now);
ireg_loops = 0;
}
}
}
- sofia_glue_sql_close(profile, 0);
su_home_unref(profile->home);
su_root_destroy(profile->s_root);
pool = profile->pool;
sofia_glue_del_profile(profile);
switch_core_hash_destroy(&profile->chat_hash);
- switch_core_hash_destroy(&profile->db_hash);
switch_thread_rwlock_unlock(profile->rwlock);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name);
profile->dbname = switch_core_strdup(profile->pool, url);
switch_core_hash_init(&profile->chat_hash, profile->pool);
- switch_core_hash_init(&profile->db_hash, profile->pool);
switch_thread_rwlock_create(&profile->rwlock, profile->pool);
switch_mutex_init(&profile->flag_mutex, SWITCH_MUTEX_NESTED, profile->pool);
profile->dtmf_duration = 100;
}
-void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune)
-{
- switch_hash_index_t *hi;
- const void *var;
- void *val;
- sofia_cache_db_handle_t *dbh = NULL;
- int locked = 0;
- char *key;
-
- switch_mutex_lock(profile->ireg_mutex);
- top:
- locked = 0;
-
- for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
- switch_hash_this(hi, &var, NULL, &val);
- key = (char *) var;
-
- if ((dbh = (sofia_cache_db_handle_t *) val)) {
- time_t diff = 0;
-
- if (prune > 0) {
- if (prune > dbh->last_used) {
- diff = (time_t) prune - dbh->last_used;
- }
-
- if (diff < SQL_CACHE_TIMEOUT) {
- continue;
- }
- }
-
- if (switch_mutex_trylock(dbh->mutex) == SWITCH_STATUS_SUCCESS) {
- if (dbh->db) {
- switch_core_db_close(dbh->db);
- dbh->db = NULL;
- } else if (switch_odbc_available() && dbh->odbc_dbh) {
- switch_odbc_handle_destroy(&dbh->odbc_dbh);
- }
-
- switch_core_hash_delete(profile->db_hash, key);
- switch_mutex_unlock(dbh->mutex);
- switch_core_destroy_memory_pool(&dbh->pool);
- goto top;
-
- } else {
- if (!prune) locked++;
- continue;
- }
- }
- }
-
- if (locked) {
- goto top;
- }
-
- switch_mutex_unlock(profile->ireg_mutex);
-}
-
void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic)
{
switch_status_t status = SWITCH_STATUS_FALSE;
}
-void sofia_glue_release_db_handle(sofia_cache_db_handle_t **dbh)
+switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
{
- if (dbh && *dbh) {
- switch_mutex_unlock((*dbh)->mutex);
- *dbh = NULL;
+ switch_cache_db_connection_options_t options = { {0} };
+ switch_cache_db_handle_t *dbh = NULL;
+
+ if (profile->odbc_dsn && profile->odbc_user && profile->odbc_pass) {
+ 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;
}
}
-sofia_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
+void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
{
- switch_thread_id_t self = switch_thread_self();
- char thread_str[256] = "";
- sofia_cache_db_handle_t *dbh = NULL;
-
- snprintf(thread_str, sizeof(thread_str) - 1, "%lu", (unsigned long)(intptr_t)self);
-
- switch_mutex_lock(profile->ireg_mutex);
- if (!(dbh = switch_core_hash_find(profile->db_hash, thread_str))) {
- switch_memory_pool_t *pool = NULL;
- switch_core_db_t *db = NULL;
- switch_odbc_handle_t *odbc_dbh = NULL;
-
- if (switch_odbc_available() && profile->odbc_dsn) {
- if ((odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
- if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
- switch_odbc_handle_destroy(&odbc_dbh);
- }
- }
- } else {
- db = switch_core_db_open_file(profile->dbname);
- }
+ switch_cache_db_handle_t *dbh = NULL;
- if (!db && !odbc_dbh) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure!\n");
- goto end;
- }
-
- switch_core_new_memory_pool(&pool);
- dbh = switch_core_alloc(pool, sizeof(*dbh));
- dbh->pool = pool;
-
-
- if (db) dbh->db = db; else dbh->odbc_dbh = odbc_dbh;
- switch_mutex_init(&dbh->mutex, SWITCH_MUTEX_UNNESTED, dbh->pool);
- switch_mutex_lock(dbh->mutex);
-
- switch_core_hash_insert(profile->db_hash, thread_str, dbh);
+ if (mutex) {
+ switch_mutex_lock(mutex);
}
- end:
+ if (!(dbh = sofia_glue_get_db_handle(profile))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+ goto end;
+ }
- if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
+ switch_cache_db_persistant_execute_trans(dbh, sql, 1);
- switch_mutex_unlock(profile->ireg_mutex);
+ end:
+
+ switch_cache_db_release_db_handle(&dbh);
- return dbh;
+ if (mutex) {
+ switch_mutex_unlock(mutex);
+ }
}
-
-
void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
{
- sofia_cache_db_handle_t *dbh = NULL;
+ switch_cache_db_handle_t *dbh = NULL;
if (mutex) {
switch_mutex_lock(mutex);
goto end;
}
- if (switch_odbc_available() && dbh->odbc_dbh) {
- switch_odbc_statement_handle_t stmt;
- if (switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
- char *err_str;
- err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
- if (!zstr(err_str)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, err_str);
- }
- switch_safe_free(err_str);
- }
- switch_odbc_statement_handle_free(&stmt);
- } else if (profile->odbc_dsn) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
- } else {
- char *errmsg;
-
- switch_core_db_exec(dbh->db, sql, NULL, NULL, &errmsg);
-
- if (errmsg) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
- switch_core_db_free(errmsg);
- }
-
- }
+ switch_cache_db_execute_sql(dbh, sql, NULL);
end:
-
- sofia_glue_release_db_handle(&dbh);
+
+ switch_cache_db_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
{
switch_bool_t ret = SWITCH_FALSE;
char *errmsg = NULL;
- sofia_cache_db_handle_t *dbh = NULL;
+ switch_cache_db_handle_t *dbh = NULL;
if (mutex) {
switch_mutex_lock(mutex);
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 (switch_odbc_available() && dbh->odbc_dbh) {
- switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata, NULL);
- } else if (profile->odbc_dsn) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
- } else {
- switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
-
- if (errmsg) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
- free(errmsg);
- }
+ if (errmsg) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
+ free(errmsg);
}
end:
- sofia_glue_release_db_handle(&dbh);
+ switch_cache_db_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
{
- switch_core_db_stmt_t *stmt;
char *ret = NULL;
- sofia_cache_db_handle_t *dbh = NULL;
+ switch_cache_db_handle_t *dbh = NULL;
if (!(dbh = sofia_glue_get_db_handle(profile))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
- goto end;
+ return NULL;
}
if (mutex) {
switch_mutex_lock(mutex);
}
- if (switch_odbc_available() && dbh->odbc_dbh) {
- if (switch_odbc_handle_exec_string(dbh->odbc_dbh, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
- ret = resbuf;
- }
- goto end;
- }
-
- if (switch_core_db_prepare(dbh->db, sql, -1, &stmt, 0)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
- goto end;
- } else {
- int running = 1;
- int colcount;
-
- while (running < 5000) {
- int result = switch_core_db_step(stmt);
- const unsigned char *txt;
-
- if (result == SWITCH_CORE_DB_ROW) {
- if ((colcount = switch_core_db_column_count(stmt)) > 0) {
- if ((txt = switch_core_db_column_text(stmt, 0))) {
- switch_copy_string(resbuf, (char *) txt, len);
- ret = resbuf;
- } else {
- goto end;
- }
- }
- break;
- } else if (result == SWITCH_CORE_DB_BUSY) {
- running++;
- switch_cond_next();
- continue;
- }
- break;
- }
-
- switch_core_db_finalize(stmt);
- }
-
- end:
-
+ ret = switch_cache_db_execute_sql2str(dbh, sql, resbuf, len, NULL);
+
if (mutex) {
switch_mutex_unlock(mutex);
}
- sofia_glue_release_db_handle(&dbh);
+ switch_cache_db_release_db_handle(&dbh);
return ret;
}