Revision
15600
Author
anthm
Date
2009-11-21 12:57:15 -0600 (Sat, 21 Nov 2009)

Log Message

convert sofia to use new core version of cache db handles

Modified Paths

Diff

Modified: freeswitch/trunk/src/include/switch_core.h (15599 => 15600)


--- freeswitch/trunk/src/include/switch_core.h	2009-11-21 17:57:55 UTC (rev 15599)
+++ freeswitch/trunk/src/include/switch_core.h	2009-11-21 18:57:15 UTC (rev 15600)
@@ -1988,8 +1988,9 @@
 															   switch_cache_db_handle_type_t type,
 															   switch_cache_db_connection_options_t *connection_options,
 															   const char *file, const char *func, int line);
-#define switch_cache_db_get_db_handle(_a, _b) _switch_cache_db_get_db_handle(_a, _b, __FILE__, __SWITCH_FUNC__, __LINE__)
+#define switch_cache_db_get_db_handle(_a, _b, _c) _switch_cache_db_get_db_handle(_a, _b, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
 
+SWITCH_DECLARE(char *) switch_cache_db_execute_sql2str(switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err);
 SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err);
 SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, const char *sql, 
 																	 switch_core_db_callback_func_t callback, void *pdata, char **err);

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (15599 => 15600)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2009-11-21 17:57:55 UTC (rev 15599)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h	2009-11-21 18:57:15 UTC (rev 15600)
@@ -476,7 +476,6 @@
 	sofia_gateway_t *gateways;
 	su_home_t *home;
 	switch_hash_t *chat_hash;
-	switch_hash_t *db_hash;
 	//switch_core_db_t *master_db;
 	switch_thread_rwlock_t *rwlock;
 	switch_mutex_t *flag_mutex;
@@ -673,14 +672,6 @@
         char *route_uri;
 } sofia_destination_t;
 
-typedef struct {
-	switch_core_db_t *db;
-	switch_odbc_handle_t *odbc_dbh;
-	time_t last_used;
-	switch_mutex_t *mutex;
-	switch_memory_pool_t *pool;
-} sofia_cache_db_handle_t;
-
 #define sofia_test_pflag(obj, flag) ((obj)->pflags[flag] ? 1 : 0)
 #define sofia_set_pflag(obj, flag) (obj)->pflags[flag] = 1
 #define sofia_set_pflag_locked(obj, flag) assert(obj->flag_mutex != NULL);\
@@ -785,6 +776,7 @@
 
 void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic);
 void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
+void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex);
 void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot);
 void sofia_reg_check_gateway(sofia_profile_t *profile, time_t now);
 void sofia_sub_check_gateway(sofia_profile_t *profile, time_t now);

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (15599 => 15600)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-11-21 17:57:55 UTC (rev 15599)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c	2009-11-21 18:57:15 UTC (rev 15600)
@@ -916,7 +916,7 @@
 
 				switch_mutex_lock(profile->ireg_mutex);
 			
-				sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
+				//sofia_glue_actually_execute_sql(profile, "begin;\n", NULL);
 
 				while (switch_queue_trypop(profile->sql_queue, &pop) == SWITCH_STATUS_SUCCESS && pop) {
 					char *sql = (char *) pop;
@@ -941,8 +941,8 @@
 				}
 
 				//printf("TRANS:\n%s\n", sqlbuf);
-				sofia_glue_actually_execute_sql(profile, sqlbuf, NULL);
-				sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
+				sofia_glue_actually_execute_sql_trans(profile, sqlbuf, NULL);
+				//sofia_glue_actually_execute_sql(profile, "commit;\n", NULL);
 
 
 				switch_mutex_unlock(profile->ireg_mutex);
@@ -963,7 +963,6 @@
 			if (++ireg_loops >= IREG_SECONDS) {
 				time_t now = switch_epoch_time_now(NULL);
 				sofia_reg_check_expire(profile, now, 0);
-				sofia_glue_sql_close(profile, now);
 				ireg_loops = 0;
 			}
 
@@ -1264,14 +1263,12 @@
 		}
 	}
 
-	sofia_glue_sql_close(profile, 0);
 	su_home_unref(profile->home);
 	su_root_destroy(profile->s_root);
 	pool = profile->pool;
 
 	sofia_glue_del_profile(profile);
 	switch_core_hash_destroy(&profile->chat_hash);
