提交 9383fbe9 authored 作者: Leon de Rooij's avatar Leon de Rooij

Will it work ?


git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@14837 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 89b100f6
...@@ -38,7 +38,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load); ...@@ -38,7 +38,10 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load);
SWITCH_MODULE_DEFINITION(mod_odbc_query, mod_odbc_query_load, mod_odbc_query_shutdown, NULL); SWITCH_MODULE_DEFINITION(mod_odbc_query, mod_odbc_query_load, mod_odbc_query_shutdown, NULL);
static struct { static struct {
char *odbc_dsn;
switch_memory_pool_t *pool;
switch_odbc_handle_t *master_odbc; switch_odbc_handle_t *master_odbc;
switch_xml_t queries;
} globals; } globals;
static char* switch_channel_expand_variables_by_pool(switch_memory_pool_t *pool, switch_channel_t *channel, const char *in) static char* switch_channel_expand_variables_by_pool(switch_memory_pool_t *pool, switch_channel_t *channel, const char *in)
...@@ -52,30 +55,6 @@ static char* switch_channel_expand_variables_by_pool(switch_memory_pool_t *pool, ...@@ -52,30 +55,6 @@ static char* switch_channel_expand_variables_by_pool(switch_memory_pool_t *pool,
return out; return out;
} }
static switch_xml_config_item_t instructions[] = {
/* parameter name, type, reloadable, pointer, default value, options structure */
SWITCH_CONFIG_ITEM_END()
};
static switch_status_t do_config(switch_bool_t reload)
{
memset(&globals, 0, sizeof(globals));
if (switch_xml_config_parse_module_settings("odbc_query.conf", reload, instructions) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not open odbc_query.conf\n");
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(odbc_query_function)
{
do_config(SWITCH_TRUE);
return SWITCH_STATUS_SUCCESS;
}
static int odbc_query_callback(void *pArg, int argc, char **argv, char **columnName) static int odbc_query_callback(void *pArg, int argc, char **argv, char **columnName)
{ {
switch_channel_t *channel = (switch_channel_t *) pArg; switch_channel_t *channel = (switch_channel_t *) pArg;
...@@ -97,6 +76,7 @@ SWITCH_STANDARD_APP(odbc_query_app_function) ...@@ -97,6 +76,7 @@ SWITCH_STANDARD_APP(odbc_query_app_function)
char *expanded_query = NULL; char *expanded_query = NULL;
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
switch_memory_pool_t *pool = switch_core_session_get_pool(session); switch_memory_pool_t *pool = switch_core_session_get_pool(session);
switch_xml_t stored_query;
if (!channel) { if (!channel) {
return; return;
...@@ -106,6 +86,11 @@ SWITCH_STANDARD_APP(odbc_query_app_function) ...@@ -106,6 +86,11 @@ SWITCH_STANDARD_APP(odbc_query_app_function)
if (strchr(data, ' ')) { if (strchr(data, ' ')) {
query = switch_core_session_strdup(session, data); query = switch_core_session_strdup(session, data);
} else { } else {
for (stored_query = switch_xml_child(globals.queries, "query"); stored_query; stored_query = stored_query->next) {
if (!strcmp(switch_xml_attr_soft(stored_query, "name"), data)) {
query = switch_core_session_strdup(session, switch_xml_attr_soft(stored_query, "value"));
}
}
} }
expanded_query = switch_channel_expand_variables_by_pool(pool, channel, query); expanded_query = switch_channel_expand_variables_by_pool(pool, channel, query);
...@@ -115,6 +100,81 @@ SWITCH_STANDARD_APP(odbc_query_app_function) ...@@ -115,6 +100,81 @@ SWITCH_STANDARD_APP(odbc_query_app_function)
} }
} }
static switch_status_t do_config(switch_bool_t reload)
{
char *cf = "odbc_query.conf";
switch_xml_t cfg, xml = NULL, param, settings, queries;
char *odbc_user = NULL;
char *odbc_pass = NULL;
memset(&globals, 0, sizeof(globals));
switch_status_t status = SWITCH_STATUS_FALSE;
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
status = SWITCH_STATUS_FALSE;
goto done;
}
/* get settings - only odbc-dsn for now */
if ((settings = switch_xml_child(cfg, "settings"))) {
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value");
if (!strcasecmp(var, "odbc-dsn")) {
globals.odbc_dsn = switch_core_strdup(globals.pool, val);
if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
*odbc_user++ = '\0';
if ((odbc_pass = strchr(odbc_user, ':'))) {
*odbc_pass++ = '\0';
}
}
}
}
}
/* check if odbc_dsn is set */
if (!globals.odbc_dsn) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No odbc-dsn setting is set!\n");
goto done;
}
/* make odbc connection */
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");
goto done;
}
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");
goto done;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", globals.odbc_dsn);
}
/* copy entire queries xml section to globals */
if ((queries = switch_xml_child(cfg, "queries"))) {
memcpy(&globals.queries, &queries, sizeof(queries));
}
status = SWITCH_STATUS_SUCCESS;
done:
switch_xml_free(xml);
return status;
}
SWITCH_STANDARD_API(odbc_query_function)
{
do_config(SWITCH_TRUE);
return SWITCH_STATUS_SUCCESS;
}
/* Macro expands to: switch_status_t mod_odbc_query_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */ /* Macro expands to: switch_status_t mod_odbc_query_load(switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool) */
SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load) SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load)
{ {
...@@ -140,8 +200,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load) ...@@ -140,8 +200,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_odbc_query_load)
Macro expands to: switch_status_t mod_odbc_query_shutdown() */ Macro expands to: switch_status_t mod_odbc_query_shutdown() */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_odbc_query_shutdown) SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_odbc_query_shutdown)
{ {
/* Cleanup dynamically allocated config settings */ switch_odbc_handle_disconnect(globals.master_odbc);
switch_xml_config_cleanup(instructions); switch_odbc_handle_destroy(&globals.master_odbc);
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论