[Freeswitch-svn] [commit] r8176 - in freeswitch/trunk/src: . include mod/applications/mod_commands

Freeswitch SVN anthm at freeswitch.org
Wed Apr 23 12:46:25 EDT 2008


Author: anthm
Date: Wed Apr 23 12:46:24 2008
New Revision: 8176

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
   freeswitch/trunk/src/switch_console.c
   freeswitch/trunk/src/switch_core_sqldb.c

Log:
update

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Wed Apr 23 12:46:24 2008
@@ -1600,6 +1600,7 @@
 SWITCH_DECLARE(void) switch_time_set_monotonic(switch_bool_t enable);
 SWITCH_DECLARE(uint32_t) switch_core_max_dtmf_duration(uint32_t duration);
 SWITCH_DECLARE(uint32_t) switch_core_default_dtmf_duration(uint32_t duration);
+SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string);
 
 ///\}
 

Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c	Wed Apr 23 12:46:24 2008
@@ -1888,45 +1888,16 @@
 #define COMPLETE_SYNTAX "add <word>|del [<word>|all]"
 SWITCH_STANDARD_API(complete_function)
 {
-	char *mydata = NULL, *argv[2] = {0};
-	int fail = 1, argc;
-	char sql[1024] = "";
 
-	if (cmd && (mydata = strdup(cmd))) {
-		if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
-			switch_core_db_t *db = switch_core_db_handle();
-			
-			if (!strcasecmp(argv[0], "add")) {
-				switch_snprintf(sql, sizeof(sql), "delete from complete where name = '%s'", argv[1]);
-				switch_core_db_persistant_execute(db, sql, 1);
-				switch_snprintf(sql, sizeof(sql), "insert into complete values ('%s')", argv[1]);
-				switch_core_db_persistant_execute(db, sql, 1);
-				stream->write_function(stream, "+OK\n");
-				fail = 0;
-			} else if (!strcasecmp(argv[0], "del")) {
-				char *what = argv[1];
-				if (!strcasecmp(what, "*")) {
-					switch_snprintf(sql, sizeof(sql), "delete from complete");
-				} else {
-					switch_snprintf(sql, sizeof(sql), "delete from complete where name = '%s'", what);
-				}
-				switch_core_db_persistant_execute(db, sql, 1);
-				stream->write_function(stream, "+OK\n");
-				fail = 0;
-			}
+	switch_status_t status;
 
-			switch_core_db_close(db);
-		}
-	}
-
-	switch_safe_free(mydata);
-
-	if (fail) {
+	if ((status = switch_console_set_complete(cmd)) == SWITCH_STATUS_SUCCESS) {
+		stream->write_function(stream, "+OK\n");
+	} else {
 		stream->write_function(stream, "-USAGE: %s\n", COMPLETE_SYNTAX);
 	}
 	
 	return SWITCH_STATUS_SUCCESS;
-
 }
 
 #define SHOW_SYNTAX "codec|application|api|dialplan|file|timer|calls|channels"

Modified: freeswitch/trunk/src/switch_console.c
==============================================================================
--- freeswitch/trunk/src/switch_console.c	(original)
+++ freeswitch/trunk/src/switch_console.c	Wed Apr 23 12:46:24 2008
@@ -371,6 +371,7 @@
 	EditLine *el;
 	int len;
 	int hits;
+	int words;
 	char last[512];
 	FILE *out;
 };
