[svn-commits] branch oej/test-this-branch r17826 - in /team/oej/test-this-branch: ./ cdr/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Thu Apr 6 07:34:35 MST 2006


Author: oej
Date: Thu Apr  6 09:34:30 2006
New Revision: 17826

URL: http://svn.digium.com/view/asterisk?rev=17826&view=rev
Log:
SQLite3 CDR driver (moy)

Added:
    team/oej/test-this-branch/cdr/cdr_sqlite3.c   (with props)
Modified:
    team/oej/test-this-branch/README.test-this-branch
    team/oej/test-this-branch/README.test-this-branch.html
    team/oej/test-this-branch/cdr/Makefile

Modified: team/oej/test-this-branch/README.test-this-branch
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/README.test-this-branch?rev=17826&r1=17825&r2=17826&view=diff
==============================================================================
--- team/oej/test-this-branch/README.test-this-branch (original)
+++ team/oej/test-this-branch/README.test-this-branch Thu Apr  6 09:34:30 2006
@@ -56,6 +56,7 @@
 - TOUPPER and TOLOWER ASCII functions (powerkill, #6668)
   (With some changes)
 - Meetme list concise cli command (moy, #6212)
+- SQLite 3 CDR driver (#6754, moy)
 
 Things that has been commited to svn trunk:
 - Abandon Queue manager event (tim_ringenbach, #6459)

Modified: team/oej/test-this-branch/README.test-this-branch.html
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/README.test-this-branch.html?rev=17826&r1=17825&r2=17826&view=diff
==============================================================================
--- team/oej/test-this-branch/README.test-this-branch.html (original)
+++ team/oej/test-this-branch/README.test-this-branch.html Thu Apr  6 09:34:30 2006
@@ -4,6 +4,7 @@
 <body>
 
 <h2>TESTING BRANCH - WELCOME!!</h2>
+<a href="http://www.meetasterisk.com"><img src="http://www.meetasterisk.com/images/meetasterisk-standing-ban.gif" align="right"></a>
 <hr/>
 
 <p>Asterisk is developed by the <a href="http://www.asterisk.org">Asterisk.org</a> user community. The
@@ -63,6 +64,8 @@
   (With some changes)<br /></li>
 <li>Meetme list concise cli command (moy, 
 <a href="http://bugs.digium.com/view.php?id=6212">#6212</a>)</li>
+<li>SQLite 3 CDR driver (moy, 
+<a href="http://bugs.digium.com/view.php?id=6754">#6754</a>)</li>
 </ul>
 
 <h3>Things that has been commited to svn trunk:</h3>

Modified: team/oej/test-this-branch/cdr/Makefile
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/cdr/Makefile?rev=17826&r1=17825&r2=17826&view=diff
==============================================================================
--- team/oej/test-this-branch/cdr/Makefile (original)
+++ team/oej/test-this-branch/cdr/Makefile Thu Apr  6 09:34:30 2006
@@ -24,6 +24,11 @@
 ifeq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/sqlite.h),)
   MODS:=$(filter-out cdr_sqlite.so,$(MODS))
 endif
+
+ifeq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/sqlite3.h),)
+  MODS:=$(filter-out cdr_sqlite3.so,$(MODS))
+endif
+
 
 ifeq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/tds.h $(CROSS_COMPILE_TARGET)/usr/local/include/tds.h $(CROSS_COMPILE_TARGET)/usr/include/freetds/tds.h),)
   MODS:=$(filter-out cdr_tds.so,$(MODS))
@@ -130,6 +135,9 @@
 cdr_sqlite.so: cdr_sqlite.o
 	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lsqlite $(MLFLAGS)
 
+cdr_sqlite3.so: cdr_sqlite3.o
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lsqlite3 $(MLFLAGS)
+
 cdr_radius.so: cdr_radius.o
 	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} -lradiusclient-ng $(MLFLAGS)
 

