This commit is contained in:
Anthony Minessale 2013-01-22 09:55:07 -06:00
parent b9a8ce5670
commit 6b6198e96f

View File

@ -331,61 +331,53 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
Dbh::Dbh(char *dsn, char *user, char *pass) Dbh::Dbh(char *dsn, char *user, char *pass)
{ {
switch_cache_db_connection_options_t options = { {0} }; dbh = NULL;
const char *prefix = "core:";
switch_cache_db_handle_type_t type;
m_connected = false;
if (strstr(dsn, prefix) == dsn) { if (!zstr(user) || !zstr(pass)) {
options.core_db_options.db_path = &dsn[strlen(prefix)]; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "user and pass params have been removed. Please specify the user and pass in the DSN.\n");
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) == SWITCH_STATUS_SUCCESS) {
m_connected = true;
}
} else if (!strncasecmp(dsn, "pgsql://", 8)) {
type = SCDB_TYPE_PGSQL;
options.pgsql_options.dsn = (char *)(dsn + 8);
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_PGSQL, &options) == SWITCH_STATUS_SUCCESS) {
m_connected = true;
} }
if (switch_cache_db_get_db_handle_dsn(&dbh, dsn) == SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DBH handle %p Connected.\n", (void *) dbh);
} else { } else {
options.odbc_options.dsn = dsn; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Connection failed. DBH NOT Connected.\n");
options.odbc_options.user = user;
options.odbc_options.pass = pass;
if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) == SWITCH_STATUS_SUCCESS) {
m_connected = true;
}
} }
} }
Dbh::~Dbh() Dbh::~Dbh()
{ {
release(); if (dbh) release();
} }
bool Dbh::release() bool Dbh::release()
{ {
if (m_connected) { if (dbh) {
switch_cache_db_release_db_handle(&dbh); switch_cache_db_release_db_handle(&dbh);
m_connected = false; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DBH handle %p released.\n", (void *) dbh);
return true; return true;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return false; return false;
} }
bool Dbh::connected() bool Dbh::connected()
{ {
return m_connected; return dbh ? true : false;
} }
bool Dbh::test_reactive(char *test_sql, char *drop_sql, char *reactive_sql) bool Dbh::test_reactive(char *test_sql, char *drop_sql, char *reactive_sql)
{ {
if (m_connected) { if (dbh) {
if (switch_cache_db_test_reactive(dbh, test_sql, drop_sql, reactive_sql) == SWITCH_TRUE) { if (switch_cache_db_test_reactive(dbh, test_sql, drop_sql, reactive_sql) == SWITCH_TRUE) {
return true; return true;
} }
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return false; return false;
} }
@ -417,7 +409,7 @@ int Dbh::query_callback(void *pArg, int argc, char **argv, char **cargv)
bool Dbh::query(char *sql, SWIGLUA_FN lua_fun) bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
{ {
if (m_connected) { if (dbh) {
if (lua_fun.L) { if (lua_fun.L) {
if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) { if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) {
return true; return true;
@ -428,21 +420,27 @@ bool Dbh::query(char *sql, SWIGLUA_FN lua_fun)
} }
} }
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return false; return false;
} }
int Dbh::affected_rows() int Dbh::affected_rows()
{ {
if (m_connected) { if (dbh) {
return switch_cache_db_affected_rows(dbh); return switch_cache_db_affected_rows(dbh);
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return 0; return 0;
} }
int Dbh::load_extension(const char *extension) int Dbh::load_extension(const char *extension)
{ {
if (m_connected) { if (dbh) {
return switch_cache_db_load_extension(dbh, extension); return switch_cache_db_load_extension(dbh, extension);
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return 0; return 0;
} }