From 6fa64e30db9d91b781fbabb8326885e33d998417 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Tue, 19 Apr 2011 10:31:19 -0500
Subject: [PATCH] use queued sql for core registration handling

---
 src/switch_core_sqldb.c | 41 +++++++----------------------------------
 1 file changed, 7 insertions(+), 34 deletions(-)

diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c
index a1baa9e2c5..106c7b0599 100644
--- a/src/switch_core_sqldb.c
+++ b/src/switch_core_sqldb.c
@@ -1642,18 +1642,12 @@ static char create_registrations_sql[] =
 SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, const char *realm, const char *token, const char *url, uint32_t expires, 
 															 const char *network_ip, const char *network_port, const char *network_proto)
 {
-	switch_cache_db_handle_t *dbh;
 	char *sql;
 
 	if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
 		return SWITCH_STATUS_FALSE;
 	}
 
-	if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
-		return SWITCH_STATUS_FALSE;
-	}
-
 	if (runtime.multiple_registrations) {
 		sql = switch_mprintf("delete from registrations where hostname='%q' and (url='%q' or token='%q')", 
 							 switch_core_get_hostname(), url, switch_str_nil(token));
@@ -1662,9 +1656,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, c
 							 user, realm, switch_core_get_hostname());
 	}
 
-	switch_cache_db_execute_sql(dbh, sql, NULL);
-	free(sql);
-
+	switch_queue_push(sql_manager.sql_queue[0], sql);
+	
 	sql = switch_mprintf("insert into registrations (reg_user,realm,token,url,expires,network_ip,network_port,network_proto,hostname) "
 						 "values ('%q','%q','%q','%q',%ld,'%q','%q','%q','%q')",
 						 switch_str_nil(user),
@@ -1677,40 +1670,29 @@ SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, c
 						 switch_str_nil(network_proto),
 						 switch_core_get_hostname()
 						 );
+
+	
+	switch_queue_push(sql_manager.sql_queue[0], sql);
 	
-	switch_cache_db_execute_sql(dbh, sql, NULL);
-	switch_cache_db_release_db_handle(&dbh);
-
-	free(sql);
-
 	return SWITCH_STATUS_SUCCESS;
 }
 
 SWITCH_DECLARE(switch_status_t) switch_core_del_registration(const char *user, const char *realm, const char *token)
 {
 
-	switch_cache_db_handle_t *dbh;
 	char *sql;
 
 	if (!switch_test_flag((&runtime), SCF_USE_SQL)) {
 		return SWITCH_STATUS_FALSE;
 	}
 
-	if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
-		return SWITCH_STATUS_FALSE;
-	}
-
 	if (!zstr(token) && runtime.multiple_registrations) {
 		sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q' and token='%q'", user, realm, switch_core_get_hostname(), token);
 	} else {
 		sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'", user, realm, switch_core_get_hostname());
 	}
 
-	switch_cache_db_execute_sql(dbh, sql, NULL);
-	switch_cache_db_release_db_handle(&dbh);
-
-	free(sql);
+	switch_queue_push(sql_manager.sql_queue[0], sql);
 
 	return SWITCH_STATUS_SUCCESS;
 }
@@ -1718,7 +1700,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_del_registration(const char *user, c
 SWITCH_DECLARE(switch_status_t) switch_core_expire_registration(int force)
 {
 	
-	switch_cache_db_handle_t *dbh;
 	char *sql;
 	time_t now;
 
@@ -1726,11 +1707,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_expire_registration(int force)
 		return SWITCH_STATUS_FALSE;
 	}
 
-	if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
-		return SWITCH_STATUS_FALSE;
-	}
-
 	now = switch_epoch_time_now(NULL);
 
 	if (force) {
@@ -1739,10 +1715,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_expire_registration(int force)
 		sql = switch_mprintf("delete from registrations where expires > 0 and expires <= %ld and hostname='%q'", now, switch_core_get_hostname());
 	}
 
-	switch_cache_db_execute_sql(dbh, sql, NULL);
-	switch_cache_db_release_db_handle(&dbh);
-
-	free(sql);
+	switch_queue_push(sql_manager.sql_queue[0], sql);
 
 	return SWITCH_STATUS_SUCCESS;