2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005, Holger Schurig
7 * Ideas taken from other cdr_*.c files
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Store CDR records in a SQLite database.
24 * \author Holger Schurig <hs4233@mail.mn-solutions.de>
25 * \extref SQLite http://www.sqlite.org/
28 * \arg \ref Config_cdr
29 * \arg http://www.sqlite.org/
31 * Creates the database and table on-the-fly
32 * \ingroup cdr_drivers
34 * \note This module has been marked deprecated in favor for cdr_sqlite3_custom
38 <depend>sqlite</depend>
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
45 #include <sys/types.h>
52 #include "asterisk/channel.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
55 #include "asterisk/utils.h"
57 #define LOG_UNIQUEID 0
58 #define LOG_USERFIELD 0
60 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
61 #define DATE_FORMAT "%Y-%m-%d %T"
63 static char *name = "sqlite";
64 static sqlite* db = NULL;
66 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
68 /*! \brief SQL table format */
69 static char sql_create_table[] = "CREATE TABLE cdr ("
70 " AcctId INTEGER PRIMARY KEY,"
74 " dcontext VARCHAR(80),"
75 " channel VARCHAR(80),"
76 " dstchannel VARCHAR(80),"
77 " lastapp VARCHAR(80),"
78 " lastdata VARCHAR(80),"
84 " disposition INTEGER,"
86 " accountcode VARCHAR(20)"
88 " ,uniqueid VARCHAR(32)"
91 " ,userfield VARCHAR(255)"
95 static int sqlite_log(struct ast_cdr *cdr)
100 char startstr[80], answerstr[80], endstr[80];
103 ast_mutex_lock(&sqlite_lock);
105 ast_localtime(&cdr->start, &tm, NULL);
106 ast_strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
108 ast_localtime(&cdr->answer, &tm, NULL);
109 ast_strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
111 ast_localtime(&cdr->end, &tm, NULL);
112 ast_strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
114 for(count=0; count<5; count++) {
115 res = sqlite_exec_printf(db,
117 "clid,src,dst,dcontext,"
118 "channel,dstchannel,lastapp,lastdata, "
120 "duration,billsec,disposition,amaflags, "
129 "'%q', '%q', '%q', '%q', "
130 "'%q', '%q', '%q', '%q', "
140 ")", NULL, NULL, &zErr,
141 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
142 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
143 startstr, answerstr, endstr,
144 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
153 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
159 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
163 ast_mutex_unlock(&sqlite_lock);
167 static int unload_module(void)
171 ast_cdr_unregister(name);
175 static int load_module(void)
181 ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
182 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
184 /* is the database there? */
185 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
186 db = sqlite_open(fn, AST_FILE_MODE, &zErr);
188 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
193 /* is the table there? */
194 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
196 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
198 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
203 /* TODO: here we should probably create an index */
206 res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
208 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
219 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");