<!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][17081] </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=17081">17081</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2010-03-23 12:11:30 -0500 (Tue, 23 Mar 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre><a href="http://jira.freeswitch.org/browse/MODAPP-409">MODAPP-409</a> Support custom sql with var expansion</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_nibblebillmod_nibblebillc">freeswitch/trunk/src/mod/applications/mod_nibblebill/mod_nibblebill.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_nibblebillmod_nibblebillc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_nibblebill/mod_nibblebill.c (17080 => 17081)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_nibblebill/mod_nibblebill.c        2010-03-23 16:35:38 UTC (rev 17080)
+++ freeswitch/trunk/src/mod/applications/mod_nibblebill/mod_nibblebill.c        2010-03-23 17:11:30 UTC (rev 17081)
</span><span class="lines">@@ -51,7 +51,7 @@
</span><span class="cx"> #include <switch.h>
</span><span class="cx">
</span><span class="cx"> /* Defaults */
</span><del>-static char SQL_LOOKUP[] = "SELECT %s FROM %s WHERE %s='%s'";
</del><ins>+static char SQL_LOOKUP[] = "SELECT %s AS nibble_balance FROM %s WHERE %s='%s'";
</ins><span class="cx"> static char SQL_SAVE[] = "UPDATE %s SET %s=%s-%f WHERE %s='%s'";
</span><span class="cx">
</span><span class="cx"> typedef struct {
</span><span class="lines">@@ -101,6 +101,8 @@
</span><span class="cx">         char *db_table;
</span><span class="cx">         char *db_column_cash;
</span><span class="cx">         char *db_column_account;
</span><ins>+        char *custom_sql_save;
+        char *custom_sql_lookup;
</ins><span class="cx">         switch_odbc_handle_t *master_odbc;
</span><span class="cx"> } globals;
</span><span class="cx">
</span><span class="lines">@@ -125,16 +127,23 @@
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_table, globals.db_table);
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_column_cash, globals.db_column_cash);
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_db_column_account, globals.db_column_account);
</span><ins>+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_custom_sql_save, globals.custom_sql_save);
+SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_custom_sql_lookup, globals.custom_sql_lookup);
</ins><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_percall_action, globals.percall_action);
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_lowbal_action, globals.lowbal_action);
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_nobal_action, globals.nobal_action);
</span><span class="cx">
</span><span class="cx"> static int nibblebill_callback(void *pArg, int argc, char **argv, char **columnNames)
</span><span class="cx"> {
</span><ins>+        int i = 0;
</ins><span class="cx">         nibblebill_results_t *cbt = (nibblebill_results_t *) pArg;
</span><span class="cx">
</span><del>-        cbt->balance = (float) atof(argv[0]);
-
</del><ins>+        for (i = 0; i < argc; i++) {
+                if (!strcasecmp(columnNames[i], "nibble_balance")) {
+                        cbt->balance = (float) atof(argv[0]);
+                }
+        }
+        
</ins><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -167,6 +176,10 @@
</span><span class="cx">                                 set_global_db_column_cash(val);
</span><span class="cx">                         } else if (!strcasecmp(var, "db_column_account")) {
</span><span class="cx">                                 set_global_db_column_account(val);
</span><ins>+                        } else if (!strcasecmp(var, "custom_sql_save")) {
+                                set_global_custom_sql_save(val);
+                        } else if (!strcasecmp(var, "custom_sql_lookup")) {
+                                set_global_custom_sql_lookup(val);
</ins><span class="cx">                         } else if (!strcasecmp(var, "percall_action")) {
</span><span class="cx">                                 set_global_percall_action(val);
</span><span class="cx">                         } else if (!strcasecmp(var, "percall_max_amt")) {
</span><span class="lines">@@ -285,17 +298,29 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> /* At this time, billing never succeeds if you don't have a database. */
</span><del>-static switch_status_t bill_event(float billamount, const char *billaccount)
</del><ins>+static switch_status_t bill_event(float billamount, const char *billaccount, switch_channel_t *channel)
</ins><span class="cx"> {
</span><del>-        char sql[1024] = "";
-        switch_odbc_statement_handle_t stmt;
</del><ins>+        switch_stream_handle_t sql_stream = { 0 };
+        char *sql = NULL;
+        switch_odbc_statement_handle_t stmt = NULL;
+        SWITCH_STANDARD_STREAM(sql_stream);
</ins><span class="cx">
</span><span class="cx">         if (!switch_odbc_available()) {
</span><del>-                return SWITCH_STATUS_SUCCESS;
</del><ins>+                goto end;
</ins><span class="cx">         }
</span><span class="cx">
</span><del>-        switch_snprintf(sql, 1024, SQL_SAVE, globals.db_table, globals.db_column_cash, globals.db_column_cash, billamount, globals.db_column_account,
-                                        billaccount);
</del><ins>+        if (globals.custom_sql_save) {
+                if (switch_string_var_check_const(globals.custom_sql_save) || switch_string_has_escaped_data(globals.custom_sql_save)) {
+                        switch_channel_set_variable_printf(channel, "nibble_increment", "%f", billamount, SWITCH_FALSE);
+                        sql = switch_channel_expand_variables(channel, globals.custom_sql_save);
+                } else {
+                        sql = globals.custom_sql_save;
+                }
+        } else {
+                sql_stream.write_function(&sql_stream, SQL_SAVE, globals.db_table, globals.db_column_cash, globals.db_column_cash, billamount, globals.db_column_account,
+                                                billaccount);
+                sql = sql_stream.data;
+        }
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing update query\n[%s]\n", sql);
</span><span class="cx">
</span><span class="cx">         if (switch_odbc_handle_exec(globals.master_odbc, sql, &stmt, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="lines">@@ -306,40 +331,63 @@
</span><span class="cx">         } else {
</span><span class="cx">                 /* TODO: Failover to a flat/text file if DB is unavailable */
</span><span class="cx">
</span><del>-                return SWITCH_STATUS_SUCCESS;
</del><ins>+                goto end;
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         switch_odbc_statement_handle_free(&stmt);
</span><span class="cx">
</span><ins>+end:
+        if (sql != globals.custom_sql_save) {
+                switch_safe_free(sql);
+        }
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx">
</span><del>-static float get_balance(const char *billaccount)
</del><ins>+static float get_balance(const char *billaccount, switch_channel_t *channel)
</ins><span class="cx"> {
</span><del>-        char sql[1024] = "";
</del><ins>+        switch_stream_handle_t sql_stream = { 0 };
+        char *sql = NULL;
</ins><span class="cx">         nibblebill_results_t pdata;
</span><span class="cx">         float balance = 0.00f;
</span><ins>+        SWITCH_STANDARD_STREAM(sql_stream);
</ins><span class="cx">
</span><span class="cx">         if (!switch_odbc_available()) {
</span><del>-                return -1.00f;
</del><ins>+                balance = -1.00f;
+                goto end;
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         memset(&pdata, 0, sizeof(pdata));
</span><del>-        snprintf(sql, 1024, SQL_LOOKUP, globals.db_column_cash, globals.db_table, globals.db_column_account, billaccount);
</del><span class="cx">
</span><ins>+        if (globals.custom_sql_lookup) {
+                if (switch_string_var_check_const(globals.custom_sql_lookup) || switch_string_has_escaped_data(globals.custom_sql_lookup)) {
+                        sql = switch_channel_expand_variables(channel, globals.custom_sql_lookup);
+                } else {
+                        sql = globals.custom_sql_lookup;
+                }
+        } else {
+                sql_stream.write_function(&sql_stream, SQL_LOOKUP, globals.db_column_cash, globals.db_table, globals.db_column_account, billaccount);
+                sql = sql_stream.data;
+        }
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing lookup query\n[%s]\n", sql);
+        
</ins><span class="cx">         if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, nibblebill_callback, &pdata, NULL) != SWITCH_ODBC_SUCCESS) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error running this query: [%s]\n", sql);
</span><span class="cx">                 /* Return -1 for safety */
</span><span class="cx">
</span><del>-                return -1.00f;
</del><ins>+                balance = -1.00f;
+                goto end;
</ins><span class="cx">         } else {
</span><span class="cx">                 /* Successfully retrieved! */
</span><span class="cx">                 balance = pdata.balance;
</span><span class="cx">
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Retrieved current balance for account %s (balance = %f)\n", billaccount, balance);
</span><span class="cx">         }
</span><del>-
</del><ins>+        
+end:
+        if (sql != globals.custom_sql_lookup) {
+                switch_safe_free(sql);
+        }
</ins><span class="cx">         return balance;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -408,7 +456,7 @@
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Not billing %s - call is not in answered state\n", billaccount);
</span><span class="cx">
</span><span class="cx">                 /* See if this person has enough money left to continue the call */
</span><del>-                balance = get_balance(billaccount);
</del><ins>+                balance = get_balance(billaccount, channel);
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Comparing %f to hangup balance of %f\n", balance, nobal_amt);
</span><span class="cx">                 if (balance <= nobal_amt) {
</span><span class="cx">                         /* Not enough money - reroute call to nobal location */
</span><span class="lines">@@ -464,7 +512,7 @@
</span><span class="cx">                                                  uuid, nibble_data->total);
</span><span class="cx">
</span><span class="cx">                 /* DO ODBC BILLING HERE and reset counters if it's successful! */
</span><del>-                if (bill_event(billamount, billaccount) == SWITCH_STATUS_SUCCESS) {
</del><ins>+                if (bill_event(billamount, billaccount, channel) == SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                         /* Increment total cost */
</span><span class="cx">                         nibble_data->total += billamount;
</span><span class="cx">
</span><span class="lines">@@ -491,7 +539,7 @@
</span><span class="cx">                 /* don't verify balance and transfer to nobal if we're done with call */
</span><span class="cx">                 if (switch_channel_get_state(channel) != CS_REPORTING && switch_channel_get_state(channel) != CS_HANGUP) {
</span><span class="cx">                         /* See if this person has enough money left to continue the call */
</span><del>-                        balance = get_balance(billaccount);
</del><ins>+                        balance = get_balance(billaccount, channel);
</ins><span class="cx">                         if (balance <= nobal_amt) {
</span><span class="cx">                                 /* Not enough money - reroute call to nobal location */
</span><span class="cx">                                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Balance of %f fell below allowed amount of %f! (Account %s)\n",
</span><span class="lines">@@ -719,7 +767,7 @@
</span><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         /* Add or remove amount from adjusted billing here. Note, we bill the OPPOSITE */
</span><del>-        if (bill_event(-amount, billaccount) == SWITCH_STATUS_SUCCESS) {
</del><ins>+        if (bill_event(-amount, billaccount, channel) == SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Recorded adjustment to %s for $%f\n", billaccount, amount);
</span><span class="cx">         } else {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed to record adjustment to %s for $%f\n", billaccount, amount);
</span><span class="lines">@@ -846,7 +894,7 @@
</span><span class="cx">
</span><span class="cx">         billaccount = switch_channel_get_variable(channel, "nibble_account");
</span><span class="cx">         if (billaccount) {
</span><del>-                switch_channel_set_variable_printf(channel, "nibble_current_balance", "%f", get_balance(billaccount));
</del><ins>+                switch_channel_set_variable_printf(channel, "nibble_current_balance", "%f", get_balance(billaccount, channel));
</ins><span class="cx">         }                        
</span><span class="cx">         
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>