Added: team/oej/test-this-branch/cdr/cdr_sqlite3.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/cdr/cdr_sqlite3.c?rev=17826&view=auto
==============================================================================
--- team/oej/test-this-branch/cdr/cdr_sqlite3.c (added)
+++ team/oej/test-this-branch/cdr/cdr_sqlite3.c Thu Apr  6 09:34:30 2006
@@ -1,0 +1,235 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2006, Moises Silva
+ *
+ *
+ * Based on original cdr_sqlite.c file by Holger Schurig
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Store CDR records in a SQLite3 database.
+ * 
+ * \author Moises Silva <moy at ivsol.net>
+ *
+ * See also
+ * \arg \ref Config_cdr
+ * \arg http://www.sqlite.org/
+ * 
+ * Creates the database and table on-the-fly
+ * \ingroup cdr_drivers
+ */
+
+#include <sys/types.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sqlite3.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/channel.h"
+#include "asterisk/module.h"
+#include "asterisk/logger.h"
+#include "asterisk/utils.h"
+
+#define LOG_UNIQUEID	1
+#define LOG_USERFIELD	1
+
+/* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
+#define DATE_FORMAT "%Y-%m-%d %T"
+
+static char *desc = "SQLite3 CDR Backend";
+static char *name = "sqlite3";
+static sqlite3* sqlite3_db = NULL;
+
+AST_MUTEX_DEFINE_STATIC(sqlite3_lock);
+
+/*! \brief SQL table format */
+static char sql_create_table[] = "CREATE TABLE cdr ("
+"	AcctId		INTEGER PRIMARY KEY,"
+"	clid		VARCHAR(80),"
+"	src		VARCHAR(80),"
+"	dst		VARCHAR(80),"
+"	dcontext	VARCHAR(80),"
+"	channel		VARCHAR(80),"
+"	dstchannel	VARCHAR(80),"
+"	lastapp		VARCHAR(80),"
+"	lastdata	VARCHAR(80),"
+"	start		CHAR(19),"
+"	answer		CHAR(19),"
+"	end		CHAR(19),"
+"	duration	INTEGER,"
+"	billsec		INTEGER,"
+"	disposition	INTEGER,"
+"	amaflags	INTEGER,"
+"	accountcode	VARCHAR(20)"
+#if LOG_UNIQUEID
+"	,uniqueid	VARCHAR(32)"
+#endif
+#if LOG_USERFIELD
+"	,userfield	VARCHAR(255)"
+#endif
+");";
+
+static int sqlite_log(struct ast_cdr *cdr)
+{
+	int result = 0;
+	char *query_error;
+	struct tm tm;
+	time_t t;
+	char startstr[80], answerstr[80], endstr[80];
+	int count;
+
+	ast_mutex_lock(&sqlite3_lock);
+
+	t = cdr->start.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
+
+	t = cdr->answer.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
+
+	t = cdr->end.tv_sec;
+	localtime_r(&t, &tm);
+	strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
+	char *sql_query = sqlite3_mprintf(
+			"INSERT INTO cdr ("
+				"clid,src,dst,dcontext,"
+				"channel,dstchannel,lastapp,lastdata, "
+				"start,answer,end,"
+				"duration,billsec,disposition,amaflags, "
+				"accountcode"
+#				if LOG_UNIQUEID
+				",uniqueid"
+#				endif
+#				if LOG_USERFIELD
+				",userfield"
+#				endif
+			") VALUES ("
+				"'%q', '%q', '%q', '%q', "
+				"'%q', '%q', '%q', '%q', "
+				"'%q', '%q', '%q', "
+				"%d, %d, %d, %d, "
+				"'%q'"
+#				if LOG_UNIQUEID
+				",'%q'"
+#				endif
+#				if LOG_USERFIELD
+				",'%q'"
+#				endif
+			")", cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
+				cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
+				startstr, answerstr, endstr,
+				cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
+				cdr->accountcode
+#				if LOG_UNIQUEID
+				,cdr->uniqueid
+#				endif
+#				if LOG_USERFIELD
+				,cdr->userfield
+#				endif
+			);
+	for ( count = 0; count < 5; count++ ) {
+		result = sqlite3_exec(sqlite3_db, sql_query, NULL, NULL, &query_error);
+		/* if not busy or locked, then end here, otherwise try again in 200 us */
+		if ( SQLITE_BUSY != result && SQLITE_LOCKED != result ) {
+			break;
+		}
+		usleep(200);
+	}
+	
+	if ( query_error ) {
+		ast_log(LOG_ERROR, "cdr_sqlite: %s\n", query_error);
+		sqlite3_free(query_error);		
+	}
+	sqlite3_free(sql_query);
+	ast_mutex_unlock(&sqlite3_lock);
+	return result;
+}
+
+char *description(void)
+{
+	return desc;
+}
+
+int unload_module(void)
+{
+	if (sqlite3_db)
+		sqlite3_close(sqlite3_db);
+	ast_cdr_unregister(name);
+	return 0;
+}
+
+int load_module(void)
+{
+	char database_file[PATH_MAX];
+	int result;
+
+	/* is the database there? */
+	snprintf(database_file, sizeof(database_file), "%s/sqlite3_cdr.db", ast_config_AST_LOG_DIR);
+	
+	result = sqlite3_open(database_file, &sqlite3_db);
+		
+	if ( SQLITE_OK != result ) {
+		const char *error_message = sqlite3_errmsg(sqlite3_db);
+		int error_number          = sqlite3_errcode(sqlite3_db);
+		ast_log(LOG_ERROR, "cdr_sqlite3: [errno %d] %s\n", error_number, error_message);
+		/* according to comments in sqlite3.h sqlite3_open always return a valid handler to close */
+		sqlite3_close(sqlite3_db);		
+		return -1;
+	}
+
+	/* is the table there? */
+	result = sqlite3_exec(sqlite3_db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
+	if ( SQLITE_OK != result ) {
+		char *query_error;
+		result = sqlite3_exec(sqlite3_db, sql_create_table, NULL, NULL, &query_error);
+		if ( SQLITE_OK != result ) {
+			ast_log(LOG_ERROR, "cdr_sqlite3: Unable to create table 'cdr': %s\n", query_error);
+			sqlite3_free(query_error);
+			sqlite3_close(sqlite3_db);
+			return -1;
+		}
+
+		/* TODO: here we should probably create an index */
+	}
+	
+	result = ast_cdr_register(name, desc, sqlite_log);
+	if ( result ) {
+		ast_log(LOG_ERROR, "Unable to register SQLite3 CDR handling\n");
+		return -1;
+	}
+	return 0;
+}
+
+int reload(void)
+{
+	return 0;
+}
+
+int usecount(void)
+{
+	return 0;
+}
+
+char *key()
+{
+	return ASTERISK_GPL_KEY;
+}

Propchange: team/oej/test-this-branch/cdr/cdr_sqlite3.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/oej/test-this-branch/cdr/cdr_sqlite3.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/oej/test-this-branch/cdr/cdr_sqlite3.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the svn-commits mailing list