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