提交 55741519 authored 作者: Michael Jerris's avatar Michael Jerris

change odbc handling in code to be runtime instead of build time

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/scripts/contrib@13936 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 831e3e49
BASE=../../../..
include $(BASE)/build/modmake.rules
LOCAL_CFLAGS += `if test -f $(BASE)/.libs/libfreeswitch_la-switch_odbc.o ; then echo -DSWITCH_HAVE_ODBC; fi ;`
......@@ -50,15 +50,10 @@
#include <switch.h>
#ifdef SWITCH_HAVE_ODBC
#include <switch_odbc.h>
#endif
/* Defaults */
#ifdef SWITCH_HAVE_ODBC
static char SQL_LOOKUP[] = "SELECT %s FROM %s WHERE %s='%s'";
static char SQL_SAVE[] = "UPDATE %s SET %s=%s-%f WHERE %s='%s'";
#endif
typedef struct
{
......@@ -110,11 +105,7 @@ static struct
char *db_table;
char *db_column_cash;
char *db_column_account;
#ifdef SWITCH_HAVE_ODBC
switch_odbc_handle_t *master_odbc;
#else
void *padding1; /* Keep structures same size */
#endif
} globals;
static void nibblebill_pause(switch_core_session_t *session);
......@@ -142,7 +133,6 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_percall_action, globals.percall_act
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_lowbal_action, globals.lowbal_action);
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_nobal_action, globals.nobal_action);
#ifdef SWITCH_HAVE_ODBC
static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnNames)
{
nibblebill_results_t *cbt = (nibblebill_results_t *) pArg;
......@@ -151,7 +141,6 @@ static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnN
return 0;
}
#endif
static switch_status_t load_config(void)
{
......@@ -221,8 +210,7 @@ setdefaults:
set_global_nobal_action("hangup");
}
#ifdef SWITCH_HAVE_ODBC
if (globals.db_dsn) {
if (switch_odbc_available() && 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 create handle to ODBC Database!\n");
status = SWITCH_STATUS_FALSE;
......@@ -230,25 +218,23 @@ setdefaults:
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database handle!\n");
}
if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot connect to ODBC driver/database %s (user: %s / pass %s)!\n", globals.db_dsn, globals.db_username, globals.db_password);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
"Cannot connect to ODBC driver/database %s (user: %s / pass %s)!\n",
globals.db_dsn, globals.db_username, globals.db_password);
status = SWITCH_STATUS_FALSE;
goto done;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opened ODBC Database!\n");
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.db_dsn);
} else {
#endif
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "ODBC does not appear to be installed in the core. You need to run ./configure --enable-core-odbc-support\n");
#ifdef SWITCH_HAVE_ODBC
} else if (globals.db_dsn) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
"ODBC does not appear to be installed in the core. You need to run ./configure --enable-core-odbc-support\n");
}
#endif
#ifdef SWITCH_HAVE_ODBC
done:
#endif
if (xml) {
switch_xml_free(xml);
......@@ -307,49 +293,50 @@ static void transfer_call(switch_core_session_t *session, char *destination)
/* At this time, billing never succeeds if you don't have a database. */
static switch_status_t bill_event(float billamount, const char *billaccount)
{
#ifdef SWITCH_HAVE_ODBC
char sql[1024] = "";
SQLHSTMT stmt;
switch_odbc_statement_handle_t stmt;
if (!switch_odbc_available()) {
return SWITCH_STATUS_SUCCESS;
}
snprintf(sql, 1024, SQL_SAVE, globals.db_table, globals.db_column_cash, globals.db_column_cash, billamount, globals.db_column_account, billaccount);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing update query\n[%s]\n", sql);
if (switch_odbc_handle_exec(globals.master_odbc, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
char *err_str;
err_str = switch_odbc_handle_get_error(globals.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);
} else {
#endif
/* TODO: Failover to a flat/text file if DB is unavailable */
return SWITCH_STATUS_SUCCESS;
#ifdef SWITCH_HAVE_ODBC
}
switch_odbc_statement_handle_free(&stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
#endif
return SWITCH_STATUS_SUCCESS;
}
static float get_balance(const char *billaccount)
{
#ifdef SWITCH_HAVE_ODBC
char sql[1024] = "";
nibblebill_results_t pdata;
float balance = 0.00;
if (!switch_odbc_avaliable()) {
return -1.0f;
}
memset(&pdata, 0, sizeof(pdata));
snprintf(sql, 1024, SQL_LOOKUP, globals.db_column_cash, globals.db_table, globals.db_column_account, billaccount);
if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, nibblebill_callback, &pdata) != SWITCH_ODBC_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error running this query: [%s]\n", sql);
#endif
/* TODO: Return -1 for safety */
return -1.00;
#ifdef SWITCH_HAVE_ODBC
return -1.00f;
} else {
/* Successfully retrieved! */
balance = pdata.balance;
......@@ -357,7 +344,6 @@ static float get_balance(const char *billaccount)
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Retrieved current balance for account %s (balance = %f)\n", billaccount, balance);
}
#endif
return balance;
}
......@@ -879,10 +865,7 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_nibblebill_shutdown)
{
switch_event_unbind(&globals.node);
switch_core_remove_state_handler(&nibble_state_handler);
#ifdef SWITCH_HAVE_ODBC
switch_odbc_handle_disconnect(globals.master_odbc);
#endif
return SWITCH_STATUS_UNLOAD;
}
......
BASE=../../../..
include $(BASE)/build/modmake.rules
LOCAL_CFLAGS += `if test -f $(BASE)/.libs/libfreeswitch_la-switch_odbc.o ; then echo -DSWITCH_HAVE_ODBC; fi ;`
......@@ -33,9 +33,6 @@
*
*/
#include <switch.h>
#ifdef SWITCH_HAVE_ODBC
#include <switch_odbc.h>
#endif
typedef enum {
XML_ODBC_CONFIG = 0,
......@@ -68,11 +65,7 @@ static struct {
switch_xml_t templates_tag;
switch_mutex_t *mutex;
switch_memory_pool_t *pool;
#ifdef SWITCH_HAVE_ODBC
switch_odbc_handle_t *master_odbc;
#else
void *filler1;
#endif
} globals;
......@@ -406,8 +399,7 @@ logger("X");
binding = NULL;
}
#ifdef SWITCH_HAVE_ODBC
if (globals.odbc_dsn) {
if (switch_odbc_available() && globals.odbc_dsn) {
if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
......@@ -423,7 +415,6 @@ logger("X");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.odbc_dsn);
}
#endif
done:
......@@ -441,11 +432,11 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_xml_odbc_load)
switch_api_interface_t *xml_odbc_api_interface;
#ifndef SWITCH_HAVE_ODBC
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;
#endif
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;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "XML ODBC module loading...\n");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论