[svn-commits] seanbright: branch seanbright/cdr-tds-conversion r123826 - /team/seanbright/c...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Thu Jun 19 07:10:41 CDT 2008
Author: seanbright
Date: Thu Jun 19 07:10:40 2008
New Revision: 123826
URL: http://svn.digium.com/view/asterisk?view=rev&rev=123826
Log:
First shot at converting from libtds to dblib. Needs testing and tweaking and
such. We really need a test harness to exercise the CDR infrastructure... I
wonder if murf is awake yet ;-)
Modified:
team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c
Modified: team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c
URL: http://svn.digium.com/view/asterisk/team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c?view=diff&rev=123826&r1=123825&r2=123826
==============================================================================
--- team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c (original)
+++ team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c Thu Jun 19 07:10:40 2008
@@ -66,18 +66,13 @@
#include <time.h>
#include <math.h>
-#include <tds.h>
-#include <tdsconvert.h>
-#include <ctype.h>
-
#include "asterisk/config.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
-#ifdef FREETDS_PRE_0_62
-#warning "You have older TDS, you should upgrade!"
-#endif
+#include <sqlfront.h>
+#include <sybdb.h>
#define DATE_FORMAT "%Y/%m/%d %T"
@@ -87,16 +82,15 @@
struct cdr_tds_config {
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(hostname);
- AST_STRING_FIELD(dbname);
- AST_STRING_FIELD(dbuser);
+ AST_STRING_FIELD(database);
+ AST_STRING_FIELD(username);
AST_STRING_FIELD(password);
AST_STRING_FIELD(table);
AST_STRING_FIELD(charset);
AST_STRING_FIELD(language);
);
- TDSSOCKET *tds;
- TDSLOGIN *login;
- TDSCONTEXT *context;
+ DBPROCESS *dbproc;
+ LOGINREC *login;
unsigned int connected:1;
ast_mutex_t lock;
};
@@ -115,11 +109,11 @@
{
char sqlcmd[2048], start[80], answer[80], end[80];
char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
+/*
int res = 0;
int retried = 0;
-#ifdef FREETDS_PRE_0_62
- TDS_INT result_type;
-#endif
+*/
+ RETCODE erc;
ast_mutex_lock(&settings->lock);
@@ -140,8 +134,7 @@
get_date(answer, cdr->answer);
get_date(end, cdr->end);
- sprintf(
- sqlcmd,
+ erc = dbfcmd(settings->dbproc,
"INSERT INTO %s "
"("
"accountcode, "
@@ -202,42 +195,27 @@
uniqueid
);
- do {
- if (!settings->connected) {
- if (mssql_connect())
- ast_log(LOG_ERROR, "Failed to reconnect to SQL database.\n");
- else
- ast_log(LOG_WARNING, "Reconnected to SQL database.\n");
-
- retried = 1; /* note that we have now tried */
- }
-
-#ifdef FREETDS_PRE_0_62
- if (!settings->connected || (tds_submit_query(settings->tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(settings->tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
-#else
- if (!settings->connected || (tds_submit_query(settings->tds, sqlcmd) != TDS_SUCCEED) || (tds_process_simple_query(settings->tds) != TDS_SUCCEED))
-#endif
- {
- ast_log(LOG_ERROR, "Failed to insert Call Data Record into SQL database.\n");
-
- mssql_disconnect(); /* this is ok even if we are already disconnected */
- }
- } while (!settings->connected && !retried);
-
- ast_free(accountcode);
- ast_free(src);
- ast_free(dst);
- ast_free(dcontext);
- ast_free(clid);
- ast_free(channel);
- ast_free(dstchannel);
- ast_free(lastapp);
- ast_free(lastdata);
- ast_free(uniqueid);
+ if (erc == FAIL) {
+ ast_log(LOG_ERROR, "Failed to build query\n");
+ ast_mutex_unlock(&settings->lock);
+ return -1;
+ }
+
+ erc = dbsqlexec(settings->dbproc);
+
+ if (erc == FAIL) {
+ ast_log(LOG_ERROR, "Failed to send query\n");
+ ast_mutex_unlock(&settings->lock);
+ return -1;
+ }
+
+ while ((erc = dbresults(settings->dbproc)) != NO_MORE_RESULTS) {
+ while (dbnextrow(settings->dbproc) != NO_MORE_ROWS);
+ }
ast_mutex_unlock(&settings->lock);
- return res;
+ return 0;
}
static char *anti_injection(const char *str, int len)
@@ -293,20 +271,10 @@
{
if (!settings)
return 0;
-
- if (settings->tds) {
- tds_free_socket(settings->tds);
- settings->tds = NULL;
- }
-
- if (settings->context) {
- tds_free_context(settings->context);
- settings->context = NULL;
- }
-
- if (settings->login) {
- tds_free_login(settings->login);
- settings->login = NULL;
+
+ if (settings->dbproc) {
+ dbclose(settings->dbproc);
+ settings->dbproc = NULL;
}
settings->connected = 0;
@@ -316,94 +284,30 @@
static int mssql_connect(void)
{
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
- TDSCONNECTION *connection = NULL;
-#else
- TDSCONNECTINFO *connection = NULL;
-#endif
- char query[128];
-
- /* Connect to M$SQL Server */
- if (!(settings->login = tds_alloc_login())) {
- ast_log(LOG_ERROR, "tds_alloc_login() failed.\n");
+ DBSETLAPP(settings->login, "TSQL");
+ DBSETLUSER(settings->login, settings->username);
+ DBSETLPWD(settings->login, settings->password);
+ DBSETLCHARSET(settings->login, settings->charset);
+ DBSETLNATLANG(settings->login, settings->language);
+
+ if ((settings->dbproc = dbopen(settings->login, settings->hostname)) == NULL) {
+ ast_log(LOG_ERROR, "Unable to connect to %s\n", settings->hostname);
return -1;
}
- tds_set_server(settings->login, settings->hostname);
- tds_set_user(settings->login, settings->dbuser);
- tds_set_passwd(settings->login, settings->password);
- tds_set_app(settings->login, "TSQL");
- tds_set_library(settings->login, "TDS-Library");
-#ifndef FREETDS_PRE_0_62
- tds_set_client_charset(settings->login, settings->charset);
-#endif
- tds_set_language(settings->login, settings->language);
- tds_set_packet(settings->login, 512);
- tds_set_version(settings->login, 7, 0);
-
-#ifdef FREETDS_0_64
- if (!(settings->context = tds_alloc_context(NULL)))
-#else
- if (!(settings->context = tds_alloc_context()))
-#endif
- {
- ast_log(LOG_ERROR, "tds_alloc_context() failed.\n");
- goto connect_fail;
- }
-
- if (!(settings->tds = tds_alloc_socket(settings->context, 512))) {
- ast_log(LOG_ERROR, "tds_alloc_socket() failed.\n");
- goto connect_fail;
- }
-
- tds_set_parent(settings->tds, NULL);
- connection = tds_read_config_info(settings->tds, settings->login, settings->context->locale);
- if (!connection) {
- ast_log(LOG_ERROR, "tds_read_config() failed.\n");
- goto connect_fail;
- }
-
- if (tds_connect(settings->tds, connection) == TDS_FAIL) {
- ast_log(LOG_ERROR, "Failed to connect to MSSQL server.\n");
- settings->tds = NULL; /* freed by tds_connect() on error */
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
- tds_free_connection(connection);
-#else
- tds_free_connect(connection);
-#endif
- connection = NULL;
- goto connect_fail;
- }
-#if (defined(FREETDS_0_63) || defined(FREETDS_0_64))
- tds_free_connection(connection);
-#else
- tds_free_connect(connection);
-#endif
- connection = NULL;
-
- sprintf(query, "USE %s", settings->dbname);
-#ifdef FREETDS_PRE_0_62
- if ((tds_submit_query(settings->tds, query) != TDS_SUCCEED) || (tds_process_simple_query(settings->tds, &result_type) != TDS_SUCCEED || result_type != TDS_CMD_SUCCEED))
-#else
- if ((tds_submit_query(settings->tds, query) != TDS_SUCCEED) || (tds_process_simple_query(settings->tds) != TDS_SUCCEED))
-#endif
- {
- ast_log(LOG_ERROR, "Could not change database (%s)\n", settings->dbname);
- goto connect_fail;
+ if (dbuse(settings->dbproc, settings->database) == FAIL) {
+ ast_log(LOG_ERROR, "Unable to select database %s\n", settings->database);
+ return -1;
}
settings->connected = 1;
+
return 0;
-
-connect_fail:
- mssql_disconnect();
- return -1;
}
static void cdr_tds_config_destroy(void)
{
- if (settings)
- {
+ if (settings) {
ast_mutex_destroy(&settings->lock);
ast_string_field_free_memory(settings);
ast_free(settings);
@@ -413,11 +317,22 @@
static int tds_unload_module(void)
{
mssql_disconnect();
-
ast_cdr_unregister(name);
-
cdr_tds_config_destroy();
-
+ dbexit();
+
+ return 0;
+}
+
+static int tds_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
+{
+ ast_log(LOG_ERROR, "%s / %s\n", dberrstr, oserrstr);
+ return INT_CANCEL;
+}
+
+static int tds_message_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
+{
+ ast_log(LOG_NOTICE, "Error: %s\n", msgtext);
return 0;
}
@@ -444,6 +359,15 @@
if (reload) {
ast_string_field_init(settings, 0);
} else {
+ if (dbinit() == FAIL) {
+ ast_log(LOG_ERROR, "Failed to initialize db-lib\n");
+ ast_config_destroy(cfg);
+ return 0;
+ }
+
+ dberrhandle(tds_error_handler);
+ dbmsghandle(tds_message_handler);
+
settings = ast_calloc(1, sizeof(*settings));
if (!settings || ast_string_field_init(settings, 256)) {
@@ -455,6 +379,14 @@
return 0;
}
+ if ((settings->login = dblogin()) == NULL) {
+ ast_log(LOG_ERROR, "Unable to allocate login structure for db-lib\n");
+ ast_string_field_free_memory(settings);
+ ast_free(settings);
+ ast_config_destroy(cfg);
+ return 0;
+ }
+
ast_mutex_init(&settings->lock);
}
@@ -467,14 +399,14 @@
ptr = ast_variable_retrieve(cfg, "global", "dbname");
if (ptr) {
- ast_string_field_set(settings, dbname, ptr);
+ ast_string_field_set(settings, database, ptr);
} else {
ast_log(LOG_ERROR, "Database dbname not specified.\n");
}
ptr = ast_variable_retrieve(cfg, "global", "user");
if (ptr) {
- ast_string_field_set(settings, dbuser, ptr);
+ ast_string_field_set(settings, username, ptr);
} else {
ast_log(LOG_ERROR, "Database dbuser not specified.\n");
}
More information about the svn-commits
mailing list