<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15487] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15487">15487</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-11-16 18:12:54 -0600 (Mon, 16 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>add support for running core-db over odbc</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeprivateswitch_core_pvth">freeswitch/trunk/src/include/private/switch_core_pvt.h</a></li>
<li><a href="#freeswitchtrunksrcincludeswitch_coreh">freeswitch/trunk/src/include/switch_core.h</a></li>
<li><a href="#freeswitchtrunksrcincludeswitch_odbch">freeswitch/trunk/src/include/switch_odbc.h</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc">freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_commandsmod_commandsc">freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_dptoolsmod_dptoolsc">freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_fifomod_fifoc">freeswitch/trunk/src/mod/applications/mod_fifo/mod_fifo.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_limitmod_limitc">freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_voicemailmod_voicemailc">freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac">freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_sofiasofia_regc">freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_channelc">freeswitch/trunk/src/switch_channel.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_consolec">freeswitch/trunk/src/switch_console.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_corec">freeswitch/trunk/src/switch_core.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_core_dbc">freeswitch/trunk/src/switch_core_db.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_core_sqldbc">freeswitch/trunk/src/switch_core_sqldb.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_odbcc">freeswitch/trunk/src/switch_odbc.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeprivateswitch_core_pvth"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/private/switch_core_pvt.h (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/private/switch_core_pvt.h        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/include/private/switch_core_pvt.h        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -207,11 +207,14 @@
</span><span class="cx">         switch_frame_t dummy_cng_frame;
</span><span class="cx">         char dummy_data[5];
</span><span class="cx">         switch_bool_t colorize_console;
</span><ins>+        char *odbc_dsn;
+        char *odbc_user;
+        char *odbc_pass;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> extern struct switch_runtime runtime;
</span><span class="cx"> 
</span><del>-void switch_core_sqldb_start(switch_memory_pool_t *pool);
</del><ins>+switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage);
</ins><span class="cx"> void switch_core_sqldb_stop(void);
</span><span class="cx"> void switch_core_session_init(switch_memory_pool_t *pool);
</span><span class="cx"> void switch_core_session_uninit(void);
</span></span></pre></div>
<a id="freeswitchtrunksrcincludeswitch_coreh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_core.h (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_core.h        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/include/switch_core.h        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -1415,6 +1415,7 @@
</span><span class="cx"> 
</span><span class="cx"> */
</span><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute(switch_core_db_t *db, char *sql, uint32_t retries);
</span><ins>+SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute_trans(switch_core_db_t *db, char *sql, uint32_t retries);
</ins><span class="cx"> 
</span><span class="cx"> /*! 
</span><span class="cx">   \brief perform a test query then perform a reactive query if the first one fails
</span><span class="lines">@@ -1425,12 +1426,6 @@
</span><span class="cx"> */
</span><span class="cx"> SWITCH_DECLARE(void) switch_core_db_test_reactive(switch_core_db_t *db, char *test_sql, char *drop_sql, char *reactive_sql);
</span><span class="cx"> 
</span><del>-#define SWITCH_CORE_DB &quot;core&quot;
-/*!
-  \brief Open the default system database
-*/
-#define switch_core_db_handle() switch_core_db_open_file(SWITCH_CORE_DB)
-
</del><span class="cx"> ///\}
</span><span class="cx"> 
</span><span class="cx"> ///\defgroup Media File Functions
</span><span class="lines">@@ -1930,13 +1925,17 @@
</span><span class="cx"> } switch_cache_db_handle_t;
</span><span class="cx"> 
</span><span class="cx"> SWITCH_DECLARE(void) switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh);
</span><del>-SWITCH_DECLARE(switch_cache_db_handle_t *)switch_cache_db_get_db_handle(const char *db_name, const char *odbc_user, const char *odbc_pass);
-SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql);
</del><ins>+SWITCH_DECLARE(switch_status_t) switch_cache_db_get_db_handle(switch_cache_db_handle_t **dbh, 
+                                                                                                                                                const char *db_name, const char *odbc_user, const char *odbc_pass);
+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err);
</ins><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, char *sql, 
</span><del>-                                                                                                                                         switch_core_db_callback_func_t callback, void *pdata);
</del><ins>+                                                                                                                                         switch_core_db_callback_func_t callback, void *pdata, char **err);
</ins><span class="cx"> 
</span><ins>+SWITCH_DECLARE(switch_status_t)switch_core_db_handle(switch_cache_db_handle_t **dbh);
+SWITCH_DECLARE(void) switch_cache_db_test_reactive(switch_cache_db_handle_t *db, char *test_sql, char *drop_sql, char *reactive_sql);
+SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, char *sql, uint32_t retries);
+SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute_trans(switch_cache_db_handle_t *dbh, char *sql, uint32_t retries);
</ins><span class="cx"> 
</span><del>-
</del><span class="cx"> SWITCH_END_EXTERN_C
</span><span class="cx"> #endif
</span><span class="cx"> /* For Emacs:
</span></span></pre></div>
<a id="freeswitchtrunksrcincludeswitch_odbch"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_odbc.h (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_odbc.h        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/include/switch_odbc.h        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -77,7 +77,7 @@
</span><span class="cx">   \note none
</span><span class="cx"> */
</span><span class="cx"> SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec_detailed(const char *file, const char *func, int line, switch_odbc_handle_t *handle,
</span><del>-                                                                                                                                          char *sql, switch_core_db_callback_func_t callback, void *pdata);
</del><ins>+                                                                                                                                                           char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err);
</ins><span class="cx"> /*!
</span><span class="cx">   \brief Execute the sql query and issue a callback for each row returned
</span><span class="cx">   \param handle the ODBC handle
</span><span class="lines">@@ -87,9 +87,9 @@
</span><span class="cx">   \return SWITCH_STATUS_SUCCESS if the operation was successful
</span><span class="cx">   \note none
</span><span class="cx"> */
</span><del>-#define switch_odbc_handle_callback_exec(handle,  sql,  callback, pdata) \
</del><ins>+#define switch_odbc_handle_callback_exec(handle,  sql,  callback, pdata, err) \
</ins><span class="cx">                 switch_odbc_handle_callback_exec_detailed(__FILE__, (char * )__SWITCH_FUNC__, __LINE__, \
</span><del>-                                                                                                  handle, sql, callback, pdata)
</del><ins>+                                                                                                  handle, sql, callback, pdata, err)
</ins><span class="cx"> 
</span><span class="cx">                                                                                                                                           
</span><span class="cx"> SWITCH_DECLARE(char *) switch_odbc_handle_get_error(switch_odbc_handle_t *handle, switch_odbc_statement_handle_t stmt);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -186,7 +186,7 @@
</span><span class="cx">         
</span><span class="cx">         switch_mutex_lock(globals.db_mutex);
</span><span class="cx">         if (globals.odbc_dsn) {
</span><del>-                if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata)
</del><ins>+                if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata, NULL)
</ins><span class="cx">                                 == SWITCH_ODBC_FAIL) {
</span><span class="cx">                         retval = SWITCH_FALSE;
</span><span class="cx">                 } else {
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_commandsmod_commandsc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -2914,7 +2914,7 @@
</span><span class="cx"> {
</span><span class="cx">         char sql[1024];
</span><span class="cx">         char *errmsg;
</span><del>-        switch_core_db_t *db;
</del><ins>+        switch_cache_db_handle_t *db;
</ins><span class="cx">         struct holder holder = { 0 };
</span><span class="cx">         int help = 0;
</span><span class="cx">         char *mydata = NULL, *argv[6] = { 0 };
</span><span class="lines">@@ -2923,7 +2923,7 @@
</span><span class="cx">         switch_core_flag_t cflags = switch_core_flags();
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="cx"> 
</span><del>-        db = switch_core_db_handle();
</del><ins>+        switch_core_db_handle(&amp;db);
</ins><span class="cx">         
</span><span class="cx">         holder.justcount = 0;
</span><span class="cx">         
</span><span class="lines">@@ -3090,7 +3090,7 @@
</span><span class="cx">                                 holder.delim = &quot;,&quot;;
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><del>-                switch_core_db_exec(db, sql, show_callback, &amp;holder, &amp;errmsg);
</del><ins>+                switch_cache_db_execute_sql_callback(db, sql, show_callback, &amp;holder, &amp;errmsg);
</ins><span class="cx">                 if (holder.http) {
</span><span class="cx">                         holder.stream-&gt;write_function(holder.stream, &quot;&lt;/table&gt;&quot;);
</span><span class="cx">                 }
</span><span class="lines">@@ -3106,7 +3106,7 @@
</span><span class="cx">                         stream-&gt;write_function(stream, &quot;\n%u total.\n&quot;, holder.count);
</span><span class="cx">                 }
</span><span class="cx">         } else if (!strcasecmp(as, &quot;xml&quot;)) {
</span><del>-                switch_core_db_exec(db, sql, show_as_xml_callback, &amp;holder, &amp;errmsg);
</del><ins>+                switch_cache_db_execute_sql_callback(db, sql, show_as_xml_callback, &amp;holder, &amp;errmsg);
</ins><span class="cx"> 
</span><span class="cx">                 if (errmsg) {
</span><span class="cx">                         stream-&gt;write_function(stream, &quot;-ERR SQL Error [%s]\n&quot;, errmsg);
</span><span class="lines">@@ -3141,7 +3141,7 @@
</span><span class="cx">         switch_safe_free(mydata);
</span><span class="cx"> 
</span><span class="cx">         if (db) {
</span><del>-                switch_core_db_close(db);
</del><ins>+                switch_cache_db_release_db_handle(&amp;db);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return status;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_dptoolsmod_dptoolsc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -336,7 +336,7 @@
</span><span class="cx">                 switch_channel_t *channel = switch_core_session_get_channel(session);
</span><span class="cx">                 const char *require_group = switch_channel_get_variable(channel, &quot;eavesdrop_require_group&quot;);
</span><span class="cx">                 if (!strcasecmp((char *) data, &quot;all&quot;)) {
</span><del>-                        switch_core_db_t *db = switch_core_db_handle();
</del><ins>+                        switch_cache_db_handle_t *db = NULL;
</ins><span class="cx">                         char *errmsg = NULL;
</span><span class="cx">                         struct e_data e_data = { {0} };
</span><span class="cx">                         char *sql = switch_mprintf(&quot;select uuid from channels where uuid != '%q'&quot;, switch_core_session_get_uuid(session));
</span><span class="lines">@@ -347,12 +347,14 @@
</span><span class="cx">                         char terminator;
</span><span class="cx">                         switch_status_t status;
</span><span class="cx"> 
</span><ins>+                        switch_core_db_handle(&amp;db);
+
</ins><span class="cx">                         while (switch_channel_ready(channel)) {
</span><span class="cx">                                 for (x = 0; x &lt; MAX_SPY; x++) {
</span><span class="cx">                                         switch_safe_free(e_data.uuid_list[x]);
</span><span class="cx">                                 }
</span><span class="cx">                                 e_data.total = 0;
</span><del>-                                switch_core_db_exec(db, sql, e_callback, &amp;e_data, &amp;errmsg);
</del><ins>+                                switch_cache_db_execute_sql_callback(db, sql, e_callback, &amp;e_data, &amp;errmsg);
</ins><span class="cx">                                 if (errmsg) {
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, &quot;Error: %s\n&quot;, errmsg);
</span><span class="cx">                                         switch_core_db_free(errmsg);
</span><span class="lines">@@ -393,7 +395,7 @@
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         free(sql);
</span><del>-                        switch_core_db_close(db);
</del><ins>+                        switch_cache_db_release_db_handle(&amp;db);
</ins><span class="cx"> 
</span><span class="cx">                 } else {
</span><span class="cx">                         switch_ivr_eavesdrop_session(session, data, require_group, ED_DTMF);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_fifomod_fifoc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_fifo/mod_fifo.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_fifo/mod_fifo.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_fifo/mod_fifo.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -339,7 +339,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (switch_odbc_available() &amp;&amp; globals.odbc_dsn) {
</span><del>-                switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
</del><ins>+                switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata, NULL);
</ins><span class="cx">         } else {
</span><span class="cx">                 if (!(db = switch_core_db_open_file(globals.dbname))) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, globals.dbname);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_limitmod_limitc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_limit/mod_limit.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -138,7 +138,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (switch_odbc_available() &amp;&amp; globals.odbc_dsn) {
</span><del>-                switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
</del><ins>+                switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata, NULL);
</ins><span class="cx">         } else {
</span><span class="cx">                 if (!(db = switch_core_db_open_file(globals.dbname))) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, globals.dbname);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_voicemailmod_voicemailc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/applications/mod_voicemail/mod_voicemail.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -201,7 +201,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (switch_odbc_available() &amp;&amp; profile-&gt;odbc_dsn) {
</span><del>-                switch_odbc_handle_callback_exec(profile-&gt;master_odbc, sql, callback, pdata);
</del><ins>+                switch_odbc_handle_callback_exec(profile-&gt;master_odbc, sql, callback, pdata, NULL);
</ins><span class="cx">         } else {
</span><span class="cx">                 if (!(db = switch_core_db_open_file(profile-&gt;dbname))) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB %s\n&quot;, profile-&gt;dbname);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiamod_sofiac"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -2876,7 +2876,8 @@
</span><span class="cx">         switch_channel_t *nchannel;
</span><span class="cx">         char *host = NULL, *dest_to = NULL;
</span><span class="cx">         const char *hval = NULL;
</span><del>-
</del><ins>+        char *not_const = NULL;
+                
</ins><span class="cx">         *new_session = NULL;
</span><span class="cx"> 
</span><span class="cx">         if (!outbound_profile || zstr(outbound_profile-&gt;destination_number)) {
</span><span class="lines">@@ -3115,10 +3116,12 @@
</span><span class="cx"> 
</span><span class="cx">         caller_profile = switch_caller_profile_clone(nsession, outbound_profile);
</span><span class="cx">         
</span><del>-
</del><ins>+        
</ins><span class="cx">         caller_profile-&gt;destination_number = switch_sanitize_number(caller_profile-&gt;destination_number);
</span><del>-        caller_profile-&gt;caller_id_name = switch_sanitize_number((char *)caller_profile-&gt;caller_id_name);
-        caller_profile-&gt;caller_id_number = switch_sanitize_number((char *)caller_profile-&gt;caller_id_number);
</del><ins>+        not_const = (char *)caller_profile-&gt;caller_id_name;
+        caller_profile-&gt;caller_id_name = switch_sanitize_number(not_const);
+        not_const = (char *)caller_profile-&gt;caller_id_number;
+        caller_profile-&gt;caller_id_number = switch_sanitize_number(not_const);
</ins><span class="cx">         
</span><span class="cx">         //caller_profile-&gt;destination_number = switch_core_strdup(caller_profile-&gt;pool, dest_num);
</span><span class="cx">         switch_channel_set_caller_profile(nchannel, caller_profile);
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_gluec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -4008,12 +4008,14 @@
</span><span class="cx">                 if ((dbh = (sofia_cache_db_handle_t *) val)) {
</span><span class="cx">                         time_t diff = 0;
</span><span class="cx"> 
</span><del>-                        if (prune &gt; 0 &amp;&amp; prune &gt; dbh-&gt;last_used) {
-                                diff = (time_t) prune - dbh-&gt;last_used;
-                        }
</del><ins>+                        if (prune &gt; 0) {
+                                if (prune &gt; dbh-&gt;last_used) {
+                                        diff = (time_t) prune - dbh-&gt;last_used;
+                                }
</ins><span class="cx">                         
</span><del>-                        if (diff &lt; SQL_CACHE_TIMEOUT) {
-                                continue;
</del><ins>+                                if (diff &lt; SQL_CACHE_TIMEOUT) {
+                                        continue;
+                                }
</ins><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         if (switch_mutex_trylock(dbh-&gt;mutex) == SWITCH_STATUS_SUCCESS) {
</span><span class="lines">@@ -4121,7 +4123,7 @@
</span><span class="cx"> 
</span><span class="cx">                 
</span><span class="cx">                 if (db) dbh-&gt;db = db; else dbh-&gt;odbc_dbh = odbc_dbh;
</span><del>-                switch_mutex_init(&amp;dbh-&gt;mutex, SWITCH_MUTEX_NESTED, dbh-&gt;pool);
</del><ins>+                switch_mutex_init(&amp;dbh-&gt;mutex, 0, dbh-&gt;pool);
</ins><span class="cx">                 switch_mutex_lock(dbh-&gt;mutex);
</span><span class="cx"> 
</span><span class="cx">                 switch_core_hash_insert(profile-&gt;db_hash, thread_str, dbh);
</span><span class="lines">@@ -4202,7 +4204,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">         if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
</span><del>-                switch_odbc_handle_callback_exec(dbh-&gt;odbc_dbh, sql, callback, pdata);
</del><ins>+                switch_odbc_handle_callback_exec(dbh-&gt;odbc_dbh, sql, callback, pdata, NULL);
</ins><span class="cx">         } else if (profile-&gt;odbc_dsn) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ODBC IS NOT AVAILABLE!\n&quot;);
</span><span class="cx">         } else {
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_sofiasofia_regc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -1678,7 +1678,9 @@
</span><span class="cx">         if ((regtype == REG_REGISTER) &amp;&amp; sofia_test_pflag(profile, PFLAG_CHECKUSER)) {
</span><span class="cx">                 if (zstr(username) || zstr(to_user) || strcasecmp(to_user, username)) {
</span><span class="cx">                         /* Names don't match, so fail */
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SIP username %s does not match auth username\n&quot;, switch_str_nil(to_user));
</del><ins>+                        if (profile-&gt;debug) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SIP username %s does not match auth username\n&quot;, switch_str_nil(to_user));
+                        }
</ins><span class="cx">                         goto end;
</span><span class="cx">                 }
</span><span class="cx">         }
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_channelc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_channel.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_channel.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_channel.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -1403,7 +1403,8 @@
</span><span class="cx">         switch_caller_profile_t *caller_profile, *originator_caller_profile = NULL, *originatee_caller_profile = NULL;
</span><span class="cx">         switch_codec_implementation_t impl = {0};
</span><span class="cx">         char state_num[25];
</span><del>-
</del><ins>+        const char *v;
+        
</ins><span class="cx">         switch_mutex_lock(channel-&gt;profile_mutex);
</span><span class="cx"> 
</span><span class="cx">         if ((caller_profile = switch_channel_get_caller_profile(channel))) {
</span><span class="lines">@@ -1422,7 +1423,15 @@
</span><span class="cx">         switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Presence-Call-Direction&quot;,
</span><span class="cx">                                                                    channel-&gt;direction == SWITCH_CALL_DIRECTION_OUTBOUND ? &quot;outbound&quot; : &quot;inbound&quot;);
</span><span class="cx"> 
</span><ins>+        if ((v = switch_channel_get_variable(channel, &quot;presence_id&quot;))) {
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Channel-Presence-ID&quot;, v);
+        }
</ins><span class="cx"> 
</span><ins>+        if ((v = switch_channel_get_variable(channel, &quot;presence_data&quot;))) {
+                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Channel-Presence-Data&quot;, v);
+        }
+
+
</ins><span class="cx">         if (switch_channel_test_flag(channel, CF_ANSWERED)) {
</span><span class="cx">                 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;Answer-State&quot;, &quot;answered&quot;);
</span><span class="cx">         } else if (switch_channel_test_flag(channel, CF_EARLY_MEDIA)) {
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_consolec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_console.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_console.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_console.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -199,12 +199,15 @@
</span><span class="cx">         char *r = NULL;
</span><span class="cx">         char *sql = NULL;
</span><span class="cx">         char *exp = NULL;
</span><del>-        switch_core_db_t *db = switch_core_db_handle();
</del><ins>+        switch_cache_db_handle_t *db = NULL;
</ins><span class="cx">         int full = 0;
</span><span class="cx"> 
</span><ins>+
+        switch_core_db_handle(&amp;db);
+
</ins><span class="cx">         sql = switch_mprintf(&quot;select command from aliases where alias='%q'&quot;, cmd);
</span><span class="cx"> 
</span><del>-        switch_core_db_exec(db, sql, alias_callback, &amp;r, &amp;errmsg);
</del><ins>+        switch_cache_db_execute_sql_callback(db, sql, alias_callback, &amp;r, &amp;errmsg);
</ins><span class="cx"> 
</span><span class="cx">         if (errmsg) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;error [%s][%s]\n&quot;, sql, errmsg);
</span><span class="lines">@@ -216,7 +219,7 @@
</span><span class="cx">         if (!r) {
</span><span class="cx">                 sql = switch_mprintf(&quot;select command from aliases where alias='%q %q'&quot;, cmd, arg);
</span><span class="cx"> 
</span><del>-                switch_core_db_exec(db, sql, alias_callback, &amp;r, &amp;errmsg);
</del><ins>+                switch_cache_db_execute_sql_callback(db, sql, alias_callback, &amp;r, &amp;errmsg);
</ins><span class="cx"> 
</span><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;error [%s][%s]\n&quot;, sql, errmsg);
</span><span class="lines">@@ -240,7 +243,8 @@
</span><span class="cx">                 exp = cmd;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        switch_core_db_close(db);
</del><ins>+        switch_cache_db_release_db_handle(&amp;db);
+
</ins><span class="cx">         return exp;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -524,7 +528,7 @@
</span><span class="cx"> 
</span><span class="cx"> static unsigned char complete(EditLine * el, int ch)
</span><span class="cx"> {
</span><del>-        switch_core_db_t *db = switch_core_db_handle();
</del><ins>+        switch_cache_db_handle_t *db = NULL;
</ins><span class="cx">         char *sql;
</span><span class="cx">         const LineInfo *lf = el_line(el);
</span><span class="cx">         char *dup = strdup(lf-&gt;buffer);
</span><span class="lines">@@ -534,6 +538,8 @@
</span><span class="cx">         struct helper h = { el };
</span><span class="cx">         unsigned char ret = CC_REDISPLAY;
</span><span class="cx"> 
</span><ins>+        switch_core_db_handle(&amp;db);
+
</ins><span class="cx">         h.out = switch_core_get_console();
</span><span class="cx"> 
</span><span class="cx">         if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
</span><span class="lines">@@ -560,12 +566,14 @@
</span><span class="cx">         fprintf(h.out, &quot;\n\n&quot;);
</span><span class="cx"> 
</span><span class="cx">         if (h.words == 0) {
</span><del>-                sql = switch_mprintf(&quot;select distinct name from interfaces where type='api' and name like '%s%%' order by name&quot;, buf);
</del><ins>+                sql = switch_mprintf(&quot;select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name&quot;, 
+                                                         buf, switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">         } else {
</span><del>-                sql = switch_mprintf(&quot;select distinct uuid from channels where uuid like '%s%%' order by uuid&quot;, buf);
</del><ins>+                sql = switch_mprintf(&quot;select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid&quot;, 
+                                                         buf, switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        switch_core_db_exec(db, sql, comp_callback, &amp;h, &amp;errmsg);
</del><ins>+        switch_cache_db_execute_sql_callback(db, sql, comp_callback, &amp;h, &amp;errmsg);
</ins><span class="cx"> 
</span><span class="cx">         if (errmsg) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;error [%s][%s]\n&quot;, sql, errmsg);
</span><span class="lines">@@ -586,7 +594,8 @@
</span><span class="cx">                 argc = switch_separate_string(dupdup, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
</span><span class="cx"> 
</span><span class="cx">                 if (h.words == 0) {
</span><del>-                        stream.write_function(&amp;stream, &quot;select distinct a1 from complete where &quot; &quot;a1 not in (select name from interfaces) %s &quot;, argc ? &quot;and&quot; : &quot;&quot;);
</del><ins>+                        stream.write_function(&amp;stream, &quot;select distinct a1 from complete where &quot; &quot;a1 not in (select name from interfaces where hostname='%s') %s &quot;, 
+                                                                  switch_core_get_variable(&quot;hostname&quot;), argc ? &quot;and&quot; : &quot;&quot;);
</ins><span class="cx">                 } else {
</span><span class="cx">                         stream.write_function(&amp;stream, &quot;select distinct a%d from complete where &quot;, h.words + 1);
</span><span class="cx"> 
</span><span class="lines">@@ -596,8 +605,10 @@
</span><span class="cx">                         stream.write_function(&amp;stream, &quot;(a%d = '' or a%d like '%s%%')%s&quot;, x + 1, x + 1, switch_str_nil(argv[x]), x == argc - 1 ? &quot;&quot; : &quot; and &quot;);
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                switch_core_db_exec(db, stream.data, comp_callback, &amp;h, &amp;errmsg);
</del><ins>+                stream.write_function(&amp;stream, &quot; and hostname='%s'&quot;, switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx"> 
</span><ins>+                switch_cache_db_execute_sql_callback(db, stream.data, comp_callback, &amp;h, &amp;errmsg);
+
</ins><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;error [%s][%s]\n&quot;, (char *) stream.data, errmsg);
</span><span class="cx">                         free(errmsg);
</span><span class="lines">@@ -626,7 +637,7 @@
</span><span class="cx">         switch_safe_free(sql);
</span><span class="cx">         switch_safe_free(dup);
</span><span class="cx"> 
</span><del>-        switch_core_db_close(db);
</del><ins>+        switch_cache_db_release_db_handle(&amp;db);
</ins><span class="cx"> 
</span><span class="cx">         return (ret);
</span><span class="cx"> }
</span><span class="lines">@@ -640,40 +651,44 @@
</span><span class="cx"> 
</span><span class="cx">         if (string &amp;&amp; (mydata = strdup(string))) {
</span><span class="cx">                 if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
</span><del>-                        switch_core_db_t *db = switch_core_db_handle();
</del><ins>+                        switch_cache_db_handle_t *db = NULL;
</ins><span class="cx">                         switch_stream_handle_t mystream = { 0 };
</span><span class="cx">                         SWITCH_STANDARD_STREAM(mystream);
</span><span class="cx"> 
</span><del>-
</del><ins>+                        switch_core_db_handle(&amp;db);
+                        
</ins><span class="cx">                         if (!strcasecmp(argv[0], &quot;stickyadd&quot;)) {
</span><span class="cx">                                 mystream.write_function(&amp;mystream, &quot;insert into complete values (1,&quot;);
</span><span class="cx">                                 for (x = 0; x &lt; 10; x++) {
</span><del>-                                        mystream.write_function(&amp;mystream, &quot;'%s'%s&quot;, switch_str_nil(argv[x + 1]), x == 9 ? &quot;)&quot; : &quot;, &quot;);
</del><ins>+                                        mystream.write_function(&amp;mystream, &quot;'%s', &quot;, switch_str_nil(argv[x + 1]));
</ins><span class="cx">                                 }
</span><del>-                                switch_core_db_persistant_execute(db, mystream.data, 5);
</del><ins>+                                mystream.write_function(&amp;mystream, &quot; '%s')&quot;, switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, mystream.data, 5);
</ins><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         } else if (!strcasecmp(argv[0], &quot;add&quot;)) {
</span><span class="cx">                                 mystream.write_function(&amp;mystream, &quot;insert into complete values (0,&quot;);
</span><span class="cx">                                 for (x = 0; x &lt; 10; x++) {
</span><del>-                                        mystream.write_function(&amp;mystream, &quot;'%s'%s&quot;, switch_str_nil(argv[x + 1]), x == 9 ? &quot;)&quot; : &quot;, &quot;);
</del><ins>+                                        mystream.write_function(&amp;mystream, &quot;'%s', &quot;, switch_str_nil(argv[x + 1]));
</ins><span class="cx">                                 }
</span><del>-                                switch_core_db_persistant_execute(db, mystream.data, 5);
</del><ins>+                                mystream.write_function(&amp;mystream, &quot; '%s')&quot;, switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, mystream.data, 5);
</ins><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         } else if (!strcasecmp(argv[0], &quot;del&quot;)) {
</span><span class="cx">                                 char *what = argv[1];
</span><span class="cx">                                 if (!strcasecmp(what, &quot;*&quot;)) {
</span><del>-                                        switch_core_db_persistant_execute(db, &quot;delete from complete&quot;, 1);
</del><ins>+                                        switch_cache_db_persistant_execute(db, &quot;delete from complete&quot;, 1);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         mystream.write_function(&amp;mystream, &quot;delete from complete where &quot;);
</span><span class="cx">                                         for (x = 0; x &lt; argc - 1; x++) {
</span><span class="cx">                                                 mystream.write_function(&amp;mystream, &quot;a%d = '%s'%s&quot;, x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? &quot;&quot; : &quot; and &quot;);
</span><span class="cx">                                         }
</span><del>-                                        switch_core_db_persistant_execute(db, mystream.data, 1);
</del><ins>+                                        mystream.write_function(&amp;mystream, &quot; and hostname='%s'&quot;, switch_core_get_variable(&quot;hostname&quot;));
+                                        switch_cache_db_persistant_execute(db, mystream.data, 1);
</ins><span class="cx">                                 }
</span><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         }
</span><span class="cx">                         switch_safe_free(mystream.data);
</span><del>-                        switch_core_db_close(db);
</del><ins>+                        switch_cache_db_release_db_handle(&amp;db);
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -692,36 +707,40 @@
</span><span class="cx"> 
</span><span class="cx">         if (string &amp;&amp; (mydata = strdup(string))) {
</span><span class="cx">                 if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) &gt;= 2) {
</span><del>-                        switch_core_db_t *db = switch_core_db_handle();
</del><ins>+                        switch_cache_db_handle_t *db = NULL;
</ins><span class="cx">                         char *sql = NULL;
</span><span class="cx"> 
</span><ins>+                        switch_core_db_handle(&amp;db);
</ins><span class="cx"> 
</span><span class="cx">                         if (!strcasecmp(argv[0], &quot;stickyadd&quot;) &amp;&amp; argc == 3) {
</span><del>-                                sql = switch_mprintf(&quot;delete from aliases where alias='%q'&quot;, argv[1]);
-                                switch_core_db_persistant_execute(db, sql, 5);
</del><ins>+                                sql = switch_mprintf(&quot;delete from aliases where alias='%q' and hostname='%q'&quot;, argv[1], switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, sql, 5);
</ins><span class="cx">                                 switch_safe_free(sql);
</span><del>-                                sql = switch_mprintf(&quot;insert into aliases (sticky, alias, command) values (1, '%q','%q')&quot;, argv[1], argv[2]);
-                                switch_core_db_persistant_execute(db, sql, 5);
</del><ins>+                                sql = switch_mprintf(&quot;insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')&quot;, 
+                                                                         argv[1], argv[2], switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, sql, 5);
</ins><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         } else if (!strcasecmp(argv[0], &quot;add&quot;) &amp;&amp; argc == 3) {
</span><del>-                                sql = switch_mprintf(&quot;delete from aliases where alias='%q'&quot;, argv[1]);
-                                switch_core_db_persistant_execute(db, sql, 5);
</del><ins>+                                sql = switch_mprintf(&quot;delete from aliases where alias='%q' and hostname='%q'&quot;, argv[1], switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, sql, 5);
</ins><span class="cx">                                 switch_safe_free(sql);
</span><del>-                                sql = switch_mprintf(&quot;insert into aliases (sticky, alias, command) values (0, '%q','%q')&quot;, argv[1], argv[2]);
-                                switch_core_db_persistant_execute(db, sql, 5);
</del><ins>+                                sql = switch_mprintf(&quot;insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q')&quot;, 
+                                                                         argv[1], argv[2], switch_core_get_variable(&quot;hostname&quot;));
+                                switch_cache_db_persistant_execute(db, sql, 5);
</ins><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         } else if (!strcasecmp(argv[0], &quot;del&quot;) &amp;&amp; argc == 2) {
</span><span class="cx">                                 char *what = argv[1];
</span><span class="cx">                                 if (!strcasecmp(what, &quot;*&quot;)) {
</span><del>-                                        switch_core_db_persistant_execute(db, &quot;delete from aliases&quot;, 1);
</del><ins>+                                        sql = switch_mprintf(&quot;delete from aliases where hostname='%q'&quot;, switch_core_get_variable(&quot;hostname&quot;));
+                                        switch_cache_db_persistant_execute(db, sql, 1);
</ins><span class="cx">                                 } else {
</span><del>-                                        sql = switch_mprintf(&quot;delete from aliases where alias='%q'&quot;, argv[1]);
-                                        switch_core_db_persistant_execute(db, sql, 5);
</del><ins>+                                        sql = switch_mprintf(&quot;delete from aliases where alias='%q' and hostname='%q'&quot;, argv[1], switch_core_get_variable(&quot;hostname&quot;));
+                                        switch_cache_db_persistant_execute(db, sql, 5);
</ins><span class="cx">                                 }
</span><span class="cx">                                 status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">                         }
</span><span class="cx">                         switch_safe_free(sql);
</span><del>-                        switch_core_db_close(db);
</del><ins>+                        switch_cache_db_release_db_handle(&amp;db);
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_corec"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_core.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -1282,9 +1282,10 @@
</span><span class="cx"> 
</span><span class="cx">         switch_core_state_machine_init(runtime.memory_pool);
</span><span class="cx"> 
</span><del>-        if (switch_test_flag((&amp;runtime), SCF_USE_SQL)) {
-                switch_core_sqldb_start(runtime.memory_pool);
</del><ins>+        if (switch_core_sqldb_start(runtime.memory_pool, switch_test_flag((&amp;runtime), SCF_USE_SQL) ? SWITCH_TRUE : SWITCH_FALSE) != SWITCH_STATUS_SUCCESS) {
+                abort();
</ins><span class="cx">         }
</span><ins>+
</ins><span class="cx">         switch_scheduler_task_thread_start();
</span><span class="cx"> 
</span><span class="cx">         switch_rtp_init(runtime.memory_pool);
</span><span class="lines">@@ -1422,6 +1423,18 @@
</span><span class="cx">                                         switch_rtp_set_start_port((switch_port_t) atoi(val));
</span><span class="cx">                                 } else if (!strcasecmp(var, &quot;rtp-end-port&quot;) &amp;&amp; !zstr(val)) {
</span><span class="cx">                                         switch_rtp_set_end_port((switch_port_t) atoi(val));
</span><ins>+                                } else if (!strcasecmp(var, &quot;core-db-dsn&quot;) &amp;&amp; !zstr(val)) {
+                                        if (switch_odbc_available()) {
+                                                runtime.odbc_dsn = switch_core_strdup(runtime.memory_pool, val);
+                                                if ((runtime.odbc_user = strchr(runtime.odbc_dsn, ':'))) {
+                                                        *runtime.odbc_user++ = '\0';
+                                                        if ((runtime.odbc_pass = strchr(runtime.odbc_user, ':'))) {
+                                                                *runtime.odbc_pass++ = '\0';
+                                                        }
+                                                }
+                                        } else {
+                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ODBC IS NOT AVAILABLE!\n&quot;);
+                                        }
</ins><span class="cx"> #ifdef ENABLE_ZRTP
</span><span class="cx">                                 } else if (!strcasecmp(var, &quot;rtp-enable-zrtp&quot;)) {
</span><span class="cx">                                         switch_core_set_variable(&quot;zrtp_enabled&quot;, val);
</span><span class="lines">@@ -1446,7 +1459,20 @@
</span><span class="cx"> 
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#define SWITCH_CORE_DB &quot;core&quot;
+/*!
+  \brief Open the default system database
+*/
+SWITCH_DECLARE(switch_status_t)switch_core_db_handle(switch_cache_db_handle_t **dbh)
+{
+        if (runtime.odbc_dsn &amp;&amp; runtime.odbc_user &amp;&amp; runtime.odbc_pass) {
+                return switch_cache_db_get_db_handle(dbh, runtime.odbc_dsn, runtime.odbc_user, runtime.odbc_pass);
+        } else {
+                return switch_cache_db_get_db_handle(dbh, SWITCH_CORE_DB, NULL, NULL);
+        }
+}
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(switch_core_flag_t flags, switch_bool_t console, const char **err)
</span><span class="cx"> {
</span><span class="cx">         switch_event_t *event;
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_core_dbc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core_db.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core_db.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_core_db.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -241,6 +241,111 @@
</span><span class="cx"> 
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+
+SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute_trans(switch_core_db_t *db, char *sql, uint32_t retries)
+{
+        char *errmsg;
+        switch_status_t status = SWITCH_STATUS_FALSE;
+        uint8_t forever = 0;
+        unsigned begin_retries = 100;
+        uint8_t again = 0;
+
+        if (!retries) {
+                forever = 1;
+                retries = 1000;
+        }
+
+again:
+
+        while (begin_retries &gt; 0) {
+                again = 0;
+
+                switch_core_db_exec(db, &quot;BEGIN&quot;, NULL, NULL, &amp;errmsg);
+
+                if (errmsg) {
+                        begin_retries--;
+                        if (strstr(errmsg, &quot;cannot start a transaction within a transaction&quot;)) {
+                                again = 1;
+                        } else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL Retry [%s]\n&quot;, errmsg);
+                        }
+                        switch_core_db_free(errmsg);
+                        errmsg = NULL;
+
+                        if (again) {
+                                switch_core_db_exec(db, &quot;COMMIT&quot;, NULL, NULL, NULL);
+                                goto again;
+                        }
+
+                        switch_yield(100000);
+
+                        if (begin_retries == 0) {
+                                goto done;
+                        }
+                } else {
+                        break;
+                }
+
+        }
+
+        while (retries &gt; 0) {
+                switch_core_db_exec(db, sql, NULL, NULL, &amp;errmsg);
+                if (errmsg) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n&quot;, errmsg);
+                        switch_core_db_free(errmsg);
+                        errmsg = NULL;
+                        switch_yield(100000);
+                        retries--;
+                        if (retries == 0 &amp;&amp; forever) {
+                                retries = 1000;
+                                continue;
+                        }
+                } else {
+                        status = SWITCH_STATUS_SUCCESS;
+                        break;
+                }
+        }
+
+done:
+
+        switch_core_db_exec(db, &quot;COMMIT&quot;, NULL, NULL, NULL);
+
+        return status;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute(switch_core_db_t *db, char *sql, uint32_t retries)
+{
+        char *errmsg;
+        switch_status_t status = SWITCH_STATUS_FALSE;
+        uint8_t forever = 0;
+
+        if (!retries) {
+                forever = 1;
+                retries = 1000;
+        }
+
+        while (retries &gt; 0) {
+                switch_core_db_exec(db, sql, NULL, NULL, &amp;errmsg);
+                if (errmsg) {
+                        //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n&quot;, errmsg);
+                        switch_core_db_free(errmsg);
+                        switch_yield(100000);
+                        retries--;
+                        if (retries == 0 &amp;&amp; forever) {
+                                retries = 1000;
+                                continue;
+                        }
+                } else {
+                        status = SWITCH_STATUS_SUCCESS;
+                        break;
+                }
+        }
+
+        return status;
+}
+
+
+
</ins><span class="cx"> /* For Emacs:
</span><span class="cx">  * Local Variables:
</span><span class="cx">  * mode:c
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_core_sqldbc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core_sqldb.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core_sqldb.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_core_sqldb.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -36,13 +36,13 @@
</span><span class="cx"> #include &quot;private/switch_core_pvt.h&quot;
</span><span class="cx"> 
</span><span class="cx"> static struct {
</span><del>-        switch_core_db_t *db;
-        switch_core_db_t *event_db;
</del><ins>+        switch_cache_db_handle_t *event_db;
</ins><span class="cx">         switch_queue_t *sql_queue[2];
</span><span class="cx">         switch_memory_pool_t *memory_pool;
</span><span class="cx">         switch_event_node_t *event_node;
</span><span class="cx">         switch_thread_t *thread;
</span><span class="cx">         int thread_running;
</span><ins>+        switch_bool_t manage;
</ins><span class="cx"> } sql_manager;
</span><span class="cx"> 
</span><span class="cx"> static switch_mutex_t *dbh_mutex = NULL;
</span><span class="lines">@@ -58,7 +58,7 @@
</span><span class="cx">         switch_cache_db_handle_t *dbh = NULL;
</span><span class="cx">         int locked = 0;
</span><span class="cx">         char *key;
</span><del>-
</del><ins>+        
</ins><span class="cx">         switch_mutex_lock(dbh_mutex);
</span><span class="cx">  top:
</span><span class="cx">         locked = 0;
</span><span class="lines">@@ -74,11 +74,12 @@
</span><span class="cx">                                 diff = (time_t) prune - dbh-&gt;last_used;
</span><span class="cx">                         }
</span><span class="cx">                         
</span><del>-                        if (diff &lt; SQL_CACHE_TIMEOUT) {
</del><ins>+                        if (prune &gt; 0 &amp;&amp; diff &lt; SQL_CACHE_TIMEOUT) {
</ins><span class="cx">                                 continue;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         if (switch_mutex_trylock(dbh-&gt;mutex) == SWITCH_STATUS_SUCCESS) {
</span><ins>+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Dropping idle DB connection %s\n&quot;, key);
</ins><span class="cx">                                 if (dbh-&gt;db) {
</span><span class="cx">                                         switch_core_db_close(dbh-&gt;db);
</span><span class="cx">                                         dbh-&gt;db = NULL;
</span><span class="lines">@@ -113,18 +114,19 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(switch_cache_db_handle_t *)switch_cache_db_get_db_handle(const char *db_name, const char *odbc_user, const char *odbc_pass)
</del><ins>+SWITCH_DECLARE(switch_status_t)switch_cache_db_get_db_handle(switch_cache_db_handle_t **dbh,
+                                                                                                                         const char *db_name, const char *odbc_user, const char *odbc_pass)
</ins><span class="cx"> {
</span><span class="cx">         switch_thread_id_t self = switch_thread_self();
</span><span class="cx">         char thread_str[256] = &quot;&quot;;
</span><del>-        switch_cache_db_handle_t *dbh = NULL;
</del><ins>+        switch_cache_db_handle_t *new_dbh = NULL;
</ins><span class="cx">         
</span><span class="cx">         switch_assert(db_name);
</span><span class="cx"> 
</span><span class="cx">         snprintf(thread_str, sizeof(thread_str) - 1, &quot;%s_%lu&quot;, db_name, (unsigned long)(intptr_t)self);
</span><span class="cx"> 
</span><span class="cx">         switch_mutex_lock(dbh_mutex);
</span><del>-        if (!(dbh = switch_core_hash_find(dbh_hash, thread_str))) {
</del><ins>+        if (!(new_dbh = switch_core_hash_find(dbh_hash, thread_str))) {
</ins><span class="cx">                 switch_memory_pool_t *pool = NULL;
</span><span class="cx">                 switch_core_db_t *db = NULL;
</span><span class="cx">                 switch_odbc_handle_t *odbc_dbh = NULL;
</span><span class="lines">@@ -145,83 +147,96 @@
</span><span class="cx">                 }
</span><span class="cx"> 
</span><span class="cx">                 switch_core_new_memory_pool(&amp;pool);
</span><del>-                dbh = switch_core_alloc(pool, sizeof(*dbh));
-                dbh-&gt;pool = pool;
</del><ins>+                new_dbh = switch_core_alloc(pool, sizeof(*new_dbh));
+                new_dbh-&gt;pool = pool;
</ins><span class="cx"> 
</span><span class="cx">                 
</span><del>-                if (db) dbh-&gt;db = db; else dbh-&gt;odbc_dbh = odbc_dbh;
-                switch_mutex_init(&amp;dbh-&gt;mutex, SWITCH_MUTEX_NESTED, dbh-&gt;pool);
-                switch_mutex_lock(dbh-&gt;mutex);
</del><ins>+                if (db) new_dbh-&gt;db = db; else new_dbh-&gt;odbc_dbh = odbc_dbh;
+                switch_mutex_init(&amp;new_dbh-&gt;mutex, 0, new_dbh-&gt;pool);
+                switch_mutex_lock(new_dbh-&gt;mutex);
</ins><span class="cx"> 
</span><del>-                switch_core_hash_insert(dbh_hash, thread_str, dbh);
</del><ins>+                switch_core_hash_insert(dbh_hash, thread_str, new_dbh);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">  end:
</span><span class="cx"> 
</span><del>-        if (dbh) dbh-&gt;last_used = switch_epoch_time_now(NULL);
</del><ins>+        if (new_dbh) new_dbh-&gt;last_used = switch_epoch_time_now(NULL);
</ins><span class="cx"> 
</span><span class="cx">         switch_mutex_unlock(dbh_mutex);
</span><span class="cx"> 
</span><del>-        return dbh;
</del><ins>+        *dbh = new_dbh;
+
+        return *dbh ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql)
</del><ins>+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql, char **err)
</ins><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><ins>+        char *errmsg = NULL;
</ins><span class="cx"> 
</span><ins>+        if (err) *err = NULL;
+
</ins><span class="cx">         if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
</span><span class="cx">                 switch_odbc_statement_handle_t stmt;
</span><span class="cx">                 if ((status = switch_odbc_handle_exec(dbh-&gt;odbc_dbh, sql, &amp;stmt)) != SWITCH_ODBC_SUCCESS) {
</span><del>-                        char *err_str;
-                        err_str = switch_odbc_handle_get_error(dbh-&gt;odbc_dbh, stmt);
-                        if (!zstr(err_str)) {
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ERR: [%s]\n[%s]\n&quot;, sql, err_str);
-                        }
-                        switch_safe_free(err_str);
</del><ins>+                        errmsg = switch_odbc_handle_get_error(dbh-&gt;odbc_dbh, stmt);
</ins><span class="cx">                 }
</span><span class="cx">                 switch_odbc_statement_handle_free(&amp;stmt);
</span><span class="cx">         } else {
</span><del>-                char *errmsg;
-
</del><span class="cx">         status = switch_core_db_exec(dbh-&gt;db, sql, NULL, NULL, &amp;errmsg);
</span><ins>+        }
</ins><span class="cx"> 
</span><del>-        if (errmsg) {
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n%s\n&quot;, errmsg, sql);
-            switch_core_db_free(errmsg);
-        }
-                
</del><ins>+        if (errmsg) {
+                if (!switch_stristr(&quot;already exists&quot;, errmsg)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n%s\n&quot;, errmsg, sql);
+                }
+                if (err) {
+                        *err = errmsg;
+                } else {
+                        free(errmsg);
+                }
</ins><span class="cx">         }
</span><del>-
</del><ins>+        
</ins><span class="cx">         return status;
</span><span class="cx"> 
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, 
-                                                                                                                                         char *sql, switch_core_db_callback_func_t callback, void *pdata)
-        
</del><ins>+SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute(switch_cache_db_handle_t *dbh, char *sql, uint32_t retries)
</ins><span class="cx"> {
</span><ins>+        char *errmsg = NULL;
</ins><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><del>-        char *errmsg = NULL;
-        
-        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
-                status = switch_odbc_handle_callback_exec(dbh-&gt;odbc_dbh, sql, callback, pdata);
-        } else {
-                status = switch_core_db_exec(dbh-&gt;db, sql, callback, pdata, &amp;errmsg);
-                
</del><ins>+        uint8_t forever = 0;
+
+        if (!retries) {
+                forever = 1;
+                retries = 1000;
+        }
+
+        while (retries &gt; 0) {
+                switch_cache_db_execute_sql(dbh, sql, &amp;errmsg);
</ins><span class="cx">                 if (errmsg) {
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR: [%s] %s\n&quot;, sql, errmsg);
-                        free(errmsg);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n&quot;, errmsg);
+                        switch_safe_free(errmsg);
+                        switch_yield(100000);
+                        retries--;
+                        if (retries == 0 &amp;&amp; forever) {
+                                retries = 1000;
+                                continue;
+                        }
+                } else {
+                        status = SWITCH_STATUS_SUCCESS;
+                        break;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_status_t switch_core_db_persistant_execute_trans(switch_core_db_t *db, char *sql, uint32_t retries)
</del><ins>+SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute_trans(switch_cache_db_handle_t *dbh, char *sql, uint32_t retries)
</ins><span class="cx"> {
</span><del>-        char *errmsg;
</del><ins>+        char *errmsg = NULL;
</ins><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="cx">         uint8_t forever = 0;
</span><span class="cx">         unsigned begin_retries = 100;
</span><span class="lines">@@ -237,7 +252,7 @@
</span><span class="cx">         while (begin_retries &gt; 0) {
</span><span class="cx">                 again = 0;
</span><span class="cx"> 
</span><del>-                switch_core_db_exec(db, &quot;BEGIN&quot;, NULL, NULL, &amp;errmsg);
</del><ins>+                switch_cache_db_execute_sql(dbh,  &quot;BEGIN&quot;, &amp;errmsg);
</ins><span class="cx"> 
</span><span class="cx">                 if (errmsg) {
</span><span class="cx">                         begin_retries--;
</span><span class="lines">@@ -246,11 +261,11 @@
</span><span class="cx">                         } else {
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL Retry [%s]\n&quot;, errmsg);
</span><span class="cx">                         }
</span><del>-                        switch_core_db_free(errmsg);
</del><ins>+                        free(errmsg);
</ins><span class="cx">                         errmsg = NULL;
</span><span class="cx"> 
</span><span class="cx">                         if (again) {
</span><del>-                                switch_core_db_exec(db, &quot;COMMIT&quot;, NULL, NULL, NULL);
</del><ins>+                                switch_cache_db_execute_sql(dbh,  &quot;COMMIT&quot;, NULL);
</ins><span class="cx">                                 goto again;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="lines">@@ -266,10 +281,10 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         while (retries &gt; 0) {
</span><del>-                switch_core_db_exec(db, sql, NULL, NULL, &amp;errmsg);
</del><ins>+                switch_cache_db_execute_sql(dbh,  sql, &amp;errmsg);
</ins><span class="cx">                 if (errmsg) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n&quot;, errmsg);
</span><del>-                        switch_core_db_free(errmsg);
</del><ins>+                        free(errmsg);
</ins><span class="cx">                         errmsg = NULL;
</span><span class="cx">                         switch_yield(100000);
</span><span class="cx">                         retries--;
</span><span class="lines">@@ -285,45 +300,70 @@
</span><span class="cx"> 
</span><span class="cx"> done:
</span><span class="cx"> 
</span><del>-        switch_core_db_exec(db, &quot;COMMIT&quot;, NULL, NULL, NULL);
</del><ins>+        switch_cache_db_execute_sql(dbh,  &quot;COMMIT&quot;, NULL);
</ins><span class="cx"> 
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-SWITCH_DECLARE(switch_status_t) switch_core_db_persistant_execute(switch_core_db_t *db, char *sql, uint32_t retries)
</del><ins>+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, 
+                                                                                                                                         char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
+        
</ins><span class="cx"> {
</span><del>-        char *errmsg;
</del><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><del>-        uint8_t forever = 0;
</del><ins>+        char *errmsg = NULL;
</ins><span class="cx"> 
</span><del>-        if (!retries) {
-                forever = 1;
-                retries = 1000;
-        }
</del><ins>+        if (err) *err = NULL;
</ins><span class="cx"> 
</span><del>-        while (retries &gt; 0) {
-                switch_core_db_exec(db, sql, NULL, NULL, &amp;errmsg);
</del><ins>+        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
+                status = switch_odbc_handle_callback_exec(dbh-&gt;odbc_dbh, sql, callback, pdata, err);
+        } else {
+                status = switch_core_db_exec(dbh-&gt;db, sql, callback, pdata, &amp;errmsg);
+                
</ins><span class="cx">                 if (errmsg) {
</span><del>-                        //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR [%s]\n&quot;, errmsg);
-                        switch_core_db_free(errmsg);
-                        switch_yield(100000);
-                        retries--;
-                        if (retries == 0 &amp;&amp; forever) {
-                                retries = 1000;
-                                continue;
-                        }
-                } else {
-                        status = SWITCH_STATUS_SUCCESS;
-                        break;
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;SQL ERR: [%s] %s\n&quot;, sql, errmsg);
+                        free(errmsg);
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+SWITCH_DECLARE(void) switch_cache_db_test_reactive(switch_cache_db_handle_t *dbh, char *test_sql, char *drop_sql, char *reactive_sql)
+{
+        char *errmsg;
</ins><span class="cx"> 
</span><ins>+        if (switch_odbc_available() &amp;&amp; dbh-&gt;odbc_dbh) {
+                if (switch_odbc_handle_exec(dbh-&gt;odbc_dbh, test_sql, NULL) != SWITCH_ODBC_SUCCESS) {
+                        switch_odbc_handle_exec(dbh-&gt;odbc_dbh, drop_sql, NULL);
+                        switch_odbc_handle_exec(dbh-&gt;odbc_dbh, reactive_sql, NULL);
+                }
+        } else if (dbh-&gt;db) {
+                if (test_sql) {
+                        switch_core_db_exec(dbh-&gt;db, test_sql, NULL, NULL, &amp;errmsg);
</ins><span class="cx"> 
</span><ins>+                        if (errmsg) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL ERR [%s]\n[%s]\nAuto Generating Table!\n&quot;, errmsg, test_sql);
+                                switch_core_db_free(errmsg);
+                                errmsg = NULL;
+                                if (drop_sql) {
+                                        switch_core_db_exec(dbh-&gt;db, drop_sql, NULL, NULL, &amp;errmsg);
+                                }
+                                if (errmsg) {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL ERR [%s]\n[%s]\n&quot;, errmsg, reactive_sql);
+                                        switch_core_db_free(errmsg);
+                                        errmsg = NULL;
+                                }
+                                switch_core_db_exec(dbh-&gt;db, reactive_sql, NULL, NULL, &amp;errmsg);
+                                if (errmsg) {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL ERR [%s]\n[%s]\n&quot;, errmsg, reactive_sql);
+                                        switch_core_db_free(errmsg);
+                                        errmsg = NULL;
+                                }
+                        }
+                }
+        }
</ins><span class="cx"> 
</span><ins>+}
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -340,24 +380,34 @@
</span><span class="cx">         switch_size_t newlen;
</span><span class="cx">         int lc = 0;
</span><span class="cx">         uint32_t loops = 0, sec = 0;
</span><ins>+        uint32_t l1 = 1000;
</ins><span class="cx"> 
</span><span class="cx">         switch_assert(sqlbuf);
</span><span class="cx"> 
</span><ins>+        if (!sql_manager.manage) {
+                l1 = 10;
+        }
+
</ins><span class="cx">         if (!sql_manager.event_db) {
</span><del>-                sql_manager.event_db = switch_core_db_handle();
</del><ins>+                switch_core_db_handle(&amp;sql_manager.event_db);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         sql_manager.thread_running = 1;
</span><span class="cx"> 
</span><del>-        for (;;) {
-                if (++loops == 1000) {
</del><ins>+        while(sql_manager.thread_running == 1) {
+                if (++loops == l1) {
</ins><span class="cx">                         if (++sec == SQL_CACHE_TIMEOUT) {
</span><span class="cx">                                 sql_close(switch_epoch_time_now(NULL));
</span><span class="cx">                                 sec = 0;
</span><span class="cx">                         }
</span><span class="cx">                         loops = 0;
</span><span class="cx">                 }
</span><del>-
</del><ins>+                
+                if (!sql_manager.manage) {
+                        switch_yield(100000);
+                        continue;
+                }
+                
</ins><span class="cx">                 if (switch_queue_trypop(sql_manager.sql_queue[0], &amp;pop) == SWITCH_STATUS_SUCCESS || 
</span><span class="cx">                         switch_queue_trypop(sql_manager.sql_queue[1], &amp;pop) == SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                         sql = (char *) pop;
</span><span class="lines">@@ -396,7 +446,7 @@
</span><span class="cx">                 
</span><span class="cx">                 
</span><span class="cx">                 if (trans &amp;&amp; ((itterations == target) || (nothing_in_queue &amp;&amp; ++lc &gt;= 500))) {
</span><del>-                        if (switch_core_db_persistant_execute_trans(sql_manager.event_db, sqlbuf, 100) != SWITCH_STATUS_SUCCESS) {
</del><ins>+                        if (switch_cache_db_persistant_execute_trans(sql_manager.event_db, sqlbuf, 100) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;SQL thread unable to commit transaction, records lost!\n&quot;);
</span><span class="cx">                         }
</span><span class="cx">                         itterations = 0;
</span><span class="lines">@@ -421,6 +471,9 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         free(sqlbuf);
</span><ins>+
+        sql_manager.thread_running = 0;
+
</ins><span class="cx">         return NULL;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -432,45 +485,65 @@
</span><span class="cx"> 
</span><span class="cx">         switch (event-&gt;event_id) {
</span><span class="cx">         case SWITCH_EVENT_ADD_SCHEDULE:
</span><del>-                sql = switch_mprintf(&quot;insert into tasks values('%q','%q','%q','%q')&quot;,
-                                                         switch_event_get_header_nil(event, &quot;task-id&quot;),
-                                                         switch_event_get_header_nil(event, &quot;task-desc&quot;),
-                                                         switch_event_get_header_nil(event, &quot;task-group&quot;), 
-                                                         switch_event_get_header_nil(event, &quot;task-sql_manager&quot;)
-                                                         );
</del><ins>+                {
+                        const char *id = switch_event_get_header(event, &quot;task-id&quot;);
+                        const char *manager = switch_event_get_header(event, &quot;task-sql_manager&quot;);
+                        
+                        if (id) {
+                                sql = switch_mprintf(&quot;insert into tasks values(%q,'%q','%q',%q, '%q')&quot;,
+                                                                         id,
+                                                                         switch_event_get_header_nil(event, &quot;task-desc&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;task-group&quot;), 
+                                                                         manager ? manager : &quot;0&quot;,
+                                                                         switch_core_get_variable(&quot;hostname&quot;)
+                                                                         );
+                        }
+                }
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_DEL_SCHEDULE:
</span><span class="cx">         case SWITCH_EVENT_EXE_SCHEDULE:
</span><del>-                sql = switch_mprintf(&quot;delete from tasks where task_id=%q&quot;, switch_event_get_header_nil(event, &quot;task-id&quot;));
</del><ins>+                sql = switch_mprintf(&quot;delete from tasks where task_id=%q and hostname='%q'&quot;, 
+                                                         switch_event_get_header_nil(event, &quot;task-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_RE_SCHEDULE:
</span><del>-                sql = switch_mprintf(&quot;update tasks set task_desc='%q',task_group='%q', task_sql_manager='%q' where task_id=%q&quot;,
-                                                         switch_event_get_header_nil(event, &quot;task-desc&quot;), 
-                                                         switch_event_get_header_nil(event, &quot;task-group&quot;), 
-                                                         switch_event_get_header_nil(event, &quot;task-sql_manager&quot;), 
-                                                         switch_event_get_header_nil(event, &quot;task-id&quot;));
</del><ins>+                {
+                        const char *id = switch_event_get_header(event, &quot;task-id&quot;);
+                        const char *manager = switch_event_get_header(event, &quot;task-sql_manager&quot;);
+                
+                        if (id) {
+                                sql = switch_mprintf(&quot;update tasks set task_desc='%q',task_group='%q', task_sql_manager=%q where task_id=%q and hostname='%q'&quot;,
+                                                                         switch_event_get_header_nil(event, &quot;task-desc&quot;), 
+                                                                         switch_event_get_header_nil(event, &quot;task-group&quot;), 
+                                                                         manager ? manager : &quot;0&quot;,
+                                                                         id, switch_core_get_variable(&quot;hostname&quot;));
+                        }
+                }
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_DESTROY:
</span><del>-                sql = switch_mprintf(&quot;delete from channels where uuid='%q'&quot;, switch_event_get_header_nil(event, &quot;unique-id&quot;));
</del><ins>+                sql = switch_mprintf(&quot;delete from channels where uuid='%q' and hostname='%q'&quot;, 
+                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_UUID:
</span><span class="cx">                 {
</span><span class="cx">                         sql = switch_mprintf(
</span><del>-                                                                 &quot;update channels set uuid='%q' where uuid='%q';&quot;
-                                                                 &quot;update calls set caller_uuid='%q' where caller_uuid='%q';&quot;
-                                                                 &quot;update calls set callee_uuid='%q' where callee_uuid='%q'&quot;,
</del><ins>+                                                                 &quot;update channels set uuid='%q' where uuid='%q' and hostname='%q';&quot;
+                                                                 &quot;update calls set caller_uuid='%q' where caller_uuid='%q' and hostname='%q';&quot;
+                                                                 &quot;update calls set callee_uuid='%q' where callee_uuid='%q' and hostname='%q'&quot;,
</ins><span class="cx">                                                                  switch_event_get_header_nil(event, &quot;unique-id&quot;),
</span><span class="cx">                                                                  switch_event_get_header_nil(event, &quot;old-unique-id&quot;),
</span><ins>+                                                                 switch_core_get_variable(&quot;hostname&quot;),
</ins><span class="cx">                                                                  switch_event_get_header_nil(event, &quot;unique-id&quot;),
</span><span class="cx">                                                                  switch_event_get_header_nil(event, &quot;old-unique-id&quot;),
</span><ins>+                                                                 switch_core_get_variable(&quot;hostname&quot;),
</ins><span class="cx">                                                                  switch_event_get_header_nil(event, &quot;unique-id&quot;),
</span><del>-                                                                 switch_event_get_header_nil(event, &quot;old-unique-id&quot;)
</del><ins>+                                                                 switch_event_get_header_nil(event, &quot;old-unique-id&quot;),
+                                                                 switch_core_get_variable(&quot;hostname&quot;)
</ins><span class="cx">                                                                  );
</span><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_CREATE:
</span><del>-                sql = switch_mprintf(&quot;insert into channels (uuid,direction,created,created_epoch, name,state,dialplan,context) &quot;
-                                                         &quot;values('%q','%q','%q','%ld','%q','%q','%q','%q')&quot;,
</del><ins>+                sql = switch_mprintf(&quot;insert into channels (uuid,direction,created,created_epoch, name,state,dialplan,context,hostname) &quot;
+                                                         &quot;values('%q','%q','%q','%ld','%q','%q','%q','%q','%q')&quot;,
</ins><span class="cx">                                                          switch_event_get_header_nil(event, &quot;unique-id&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;call-direction&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;event-date-local&quot;),
</span><span class="lines">@@ -478,24 +551,28 @@
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;channel-name&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;channel-state&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;caller-dialplan&quot;),
</span><del>-                                                         switch_event_get_header_nil(event, &quot;caller-context&quot;)
</del><ins>+                                                         switch_event_get_header_nil(event, &quot;caller-context&quot;),
+                                                         switch_core_get_variable(&quot;hostname&quot;)
</ins><span class="cx">                                                          );
</span><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_CODEC:
</span><span class="cx">                 sql =
</span><span class="cx">                         switch_mprintf
</span><del>-                        (&quot;update channels set read_codec='%q',read_rate='%q',write_codec='%q',write_rate='%q' where uuid='%q'&quot;,
</del><ins>+                        (&quot;update channels set read_codec='%q',read_rate='%q',write_codec='%q',write_rate='%q' where uuid='%q' and hostname='%q'&quot;,
</ins><span class="cx">                          switch_event_get_header_nil(event, &quot;channel-read-codec-name&quot;), 
</span><span class="cx">                          switch_event_get_header_nil(event, &quot;channel-read-codec-rate&quot;),
</span><span class="cx">                          switch_event_get_header_nil(event, &quot;channel-write-codec-name&quot;), 
</span><span class="cx">                          switch_event_get_header_nil(event, &quot;channel-write-codec-rate&quot;),
</span><del>-                         switch_event_get_header_nil(event, &quot;unique-id&quot;));
</del><ins>+                         switch_event_get_header_nil(event, &quot;unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_EXECUTE:
</span><del>-                sql = switch_mprintf(&quot;update channels set application='%q',application_data='%q' where uuid='%q'&quot;,
</del><ins>+                sql = switch_mprintf(&quot;update channels set application='%q',application_data='%q',&quot;
+                                                         &quot;presence_id='%q',presence_data='%q' where uuid='%q' and hostname='%q'&quot;,
</ins><span class="cx">                                                          switch_event_get_header_nil(event, &quot;application&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;application-data&quot;), 
</span><del>-                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;)
</del><ins>+                                                         switch_event_get_header_nil(event, &quot;channel-presence-id&quot;),
+                                                         switch_event_get_header_nil(event, &quot;channel-presence-data&quot;),
+                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;)
</ins><span class="cx"> 
</span><span class="cx">                                                          );
</span><span class="cx"> 
</span><span class="lines">@@ -514,26 +591,30 @@
</span><span class="cx">                         case CS_DESTROY:
</span><span class="cx">                                 break;
</span><span class="cx">                         case CS_ROUTING:
</span><del>-                                sql = switch_mprintf(&quot;update channels set state='%s',cid_name='%q',cid_num='%q',ip_addr='%s',dest='%q',dialplan='%q',context='%q' &quot;
-                                                                         &quot;where uuid='%s'&quot;,
</del><ins>+                                sql = switch_mprintf(&quot;update channels set state='%s',cid_name='%q',cid_num='%q',&quot;
+                                                                         &quot;ip_addr='%s',dest='%q',dialplan='%q',context='%q',presence_id='%q',presence_data='%q' &quot;
+                                                                         &quot;where uuid='%s' and hostname='%q'&quot;,
</ins><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;channel-state&quot;),
</span><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;caller-caller-id-name&quot;),
</span><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;caller-caller-id-number&quot;),
</span><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;caller-network-addr&quot;),
</span><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;caller-destination-number&quot;), 
</span><span class="cx">                                                                          switch_event_get_header_nil(event, &quot;caller-dialplan&quot;), 
</span><del>-                                                                         switch_event_get_header_nil(event, &quot;caller-context&quot;), 
-                                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;));
</del><ins>+                                                                         switch_event_get_header_nil(event, &quot;caller-context&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;channel-presence-id&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;channel-presence-data&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                                 break;
</span><span class="cx">                         default:
</span><del>-                                sql = switch_mprintf(&quot;update channels set state='%s' where uuid='%s'&quot;,
-                                                                         switch_event_get_header_nil(event, &quot;channel-state&quot;), switch_event_get_header_nil(event, &quot;unique-id&quot;));
</del><ins>+                                sql = switch_mprintf(&quot;update channels set state='%s' where uuid='%s' and hostname='%q'&quot;,
+                                                                         switch_event_get_header_nil(event, &quot;channel-state&quot;), 
+                                                                         switch_event_get_header_nil(event, &quot;unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                                 break;
</span><span class="cx">                         }
</span><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_BRIDGE:
</span><del>-                sql = switch_mprintf(&quot;insert into calls values ('%s', '%ld', '%s','%q','%q','%q','%q','%s','%q','%q','%q','%q','%s')&quot;,
</del><ins>+                sql = switch_mprintf(&quot;insert into calls values ('%s', '%ld', '%s','%q','%q','%q','%q','%s','%q','%q','%q','%q','%s','%q')&quot;,
</ins><span class="cx">                                                          switch_event_get_header_nil(event, &quot;event-date-local&quot;),
</span><span class="cx">                                                          (long)switch_epoch_time_now(NULL),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;event-calling-function&quot;),
</span><span class="lines">@@ -545,14 +626,23 @@
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;Other-Leg-caller-id-name&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;Other-Leg-caller-id-number&quot;),
</span><span class="cx">                                                          switch_event_get_header_nil(event, &quot;Other-Leg-destination-number&quot;),
</span><del>-                                                         switch_event_get_header_nil(event, &quot;Other-Leg-channel-name&quot;), switch_event_get_header_nil(event, &quot;Other-Leg-unique-id&quot;)
</del><ins>+                                                         switch_event_get_header_nil(event, &quot;Other-Leg-channel-name&quot;), 
+                                                         switch_event_get_header_nil(event, &quot;Other-Leg-unique-id&quot;),
+                                                         switch_core_get_variable(&quot;hostname&quot;)
</ins><span class="cx">                         );
</span><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_CHANNEL_UNBRIDGE:
</span><del>-                sql = switch_mprintf(&quot;delete from calls where caller_uuid='%s'&quot;, switch_event_get_header_nil(event, &quot;caller-unique-id&quot;));
</del><ins>+                sql = switch_mprintf(&quot;delete from calls where caller_uuid='%s' and hostname='%q'&quot;, 
+                                                         switch_event_get_header_nil(event, &quot;caller-unique-id&quot;), switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_SHUTDOWN:
</span><del>-                sql = switch_mprintf(&quot;delete from channels;delete from interfaces;delete from calls&quot;);
</del><ins>+                sql = switch_mprintf(&quot;delete from channels where hostname='%q';&quot;
+                                                         &quot;delete from interfaces where hostname='%q';&quot;
+                                                         &quot;delete from calls where hostname='%q'&quot;,
+                                                         switch_core_get_variable(&quot;hostname&quot;),
+                                                         switch_core_get_variable(&quot;hostname&quot;),
+                                                         switch_core_get_variable(&quot;hostname&quot;)
+                                                         );
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_EVENT_LOG:
</span><span class="cx">                 return;
</span><span class="lines">@@ -566,9 +656,10 @@
</span><span class="cx">                         const char *filename = switch_event_get_header_nil(event, &quot;filename&quot;);
</span><span class="cx">                         if (!zstr(type) &amp;&amp; !zstr(name)) {
</span><span class="cx">                                 sql =
</span><del>-                                        switch_mprintf(&quot;insert into interfaces (type,name,description,syntax,key,filename) values('%q','%q','%q','%q','%q','%q')&quot;,
-                                                                   type, name, switch_str_nil(description), switch_str_nil(syntax), switch_str_nil(key), switch_str_nil(filename)
-                                        );
</del><ins>+                                        switch_mprintf(&quot;insert into interfaces (type,name,description,syntax,key,filename,hostname) values('%q','%q','%q','%q','%q','%q','%q')&quot;,
+                                                                   type, name, switch_str_nil(description), switch_str_nil(syntax), 
+                                                                   switch_str_nil(key), switch_str_nil(filename), switch_core_get_variable(&quot;hostname&quot;)
+                                                                   );
</ins><span class="cx">                         }
</span><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="lines">@@ -577,7 +668,8 @@
</span><span class="cx">                         const char *type = switch_event_get_header_nil(event, &quot;type&quot;);
</span><span class="cx">                         const char *name = switch_event_get_header_nil(event, &quot;name&quot;);
</span><span class="cx">                         if (!zstr(type) &amp;&amp; !zstr(name)) {
</span><del>-                                sql = switch_mprintf(&quot;delete from interfaces where type='%q' and name='%q'&quot;, type, name);
</del><ins>+                                sql = switch_mprintf(&quot;delete from interfaces where type='%q' and name='%q' and hostname='%q'&quot;, type, name, 
+                                                                         switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                         }
</span><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="lines">@@ -588,9 +680,11 @@
</span><span class="cx">                         if (zstr(type)) {
</span><span class="cx">                                 break;
</span><span class="cx">                         }
</span><del>-                        sql = switch_mprintf(&quot;update channels set secure='%s' where uuid='%s'&quot;,
</del><ins>+                        sql = switch_mprintf(&quot;update channels set secure='%s' where uuid='%s' and hostname='%q'&quot;,
</ins><span class="cx">                                                                  type,
</span><del>-                                                                 switch_event_get_header_nil(event, &quot;caller-unique-id&quot;));
</del><ins>+                                                                 switch_event_get_header_nil(event, &quot;caller-unique-id&quot;),
+                                                                 switch_core_get_variable(&quot;hostname&quot;)
+                                                                 );
</ins><span class="cx">                         break;
</span><span class="cx">                 }
</span><span class="cx">         case SWITCH_EVENT_NAT:
</span><span class="lines">@@ -598,14 +692,17 @@
</span><span class="cx">                         const char *op = switch_event_get_header_nil(event, &quot;op&quot;);
</span><span class="cx">                         switch_bool_t sticky = switch_true(switch_event_get_header_nil(event, &quot;sticky&quot;));
</span><span class="cx">                         if (!strcmp(&quot;add&quot;, op)) {
</span><del>-                                sql = switch_mprintf(&quot;insert into nat (port, proto, sticky) values (%s, %s, %d)&quot;,
-                                                                        switch_event_get_header_nil(event, &quot;port&quot;),
-                                                                        switch_event_get_header_nil(event, &quot;proto&quot;),
-                                                                        sticky);
</del><ins>+                                sql = switch_mprintf(&quot;insert into nat (port, proto, sticky, hostname) values (%s, %s, %d,'%q')&quot;,
+                                                                         switch_event_get_header_nil(event, &quot;port&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;proto&quot;),
+                                                                         sticky,
+                                                                         switch_core_get_variable(&quot;hostname&quot;)
+                                                                         );
</ins><span class="cx">                         } else if (!strcmp(&quot;del&quot;, op)) {
</span><del>-                                sql = switch_mprintf(&quot;delete from nat where port=%s and proto=%s&quot;,
-                                                                        switch_event_get_header_nil(event, &quot;port&quot;),
-                                                                        switch_event_get_header_nil(event, &quot;proto&quot;));
</del><ins>+                                sql = switch_mprintf(&quot;delete from nat where port=%s and proto=%s and hostname='%q'&quot;,
+                                                                         switch_event_get_header_nil(event, &quot;port&quot;),
+                                                                         switch_event_get_header_nil(event, &quot;proto&quot;),
+                                                                         switch_core_get_variable(&quot;hostname&quot;));
</ins><span class="cx">                         } else if (!strcmp(&quot;status&quot;, op)) {
</span><span class="cx">                                 /* call show nat api */
</span><span class="cx">                         } else if (!strcmp(&quot;status_response&quot;, op)) {
</span><span class="lines">@@ -629,146 +726,219 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void switch_core_sqldb_start(switch_memory_pool_t *pool)
</del><ins>+
+static char create_complete_sql[] =
+        &quot;CREATE TABLE complete (\n&quot;
+        &quot;   sticky  INTEGER,\n&quot; 
+        &quot;   a1  VARCHAR(4096),\n&quot; 
+        &quot;   a2  VARCHAR(4096),\n&quot; 
+        &quot;   a3  VARCHAR(4096),\n&quot; 
+        &quot;   a4  VARCHAR(4096),\n&quot; 
+        &quot;   a5  VARCHAR(4096),\n&quot; 
+        &quot;   a6  VARCHAR(4096),\n&quot; 
+        &quot;   a7  VARCHAR(4096),\n&quot; 
+        &quot;   a8  VARCHAR(4096),\n&quot; 
+        &quot;   a9  VARCHAR(4096),\n&quot; 
+        &quot;   a10 VARCHAR(4096),\n&quot; 
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;;
+
+static char create_alias_sql[] =
+        &quot;CREATE TABLE aliases (\n&quot;
+        &quot;   sticky  INTEGER,\n&quot; 
+        &quot;   alias  VARCHAR(4096),\n&quot; 
+        &quot;   command  VARCHAR(4096),\n&quot; 
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;;
+
+static char create_channels_sql[] =
+        &quot;CREATE TABLE channels (\n&quot;
+        &quot;   uuid  VARCHAR(4096),\n&quot;
+        &quot;   direction  VARCHAR(4096),\n&quot;
+        &quot;   created  VARCHAR(4096),\n&quot;
+        &quot;   created_epoch  INTEGER,\n&quot;
+        &quot;   name  VARCHAR(4096),\n&quot;
+        &quot;   state  VARCHAR(4096),\n&quot;
+        &quot;   cid_name  VARCHAR(4096),\n&quot;
+        &quot;   cid_num  VARCHAR(4096),\n&quot;
+        &quot;   ip_addr  VARCHAR(4096),\n&quot;
+        &quot;   dest  VARCHAR(4096),\n&quot;
+        &quot;   application  VARCHAR(4096),\n&quot;
+        &quot;   application_data  VARCHAR(4096),\n&quot;
+        &quot;   dialplan VARCHAR(4096),\n&quot;
+        &quot;   context VARCHAR(4096),\n&quot;
+        &quot;   read_codec  VARCHAR(4096),\n&quot; 
+        &quot;   read_rate  VARCHAR(4096),\n&quot; 
+        &quot;   write_codec  VARCHAR(4096),\n&quot; 
+        &quot;   write_rate  VARCHAR(4096),\n&quot; 
+        &quot;   secure VARCHAR(4096),\n&quot;
+        &quot;   hostname VARCHAR(4096),\n&quot; 
+        &quot;   presence_id VARCHAR(4096),\n&quot; 
+        &quot;   presence_data VARCHAR(4096)\n&quot; 
+        &quot;);\ncreate index uuindex on channels (uuid,hostname);\n&quot;;
+static char create_calls_sql[] =
+        &quot;CREATE TABLE calls (\n&quot;
+        &quot;   call_created  VARCHAR(4096),\n&quot;
+        &quot;   call_created_epoch  INTEGER,\n&quot;
+        &quot;   function  VARCHAR(4096),\n&quot;
+        &quot;   caller_cid_name  VARCHAR(4096),\n&quot;
+        &quot;   caller_cid_num   VARCHAR(4096),\n&quot;
+        &quot;   caller_dest_num  VARCHAR(4096),\n&quot;
+        &quot;   caller_chan_name VARCHAR(4096),\n&quot;
+        &quot;   caller_uuid      VARCHAR(4096),\n&quot;
+        &quot;   callee_cid_name  VARCHAR(4096),\n&quot;
+        &quot;   callee_cid_num   VARCHAR(4096),\n&quot;
+        &quot;   callee_dest_num  VARCHAR(4096),\n&quot; 
+        &quot;   callee_chan_name VARCHAR(4096),\n&quot; 
+        &quot;   callee_uuid      VARCHAR(4096),\n&quot; 
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;
+        &quot;create index eruuindex on calls (caller_uuid,hostname);\n&quot;
+        &quot;create index eeuuindex on calls (callee_uuid,hostname);\n&quot;;
+static char create_interfaces_sql[] =
+        &quot;CREATE TABLE interfaces (\n&quot;
+        &quot;   type             VARCHAR(4096),\n&quot;
+        &quot;   name             VARCHAR(4096),\n&quot; 
+        &quot;   description      VARCHAR(4096),\n&quot; 
+        &quot;   key              VARCHAR(4096),\n&quot; 
+        &quot;   filename         VARCHAR(4096),\n&quot; 
+        &quot;   syntax           VARCHAR(4096),\n&quot; 
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;;
+static char create_tasks_sql[] =
+        &quot;CREATE TABLE tasks (\n&quot;
+        &quot;   task_id             INTEGER,\n&quot;
+        &quot;   task_desc           VARCHAR(4096),\n&quot; 
+        &quot;   task_group          VARCHAR(4096),\n&quot; 
+        &quot;   task_sql_manager    INTEGER,\n&quot; 
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;;
+static char create_nat_sql[] = 
+        &quot;CREATE TABLE nat (\n&quot;
+        &quot;   sticky  INTEGER,\n&quot; 
+        &quot;        port        INTEGER,\n&quot;
+        &quot;        proto        INTEGER,\n&quot;
+        &quot;   hostname VARCHAR(4096)\n&quot; 
+        &quot;);\n&quot;;
+
+switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage)
</ins><span class="cx"> {
</span><span class="cx">         switch_threadattr_t *thd_attr;
</span><ins>+        switch_cache_db_handle_t *dbh;
</ins><span class="cx"> 
</span><span class="cx">         sql_manager.memory_pool = pool;
</span><del>-
</del><ins>+        sql_manager.manage = manage;
+        
</ins><span class="cx">         switch_mutex_init(&amp;dbh_mutex, SWITCH_MUTEX_NESTED, sql_manager.memory_pool);
</span><span class="cx">         switch_core_hash_init(&amp;dbh_hash, sql_manager.memory_pool);
</span><span class="cx"> 
</span><ins>+ top:
+        
</ins><span class="cx">         /* Activate SQL database */
</span><del>-        if ((sql_manager.db = switch_core_db_handle()) == 0) {
</del><ins>+        if (switch_core_db_handle(&amp;dbh) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error Opening DB!\n&quot;);
</span><ins>+
+                if (runtime.odbc_dsn) {
+                        runtime.odbc_dsn = NULL;
+                        runtime.odbc_user = NULL;
+                        runtime.odbc_pass = NULL;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, &quot;Falling back to sqlite.\n&quot;);
+                        goto top;
+                }
+
+
</ins><span class="cx">                 switch_clear_flag((&amp;runtime), SCF_USE_SQL);
</span><ins>+                return SWITCH_STATUS_FALSE;
+        }
+
+        
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Opening DB\n&quot;);
+        if (dbh-&gt;db) {
+                switch_cache_db_execute_sql(dbh, &quot;drop table channels&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;drop table calls&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;drop table interfaces&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;drop table tasks&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;PRAGMA synchronous=OFF;&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;PRAGMA count_changes=OFF;&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;PRAGMA cache_size=8000&quot;, NULL);
+                switch_cache_db_execute_sql(dbh, &quot;PRAGMA temp_store=MEMORY;&quot;, NULL);
</ins><span class="cx">         } else {
</span><del>-                char create_complete_sql[] =
-                        &quot;CREATE TABLE complete (\n&quot;
-                        &quot;   sticky  INTEGER,\n&quot; 
-                        &quot;   a1  VARCHAR(255),\n&quot; 
-                        &quot;   a2  VARCHAR(255),\n&quot; 
-                        &quot;   a3  VARCHAR(255),\n&quot; 
-                        &quot;   a4  VARCHAR(255),\n&quot; 
-                        &quot;   a5  VARCHAR(255),\n&quot; 
-                        &quot;   a6  VARCHAR(255),\n&quot; 
-                        &quot;   a7  VARCHAR(255),\n&quot; 
-                        &quot;   a8  VARCHAR(255),\n&quot; 
-                        &quot;   a9  VARCHAR(255),\n&quot; 
-                        &quot;   a10 VARCHAR(255)\n&quot; 
-                        &quot;);\n&quot;;
</del><ins>+                char sql[512] = &quot;&quot;;
+                char *tables[] = {&quot;channels&quot;, &quot;calls&quot;, &quot;interfaces&quot;, &quot;tasks&quot;, NULL};
+                int i;
+                const char *hostname = switch_core_get_variable(&quot;hostname&quot;);
</ins><span class="cx"> 
</span><del>-                char create_alias_sql[] =
-                        &quot;CREATE TABLE aliases (\n&quot;
-                        &quot;   sticky  INTEGER,\n&quot; 
-                        &quot;   alias  VARCHAR(255),\n&quot; 
-                        &quot;   command  VARCHAR(255)\n&quot; 
-                        &quot;);\n&quot;;
</del><ins>+                for(i = 0; tables[i]; i++) {
+                        switch_snprintf(sql, sizeof(sql), &quot;delete from %s where hostname='%s'&quot;, tables[i], hostname);
+                        switch_cache_db_execute_sql(dbh, sql, NULL);
+                }
+        }
</ins><span class="cx"> 
</span><del>-                char create_channels_sql[] =
-                        &quot;CREATE TABLE channels (\n&quot;
-                        &quot;   uuid  VARCHAR(255),\n&quot;
-                        &quot;   direction  VARCHAR(255),\n&quot;
-                        &quot;   created  VARCHAR(255),\n&quot;
-                        &quot;   created_epoch  INTEGER,\n&quot;
-                        &quot;   name  VARCHAR(255),\n&quot;
-                        &quot;   state  VARCHAR(255),\n&quot;
-                        &quot;   cid_name  VARCHAR(255),\n&quot;
-                        &quot;   cid_num  VARCHAR(255),\n&quot;
-                        &quot;   ip_addr  VARCHAR(255),\n&quot;
-                        &quot;   dest  VARCHAR(255),\n&quot;
-                        &quot;   application  VARCHAR(255),\n&quot;
-                        &quot;   application_data  VARCHAR(255),\n&quot;
-                        &quot;   dialplan VARCHAR(255),\n&quot;
-                        &quot;   context VARCHAR(255),\n&quot;
-                        &quot;   read_codec  VARCHAR(255),\n&quot; 
-                        &quot;   read_rate  VARCHAR(255),\n&quot; 
-                        &quot;   write_codec  VARCHAR(255),\n&quot; 
-                        &quot;   write_rate  VARCHAR(255),\n&quot; 
-                        &quot;   secure VARCHAR(255)\n&quot;
-                        &quot;);\ncreate index uuindex on channels (uuid);\n&quot;;
-                char create_calls_sql[] =
-                        &quot;CREATE TABLE calls (\n&quot;
-                        &quot;   call_created  VARCHAR(255),\n&quot;
-                        &quot;   call_created_epoch  INTEGER,\n&quot;
-                        &quot;   function  VARCHAR(255),\n&quot;
-                        &quot;   caller_cid_name  VARCHAR(255),\n&quot;
-                        &quot;   caller_cid_num   VARCHAR(255),\n&quot;
-                        &quot;   caller_dest_num  VARCHAR(255),\n&quot;
-                        &quot;   caller_chan_name VARCHAR(255),\n&quot;
-                        &quot;   caller_uuid      VARCHAR(255),\n&quot;
-                        &quot;   callee_cid_name  VARCHAR(255),\n&quot;
-                        &quot;   callee_cid_num   VARCHAR(255),\n&quot;
-                        &quot;   callee_dest_num  VARCHAR(255),\n&quot; 
-                        &quot;   callee_chan_name VARCHAR(255),\n&quot; 
-                        &quot;   callee_uuid      VARCHAR(255)\n&quot; 
-                        &quot;);\n&quot;
-                        &quot;create index eruuindex on calls (caller_uuid);\n&quot;
-                        &quot;create index eeuuindex on calls (callee_uuid);\n&quot;;
-                char create_interfaces_sql[] =
-                        &quot;CREATE TABLE interfaces (\n&quot;
-                        &quot;   type             VARCHAR(255),\n&quot;
-                        &quot;   name             VARCHAR(255),\n&quot; 
-                        &quot;   description      VARCHAR(255),\n&quot; 
-                        &quot;   key              VARCHAR(255),\n&quot; 
-                        &quot;   filename         VARCHAR(255),\n&quot; 
-                        &quot;   syntax           VARCHAR(255)\n&quot; 
-                        &quot;);\n&quot;;
-                char create_tasks_sql[] =
-                        &quot;CREATE TABLE tasks (\n&quot;
-                        &quot;   task_id             INTEGER(4),\n&quot;
-                        &quot;   task_desc           VARCHAR(255),\n&quot; 
-                        &quot;   task_group          VARCHAR(255),\n&quot; 
-                        &quot;   task_sql_manager    INTEGER(8)\n&quot; 
-                        &quot;);\n&quot;;
-                char create_nat_sql[] = 
-                        &quot;CREATE TABLE nat (\n&quot;
-                        &quot;   sticky  INTEGER,\n&quot; 
-                        &quot;        port        INTEGER,\n&quot;
-                        &quot;        proto        INTEGER\n&quot;
-                        &quot;);\n&quot;;
</del><ins>+                
+        switch_cache_db_test_reactive(dbh, &quot;select hostname from complete&quot;, &quot;DROP TABLE complete&quot;, create_complete_sql);
+        switch_cache_db_test_reactive(dbh, &quot;select hostname from aliases&quot;, &quot;DROP TABLE aliases&quot;, create_alias_sql);
+        switch_cache_db_test_reactive(dbh, &quot;select hostname from nat&quot;, &quot;DROP TABLE nat&quot;, create_nat_sql);
</ins><span class="cx"> 
</span><del>-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Opening DB\n&quot;);
-                switch_core_db_exec(sql_manager.db, &quot;drop table channels&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;drop table calls&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;drop table interfaces&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;drop table tasks&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;PRAGMA synchronous=OFF;&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;PRAGMA count_changes=OFF;&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;PRAGMA cache_size=8000&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;PRAGMA temp_store=MEMORY;&quot;, NULL, NULL, NULL);
</del><span class="cx"> 
</span><del>-                switch_core_db_test_reactive(sql_manager.db, &quot;select sticky from complete&quot;, &quot;DROP TABLE complete&quot;, create_complete_sql);
-                switch_core_db_test_reactive(sql_manager.db, &quot;select sticky from aliases&quot;, &quot;DROP TABLE aliases&quot;, create_alias_sql);
-                switch_core_db_test_reactive(sql_manager.db, &quot;select sticky from nat&quot;, &quot;DROP TABLE nat&quot;, create_nat_sql);
-                switch_core_db_exec(sql_manager.db, &quot;delete from complete where sticky=0&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;delete from aliases where sticky=0&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;delete from nat where sticky=0&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists alias1 on aliases (alias)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete1 on complete (a1)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete2 on complete (a2)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete3 on complete (a3)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete4 on complete (a4)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete5 on complete (a5)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete6 on complete (a6)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete7 on complete (a7)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete8 on complete (a8)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete9 on complete (a9)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create index if not exists complete10 on complete (a10)&quot;, NULL, NULL, NULL);
-                switch_core_db_exec(sql_manager.db, &quot;create unique index if not exists nat_map_port_proto on nat (port,proto)&quot;, NULL, NULL, NULL);
-                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);
-                switch_core_db_exec(sql_manager.db, create_tasks_sql, NULL, NULL, NULL);
</del><ins>+        if (dbh-&gt;odbc_dbh) {
+                char *err;
</ins><span class="cx"> 
</span><del>-                if (switch_event_bind_removable(&quot;core_db&quot;, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, core_event_handler, NULL, &amp;sql_manager.event_node) != SWITCH_STATUS_SUCCESS) {
</del><ins>+                switch_cache_db_execute_sql(dbh, &quot;begin;delete from channels where hostname='';delete from channels where hostname='';commit;&quot;, &amp;err);
+
+                if (err) {
+                        runtime.odbc_dsn = NULL;
+                        runtime.odbc_user = NULL;
+                        runtime.odbc_pass = NULL;
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Transactions not supported on your DB, disabling ODBC\n&quot;);
+                        switch_cache_db_release_db_handle(&amp;dbh);
+                        free(err);
+                        goto top;
+                }
+        }
+
+
+        if (dbh-&gt;db) {
+                switch_cache_db_execute_sql(dbh, create_channels_sql, NULL);
+                switch_cache_db_execute_sql(dbh, create_calls_sql, NULL);
+                switch_cache_db_execute_sql(dbh, create_interfaces_sql, NULL);
+                switch_cache_db_execute_sql(dbh, create_tasks_sql, NULL);
+        } else {
+                switch_cache_db_test_reactive(dbh, &quot;select hostname from channels&quot;, &quot;DROP TABLE channels&quot;, create_channels_sql);
+                switch_cache_db_test_reactive(dbh, &quot;select hostname from calls&quot;, &quot;DROP TABLE calls&quot;, create_calls_sql);
+                switch_cache_db_test_reactive(dbh, &quot;select hostname from interfaces&quot;, &quot;DROP TABLE interfaces&quot;, create_interfaces_sql);
+                switch_cache_db_test_reactive(dbh, &quot;select hostname from tasks&quot;, &quot;DROP TABLE tasks&quot;, create_tasks_sql);
+                
+        }
+
+        switch_cache_db_execute_sql(dbh, &quot;delete from complete where sticky=0&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;delete from aliases where sticky=0&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;delete from nat where sticky=0&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index alias1 on aliases (alias)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete1 on complete (a1,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete2 on complete (a2,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete3 on complete (a3,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete4 on complete (a4,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete5 on complete (a5,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete6 on complete (a6,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete7 on complete (a7,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete8 on complete (a8,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete9 on complete (a9,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index complete10 on complete (a10,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create unique index nat_map_port_proto on nat (port,proto,hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index channels1 on channels(hostname)&quot;, NULL);
+        switch_cache_db_execute_sql(dbh, &quot;create index calls1 on calls(hostname)&quot;, NULL);
+
+
+        if (sql_manager.manage) {
+                if (switch_event_bind_removable(&quot;core_db&quot;, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, 
+                                                                                core_event_handler, NULL, &amp;sql_manager.event_node) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't bind event handler!\n&quot;);
</span><span class="cx">                 }
</span><ins>+
+                switch_queue_create(&amp;sql_manager.sql_queue[0], SWITCH_SQL_QUEUE_LEN, sql_manager.memory_pool);
+                switch_queue_create(&amp;sql_manager.sql_queue[1], SWITCH_SQL_QUEUE_LEN, sql_manager.memory_pool);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        switch_queue_create(&amp;sql_manager.sql_queue[0], SWITCH_SQL_QUEUE_LEN, sql_manager.memory_pool);
-        switch_queue_create(&amp;sql_manager.sql_queue[1], SWITCH_SQL_QUEUE_LEN, sql_manager.memory_pool);
-
</del><span class="cx">         switch_threadattr_create(&amp;thd_attr, sql_manager.memory_pool);
</span><span class="cx">         switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
</span><span class="cx">         switch_thread_create(&amp;sql_manager.thread, thd_attr, switch_core_sql_thread, NULL, sql_manager.memory_pool);
</span><span class="lines">@@ -776,6 +946,10 @@
</span><span class="cx">         while (!sql_manager.thread_running) {
</span><span class="cx">                 switch_yield(10000);
</span><span class="cx">         }
</span><ins>+        
+        switch_cache_db_release_db_handle(&amp;dbh);
+
+        return SWITCH_STATUS_SUCCESS;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void switch_core_sqldb_stop(void)
</span><span class="lines">@@ -783,17 +957,23 @@
</span><span class="cx">         switch_status_t st;
</span><span class="cx"> 
</span><span class="cx">         switch_event_unbind(&amp;sql_manager.event_node);
</span><ins>+        
+        switch_cache_db_release_db_handle(&amp;sql_manager.event_db);
+        sql_close(0);
</ins><span class="cx"> 
</span><del>-        switch_queue_push(sql_manager.sql_queue[0], NULL);
-        switch_queue_push(sql_manager.sql_queue[1], NULL);
</del><ins>+        if (sql_manager.thread &amp;&amp; sql_manager.thread_running) {
</ins><span class="cx"> 
</span><del>-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, &quot;Waiting for unfinished SQL transactions\n&quot;);
-        switch_thread_join(&amp;st, sql_manager.thread);
-        sql_close(0);
</del><ins>+                if (sql_manager.manage) {
+                        switch_queue_push(sql_manager.sql_queue[0], NULL);
+                        switch_queue_push(sql_manager.sql_queue[1], NULL);
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, &quot;Waiting for unfinished SQL transactions\n&quot;);
+                }
</ins><span class="cx"> 
</span><del>-        switch_core_db_close(sql_manager.db);
-        switch_core_db_close(sql_manager.event_db);
</del><ins>+                sql_manager.thread_running = -1;
+                switch_thread_join(&amp;st, sql_manager.thread);
+        }
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">         switch_core_hash_destroy(&amp;dbh_hash);
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_odbcc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_odbc.c (15486 => 15487)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_odbc.c        2009-11-16 21:14:38 UTC (rev 15486)
+++ freeswitch/trunk/src/switch_odbc.c        2009-11-17 00:12:54 UTC (rev 15487)
</span><span class="lines">@@ -424,7 +424,7 @@
</span><span class="cx"> 
</span><span class="cx"> SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_callback_exec_detailed(const char *file, const char *func, int line,
</span><span class="cx">                                                                                                                                                            switch_odbc_handle_t *handle,
</span><del>-                                                                                                                                                           char *sql, switch_core_db_callback_func_t callback, void *pdata)
</del><ins>+                                                                                                                                                           char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err)
</ins><span class="cx"> {
</span><span class="cx"> #ifdef SWITCH_HAVE_ODBC
</span><span class="cx">         SQLHSTMT stmt = NULL;
</span><span class="lines">@@ -432,7 +432,6 @@
</span><span class="cx">         SQLLEN m = 0, t = 0;
</span><span class="cx">         char *err_str = NULL;
</span><span class="cx">         int result;
</span><del>-        int err = 0;
</del><span class="cx">         
</span><span class="cx">         switch_assert(callback != NULL);
</span><span class="cx"> 
</span><span class="lines">@@ -441,12 +440,12 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (SQLAllocHandle(SQL_HANDLE_STMT, handle-&gt;con, &amp;stmt) != SQL_SUCCESS) {
</span><del>-                err_str = &quot;Unable to SQL allocate handle.&quot;;
</del><ins>+                err_str = strdup(&quot;Unable to SQL allocate handle.&quot;);
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (SQLPrepare(stmt, (unsigned char *) sql, SQL_NTS) != SQL_SUCCESS) {
</span><del>-                err_str = &quot;Unable to prepare SQL statement.&quot;;
</del><ins>+                err_str = strdup(&quot;Unable to prepare SQL statement.&quot;);
</ins><span class="cx">                 goto error;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -517,27 +516,24 @@
</span><span class="cx">         
</span><span class="cx">         SQLFreeHandle(SQL_HANDLE_STMT, stmt);
</span><span class="cx"> 
</span><del>-        if (!err) {
-                return SWITCH_ODBC_SUCCESS;
-        }
-
</del><span class="cx">  error:
</span><span class="cx"> 
</span><del>-        /* err_str is already defined  for some error cases */
-        if (err_str != NULL) {
-                switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, &quot;ERR: [%s]\n[%s]\n&quot;, sql, switch_str_nil(err_str));
-                err_str = NULL;
-        }
-        
</del><span class="cx">         if (stmt) {
</span><span class="cx">                 err_str = switch_odbc_handle_get_error(handle, stmt);
</span><del>-                if (!zstr(err_str)) {
-                        switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, &quot;ERR: [%s]\n[%s]\n&quot;, sql, switch_str_nil(err_str));
-                }
-                switch_safe_free(err_str);
</del><span class="cx">                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        if (err_str) {
+                switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_ERROR, &quot;ERR: [%s]\n[%s]\n&quot;, sql, switch_str_nil(err_str));
+                if (err) {
+                        *err = err_str;
+                } else {
+                        *err = NULL;
+                        free(err_str);
+                }
+        }
+        
+
</ins><span class="cx"> #endif
</span><span class="cx">         return SWITCH_ODBC_FAIL;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>