[Freeswitch-trunk] [commit] r13885 - in freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc: . xml_odbc_templates

FreeSWITCH SVN ledr at freeswitch.org
Sun Jun 21 07:21:23 PDT 2009


Author: ledr
Date: Sun Jun 21 09:21:22 2009
New Revision: 13885

Log:
Created templating based on sql queries to generate XML - not finished yet


Added:
   freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/
   freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/console.conf.xml
   freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/dialplan.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/structure.sql
   freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc.conf.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	Sun Jun 21 09:21:22 2009
@@ -38,7 +38,10 @@
 #endif
 
 typedef enum {
-	XML_ODBC_DIRECTORY
+	XML_ODBC_CONFIG = 0,
+	XML_ODBC_DIRECTORY = 0,
+	XML_ODBC_DIALPLAN = 0,
+	XML_ODBC_PHRASE = 0
 } xml_odbc_query_type_t;
 
 SWITCH_MODULE_LOAD_FUNCTION(mod_xml_odbc_load);
@@ -47,7 +50,31 @@
 
 static switch_bool_t debug = SWITCH_FALSE;
 
-#define XML_ODBC_SYNTAX "[debug_on|debug_off]"
+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);
+
+#define XML_ODBC_SYNTAX "[debug_on|debug_off|render_template]"
+
+static int logi = 0;
+static void logger(char *str)
+{
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG[%i] [%s]\n", ++logi, str);
+}
+
+static struct {
+	char *dbname;
+	char *odbc_dsn;
+	char *template;
+	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;
+
 
 SWITCH_STANDARD_API(xml_odbc_function)
 {
@@ -63,6 +90,16 @@
 		debug = SWITCH_TRUE;
 	} else if (!strcasecmp(cmd, "debug_off")) {
 		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);
 	} else {
 		goto usage;
 	}
@@ -80,122 +117,152 @@
 	char *bindings;
 } xml_binding_t;
 
-static struct {
-	char *dbname;
-	char *odbc_dsn;
-	char *query_domain_key_user;
-	char *query_domain_params;
-	char *query_domain_variables;
-	char *query_user_attrs;
-	char *query_user_params;
-	char *query_user_variables;
-	char *query_groups;
-	switch_mutex_t *mutex;
-	switch_memory_pool_t *pool;
-#ifdef SWITCH_HAVE_ODBC
-	switch_odbc_handle_t *master_odbc;
-#else
-	void *filler1;
-#endif
-} globals;
 
-typedef struct did_uid_helper {
-	char *domain_id;
-	char *user_id;
-} did_uid_helper_t;
+typedef struct xml_odbc_query_helper {
+	switch_xml_t xml_in;
+	switch_xml_t xml_out;
+	int *off;
+	switch_hash_t *hash;
+} xml_odbc_query_helper_t;
 
-static int did_uid_callback(void *pArg, int argc, char **argv, char **columnNames)
+
+static int xml_odbc_query_callback(void *pArg, int argc, char **argv, char **columnName)
 {
-	did_uid_helper_t *h = (did_uid_helper_t *) pArg;
+	xml_odbc_query_helper_t *qh = (xml_odbc_query_helper_t *) pArg;
+	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 ?
+	}
 