-	switch_core_hash_destroy(&profile->db_hash);
 
 	switch_thread_rwlock_unlock(profile->rwlock);
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Write unlock %s\n", profile->name);
@@ -2393,7 +2390,6 @@
 
 				profile->dbname = switch_core_strdup(profile->pool, url);
 				switch_core_hash_init(&profile->chat_hash, profile->pool);
-				switch_core_hash_init(&profile->db_hash, profile->pool);
 				switch_thread_rwlock_create(&profile->rwlock, profile->pool);
 				switch_mutex_init(&profile->flag_mutex, SWITCH_MUTEX_NESTED, profile->pool);
 				profile->dtmf_duration = 100;

Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (15599 => 15600)


--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	2009-11-21 17:57:55 UTC (rev 15599)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c	2009-11-21 18:57:15 UTC (rev 15600)
@@ -4080,63 +4080,6 @@
 
 }
 
-void sofia_glue_sql_close(sofia_profile_t *profile, time_t prune)
-{
-	switch_hash_index_t *hi;
-	const void *var;
-	void *val;
-	sofia_cache_db_handle_t *dbh = NULL;
-	int locked = 0;
-	char *key;
-
-	switch_mutex_lock(profile->ireg_mutex);
- top:
-	locked = 0;
-	
-	for (hi = switch_hash_first(NULL, profile->db_hash); hi; hi = switch_hash_next(hi)) {
-		switch_hash_this(hi, &var, NULL, &val);
-		key = (char *) var;
-
-		if ((dbh = (sofia_cache_db_handle_t *) val)) {
-			time_t diff = 0;
-
-			if (prune > 0) {
-				if (prune > dbh->last_used) {
-					diff = (time_t) prune - dbh->last_used;
-				}
-			
-				if (diff < SQL_CACHE_TIMEOUT) {
-					continue;
-				}
-			}
-
-			if (switch_mutex_trylock(dbh->mutex) == SWITCH_STATUS_SUCCESS) {
-				if (dbh->db) {
-					switch_core_db_close(dbh->db);
-					dbh->db = NULL;
-				} else if (switch_odbc_available() && dbh->odbc_dbh) {
-					switch_odbc_handle_destroy(&dbh->odbc_dbh);
-				}
-
-				switch_core_hash_delete(profile->db_hash, key);
-				switch_mutex_unlock(dbh->mutex);
-				switch_core_destroy_memory_pool(&dbh->pool);
-				goto top;
-	
-			} else {
-				if (!prune) locked++;
-				continue;
-			}
-		}
-	}
-
-	if (locked) {
-		goto top;
-	}
-
-	switch_mutex_unlock(profile->ireg_mutex);
-}
-
 void sofia_glue_execute_sql(sofia_profile_t *profile, char **sqlp, switch_bool_t sql_already_dynamic)
 {
 	switch_status_t status = SWITCH_STATUS_FALSE;
@@ -4172,69 +4115,52 @@
 }
 
 
