refactor db stuff to use single string dsn to avoid code duplication and introduce switch_sql_queue_manager api to create transactional sql queues to aggregate a bunch of sql stmts into transactions

This commit is contained in:
Anthony Minessale committed 2012-10-09 20:20:32 -05:00
1 parent fc764b27bf
commit e1fe289672
20 files changed
+766 -553

No files matched your search

@@ -410,8 +410,6 @@ static struct {
switch_hash_t *queue_hash;
int debug;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
char *dbname;
int32_t threads;
int32_t running;
@@ -506,23 +504,21 @@ static void destroy_queue(const char *queue_name, switch_bool_t block)
switch_cache_db_handle_t *cc_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
return dbh;
dsn = globals.odbc_dsn;
} else {
options.core_db_options.db_path = globals.dbname;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
return dbh;
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
/*!
* \brief Sets the queue's configuration instructions
@@ -1276,15 +1272,6 @@ static switch_status_t load_config(void)
globals.dbname = strdup(val);
} else if (!strcasecmp(var, "odbc-dsn")) {
globals.odbc_dsn = strdup(val);
if (!zstr(globals.odbc_dsn)) {
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*(globals.odbc_user++) = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*(globals.odbc_pass++) = '\0';
}
}
}
}
}
}
@@ -57,8 +57,6 @@ static struct {
int cache_expire;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
char *sql;
char *citystate_sql;
@@ -90,25 +88,19 @@ static switch_event_node_t *reload_xml_event = NULL;
static switch_cache_db_handle_t *cidlookup_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
char *dsn;
if ((dsn = strstr(globals.odbc_dsn, "pgsql;")) != NULL) {
options.pgsql_options.dsn = (char*)(dsn + 6);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
} else {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
}
dsn = globals.odbc_dsn;
} else {
return NULL;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -120,11 +112,6 @@ static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const
switch_cache_db_handle_t *dbh = NULL;
if (!switch_odbc_available() && !switch_pgsql_available()) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC and PGSQL are not compiled in. Do not configure odbc-dsn parameter!\n");
return SWITCH_STATUS_FALSE;
}
if ((callback_type == CONFIG_LOAD || callback_type == CONFIG_RELOAD) && changed) {
if (zstr(newvalue)) {
@@ -132,13 +119,6 @@ static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const
} else {
switch_safe_free(globals.odbc_dsn);
globals.odbc_dsn = strdup(newvalue);
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*globals.odbc_user++ = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*globals.odbc_pass++ = '\0';
}
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connecting to dsn: %s\n", globals.odbc_dsn);
if (!(dbh = cidlookup_get_db_handle())) {
@@ -572,7 +552,7 @@ static cid_data_t *do_lookup(switch_memory_pool_t *pool, switch_event_t *event,
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "caller_id_number", number);
/* database always wins */
if ((switch_odbc_available() || switch_pgsql_available()) && globals.odbc_dsn && globals.sql) {
if (globals.odbc_dsn && globals.sql) {
name = do_db_lookup(pool, event, number, globals.sql);
if (name) {
cid->name = name;
@@ -618,7 +598,7 @@ static cid_data_t *do_lookup(switch_memory_pool_t *pool, switch_event_t *event,
switch_assert(cid);
}
/* append area if we can */
if (!cid->area && !skipcitystate && strlen(number) == 11 && number[0] == '1' && (switch_odbc_available() || switch_pgsql_available()) && globals.odbc_dsn && globals.citystate_sql) {
if (!cid->area && !skipcitystate && strlen(number) == 11 && number[0] == '1' && globals.odbc_dsn && globals.citystate_sql) {
/* yes, this is really area */
name = do_db_lookup(pool, event, number, globals.citystate_sql);
@@ -776,7 +756,7 @@ SWITCH_STANDARD_API(cidlookup_function)
stream->write_function(stream, " odbc-dsn: %s\n sql: %s\n citystate-sql: %s\n",
globals.odbc_dsn ? globals.odbc_dsn : "(null)",
globals.sql ? globals.sql : "(null)", globals.citystate_sql ? globals.citystate_sql : "(null)");
stream->write_function(stream, " ODBC Compiled: %s; PGSQL Compiled: %s\n", switch_odbc_available() ? "true" : "false", switch_pgsql_available() ? "true" : "false");
switch_goto_status(SWITCH_STATUS_SUCCESS, done);
}
+12 -31
View File
@@ -44,8 +44,6 @@ static struct {
char hostname[256];
char *dbname;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
switch_mutex_t *mutex;
switch_mutex_t *db_hash_mutex;
switch_hash_t *db_hash;
@@ -88,31 +86,21 @@ static char group_sql[] =
switch_cache_db_handle_t *limit_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
char *dsn;
if ((dsn = strstr(globals.odbc_dsn, "pgsql;")) != NULL) {
options.pgsql_options.dsn = (char*)(dsn + 6);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
} else {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
}
return dbh;
dsn = globals.odbc_dsn;
} else {
options.core_db_options.db_path = globals.dbname;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
return dbh;
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -304,15 +292,8 @@ static switch_status_t do_config()
}
if (globals.odbc_dsn) {
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*globals.odbc_user++ = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*globals.odbc_pass++ = '\0';
}
}
if (!(dbh = limit_get_db_handle())) {
globals.odbc_dsn = globals.odbc_user = globals.odbc_pass;
globals.odbc_dsn = NULL;
}
}
@@ -74,8 +74,6 @@ static struct {
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
char odbc_dsn[1024];
char *odbc_user;
char *odbc_pass;
} globals;
#define DIR_PROFILE_CONFIGITEM_COUNT 100
@@ -192,25 +190,21 @@ char *string_to_keypad_digit(const char *in)
switch_cache_db_handle_t *directory_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
dsn = globals.odbc_dsn;
} else {
options.core_db_options.db_path = globals.dbname;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
static switch_status_t directory_execute_sql(char *sql, switch_mutex_t *mutex)
@@ -464,12 +458,6 @@ static switch_status_t load_config(switch_bool_t reload)
if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) {
if (switch_odbc_available()) {
switch_set_string(globals.odbc_dsn, val);
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*globals.odbc_user++ = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*globals.odbc_pass++ = '\0';
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
}
@@ -138,23 +138,23 @@ static switch_status_t load_config(void)
set_global_db_dsn("easyroute");
}
if (switch_odbc_available() && globals.db_dsn) {
if (globals.db_dsn) {
if (!(globals.master_odbc = switch_odbc_handle_new(globals.db_dsn, globals.db_username, globals.db_password))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open Database!\n");
status = SWITCH_STATUS_FALSE;
goto reallydone;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened Database!\n");
}
if (globals.odbc_num_retries) {
switch_odbc_set_num_retries(globals.master_odbc, globals.odbc_num_retries);
}
if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open Database!\n");
status = SWITCH_STATUS_FALSE;
goto reallydone;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened Database!\n");
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.db_dsn);
if (!globals.custom_query) {
@@ -192,12 +192,6 @@ static switch_status_t route_lookup(char *dn, easyroute_results_t *results, int
char *sql = NULL;
route_callback_t pdata;
if (!switch_odbc_available()) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
"mod_easyroute requires core ODBC support. Please refer to the documentation on how to enable this\n");
return sstatus;
}
memset(&pdata, 0, sizeof(pdata));
if (!globals.custom_query) {
sql = switch_mprintf(SQL_LOOKUP, dn);
@@ -339,12 +333,6 @@ SWITCH_STANDARD_API(easyroute_function)
goto done;
}
if (!switch_odbc_available()) {
stream->write_function(stream, "mod_easyroute requires you enable core odbc support\n");
status = SWITCH_STATUS_SUCCESS;
goto done;
}
if (!cmd || !(mydata = strdup(cmd))) {
stream->write_function(stream, "Usage: easyroute <number>\n");
status = SWITCH_STATUS_SUCCESS;
+11 -31
View File
@@ -588,8 +588,6 @@ static struct {
char hostname[256];
char *dbname;
char odbc_dsn[1024];
char *odbc_user;
char *odbc_pass;
int node_thread_running;
switch_odbc_handle_t *master_odbc;
int threads;
@@ -727,33 +725,21 @@ static void cancel_consumer_outbound_call(const char *key, switch_call_cause_t c
switch_cache_db_handle_t *fifo_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
char *dsn;
if ((dsn = strstr(globals.odbc_dsn, "pgsql;")) != NULL) {
options.pgsql_options.dsn = (char*)(dsn + 6);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
} else {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
}
return dbh;
dsn = globals.odbc_dsn;
} else {
options.core_db_options.db_path = globals.dbname;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -4020,12 +4006,6 @@ static switch_status_t load_config(int reload, int del_all)
if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) {
if (switch_odbc_available() || switch_pgsql_available()) {
switch_set_string(globals.odbc_dsn, val);
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*globals.odbc_user++ = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*globals.odbc_pass++ = '\0';
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
}
+11 -22
View File
@@ -142,8 +142,6 @@ static struct {
switch_memory_pool_t *pool;
char *dbname;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
switch_mutex_t *mutex;
switch_hash_t *profile_hash;
profile_t *default_profile;
@@ -430,16 +428,19 @@ static switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, c
static switch_cache_db_handle_t *lcr_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(globals.odbc_dsn)) {
options.odbc_options.dsn = globals.odbc_dsn;
options.odbc_options.user = globals.odbc_user;
options.odbc_options.pass = globals.odbc_pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
dsn = globals.odbc_dsn;
} else {
dsn = globals.dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -998,12 +999,6 @@ static switch_status_t lcr_load_config()
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
switch_safe_free(globals.odbc_dsn);
globals.odbc_dsn = strdup(val);
if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
*globals.odbc_user++ = '\0';
if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
*globals.odbc_pass++ = '\0';
}
}
}
}
}
@@ -1011,8 +1006,8 @@ static switch_status_t lcr_load_config()
/* initialize sql here, 'cause we need to verify custom_sql for each profile below */
if (globals.odbc_dsn) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
, "dsn is \"%s\", user is \"%s\"\n"
, globals.odbc_dsn, globals.odbc_user
, "dsn is \"%s\"\n"
, globals.odbc_dsn
);
if (!(dbh = lcr_get_db_handle())) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
@@ -2021,12 +2016,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load)
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
if (!switch_odbc_available()) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must have ODBC support in FreeSWITCH to use this module\n");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\t./configure --enable-core-odbc-support\n");
return SWITCH_STATUS_FALSE;
}
globals.pool = pool;
if (switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
@@ -83,8 +83,6 @@ struct vm_profile {
char *name;
char *dbname;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
char terminator_key[2];
char play_new_messages_key[2];
char play_saved_messages_key[2];
@@ -163,31 +161,21 @@ typedef struct vm_profile vm_profile_t;
switch_cache_db_handle_t *vm_get_db_handle(vm_profile_t *profile)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(profile->odbc_dsn)) {
char *dsn;
if ((dsn = strstr(profile->odbc_dsn, "pgsql;")) != NULL) {
options.pgsql_options.dsn = (char*)(dsn + 6);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
} else {
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;
dsn = profile->odbc_dsn;
} 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;
dsn = profile->dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -722,15 +710,6 @@ static vm_profile_t *load_profile(const char *profile_name)
switch_thread_rwlock_create(&profile->rwlock, pool);
profile->name = switch_core_strdup(pool, profile_name);
if (!zstr(profile->odbc_dsn)) {
if ((profile->odbc_user = strchr(profile->odbc_dsn, ':'))) {
*(profile->odbc_user++) = '\0';
if ((profile->odbc_pass = strchr(profile->odbc_user, ':'))) {
*(profile->odbc_pass++) = '\0';
}
}
}
if (zstr(profile->dbname)) {
profile->dbname = switch_core_sprintf(profile->pool, "voicemail_%s", profile_name);
}
+102 -121
View File
@@ -133,11 +133,8 @@ struct mdl_profile {
char *dbname;
char *avatar;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
switch_bool_t purge;
switch_thread_rwlock_t *rwlock;
switch_odbc_handle_t *master_odbc;
switch_mutex_t *mutex;
ldl_handle_t *handle;
uint32_t flags;
@@ -324,73 +321,110 @@ static char *translate_rpid(char *in, char *ext)
}
static void mdl_execute_sql(mdl_profile_t *profile, char *sql, switch_mutex_t *mutex)
static switch_cache_db_handle_t *mdl_get_db_handle(mdl_profile_t *profile)
{
switch_core_db_t *db;
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);
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(profile->odbc_dsn)) {
dsn = profile->odbc_dsn;
} 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_core_db_persistant_execute(db, sql, 1);
switch_core_db_close(db);
dsn = profile->dbname;
}
end:
if (mutex) {
switch_mutex_unlock(mutex);
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
static switch_bool_t mdl_execute_sql_callback(mdl_profile_t *profile,
switch_mutex_t *mutex, char *sql, switch_core_db_callback_func_t callback, void *pdata)
static switch_status_t mdl_execute_sql(mdl_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_core_db_exec(db, sql, callback, pdata, &errmsg);
if (!(dbh = mdl_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;
}
char *mdl_execute_sql2str(mdl_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
{
switch_cache_db_handle_t *dbh = NULL;
char *ret = NULL;
if (mutex) {
switch_mutex_lock(mutex);
}
if (!(dbh = mdl_get_db_handle(profile))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
goto end;
}
ret = switch_cache_db_execute_sql2str(dbh, sql, resbuf, len, NULL);
end:
switch_cache_db_release_db_handle(&dbh);
if (mutex) {
switch_mutex_unlock(mutex);
}
return ret;
}
static switch_bool_t mdl_execute_sql_callback(mdl_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 = mdl_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);
}
@@ -398,6 +432,9 @@ static switch_bool_t mdl_execute_sql_callback(mdl_profile_t *profile,
return ret;
}
static int sub_callback(void *pArg, int argc, char **argv, char **columnNames)
{
mdl_profile_t *profile = (mdl_profile_t *) pArg;
@@ -2790,17 +2827,7 @@ static void set_profile_val(mdl_profile_t *profile, char *var, char *val)
} else if (!strcasecmp(var, "avatar")) {
profile->avatar = switch_core_strdup(module_pool, val);
} else if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) {
if (switch_odbc_available()) {
profile->odbc_dsn = switch_core_strdup(module_pool, val);
if ((profile->odbc_user = strchr(profile->odbc_dsn, ':'))) {
*profile->odbc_user++ = '\0';
if ((profile->odbc_pass = strchr(profile->odbc_user, ':'))) {
*profile->odbc_pass++ = '\0';
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
}
profile->odbc_dsn = switch_core_strdup(module_pool, val);
} else if (!strcasecmp(var, "use-rtp-timer") && switch_true(val)) {
switch_set_flag(profile, TFLAG_TIMER);
} else if (!strcasecmp(var, "dialplan") && !zstr(val)) {
@@ -3109,17 +3136,13 @@ static switch_bool_t match_profile(mdl_profile_t *profile, mdl_profile_t *new_pr
(new_profile->user_flags == profile->user_flags) && (new_profile->acl_count == profile->acl_count)
) {
uint32_t i;
if (switch_odbc_available()) {
if (!(((!new_profile->odbc_dsn && !profile->odbc_dsn) ||
(new_profile->odbc_dsn && profile->odbc_dsn && !strcasecmp(new_profile->odbc_dsn, profile->odbc_dsn))) &&
((!new_profile->odbc_user && !profile->odbc_user) ||
(new_profile->odbc_user && profile->odbc_user && !strcasecmp(new_profile->odbc_user, profile->odbc_user))) &&
((!new_profile->odbc_pass && !profile->odbc_pass) ||
(new_profile->odbc_pass && profile->odbc_pass && !strcasecmp(new_profile->odbc_pass, profile->odbc_pass)))
)) {
return SWITCH_FALSE;
}
if (!(((!new_profile->odbc_dsn && !profile->odbc_dsn) ||
(new_profile->odbc_dsn && profile->odbc_dsn && !strcasecmp(new_profile->odbc_dsn, profile->odbc_dsn)))
)) {
return SWITCH_FALSE;
}
for (i = 0; i < new_profile->acl_count; i++) {
if (strcasecmp(new_profile->acl[i], profile->acl[i]) != 0) {
@@ -3137,11 +3160,6 @@ static switch_status_t destroy_profile(char *name)
if ((profile = switch_core_hash_find(globals.profile_hash, name))) {
if (profile->user_flags & LDL_FLAG_COMPONENT) {
if (switch_odbc_available() && profile->odbc_dsn && profile->master_odbc) {
switch_odbc_handle_disconnect(profile->master_odbc);
switch_odbc_handle_destroy(&profile->master_odbc);
}
switch_mutex_destroy(profile->mutex);
}
@@ -3205,7 +3223,7 @@ static switch_status_t soft_reload(void)
if (profile && type && !strcasecmp(type, "component")) {
char dbname[256];
switch_core_db_t *db;
switch_cache_db_handle_t *dbh = NULL;
if (!profile->login && profile->name) {
profile->login = switch_core_strdup(module_pool, profile->name);
@@ -3218,28 +3236,9 @@ static switch_status_t soft_reload(void)
switch_snprintf(dbname, sizeof(dbname), "dingaling_%s", profile->name);
profile->dbname = switch_core_strdup(module_pool, dbname);
if (switch_odbc_available() && profile->odbc_dsn) {
if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
switch_odbc_handle_exec(profile->master_odbc, sub_sql, NULL, NULL);
//mdl_execute_sql(profile, sub_sql, NULL);
} else {
if ((db = switch_core_db_open_file(profile->dbname))) {
switch_core_db_test_reactive(db, "select * from jabber_subscriptions", NULL, sub_sql);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
continue;
}
switch_core_db_close(db);
if ((dbh = mdl_get_db_handle(profile))) {
switch_cache_db_test_reactive(dbh, "select * from jabber_subscriptions", NULL, sub_sql);
switch_cache_db_release_db_handle(&dbh);
}
}
@@ -3354,7 +3353,7 @@ static switch_status_t load_config(void)
if (profile && type && !strcasecmp(type, "component")) {
char dbname[256];
switch_core_db_t *db;
switch_cache_db_handle_t *dbh = NULL;
if (!profile->login && profile->name) {
profile->login = switch_core_strdup(module_pool, profile->name);
@@ -3367,28 +3366,10 @@ static switch_status_t load_config(void)
switch_snprintf(dbname, sizeof(dbname), "dingaling_%s", profile->name);
profile->dbname = switch_core_strdup(module_pool, dbname);
if (switch_odbc_available() && profile->odbc_dsn) {
if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
switch_odbc_handle_exec(profile->master_odbc, sub_sql, NULL, NULL);
//mdl_execute_sql(profile, sub_sql, NULL);
} else {
if ((db = switch_core_db_open_file(profile->dbname))) {
switch_core_db_test_reactive(db, "select * from jabber_subscriptions", NULL, sub_sql);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
continue;
}
switch_core_db_close(db);
if ((dbh = mdl_get_db_handle(profile))) {
switch_cache_db_test_reactive(dbh, "select * from jabber_subscriptions", NULL, sub_sql);
switch_cache_db_release_db_handle(&dbh);
}
}
+22 -41
View File
@@ -304,23 +304,21 @@ switch_core_session_t * skinny_profile_find_session(skinny_profile_t *profile, l
/*****************************************************************************/
switch_cache_db_handle_t *skinny_get_db_handle(skinny_profile_t *profile)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
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;
dsn = profile->odbc_dsn;
} 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;
dsn = profile->dbname;
}
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
@@ -1751,6 +1749,7 @@ static switch_status_t load_skinny_config(void)
{
char *cf = "skinny.conf";
switch_xml_t xcfg, xml, xprofiles, xprofile;
switch_cache_db_handle_t *dbh = NULL;
if (!(xml = switch_xml_open_cfg(cf, &xcfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
@@ -1770,7 +1769,6 @@ static switch_status_t load_skinny_config(void)
if ((xsettings = switch_xml_child(xprofile, "settings"))) {
switch_memory_pool_t *profile_pool = NULL;
char dbname[256];
switch_core_db_t *db;
skinny_profile_t *profile = NULL;
switch_xml_t param;
@@ -1914,34 +1912,17 @@ static switch_status_t load_skinny_config(void)
switch_snprintf(dbname, sizeof(dbname), "skinny_%s", profile->name);
profile->dbname = switch_core_strdup(profile->pool, dbname);
if (switch_odbc_available() && profile->odbc_dsn) {
if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
if (switch_odbc_handle_connect(profile->master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
switch_odbc_handle_exec(profile->master_odbc, devices_sql, NULL, NULL);
switch_odbc_handle_exec(profile->master_odbc, lines_sql, NULL, NULL);
switch_odbc_handle_exec(profile->master_odbc, buttons_sql, NULL, NULL);
switch_odbc_handle_exec(profile->master_odbc, active_lines_sql, NULL, NULL);
} else {
if ((db = switch_core_db_open_file(profile->dbname))) {
switch_core_db_test_reactive(db, "SELECT headset FROM skinny_devices", "DROP TABLE skinny_devices", devices_sql);
switch_core_db_test_reactive(db, "SELECT * FROM skinny_lines", "DROP TABLE skinny_lines", lines_sql);
switch_core_db_test_reactive(db, "SELECT * FROM skinny_buttons", "DROP TABLE skinny_buttons", buttons_sql);
switch_core_db_test_reactive(db, "SELECT * FROM skinny_active_lines", "DROP TABLE skinny_active_lines", active_lines_sql);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
continue;
}
switch_core_db_close(db);
if ((dbh = skinny_get_db_handle(profile))) {
switch_cache_db_test_reactive(dbh, "SELECT headset FROM skinny_devices", "DROP TABLE skinny_devices", devices_sql);
switch_cache_db_test_reactive(dbh, "SELECT * FROM skinny_lines", "DROP TABLE skinny_lines", lines_sql);
switch_cache_db_test_reactive(dbh, "SELECT * FROM skinny_buttons", "DROP TABLE skinny_buttons", buttons_sql);
switch_cache_db_test_reactive(dbh, "SELECT * FROM skinny_active_lines", "DROP TABLE skinny_active_lines", active_lines_sql);
switch_cache_db_release_db_handle(&dbh);
}
skinny_execute_sql_callback(profile, profile->sql_mutex, "DELETE FROM skinny_devices", NULL, NULL);
skinny_execute_sql_callback(profile, profile->sql_mutex, "DELETE FROM skinny_lines", NULL, NULL);
+4 -3
View File
@@ -626,9 +626,10 @@ struct sofia_profile {
uint32_t rtp_timeout_sec;
uint32_t rtp_hold_timeout_sec;
char *odbc_dsn;
char *odbc_user;
char *odbc_pass;
// switch_odbc_handle_t *master_odbc;
char *pre_trans_execute;
char *post_trans_execute;
char *inner_pre_trans_execute;
char *inner_post_trans_execute;
switch_queue_t *sql_queue;
char *acl[SOFIA_MAX_ACL];
uint32_t acl_count;
+9 -11
View File
@@ -4414,17 +4414,15 @@ switch_status_t config_sofia(int reload, char *profile_name)
sofia_set_flag(profile, TFLAG_CAPTURE);
nua_set_params(profile->nua, TPTAG_CAPT(mod_sofia_globals.capture_server), TAG_END());
} else if (!strcasecmp(var, "odbc-dsn") && !zstr(val)) {
if (switch_odbc_available() || switch_pgsql_available()) {
profile->odbc_dsn = switch_core_strdup(profile->pool, val);
if ((profile->odbc_user = strchr(profile->odbc_dsn, ':'))) {
*profile->odbc_user++ = '\0';
if ((profile->odbc_pass = strchr(profile->odbc_user, ':'))) {
*profile->odbc_pass++ = '\0';
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NEITHER ODBC NOR PGSQL ARE AVAILABLE!\n");
}
profile->odbc_dsn = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "db-pre-trans-execute") && !zstr(val)) {
profile->pre_trans_execute = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "db-post-trans-execute") && !zstr(val)) {
profile->post_trans_execute = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "db-inner-pre-trans-execute") && !zstr(val)) {
profile->inner_pre_trans_execute = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "db-inner-post-trans-execute") && !zstr(val)) {
profile->inner_post_trans_execute = switch_core_strdup(profile->pool, val);
} else if (!strcasecmp(var, "forward-unsolicited-mwi-notify")) {
if (switch_true(val)) {
sofia_set_pflag(profile, PFLAG_FORWARD_MWI_NOTIFY);
+19 -25
View File
@@ -36,8 +36,8 @@
#include "mod_sofia.h"
#include <switch_stun.h>
switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile);
switch_cache_db_handle_t *_sofia_glue_get_db_handle(sofia_profile_t *profile, const char *file, const char *func, int line);
#define sofia_glue_get_db_handle(_p) _sofia_glue_get_db_handle(_p, __FILE__, __SWITCH_FUNC__, __LINE__)
void sofia_glue_set_image_sdp(private_object_t *tech_pvt, switch_t38_options_t *t38_options, int insist)
{
@@ -6361,33 +6361,22 @@ void sofia_glue_execute_sql_now(sofia_profile_t *profile, char **sqlp, switch_bo
}
switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
switch_cache_db_handle_t *_sofia_glue_get_db_handle(sofia_profile_t *profile, const char *file, const char *func, int line)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
char *dsn;
if (!zstr(profile->odbc_dsn)) {
char *dsn;
if ((dsn = strstr(profile->odbc_dsn, "pgsql;")) != NULL) {
options.pgsql_options.dsn = (char*)(dsn + 6);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) != SWITCH_STATUS_SUCCESS)
dbh = NULL;
} else {
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;
dsn = profile->odbc_dsn;
} 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;
dsn = profile->dbname;
}
if (_switch_cache_db_get_db_handle_dsn(&dbh, dsn, file, func, line) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}
void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
@@ -6403,7 +6392,12 @@ void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql,
switch_mutex_lock(mutex);
}
switch_cache_db_persistant_execute_trans(dbh, sql, 1);
switch_cache_db_persistant_execute_trans_full(dbh, sql, 1,
profile->pre_trans_execute,
profile->post_trans_execute,
profile->inner_pre_trans_execute,
profile->inner_post_trans_execute
);
switch_cache_db_release_db_handle(&dbh);
@@ -79,12 +79,12 @@ SWITCH_MODULE_DEFINITION(mod_cdr_sqlite, mod_cdr_sqlite_load, mod_cdr_sqlite_shu
switch_cache_db_handle_t *cdr_get_db_handle(void)
{
switch_cache_db_connection_options_t options = { {0} };
switch_cache_db_handle_t *dbh = NULL;
options.core_db_options.db_path = globals.db_name;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS)
if (switch_cache_db_get_db_handle_dsn(&dbh, globals.db_name) != SWITCH_STATUS_SUCCESS) {
dbh = NULL;
}
return dbh;
}