-	h->domain_id = strdup(argv[0]);
-	h->user_id = strdup(argv[1]);
+	/* 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);
+	}
 
 	return 0;
 }
 
-static int set_xml_attr_callback(void *pArg, int argc, char **argv, char **columnNames)
+
+static switch_status_t xml_odbc_render_tag(switch_xml_t xml_in, switch_hash_t *hash, switch_xml_t xml_out, int *off)
 {
-	switch_xml_t *xml = (switch_xml_t *) pArg;
-	switch_xml_set_attr_d(*xml, argv[0], argv[1]);
-	return 0;
-}
+	switch_xml_t xml_in_tmp = NULL;
+	int i;
 
-typedef struct xml_helper {
-	switch_xml_t xml;
-	char *str;
-	int off;
-} xml_helper_t;
+	xml_odbc_query_helper_t query_helper;
 
-static int add_xml_child_with_nvpair_callback(void *pArg, int argc, char **argv, char **columnNames)
-{
-	xml_helper_t *h = (xml_helper_t *) pArg;
-	switch_xml_t xml = NULL;
+    if (!strcasecmp(xml_in->name, "xml-odbc-do")) {
+		char *name = NULL;
+		char *value = NULL;
+		char *zero_rows_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");
+		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 ??? 
+				xml_out = NULL;
+				off = 0; // <- ?
+				xml_odbc_render_template(zero_rows_break_to, hash, xml_out, off);
+			}
+		}
 
-	if ((xml = switch_xml_add_child_d(h->xml, h->str, h->off++))) {
-		switch_xml_set_attr_d(xml, "name", argv[0]);
-		switch_xml_set_attr_d(xml, "value", argv[1]);
-	}
+	} else {
 
-	return 0;
-}
+		/* set name if current root node */
+		if (switch_strlen_zero(xml_out->name)) { // I should match here on off instead of on name == "" ?
+			xml_out->name = strdup(xml_in->name);
+
+		/* or create a child */
+		} else if (!(xml_out = switch_xml_add_child_d(xml_out, xml_in->name, *off++))) {
+			return SWITCH_STATUS_FALSE;
+		}
 