@@ -378,10 +379,17 @@
 static int comp_callback(void *pArg, int argc, char **argv, char **columnNames)
 {
 	struct helper *h = (struct helper *) pArg;
+	char *target = NULL;
+	
+	target = argv[0];
+
+	if (!target) {
+		return -1;
+	}
 
-	fprintf(h->out, "%20s\t", argv[0]);
+	fprintf(h->out, "%20s\t", target);
 	
-	switch_copy_string(h->last, argv[0], sizeof(h->last));
+	switch_copy_string(h->last, target, sizeof(h->last));
 
 	if ((++h->hits % 4) == 0) {
 		fprintf(h->out, "\n");
@@ -400,7 +408,6 @@
 	char *p, *lp = NULL;
 	char *errmsg = NULL;
 	struct helper h = { el };
-	int words = 0;
 	unsigned char ret = CC_REDISPLAY;
 
 	h.out = switch_core_get_console();
@@ -416,7 +423,7 @@
 	for(p = buf; p && *p; p++) {
 		if (*p == ' ') {
 			lp = p;
-			words++;
+			h.words++;
 		}
 	}
 
@@ -428,7 +435,7 @@
 	
 	fprintf(h.out, "\n\n");
 
-	if (words == 0) {
+	if (h.words == 0) {
 		sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%s%%' order by name", buf);
 	} else {
 		sql = switch_mprintf("select distinct uuid from channels where uuid like '%s%%' order by uuid", buf);
@@ -444,14 +451,42 @@
 	}
 
 	if (h.hits != 1) {
-		switch_safe_free(sql);
-		sql = switch_mprintf("select distinct name from complete where name like '%s%%' order by name", buf);
-		switch_core_db_exec(db, sql, comp_callback, &h, &errmsg);	
+		char *dupdup = strdup(dup);
+		switch_assert(dupdup);
+		int x, argc = 0;
+		char *argv[10] = {0};
+		switch_stream_handle_t stream = { 0 };
+		SWITCH_STANDARD_STREAM(stream);
+
+		
+		argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+
+		if (h.words == 0) {
+			stream.write_function(&stream, 
+								  "select distinct a1 from complete where "
+								  "a1 not in (select name from interfaces) and ");
+		} else {
+			stream.write_function(&stream, 
+								  "select distinct a%d from complete where ", h.words + 1);
+								  
+		}
+
+		for(x = 0; x < argc; x++) {
+			stream.write_function(&stream, "(a%d = '' or a%d like '%s%%')%s", x+1, x+1, switch_str_nil(argv[x]), x == argc -1 ? "" : " and ");
+		}
 		
+		switch_core_db_exec(db, stream.data, comp_callback, &h, &errmsg);	
+
 		if (errmsg) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", sql, errmsg);
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error [%s][%s]\n", (char *) stream.data, errmsg);
 			free(errmsg);
 			ret = CC_ERROR;
+		}
+
+		switch_safe_free(dupdup);
+		switch_safe_free(stream.data);
+		
+		if (ret == CC_ERROR) {
 			goto end;
 		}
 	}
@@ -475,6 +510,51 @@
 	return (ret);
 }
 
+
+SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string)
+{
+	char *mydata = NULL, *argv[11] = {0};
+	int argc, x;
+	switch_status_t status = SWITCH_STATUS_FALSE;
+
+	if (string && (mydata = strdup(string))) {
+		if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+			switch_core_db_t *db = switch_core_db_handle();
+			switch_stream_handle_t mystream = { 0 };
+			SWITCH_STANDARD_STREAM(mystream);
+
+			if (!strcasecmp(argv[0], "add")) {
+				mystream.write_function(&mystream, "insert into complete values (");
+				for(x = 0; x < 10; x++) {
+					mystream.write_function(&mystream, "'%s'%s", switch_str_nil(argv[x+1]), x == 9 ? ")" : ", ");
+				}
+				switch_core_db_persistant_execute(db, mystream.data, 1);
+				status = SWITCH_STATUS_SUCCESS;
+			} else if (!strcasecmp(argv[0], "del")) {
+				char *what = argv[1];
+                if (!strcasecmp(what, "*")) {
+					switch_core_db_persistant_execute(db, "delete from complete", 1);
+				} else {
+					mystream.write_function(&mystream, "delete from complete where ");
+					for(x = 0; x < argc - 1; x++) {
+						mystream.write_function(&mystream, "a%d = '%s'%s", x+1, switch_str_nil(argv[x+1]), x == argc - 2 ? "" : " and ");
+					}
+					switch_core_db_persistant_execute(db, mystream.data, 1);
+				}
+				status = SWITCH_STATUS_SUCCESS;
+			}
+			switch_safe_free(mystream.data);
+			switch_core_db_close(db);
+		}
+	}
+
+	switch_safe_free(mydata);
+
+	return status;
+
+}
+
+
 SWITCH_DECLARE(void) switch_console_loop(void)
 {
 	switch_thread_t *thread;

Modified: freeswitch/trunk/src/switch_core_sqldb.c
==============================================================================
--- freeswitch/trunk/src/switch_core_sqldb.c	(original)
+++ freeswitch/trunk/src/switch_core_sqldb.c	Wed Apr 23 12:46:24 2008
@@ -368,7 +368,16 @@
 	} else {
 		char create_complete_sql[] =
 			"CREATE TABLE complete (\n"
-			"   name  VARCHAR(255)\n" 
+			"   a1  VARCHAR(255),\n" 
+			"   a2  VARCHAR(255),\n" 
+			"   a3  VARCHAR(255),\n" 
+			"   a4  VARCHAR(255),\n" 
+			"   a5  VARCHAR(255),\n" 
+			"   a6  VARCHAR(255),\n" 
+			"   a7  VARCHAR(255),\n" 
+			"   a8  VARCHAR(255),\n" 
+			"   a9  VARCHAR(255),\n" 
+			"   a10 VARCHAR(255)\n" 
 			");\n";
 
 		char create_channels_sql[] =
@@ -430,7 +439,7 @@
 		switch_core_db_exec(sql_manager.db, "PRAGMA cache_size=8000", NULL, NULL, NULL);
 		switch_core_db_exec(sql_manager.db, "PRAGMA temp_store=MEMORY;", NULL, NULL, NULL);
 
-		switch_core_db_exec(sql_manager.db, create_complete_sql, NULL, NULL, NULL);
+		switch_core_db_test_reactive(sql_manager.db, "select a1 from complete", "DROP TABLE complete", create_complete_sql);
 		switch_core_db_exec(sql_manager.db, create_channels_sql, NULL, NULL, NULL);
 		switch_core_db_exec(sql_manager.db, create_calls_sql, NULL, NULL, NULL);
 		switch_core_db_exec(sql_manager.db, create_interfaces_sql, NULL, NULL, NULL);



More information about the Freeswitch-svn mailing list