Пример #1
0
static double get_balance(const char *billaccount, switch_channel_t *channel)
{
	char *dsql = NULL, *sql = NULL;
	nibblebill_results_t pdata;
	double balance = 0.0;

	if (!switch_odbc_available()) {
		return -1.0;
	}

	memset(&pdata, 0, sizeof(pdata));

	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);
			if (sql != globals.custom_sql_lookup) dsql = sql;
		} else {
			sql = globals.custom_sql_lookup;
		}
	} else {
		sql = dsql = switch_mprintf("SELECT %s AS nibble_balance FROM %s WHERE %s='%s'", 
									globals.db_column_cash, globals.db_table, globals.db_column_account, billaccount);
	}

	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing lookup query\n[%s]\n", sql);
	
	if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, nibblebill_callback, &pdata, NULL) != SWITCH_ODBC_SUCCESS) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error running this query: [%s]\n", sql);
		/* Return -1 for safety */

		balance = -1.0;
	} else {
		/* Successfully retrieved! */
		balance = pdata.balance;
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Retrieved current balance for account %s (balance = %f)\n", billaccount, balance);
	}
	
	switch_safe_free(dsql);
	return balance;
}
Пример #2
0
static switch_status_t route_lookup(char *dn, easyroute_results_t *results, int noat, char *separator)
{
	switch_status_t sstatus = SWITCH_STATUS_SUCCESS;
	char *sql = NULL;
	route_callback_t pdata;

	if (!switch_odbc_available()) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT,
						  "mod_easyroute requires core ODBC support. Please refer to the documentation on how to enable this\n");
		return sstatus;
	}

	memset(&pdata, 0, sizeof(pdata));
	if (!globals.custom_query) {
		sql = switch_mprintf(SQL_LOOKUP, dn);
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing static Query\n[%s]\n", sql);
	} else {
		sql = switch_mprintf(globals.custom_query, dn);
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Doing custom Query\n[%s]\n", sql);
	}

	if (globals.mutex) {
		switch_mutex_lock(globals.mutex);
	}
	/* Do the Query */
	if (switch_odbc_handle_callback_exec(globals.master_odbc, sql, route_callback, &pdata, NULL) == SWITCH_ODBC_SUCCESS) {
		char tmp_profile[129];
		char tmp_gateway[129];

		if (zstr(pdata.limit)) {
			switch_set_string(results->limit, "9999");
		} else {
			switch_set_string(results->limit, pdata.limit);
		}

		if (zstr(pdata.techprofile)) {
			switch_set_string(tmp_profile, globals.default_techprofile);
		} else {
			switch_set_string(tmp_profile, pdata.techprofile);
		}

		if (zstr(pdata.gateway)) {
			switch_set_string(tmp_gateway, globals.default_gateway);
		} else {
			switch_set_string(tmp_gateway, pdata.gateway);
		}

		if (zstr(pdata.translated)) {
			switch_set_string(results->translated, dn);
		} else {
			switch_set_string(results->translated, pdata.translated);
		}
		if (noat) {
			switch_snprintf(results->dialstring, 256, "%s/%s%s", tmp_profile, results->translated, tmp_gateway);
		} else if (separator) {
			switch_snprintf(results->dialstring, 256, "%s/%s%s%s", tmp_profile, results->translated, separator, tmp_gateway);
		} else {
			switch_snprintf(results->dialstring, 256, "%s/%s@%s", tmp_profile, results->translated, tmp_gateway);
		}
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "THE ROUTE [%s]\n", results->dialstring);

		if (zstr(pdata.group)) {
			switch_set_string(results->group, "");
		} else {
			switch_set_string(results->group, pdata.group);
		}

		if (zstr(pdata.acctcode)) {
			switch_set_string(results->acctcode, "");
		} else {
			switch_set_string(results->acctcode, pdata.acctcode);
		}
	} else {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DB Error Setting Default Route!\n");
		switch_set_string(results->limit, "9999");
		if (noat) {
			switch_snprintf(results->dialstring, 256, "%s/%s%s", globals.default_techprofile, dn, globals.default_gateway);
		} else if (separator) {
			switch_snprintf(results->dialstring, 256, "%s/%s%s%s", globals.default_techprofile, dn, separator, globals.default_gateway);
		} else {
			switch_snprintf(results->dialstring, 256, "%s/%s@%s", globals.default_techprofile, dn, globals.default_gateway);
		}
		switch_set_string(results->group, "");
		switch_set_string(results->acctcode, "");
	}

	switch_safe_free(sql);

	if (globals.mutex) {
		switch_mutex_unlock(globals.mutex);
	}
	return sstatus;
}