-static int add_xml_group_and_user_callback(void *pArg, int argc, char **argv, char **columnName)
-{
-	xml_helper_t *h = (xml_helper_t *) pArg;
-	switch_xml_t xml = NULL;
+//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"));
 
-	if ((xml = switch_xml_add_child_d(h->xml, "group", h->off++))) {
-		switch_xml_set_attr_d(xml, "name", argv[0]);
-		if ((xml = switch_xml_add_child_d(xml, "users", h->off++))) {
-			if ((xml = switch_xml_add_child_d(xml, "user", h->off++))) {
-				switch_xml_set_attr_d(xml, "id", h->str);
-				switch_xml_set_attr_d(xml, "type", "pointer");
-			}
+		/* 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]);
+		}
+
+		/* 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);
 		}
+
 	}
 
-	return 0;
+	return SWITCH_STATUS_SUCCESS;
 }
 
-static int xml_odbc_result_not_found(switch_xml_t xml, int *off)
+
+static switch_status_t xml_odbc_render_template(char *template_name, switch_hash_t *hash, switch_xml_t xml_out, int *off)
 {
-	switch_xml_t sub = NULL;
+	switch_xml_t template_tag = NULL, sub_tag = NULL;
 
-	if ((sub = switch_xml_add_child_d(xml, "section", *off++))) {
-		switch_xml_set_attr_d(sub, "name", "result");
-		if ((sub = switch_xml_add_child_d(sub, "result", *off++))) {
-			switch_xml_set_attr_d(sub, "status", "not found");
+	for (template_tag = switch_xml_child(globals.templates_tag, "template"); template_tag; template_tag = template_tag->next) {
+		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);
+				return SWITCH_STATUS_SUCCESS;
+			}
 		}
 	}
-
-	return 0;
+	return SWITCH_STATUS_FALSE;
 }
 
+
 static switch_xml_t xml_odbc_search(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, void *user_data)
 {
 	xml_binding_t *binding = (xml_binding_t *) user_data;
 	switch_event_header_t *hi;
 
-	switch_xml_t xml = NULL, sub = NULL, sub2 = NULL, sub3 = NULL;
-
-	char *dir_user = NULL, *dir_domain = NULL, *dir_key = NULL;
-	char *sql;
-
-	int off = 0, ret = 1;
+	switch_xml_t xml_out = NULL;
+	if ((xml_out = switch_xml_new(""))) {
+		//switch_xml_set_attr_d(xml_out, "type", "freeswitch/xml");
+	}
 
 	xml_odbc_query_type_t query_type;
 
-	did_uid_helper_t pdata;
-	xml_helper_t pdata2;
+	switch_hash_t *hash;
+
+	int off = 0, ret = 1;
 
 	if (!binding) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No bindings... sorry bud returning now\n");
 		return NULL;
 	}
-
-	if (!strcmp(section, "directory")) {
+	if (!strcmp(section, "configuration")) {
+		query_type = XML_ODBC_CONFIG;
+	} else if (!strcmp(section, "directory")) {
 		query_type = XML_ODBC_DIRECTORY;
+	} else if (!strcmp(section, "dialplan")) {
+		query_type = XML_ODBC_DIALPLAN;
+	} else if (!strcmp(section, "phrases")) {
+		query_type = XML_ODBC_PHRASE;
 	} else {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid section\n");
 		return NULL;
@@ -203,147 +270,29 @@
 
 	if (params) {
 		if ((hi = params->headers)) {
-			for (; hi; hi = hi->next) {
-				switch (query_type) {
-				case XML_ODBC_DIRECTORY:
-
-//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "DEBUG in xml_odbc_search with header name=[%s], value=[%s]\n", hi->name, hi->value);
 
-					if (!strcmp(hi->name, "user")) {
-						dir_user = strdup(hi->value);
-					} else if (!strcmp(hi->name, "domain")) {
-						dir_domain = strdup(hi->value);
-					} else if (!strcmp(hi->name, "key")) {
-						dir_key = strdup(hi->value);
-					}
-					break;
-				}
+			/* 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");
 			}
-			switch (query_type) {
-			case XML_ODBC_DIRECTORY: 
-				//if (dir_user && dir_domain) {
-				if (dir_domain) {
-					if ((xml = switch_xml_new("document"))) {
-						switch_xml_set_attr_d(xml, "type", "freeswitch/xml");
-
-						if (!dir_user) {
-//						  sql = switch_mprintf("SELECT count(*) FROM dir_domains WHERE name = '%q';", dir_domain);
-//						  if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, count_callback, &pdata10741) == SWITCH_ODBC_SUCCESS) {
-// TODO: Factor out the domain search thing (with params and variables) from below, and call it from here as well..
-//							xml_odbc_result_not_found(xml, &off);
-//						  } else {
-							xml_odbc_result_not_found(xml, &off);
-//						  }
-
-						} else {
-							sql = switch_mprintf(globals.query_domain_key_user, dir_domain, dir_key, dir_user);
-							if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, did_uid_callback, &pdata) == SWITCH_ODBC_SUCCESS) {
-								if (!pdata.domain_id || !pdata.user_id) {
-									xml_odbc_result_not_found(xml, &off);
-
-								} else {
-									if ((sub = switch_xml_add_child_d(xml, "section", off++))) {
-										switch_xml_set_attr_d(sub, "name", "directory");
-
-										if ((sub = switch_xml_add_child_d(sub, "domain", off++))) {
-											switch_xml_set_attr_d(sub, "name", dir_domain);
-
-											if ((sub2 = switch_xml_add_child_d(sub, "params", off++))) {
-												sql = switch_mprintf(globals.query_domain_params, pdata.domain_id);
-												pdata2.xml = sub2;
-												pdata2.off = off;
-												pdata2.str = "param";
-												if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, add_xml_child_with_nvpair_callback, &pdata2) == SWITCH_ODBC_SUCCESS) {
-												}
-											}
-
-											if ((sub2 = switch_xml_add_child_d(sub, "variables", off++))) {
-												sql = switch_mprintf(globals.query_domain_variables, pdata.domain_id);
-												pdata2.xml = sub2;
-												pdata2.off = off;
-												pdata2.str = "variable";
-												if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, add_xml_child_with_nvpair_callback, &pdata2) == SWITCH_ODBC_SUCCESS) {
-												}
-											}
-
-											if ((sub = switch_xml_add_child_d(sub, "groups", off++))) {
-
-												if ((sub2 = switch_xml_add_child_d(sub, "group", off++))) {
-													switch_xml_set_attr_d(sub2, "name", "default");
-
-													if ((sub2 = switch_xml_add_child_d(sub2, "users", off++))) {
-
-														if ((sub2 = switch_xml_add_child_d(sub2, "user", off++))) {
-
-															sql = switch_mprintf(globals.query_user_attrs, pdata.user_id);
-															if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, set_xml_attr_callback, &sub2)) {
-															}
-
-															if ((sub3 = switch_xml_add_child_d(sub2, "params", off++))) {
-																sql = switch_mprintf(globals.query_user_params, pdata.user_id);
-																pdata2.xml = sub3;
-																pdata2.off = off;
-																pdata2.str = "param";
-																if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, add_xml_child_with_nvpair_callback, &pdata2) == SWITCH_ODBC_SUCCESS) {
-																}
-															}
-
-															if ((sub3 = switch_xml_add_child_d(sub2, "variables", off++))) {
-																sql = switch_mprintf(globals.query_user_variables, pdata.user_id);
-																pdata2.xml = sub3;
-																pdata2.off = off;
-																pdata2.str = "variable";
-																if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, add_xml_child_with_nvpair_callback, &pdata2) == SWITCH_ODBC_SUCCESS) {
-																}
-															}
-
-														}
-
-													}
-
-												}
-
-												sql = switch_mprintf(globals.query_groups, pdata.user_id);
-												pdata2.xml = sub;
-												pdata2.off = off;
-												pdata2.str = dir_user;
-												if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, add_xml_group_and_user_callback, &pdata2) == SWITCH_ODBC_SUCCESS) {
-												}
-
-											}
-										}
-									}
-								}
-							}
-						}
-					}
-
-					if (debug == SWITCH_TRUE) {
-						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Debug dump of XML generated:\n%s", switch_xml_toxml(xml, SWITCH_FALSE));
-					}
-
-					free(dir_user);
-					dir_user = NULL;
-
-					free(dir_key);
-					dir_key = NULL;
-
-					free(dir_domain);
-					dir_domain = NULL;
-
-				} else {
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
-						"Something bad happened during the query construction phase likely exten(%s) or domain(%s) is null\n", dir_user, dir_domain);
-					goto cleanup;
 
+			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);
 				}
-				break;
+				switch_core_hash_insert(hash, hi->name, hi->value); // prefix with 'h_' to avoid collision with keys returned by SELECT's ?
 			}
 
-		} else {
-			goto cleanup;
+			xml_odbc_render_template(globals.template, hash, xml_out, &off); // TODO globals.template should be replace with something specific for this section
 
 		}
+	} else {
+		goto cleanup;
+	}
+
+	if (debug == SWITCH_TRUE) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Debug dump of XML generated:\n%s", switch_xml_toxml(xml_out, SWITCH_FALSE));
 	}
 
 	ret = 0;
@@ -351,26 +300,28 @@
   cleanup:
 
 	if (ret) {
-		switch_xml_free(xml);
+		switch_xml_free(xml_out);
 		return NULL;
 	}
 	
-	return xml;
+	return xml_out;
 }
 
+
 static switch_status_t do_config()
 {
 	char *cf = "xml_odbc.conf";
-	switch_xml_t cfg, xml, bindings_tag, binding_tag, param;
+	switch_xml_t cfg, xml, bindings_tag, binding_tag, templates_tag, param;
 	xml_binding_t *binding = NULL;
 
-	//switch_core_db_t *db;
 	switch_status_t status = SWITCH_STATUS_SUCCESS;
 
 	char *odbc_user = NULL;
 	char *odbc_pass = NULL;
 	char *sql = NULL;
 
+logger("X");
+
 	if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
 		return SWITCH_STATUS_TERM;
@@ -381,6 +332,13 @@
 		goto done;
 	}
 
+	if (!(templates_tag = switch_xml_child(cfg, "templates"))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing <templates> tag!\n");
+		goto done;
+	}
+
+	globals.templates_tag = templates_tag;
+
 	for (binding_tag = switch_xml_child(bindings_tag, "binding"); binding_tag; binding_tag = binding_tag->next) {
 		char *bname = (char*) switch_xml_attr_soft(binding_tag, "name");
 
@@ -399,20 +357,8 @@
 						*odbc_pass++ = '\0';
 					}
 				}
-			} else if (!strcasecmp(var, "query-domain-key-user") && !switch_strlen_zero(val)) {
-				globals.query_domain_key_user = strdup(val);
-			} else if (!strcasecmp(var, "query-domain-params") && !switch_strlen_zero(val)) {
-				globals.query_domain_params = strdup(val);
-			} else if (!strcasecmp(var, "query-domain-variables") && !switch_strlen_zero(val)) {
-				globals.query_domain_variables = strdup(val);
-			} else if (!strcasecmp(var, "query-user-attrs") && !switch_strlen_zero(val)) {
-				globals.query_user_attrs = strdup(val);
-			} else if (!strcasecmp(var, "query-user-params") && !switch_strlen_zero(val)) {
-				globals.query_user_params = strdup(val);
-			} else if (!strcasecmp(var, "query-user-variables") && !switch_strlen_zero(val)) {
-				globals.query_user_variables = strdup(val);
-			} else if (!strcasecmp(var, "query-groups") && !switch_strlen_zero(val)) {
-				globals.query_groups = strdup(val);
+			} else if (!strcasecmp(var, "template") && !switch_strlen_zero(val)) {
+				globals.template = strdup(val);
 			}
 		}
 
@@ -459,6 +405,7 @@
 	return status;
 }
 
+
 SWITCH_MODULE_LOAD_FUNCTION(mod_xml_odbc_load)
 {
 
@@ -480,6 +427,7 @@
 	SWITCH_ADD_API(xml_odbc_api_interface, "xml_odbc", "XML ODBC", xml_odbc_function, XML_ODBC_SYNTAX);
 	switch_console_set_complete("add xml_odbc debug_on");
 	switch_console_set_complete("add xml_odbc debug_off");
+	switch_console_set_complete("add xml_odbc render_template");
 
 	if (do_config() != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load xml_odbc config file\n");
@@ -490,11 +438,13 @@
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
 SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_xml_odbc_shutdown)
 {
 	return SWITCH_STATUS_SUCCESS;
 }
 
+
 /* For Emacs:
  * Local Variables:
  * mode:c
@@ -503,5 +453,5 @@
  * c-basic-offset:4
  * End:
  * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
  */

Modified: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.sql
==============================================================================
--- freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.sql	(original)
+++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/structure.sql	Sun Jun 21 09:21:22 2009
@@ -39,21 +39,19 @@
 CREATE TABLE `dir_users` (
   `id` INT NOT NULL AUTO_INCREMENT,
   `dir_domain_id` INT NOT NULL,
+  `enabled` TINYINT(1) NOT NULL,
+  `username` VARCHAR(255) NOT NULL,
+  `cidr` VARCHAR(255),
+  `mailbox` VARCHAR(255),
+  `number-alias` VARCHAR(255),
   PRIMARY KEY (`id`),
+  KEY `dir_users_username` (`username`),
+  KEY `dir_users_cidr` (`cidr`),
+  KEY `dir_users_mailbox` (`mailbox`),
+  KEY `dir_users_number-alias` (`number-alias`),
   FOREIGN KEY (`dir_domain_id`) REFERENCES `dir_domains` (`id`)
 );
 
-CREATE TABLE `dir_user_attrs` (
-  `id` INT  NOT NULL AUTO_INCREMENT,
-  `dir_user_id` INT NOT NULL,
-  `name` VARCHAR(255) NOT NULL,
-  `value` VARCHAR(255) NOT NULL,
-  PRIMARY KEY  (`id`),
-  FOREIGN KEY (`dir_user_id`) REFERENCES `dir_users` (`id`),
-  KEY `dir_user_attrs_name` (`name`),
-  KEY `dir_user_attrs_value` (`value`)
-);
-
 CREATE TABLE `dir_user_params` (
   `id` INT NOT NULL AUTO_INCREMENT,
   `dir_user_id` INT NOT NULL,

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	Sun Jun 21 09:21:22 2009
@@ -1,35 +1,97 @@
 <configuration name="xml_odbc.conf" description="XML ODBC Configuration">
+
   <bindings>
+<!--
+    <binding name="configuration">
+      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
+      <param name="template" value="configuration"/>
+    </binding>
+-->
     <binding name="directory">
       <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
+      <!-- <param name="template" value="directory"/> -->
+      <!-- <param name="template" value="not_found"/> -->
+      <param name="template" value="someuser"/>
+    </binding>
+<!--
+    <binding name="dialplan">
+      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
+      <param name="template" value="dialplan"/>
+    </binding>
 
-      <param name="query-domain-key-user" value="SELECT
-                                                   dir_domains.id AS domain_id,
-                                                   dir_users.id AS user_id
-                                                 FROM
-                                                   dir_domains,
-                                                   dir_users,
-                                                   dir_user_attrs
-                                                 WHERE
-                                                   dir_domains.name = '%q' AND
-                                                   dir_user_attrs.name = '%q' AND
-                                                   dir_user_attrs.value = '%q' AND
-                                                   dir_domains.id = dir_users.dir_domain_id AND
-                                                   dir_users.id = dir_user_attrs.dir_user_id;"/>
-
-      <param name="query-domain-params" value="SELECT name, value FROM dir_domain_params WHERE dir_domain_id = '%q';"/>
-
-      <param name="query-domain-variables" value="SELECT name, value FROM dir_domain_variables WHERE dir_domain_id = '%q';"/>
-
-      <param name="query-user-attrs" value="SELECT name, value FROM dir_user_attrs WHERE dir_user_id = '%q';"/>
+    <binding name="phrases">
+      <param name="odbc-dsn" value="freeswitch:freeswitch:secret"/>
+      <param name="template" value="phrases"/>
+    </binding>
+-->
+  </bindings>
 
-      <param name="query-user-params" value="SELECT name, value FROM dir_user_params WHERE dir_user_id = '%q';"/>
+  <templates>
 
-      <param name="query-user-variables" value="SELECT name, value FROM dir_user_variables WHERE dir_user_id = '%q';"/>
+    <template name="configuration">
+      <xml-odbc-do name="break-to" value="${section}" on-no-template-break-to="simple_configuration"/>
+    </template>
+
+    <template name="simple_configuration">
+      <document type="freeswitch/xml"> <!-- should this tag be automatically rendered first on xml_out and left out here ? -->
+        <configuration name="${section}">
+          <settings>
+            <xml-odbc-do name="query" value="
+              SELECT
+                name, value
+              FROM
+                cnf_settings
+              WHERE
+                hostname = '${hostname}' AND
+                section = '${section}';">
+              <param name="${name}" value="${value}"/>
+            </xml-odbc-do>
+          </settings>
+        </configuration>
+      </document>
+    </template>
+  
+    <template name="not_found">
+      <document type="freeswitch/xml">
+        <section name="result">
+          <result status="not found"/>
+        </section>
+      </document>
+    </template>
+
+    <template name="someuser">
+      <document type="freeswitch/xml">
+        <section name="directory">
+          <domain name="${domain}">
+            <params>
+              <param name="some_domain_param" value="some_domain_value"/>
+            </params>
+            <variables>
+            </variables>
+            <groups>
+              <group name="default">
+                <users>
+                  <user id="someuser" mailbox="leon at toyos.nl" cidr="" number-alias="0031320227470">
+                    <params>
+                      <param name="password" value="secret"/>
+                    </params>
+                    <variables>
+                      <variable name="accountcode" value="1234"/>
+                    </variables>
+                  </user>
+                </users>
+              </group>
+              <group name="prepay">
+                <user id="someuser" type="pointer"/>
+              </group>
+            </groups>
+          </domain>
+        </section>
+      </document>
+    </template>
+  
+    <X-PRE-PROCESS cmd="include" data="xml_odbc_templates/*.xml"/>
 
-      <param name="query-groups" value="SELECT dir_groups.name FROM dir_groups, dir_groups_users
-                                        WHERE dir_groups_users.dir_user_id = '%q' AND dir_groups_users.dir_group_id = dir_groups.id;"/>
+  </templates>
 
-    </binding>
-  </bindings>
 </configuration>

Added: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/console.conf.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/console.conf.xml	Sun Jun 21 09:21:22 2009
@@ -0,0 +1,31 @@
+<include>
+  <template name="console.conf">
+    <document type="freeswitch/xml">
+      <configuration name="console.conf" description="Console Logger">
+        <mappings>
+          <xml-odbc-do name="query" value="
+            SELECT
+              name, value
+            FROM
+              cnf_console_mappings
+            WHERE
+              hostname = '${hostname}';">
+            <mapping name="${name}" value="${value}"/>
+          </xml-odbc-do>
+        </mappings>
+        <settings>
+          <xml-odbc-do name="query" value="
+            SELECT
+              name, value
+            FROM
+              cnf_settings
+            WHERE
+              hostname = '${hostname}' AND
+              section = '${section}';">
+            <param name="${name}" value="${value}"/>
+          </xml-odbc-do>
+        </settings>
+      </configuration>
+    </document>
+  </template>
+</include>

Added: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/dialplan.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/dialplan.xml	Sun Jun 21 09:21:22 2009
@@ -0,0 +1,17 @@
+<include> <!-- NOT FINISHED ! -->
+  <template name="dialplan">
+    <document type="freeswitch/xml">
+      <xml-odbc-do name="query" on-zero-rows-break-to="not_found" value="
+        SELECT * FROM dialplan WHERE dialed_number='${dialed_number}';"/>
+      <section name="dialplan" description="RE Dial Plan For FreeSwitch">
+        <context name="default">
+          <extension name="test9">
+            <condition field="destination_number" expression="^83789$">
+              <action application="bridge" data="iax/guest at conference.freeswitch.org/888"/>
+            </condition>
+          </extension>
+        </context>
+      </section>
+    </document>
+  </template>
+</include>

Added: freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/scripts/contrib/ledr/mod_xml_odbc/xml_odbc_templates/directory.xml	Sun Jun 21 09:21:22 2009
@@ -0,0 +1,91 @@
+<include>
+  <template name="directory">
+    <document type="freeswitch/xml">
+      <xml-odbc-do name="query" on-zero-rows-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
+        FROM
+          dir_domains,
+          dir_users
+        WHERE
+          dir_users.${key} = '${value}' AND
+          dir_domains.name = '${domain_name}' AND
+          dir_users.domain_id = dir_domains.id AND
+          dir_users.enabled = 1;"/>
+      <section name="directory">
+        <domain name="${domain_name}">
+          <params>
+            <xml-odbc-do name="query" value="
+              SELECT
+                name, value
+              FROM
+                dir_domain_params
+              WHERE
+                dir_domain_id = '${domain_id}';">
+              <param name="${name}" value="${value}"/>
+            </xml-odbc-do>
+          </params>
+          <variables>
+            <xml-odbc-do name="query" value="
+              SELECT
+                name, value
+              FROM
+                dir_domain_variables
+              WHERE
+                dir_domain_id = '${domain_id}';">
+              <variable name="${name}" value="${value}"/>
+            </xml-odbc-do>
+          </variables>
+          <groups>
+            <group name="default">
+              <users>
+                <user id="${username}" mailbox="${mailbox}" cidr="${cidr}" number-alias="${number-alias}">
+                  <params>
+                    <xml-odbc-do name="query" on-zero-rows-break-to="not_found" value="
+                      SELECT
+                        name, value
+                      FROM
+                        dir_domain_params
+                      WHERE
+                        dir_domain_id = '${domain_id}';">
+                      <param name="${name}" value="${value}"/>
+                    </xml-odbc-do>
+                  </params>
+                  <variables>
+                    <xml-odbc-do name="query" value="
+                      SELECT
+                        name, value
+                      FROM
+                        dir_domain_params
+                      WHERE
+                        dir_domain_id = '${domain_id}';">
+                      <variable name="${name}" value="${value}"/>
+                    </xml-odbc-do>
+                  </variables>
+                </user>
+              </users>
+            </group>
+            <xml-odbc-do name="query" value="
+              SELECT
+                name AS group_name
+              FROM
+                dir_groups,
+                dir_groups_users
+              WHERE
+                dir_groups_users.dir_group_id
+                dir_user_id = '${user_id}';">
+              <group name="${group_name}">
+                <user id="${username}" type="pointer"/>
+              </group>
+            </xml-odbc-do>
+          </groups>
+        </domain>
+      </section>
+    </document>
+  </template>
+</include>



More information about the Freeswitch-trunk mailing list