-void sofia_glue_release_db_handle(sofia_cache_db_handle_t **dbh)
+switch_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
 {
-	if (dbh && *dbh) {
-		switch_mutex_unlock((*dbh)->mutex);
-		*dbh = NULL;
+	switch_cache_db_connection_options_t options = { {0} };
+	switch_cache_db_handle_t *dbh = NULL;
+	
+	if (profile->odbc_dsn && profile->odbc_user && profile->odbc_pass) {
+		options.odbc_options.dsn = profile->odbc_dsn;
+		options.odbc_options.user = profile->odbc_user;
+		options.odbc_options.pass = profile->odbc_pass;
+
+		if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_ODBC, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
+		return dbh;
+	} else {
+		options.core_db_options.db_path = profile->dbname;
+		if (switch_cache_db_get_db_handle(&dbh, SCDB_TYPE_CORE_DB, &options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
+		return dbh;
 	}
 }
 
-sofia_cache_db_handle_t *sofia_glue_get_db_handle(sofia_profile_t *profile)
+void sofia_glue_actually_execute_sql_trans(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
 {
-	switch_thread_id_t self = switch_thread_self();
-	char thread_str[256] = "";
-	sofia_cache_db_handle_t *dbh = NULL;
-	
-	snprintf(thread_str, sizeof(thread_str) - 1, "%lu", (unsigned long)(intptr_t)self);
+	switch_cache_db_handle_t *dbh = NULL;
 
-	switch_mutex_lock(profile->ireg_mutex);
-	if (!(dbh = switch_core_hash_find(profile->db_hash, thread_str))) {
-		switch_memory_pool_t *pool = NULL;
-		switch_core_db_t *db = NULL;
-		switch_odbc_handle_t *odbc_dbh = NULL;
+	if (mutex) {
+		switch_mutex_lock(mutex);
+	}
 
-		if (switch_odbc_available() && profile->odbc_dsn) {
-			if ((odbc_dbh = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) {
-				if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
-					switch_odbc_handle_destroy(&odbc_dbh);
-				}
-			}
-		} else {
-			db = switch_core_db_open_file(profile->dbname);
-		}
-
-		if (!db && !odbc_dbh) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure!\n");
-			goto end;
-		}
-
-		switch_core_new_memory_pool(&pool);
-		dbh = switch_core_alloc(pool, sizeof(*dbh));
-		dbh->pool = pool;
-
-		
-		if (db) dbh->db = db; else dbh->odbc_dbh = odbc_dbh;
-		switch_mutex_init(&dbh->mutex, SWITCH_MUTEX_UNNESTED, dbh->pool);
-		switch_mutex_lock(dbh->mutex);
-
-		switch_core_hash_insert(profile->db_hash, thread_str, dbh);
+	if (!(dbh = sofia_glue_get_db_handle(profile))) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
+		goto end;
 	}
 
- end:
+	switch_cache_db_persistant_execute_trans(dbh, sql, 1);
 
-	if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
+  end:
+	
+	switch_cache_db_release_db_handle(&dbh);
 
-	switch_mutex_unlock(profile->ireg_mutex);
-
-	return dbh;
+	if (mutex) {
+		switch_mutex_unlock(mutex);
+	}
 }
 
-
-
 void sofia_glue_actually_execute_sql(sofia_profile_t *profile, char *sql, switch_mutex_t *mutex)
 {
-	sofia_cache_db_handle_t *dbh = NULL;
+	switch_cache_db_handle_t *dbh = NULL;
 
 	if (mutex) {
 		switch_mutex_lock(mutex);
@@ -4245,35 +4171,12 @@
 		goto end;
 	}
 
-	if (switch_odbc_available() && dbh->odbc_dbh) {
-		switch_odbc_statement_handle_t stmt;
-		if (switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt) != SWITCH_ODBC_SUCCESS) {
-			char *err_str;
-			err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
-			if (!zstr(err_str)) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, err_str);
-			}
-			switch_safe_free(err_str);
-		}
-		switch_odbc_statement_handle_free(&stmt);
-	} else if (profile->odbc_dsn) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
-	} else {
-		char *errmsg;
+	switch_cache_db_execute_sql(dbh, sql, NULL);
 
-        switch_core_db_exec(dbh->db, sql, NULL, NULL, &errmsg);
-
-        if (errmsg) {
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
-            switch_core_db_free(errmsg);
-        }
-		
-	}
-
   end:
+	
+	switch_cache_db_release_db_handle(&dbh);
 
-	sofia_glue_release_db_handle(&dbh);
-
 	if (mutex) {
 		switch_mutex_unlock(mutex);
 	}
