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)
101 char startstr[80], answerstr[80], endstr[80];
104 ast_mutex_lock(&sqlite_lock);
106 t = cdr->start.tv_sec;
107 localtime_r(&t, &tm);
108 strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
110 t = cdr->answer.tv_sec;
111 localtime_r(&t, &tm);
112 strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
115 localtime_r(&t, &tm);
116 strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
118 for(count=0; count<5; count++) {
119 res = sqlite_exec_printf(db,
121 "clid,src,dst,dcontext,"
122 "channel,dstchannel,lastapp,lastdata, "
124 "duration,billsec,disposition,amaflags, "
133 "'%q', '%q', '%q', '%q', "
134 "'%q', '%q', '%q', '%q', "
144 ")", NULL, NULL, &zErr,
145 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
146 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
147 startstr, answerstr, endstr,
148 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
157 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
163 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
167 ast_mutex_unlock(&sqlite_lock);
171 static int unload_module(void)
175 ast_cdr_unregister(name);
179 static int load_module(void)
185 ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
186 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
188 /* is the database there? */
189 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
190 db = sqlite_open(fn, AST_FILE_MODE, &zErr);
192 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
197 /* is the table there? */
198 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
200 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
202 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
207 /* TODO: here we should probably create an index */
210 res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
212 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
223 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");