This commit is contained in:
Anthony Minessale 2013-01-22 09:55:07 -06:00
parent b9a8ce5670
commit 6b6198e96f
1 changed files with 31 additions and 33 deletions

View File

@ -331,61 +331,53 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp
return SWITCH_STATUS_SUCCESS;
}
Dbh::Dbh(char *dsn, char *user, char *pass)
{
switch_cache_db_connection_options_t options = { {0} };
const char *prefix = "core:";
switch_cache_db_handle_type_t type;
m_connected = false;
dbh = NULL;
if (strstr(dsn, prefix) == dsn) {
options.core_db_options.db_path = &dsn[strlen(prefix)];
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;
}
} else {
options.odbc_options.dsn = dsn;
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;
}
}
if (!zstr(user) || !zstr(pass)) {
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_dsn(&dbh, dsn) == SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DBH handle %p Connected.\n", (void *) dbh);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Connection failed. DBH NOT Connected.\n");
}
}
Dbh::~Dbh()
{
release();
if (dbh) release();
}
bool Dbh::release()
{
if (m_connected) {
switch_cache_db_release_db_handle(&dbh);
m_connected = false;
return true;
if (dbh) {
switch_cache_db_release_db_handle(&dbh);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "DBH handle %p released.\n", (void *) dbh);
return true;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return false;
}
bool Dbh::connected()
{
return m_connected;
return dbh ? true : false;
}
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) {
return true;
}
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
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)
{
if (m_connected) {
if (dbh) {
if (lua_fun.L) {
if (switch_cache_db_execute_sql_callback(dbh, sql, query_callback, &lua_fun, NULL) == SWITCH_STATUS_SUCCESS) {
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;
}
int Dbh::affected_rows()
{
if (m_connected) {
if (dbh) {
return switch_cache_db_affected_rows(dbh);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return 0;
}
int Dbh::load_extension(const char *extension)
{
if (m_connected) {
if (dbh) {
return switch_cache_db_load_extension(dbh, extension);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DBH NOT Connected.\n");
return 0;
}