[commit] r13893 - in freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc: . xml_odbc_templates
Author: ledr Date: Mon Jun 22 12:59:15 2009 New Revision: 13893 Log: Variable expansion and queries work. Should implement some query validation as fs cores when it's wrong. Didn't figure out the (on..-)break-to yet. Added: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.mysql.sql (props changed) - copied unchanged from r13891, /freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.sql freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/testdata.mysql.sql (contents, props changed) - copied, changed from r13891, /freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/test_data.sql Removed: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.sql freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/test_data.sql Modified: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml Modified: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c ============================================================================== --- freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c (original) +++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/mod_xml_odbc.c Mon Jun 22 12:59:15 2009 @@ -50,8 +50,8 @@ static switch_bool_t debug = SWITCH_FALSE; -static switch_status_t xml_odbc_render_template(char *template, switch_hash_t *hash, switch_xml_t xml_out, int *off); -static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_hash_t *hash, switch_xml_t xml_out, int *off); +static switch_status_t xml_odbc_render_template(char *template, switch_event_t *params, switch_xml_t xml_out, int *off); +static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_event_t *params, switch_xml_t xml_out, int *off); #define XML_ODBC_SYNTAX "[debug_on|debug_off|render_template]" @@ -92,14 +92,15 @@ debug = SWITCH_FALSE; } else if (!strcasecmp(cmd, "render_template")) { // TODO make it configurable what themplate is rendered instead of static "not_found" - int off = 0; - switch_xml_t xml_out = NULL; - switch_hash_t *hash; - if (switch_core_hash_init(&hash, globals.pool) != SWITCH_STATUS_SUCCESS) { -// need_vars_map = -1; // does it need to be freed ? :) - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't init params hash!\n"); - } - xml_odbc_render_template("not_found", hash, xml_out, &off); + // int off = 0; + // switch_xml_t xml_out = NULL; +// switch_hash_t *hash; + // switch_event_t *params; +// if (switch_core_hash_init(&hash, globals.pool) != SWITCH_STATUS_SUCCESS) { +//// need_vars_map = -1; // does it need to be freed ? :) +// switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't init params hash!\n"); +// } + // xml_odbc_render_template("not_found", params, xml_out, &off); } else { goto usage; } @@ -122,7 +123,7 @@ switch_xml_t xml_in; switch_xml_t xml_out; int *off; - switch_hash_t *hash; + switch_event_t *params; } xml_odbc_query_helper_t; @@ -132,21 +133,21 @@ switch_xml_t xml_in_tmp; int i; - /* set all columnName/argv key/value pairs in qh->hash */ for (i = 0; i < argc; i++) { - switch_core_hash_insert(qh->hash, columnName[i], argv[i]); // does a hash insert overwrite old entries ? + switch_event_del_header(qh->params, columnName[i]); + switch_event_add_header_string(qh->params, SWITCH_STACK_BOTTOM, columnName[i], argv[i]); } /* render all xml children */ for (xml_in_tmp = qh->xml_in->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) { - xml_odbc_render_tag(xml_in_tmp, qh->hash, qh->xml_out, qh->off); + xml_odbc_render_tag(xml_in_tmp, qh->params, qh->xml_out, qh->off); } return 0; } -static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_hash_t *hash, switch_xml_t xml_out, int *off) +static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_event_t *params, switch_xml_t xml_out, int *off) { switch_xml_t xml_in_tmp = NULL; int i; @@ -155,35 +156,38 @@ if (!strcasecmp(xml_in->name, "xml-odbc-do")) { char *name = NULL; - char *value = NULL; - char *zero_rows_break_to = NULL; + char *value = NULL, *new_value = NULL; + char *empty_result_break_to = NULL; char *no_template_break_to = NULL; name = (char *) switch_xml_attr_soft(xml_in, "name"); value = (char *) switch_xml_attr_soft(xml_in, "value"); - zero_rows_break_to = (char *) switch_xml_attr_soft(xml_in, "on-zero-rows-break-to"); + empty_result_break_to = (char *) switch_xml_attr_soft(xml_in, "on-empty-result-break-to"); no_template_break_to = (char *) switch_xml_attr_soft(xml_in, "on-no-template-break-to"); - if (!strcasecmp(name, "break-to") && !switch_strlen_zero(value)) { // WHAT TO DO WITH FURTHER RENDERING LOWER ON THE STACK ?!?!?! - xml_out = NULL; - off = 0; // <- ? - if (xml_odbc_render_template(value, hash, xml_out, off) == SWITCH_STATUS_FALSE) { - if (!switch_strlen_zero(no_template_break_to)) { - xml_odbc_render_template(no_template_break_to, hash, xml_out, off); - } - } - } else if (!strcasecmp(name, "query") && !switch_strlen_zero(value)) { - // do an auto expasion on value to replace all ${foo} parts based on switch_hash_t hash - query_helper.xml_in = xml_in; - query_helper.xml_out = xml_out; - query_helper.off = off; - query_helper.hash = hash; - if (switch_odbc_handle_callback_exec(globals.master_odbc, value, xml_odbc_query_callback, &query_helper) == SWITCH_ODBC_SUCCESS) { - // nothing - } else if (!switch_strlen_zero(zero_rows_break_to)) { // if zero rows returned then switch_odbc_handle_callback_exec != SWITCH_ODBC_SUCCESS ??? + if (!switch_strlen_zero(value)) { + new_value = switch_event_expand_headers(params, value); + if (!strcasecmp(name, "break-to")) { // WHAT TO DO WITH FURTHER RENDERING LOWER ON THE STACK ?!?!?! xml_out = NULL; off = 0; // <- ? - xml_odbc_render_template(zero_rows_break_to, hash, xml_out, off); + if (xml_odbc_render_template(new_value, params, xml_out, off) == SWITCH_STATUS_FALSE) { + if (!switch_strlen_zero(no_template_break_to)) { + xml_odbc_render_template(no_template_break_to, params, xml_out, off); + } + } + } else if (!strcasecmp(name, "query")) { + query_helper.xml_in = xml_in; + query_helper.xml_out = xml_out; + query_helper.off = off; + query_helper.params = params; + + if (switch_odbc_handle_callback_exec(globals.master_odbc, new_value, xml_odbc_query_callback, &query_helper) == SWITCH_ODBC_SUCCESS) { + // nothing + } else if (!switch_strlen_zero(empty_result_break_to)) { // if zero rows returned then switch_odbc_handle_callback_exec != SWITCH_ODBC_SUCCESS ??? + xml_out = NULL; + off = 0; // <- ? + xml_odbc_render_template(empty_result_break_to, params, xml_out, off); + } } } @@ -198,18 +202,16 @@ return SWITCH_STATUS_FALSE; } -//SWITCH_DECLARE(void *) switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key); -logger((char *)switch_core_hash_find(hash, "domain")); - /* copy all attrs */ for (i = 0; xml_in->attr[i]; i+=2) { - // do an auto expasion on attr[i+1] to replace all ${foo} parts based on switch_hash_t hash - switch_xml_set_attr(xml_out, xml_in->attr[i], xml_in->attr[i+1]); + char *tmp_attr; + tmp_attr = switch_event_expand_headers(params, xml_in->attr[i+1]); + switch_xml_set_attr(xml_out, xml_in->attr[i], tmp_attr); } /* copy all children and render them */ for (xml_in_tmp = xml_in->child; xml_in_tmp; xml_in_tmp = xml_in_tmp->ordered) { - xml_odbc_render_tag(xml_in_tmp, hash, xml_out, off); + xml_odbc_render_tag(xml_in_tmp, params, xml_out, off); } } @@ -218,7 +220,7 @@ } -static switch_status_t xml_odbc_render_template(char *template_name, switch_hash_t *hash, switch_xml_t xml_out, int *off) +static switch_status_t xml_odbc_render_template(char *template_name, switch_event_t *params, switch_xml_t xml_out, int *off) { switch_xml_t template_tag = NULL, sub_tag = NULL; @@ -226,7 +228,7 @@ char *template_tag_name = (char*) switch_xml_attr_soft(template_tag, "name"); if (!strcmp(template_tag_name, template_name)) { for (sub_tag = template_tag->child; sub_tag; sub_tag = sub_tag->ordered) { - xml_odbc_render_tag(sub_tag, hash, xml_out, off); + xml_odbc_render_tag(sub_tag, params, xml_out, off); return SWITCH_STATUS_SUCCESS; } } @@ -247,7 +249,6 @@ xml_odbc_query_type_t query_type; - switch_hash_t *hash; int off = 0, ret = 1; @@ -270,22 +271,12 @@ if (params) { if ((hi = params->headers)) { - - /* initialize hash */ - if (switch_core_hash_init(&hash, globals.pool) != SWITCH_STATUS_SUCCESS) { -// need_vars_map = -1; // does it need to be freed ? :) - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't init params hash!\n"); - } - for (; hi; hi = hi->next) { if (debug == SWITCH_TRUE) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG in xml_odbc_search, header [%s]=[%s]\n", hi->name, hi->value); } - switch_core_hash_insert(hash, hi->name, hi->value); // prefix with 'h_' to avoid collision with keys returned by SELECT's ? } - - xml_odbc_render_template(globals.template, hash, xml_out, &off); // TODO globals.template should be replace with something specific for this section - + xml_odbc_render_template(globals.template, params, xml_out, &off); // TODO globals.template should be replace with something specific for this section } } else { goto cleanup; Copied: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/testdata.mysql.sql (from r13891, /freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/test_data.sql) ============================================================================== --- /freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/test_data.sql (original) +++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/testdata.mysql.sql Mon Jun 22 12:59:15 2009 @@ -3,6 +3,6 @@ INSERT INTO `dir_domain_variables` (`id`, `dir_domain_id`, `name`, `value`) VALUES (1, 1, 'dom_variable_name', 'dom_variable_value'); INSERT INTO `dir_groups` (`id`, `name`) VALUES (1, 'prepay'), (2, 'annoying_customers'); INSERT INTO `dir_groups_users` (`dir_group_id`, `dir_user_id`) VALUES (1, 1), (2, 1); -INSERT INTO `dir_users` (`id`, `dir_domain_id`, `username`, `cidr`, `mailbox`, `number-alias`) VALUES (1, 1, 'someuser', NULL, 'leon@toyos.nl', '0031320227470'); +INSERT INTO `dir_users` (`id`, `dir_domain_id`, `username`, `enabled`, `cidr`, `mailbox`, `number-alias`) VALUES (1, 1, 'someuser', 1, NULL, 'test@test.com', '1234'); INSERT INTO `dir_user_params` (`id`, `dir_user_id`, `name`, `value`) VALUES (1, 1, 'password', 'topsecret'), (2, 1, 'vm-password', '0000'); INSERT INTO `dir_user_variables` (`id`, `dir_user_id`, `name`, `value`) VALUES (1, 1, 'accountcode', '1234'), (2, 1, 'user_context', 'default'), (3, 1, 'vm_extension', '1234'); Modified: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml ============================================================================== --- freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml (original) +++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc.conf.xml Mon Jun 22 12:59:15 2009 @@ -9,9 +9,10 @@ --> <binding name="directory"> <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/> - <!-- <param name="template" value="directory"/> --> + <param name="template" value="directory"/> <!-- <param name="template" value="not_found"/> --> - <param name="template" value="someuser"/> + <!-- <param name="template" value="someuser"/> --> + <!-- <param name="template" value="testsql"/> --> </binding> <!-- <binding name="dialplan"> @@ -59,7 +60,15 @@ </document> </template> - <template name="someuser"> + <template name="testsql"> <!-- for testing only --> + <document type="freeswitch/xml"> + <xml-odbc-do name="query" value="SELECT name, value FROM dir_user_params WHERE dir_user_id='1';"> + <param name="${name}" value="${value}"/> + </xml-odbc-do> + </document> + </template> + + <template name="someuser"> <!-- for testing only --> <document type="freeswitch/xml"> <section name="directory"> <domain name="${domain}"> Modified: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml ============================================================================== --- freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml (original) +++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml Mon Jun 22 12:59:15 2009 @@ -1,22 +1,21 @@ <include> <template name="directory"> <document type="freeswitch/xml"> - <xml-odbc-do name="query" on-zero-rows-break-to="not_found" value=" + <xml-odbc-do name="query" on-empty-result-break-to="not_found" value=" SELECT dir_domains.id AS domain_id, dir_users.id AS user_id, - dir_users.username, dir_users.cidr, dir_users.mailbox, - dir_users.number-alias + dir_users.`number-alias` FROM dir_domains, dir_users WHERE - dir_users.${key} = '${user}' AND + dir_users.username = '${user}' AND dir_domains.name = '${domain}' AND - dir_users.domain_id = dir_domains.id AND - dir_users.enabled = 1;"/> <!-- TODO ${key} can be id when username is meant, but id is also int prim key on table --> + dir_users.dir_domain_id = dir_domains.id AND + dir_users.enabled = '1';"/> <section name="directory"> <domain name="${domain}"> <params> @@ -46,7 +45,7 @@ <users> <user id="${user}" mailbox="${mailbox}" cidr="${cidr}" number-alias="${number-alias}"> <params> - <xml-odbc-do name="query" on-zero-rows-break-to="not_found" value=" + <xml-odbc-do name="query" on-empty-result-break-to="not_found" value=" SELECT name, value FROM @@ -77,10 +76,10 @@ dir_groups, dir_groups_users WHERE - dir_groups_users.dir_group_id + dir_groups_users.dir_group_id = dir_groups.id AND dir_user_id = '${user_id}';"> <group name="${group_name}"> - <user id="${username}" type="pointer"/> + <user id="${user}" type="pointer"/> </group> </xml-odbc-do> </groups>
participants (1)
-
FreeSWITCH SVN