<!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][15680] </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=15680">15680</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-11-25 21:54:04 -0600 (Wed, 25 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>use new cached odbc api</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc">freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_cidlookupmod_cidlookupc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c (15679 => 15680)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-11-26 02:28:03 UTC (rev 15679)
+++ freeswitch/trunk/src/mod/applications/mod_cidlookup/mod_cidlookup.c        2009-11-26 03:54:04 UTC (rev 15680)
</span><span class="lines">@@ -55,12 +55,12 @@
</span><span class="cx">         int cache_expire;
</span><span class="cx">         
</span><span class="cx">         char *odbc_dsn;
</span><ins>+        char *odbc_user;
+        char *odbc_pass;
</ins><span class="cx">         char *sql;
</span><span class="cx">         char *citystate_sql;
</span><span class="cx"> 
</span><del>-        switch_mutex_t *db_mutex;
</del><span class="cx">         switch_memory_pool_t *pool;
</span><del>-        switch_odbc_handle_t *master_odbc;
</del><span class="cx"> } globals;
</span><span class="cx"> 
</span><span class="cx"> struct http_data {
</span><span class="lines">@@ -84,71 +84,62 @@
</span><span class="cx"> };
</span><span class="cx"> typedef struct callback_obj callback_t;
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> static switch_event_node_t *reload_xml_event = NULL;
</span><span class="cx"> 
</span><ins>+static switch_cache_db_handle_t *cidlookup_get_db_handle(void)
+{
+        switch_cache_db_connection_options_t options = { {0} };
+        switch_cache_db_handle_t *dbh = NULL;
+        
+        if (globals.odbc_dsn &amp;&amp; globals.odbc_user &amp;&amp; globals.odbc_pass) {
+                options.odbc_options.dsn = globals.odbc_dsn;
+                options.odbc_options.user = globals.odbc_user;
+                options.odbc_options.pass = globals.odbc_pass;
+
+                if (switch_cache_db_get_db_handle(&amp;dbh, SCDB_TYPE_ODBC, &amp;options) != SWITCH_STATUS_SUCCESS) dbh = NULL;
+        }
+        return dbh;
+}
+
+
</ins><span class="cx"> static switch_status_t config_callback_dsn(switch_xml_config_item_t *data, const char *newvalue, switch_config_callback_type_t callback_type, switch_bool_t changed)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><del>-        char *odbc_user = NULL;
-        char *odbc_pass = NULL;
-        char *odbc_dsn = NULL;
</del><span class="cx">         
</span><del>-        switch_odbc_handle_t *odbc = NULL;
</del><ins>+        switch_cache_db_handle_t *dbh = NULL;
</ins><span class="cx"> 
</span><span class="cx">         if (!switch_odbc_available()) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;ODBC is not compiled in.  Do not configure odbc-dsn parameter!\n&quot;);
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (globals.db_mutex) {
-                switch_mutex_lock(globals.db_mutex);
-        }
-        
</del><span class="cx">         if ((callback_type == CONFIG_LOAD || callback_type == CONFIG_RELOAD) &amp;&amp; changed) {
</span><span class="cx"> 
</span><span class="cx">                 if(zstr(newvalue)) {
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;No local database defined.\n&quot;);
</span><span class="cx">                 } else {
</span><del>-                        odbc_dsn = strdup(newvalue);
-                        if ((odbc_user = strchr(odbc_dsn, ':'))) {
-                                *odbc_user++ = '\0';
-                                if ((odbc_pass = strchr(odbc_user, ':'))) {
-                                        *odbc_pass++ = '\0';
</del><ins>+                        switch_safe_free(globals.odbc_dsn);
+                        globals.odbc_dsn = strdup(newvalue);
+                        if ((globals.odbc_user = strchr(globals.odbc_dsn, ':'))) {
+                                *globals.odbc_user++ = '\0';
+                                if ((globals.odbc_pass = strchr(globals.odbc_user, ':'))) {
+                                        *globals.odbc_pass++ = '\0';
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;Connecting to dsn: %s\n&quot;, globals.odbc_dsn);
</span><del>-                        
-                        /* setup dsn */
-                        
-                        if (!(odbc = switch_odbc_handle_new(odbc_dsn, odbc_user, odbc_pass))) {
</del><ins>+
+                        if (!(dbh = cidlookup_get_db_handle())) {
</ins><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Cannot Open ODBC Database!\n&quot;);
</span><span class="cx">                                 switch_goto_status(SWITCH_STATUS_FALSE, done);
</span><span class="cx">                         }
</span><del>-                        if (switch_odbc_handle_connect(odbc) != SWITCH_ODBC_SUCCESS) {
-                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Cannot Open ODBC Database!\n&quot;);
-                                switch_goto_status(SWITCH_STATUS_FALSE, done);
-                        }
</del><span class="cx">                 }
</span><del>-                
-                /* ok, we have a new connection, tear down old one */
-                if (globals.master_odbc) {
-                        switch_odbc_handle_disconnect(globals.master_odbc);
-                        switch_odbc_handle_destroy(&amp;globals.master_odbc);
-                }
-                
-                /* and swap in new connection */
-                globals.master_odbc = odbc;
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         switch_goto_status(SWITCH_STATUS_SUCCESS, done);
</span><span class="cx">         
</span><span class="cx"> done:
</span><del>-        if (globals.db_mutex) {
-                switch_mutex_unlock(globals.db_mutex);
-        }
-        switch_safe_free(odbc_dsn);
</del><ins>+        switch_cache_db_release_db_handle(&amp;dbh);
</ins><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -180,20 +171,23 @@
</span><span class="cx">         do_config(SWITCH_TRUE);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static switch_bool_t cidlookup_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata)
</del><ins>+static switch_bool_t cidlookup_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, callback_t *cbt, char **err)
</ins><span class="cx"> {
</span><span class="cx">         switch_bool_t retval = SWITCH_FALSE;
</span><ins>+        switch_cache_db_handle_t *dbh = NULL;
</ins><span class="cx">         
</span><del>-        switch_mutex_lock(globals.db_mutex);
-        if (globals.odbc_dsn) {
-                if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata, NULL)
</del><ins>+        if (globals.odbc_dsn &amp;&amp; (dbh = cidlookup_get_db_handle())) {
+                if (switch_cache_db_execute_sql_callback(dbh, sql, callback, (void *)cbt, err)
</ins><span class="cx">                                 == SWITCH_ODBC_FAIL) {
</span><span class="cx">                         retval = SWITCH_FALSE;
</span><span class="cx">                 } else {
</span><span class="cx">                         retval = SWITCH_TRUE;
</span><span class="cx">                 }
</span><ins>+        } else {
+                *err = switch_core_sprintf(cbt-&gt;pool, &quot;Unable to get ODBC handle.  dsn: %s, dbh is %s\n&quot;, globals.odbc_dsn, dbh ? &quot;not null&quot; : &quot;null&quot;);
</ins><span class="cx">         }
</span><del>-        switch_mutex_unlock(globals.db_mutex);
</del><ins>+        
+        switch_cache_db_release_db_handle(&amp;dbh);
</ins><span class="cx">         return retval;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -492,16 +486,17 @@
</span><span class="cx"> static char *do_db_lookup(switch_memory_pool_t *pool, switch_event_t *event, const char *num, const char *sql) {
</span><span class="cx">         char *name = NULL;
</span><span class="cx">         char *newsql = NULL;
</span><ins>+        char *err = NULL;
</ins><span class="cx">         callback_t cbt = { 0 };
</span><span class="cx">         cbt.pool = pool;
</span><span class="cx">         
</span><del>-        if (globals.master_odbc) {
</del><ins>+        if (globals.odbc_dsn) {
</ins><span class="cx">                 newsql = switch_event_expand_headers(event, sql);
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;SQL: %s\n&quot;, newsql);
</span><del>-                if (cidlookup_execute_sql_callback(newsql, cidlookup_callback, &amp;cbt)) {
</del><ins>+                if (cidlookup_execute_sql_callback(newsql, cidlookup_callback, &amp;cbt, &amp;err)) {
</ins><span class="cx">                         name = cbt.name;
</span><span class="cx">                 } else {
</span><del>-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unable to lookup cid\n&quot;);
</del><ins>+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unable to lookup cid: %s\n&quot;, err ? err : &quot;(null)&quot;);
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">         if (newsql != globals.sql) {
</span><span class="lines">@@ -524,7 +519,7 @@
</span><span class="cx">         switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, &quot;caller_id_number&quot;, number);
</span><span class="cx"> 
</span><span class="cx">         /* database always wins */
</span><del>-        if (switch_odbc_available() &amp;&amp; globals.master_odbc &amp;&amp; globals.sql) {
</del><ins>+        if (switch_odbc_available() &amp;&amp; globals.odbc_dsn &amp;&amp; globals.sql) {
</ins><span class="cx">                 name = do_db_lookup(pool, event, number, globals.sql);
</span><span class="cx">                 if (name) {
</span><span class="cx">                         cid-&gt;name = name;
</span><span class="lines">@@ -572,7 +567,7 @@
</span><span class="cx">         if (!cid-&gt;area &amp;&amp;
</span><span class="cx">                 !skipcitystate &amp;&amp; 
</span><span class="cx">                 strlen(number) == 11 &amp;&amp; number[0] == '1' &amp;&amp;
</span><del>-                switch_odbc_available() &amp;&amp; globals.master_odbc &amp;&amp; globals.citystate_sql) {
</del><ins>+                switch_odbc_available() &amp;&amp; globals.odbc_dsn &amp;&amp; globals.citystate_sql) {
</ins><span class="cx">                 
</span><span class="cx">                 /* yes, this is really area */
</span><span class="cx">                 name = do_db_lookup(pool, event, number, globals.citystate_sql);
</span><span class="lines">@@ -771,12 +766,6 @@
</span><span class="cx">         
</span><span class="cx">         globals.pool = pool;
</span><span class="cx"> 
</span><del>-        if (switch_odbc_available() &amp;&amp; !globals.db_mutex) {
-                if (switch_mutex_init(&amp;globals.db_mutex, SWITCH_MUTEX_UNNESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;failed to initialize db_mutex\n&quot;);
-                }
-        }
-
</del><span class="cx">         do_config(SWITCH_FALSE);
</span><span class="cx">         
</span><span class="cx">         if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &amp;reload_xml_event) != SWITCH_STATUS_SUCCESS)) {
</span><span class="lines">@@ -797,16 +786,7 @@
</span><span class="cx">   Macro expands to: switch_status_t mod_cidlookup_shutdown() */
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cidlookup_shutdown)
</span><span class="cx"> {
</span><del>-        /* Cleanup dynamically allocated config settings */
</del><span class="cx"> 
</span><del>-        if (globals.db_mutex) {
-                switch_mutex_destroy(globals.db_mutex);
-        }
-        if (globals.master_odbc) {
-                switch_odbc_handle_disconnect(globals.master_odbc);
-                switch_odbc_handle_destroy(&amp;globals.master_odbc);
-        }
-
</del><span class="cx">         switch_event_unbind(&amp;reload_xml_event);
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>