@@ -4284,7 +4187,7 @@
 {
 	switch_bool_t ret = SWITCH_FALSE;
 	char *errmsg = NULL;
-	sofia_cache_db_handle_t *dbh = NULL;
+	switch_cache_db_handle_t *dbh = NULL;
 	
 	if (mutex) {
 		switch_mutex_lock(mutex);
@@ -4294,23 +4197,17 @@
         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
         goto end;
     }
+	
+	switch_cache_db_execute_sql_callback(dbh, sql, callback, pdata, &errmsg);
 
-	if (switch_odbc_available() && dbh->odbc_dbh) {
-		switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata, NULL);
-	} else if (profile->odbc_dsn) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n");
-	} else {
-		switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
-
-		if (errmsg) {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
-			free(errmsg);
-		}
+	if (errmsg) {
+		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
+		free(errmsg);
 	}
 
   end:
 
-	sofia_glue_release_db_handle(&dbh);
+	switch_cache_db_release_db_handle(&dbh);
 
 	if (mutex) {
 		switch_mutex_unlock(mutex);
@@ -4321,66 +4218,26 @@
 
 char *sofia_glue_execute_sql2str(sofia_profile_t *profile, switch_mutex_t *mutex, char *sql, char *resbuf, size_t len)
 {
-	switch_core_db_stmt_t *stmt;
 	char *ret = NULL;
 
-	sofia_cache_db_handle_t *dbh = NULL;
+	switch_cache_db_handle_t *dbh = NULL;
 
 	if (!(dbh = sofia_glue_get_db_handle(profile))) {
         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB\n");
-        goto end;
+		return NULL;
     }
 
 	if (mutex) {
 		switch_mutex_lock(mutex);
 	}
 
-	if (switch_odbc_available() && dbh->odbc_dbh) {
-		if (switch_odbc_handle_exec_string(dbh->odbc_dbh, sql, resbuf, len) == SWITCH_ODBC_SUCCESS) {
-			ret = resbuf;
-		}
-		goto end;		
-	}
-
-	if (switch_core_db_prepare(dbh->db, sql, -1, &stmt, 0)) {
-		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
-		goto end;
-	} else {
-		int running = 1;
-		int colcount;
-
-		while (running < 5000) {
-			int result = switch_core_db_step(stmt);
-			const unsigned char *txt;
-
-			if (result == SWITCH_CORE_DB_ROW) {
-				if ((colcount = switch_core_db_column_count(stmt)) > 0) {
-					if ((txt = switch_core_db_column_text(stmt, 0))) {
-						switch_copy_string(resbuf, (char *) txt, len);
-						ret = resbuf;
-					} else {
-						goto end;
-					}
-				}
-				break;
-			} else if (result == SWITCH_CORE_DB_BUSY) {
-				running++;
-				switch_cond_next();
-				continue;
-			}
-			break;
-		}
-
-		switch_core_db_finalize(stmt);
-	}
-
- end:
-
+	ret = switch_cache_db_execute_sql2str(dbh, sql, resbuf, len, NULL);
+	
 	if (mutex) {
 		switch_mutex_unlock(mutex);
 	}
 
-	sofia_glue_release_db_handle(&dbh);
+	switch_cache_db_release_db_handle(&dbh);
 
 	return ret;
 }

Modified: freeswitch/trunk/src/switch_core_sqldb.c (15599 => 15600)


--- freeswitch/trunk/src/switch_core_sqldb.c	2009-11-21 17:57:55 UTC (rev 15599)
+++ freeswitch/trunk/src/switch_core_sqldb.c	2009-11-21 18:57:15 UTC (rev 15600)
@@ -314,8 +314,6 @@
 
 	if (err) *err = NULL;
 	
-
-
 	switch (dbh->type) {
 	case SCDB_TYPE_ODBC:
 		{
@@ -428,6 +426,63 @@
 
 }
 
+
+SWITCH_DECLARE(char *) switch_cache_db_execute_sql2str(switch_cache_db_handle_t *dbh, char *sql, char *str, size_t len, char **err)
+{
+	switch_status_t status = SWITCH_STATUS_FALSE;
+
+
+	switch (dbh->type) {
+	case SCDB_TYPE_CORE_DB:
+		{
+			switch_core_db_stmt_t *stmt;
+			
+			if (switch_core_db_prepare(dbh->native_handle.core_db_dbh, sql, -1, &stmt, 0)) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Statement Error [%s]!\n", sql);
+				return NULL;
+			} else {
+				int running = 1;
+				int colcount;
+
+				while (running < 5000) {
+					int result = switch_core_db_step(stmt);
+					const unsigned char *txt;
+					
+					if (result == SWITCH_CORE_DB_ROW) {
+						if ((colcount = switch_core_db_column_count(stmt)) > 0) {
+							if ((txt = switch_core_db_column_text(stmt, 0))) {
+								switch_copy_string(str, (char *) txt, len);
+								status = SWITCH_STATUS_SUCCESS;
+							} else {
+								goto end;
+							}
+						}
+						break;
+					} else if (result == SWITCH_CORE_DB_BUSY) {
+						running++;
+						switch_cond_next();
+						continue;
+					}
+					break;
+				}
+				
+				switch_core_db_finalize(stmt);
+			}
+		}
+		break;
+	case SCDB_TYPE_ODBC:
+		{
+			status = switch_odbc_handle_exec_string(dbh->native_handle.odbc_dbh, sql, str, len);
+		}
+		break;
+	}
+
+ end:
+
+	return status == SWITCH_STATUS_SUCCESS ? str : NULL;
+
+}
+
 SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, const char *sql, uint32_t retries)
 {
 	char *errmsg = NULL;