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>
27 * \arg \ref Config_cdr
28 * \arg http://www.sqlite.org/
30 * Creates the database and table on-the-fly
31 * \ingroup cdr_drivers
35 <depend>sqlite</depend>
40 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
42 #include <sys/types.h>
49 #include "asterisk/channel.h"
50 #include "asterisk/module.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/utils.h"
54 #define LOG_UNIQUEID 0
55 #define LOG_USERFIELD 0
57 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
58 #define DATE_FORMAT "%Y-%m-%d %T"
60 static char *name
= "sqlite";
61 static sqlite
* db
= NULL
;
63 AST_MUTEX_DEFINE_STATIC(sqlite_lock
);
65 /*! \brief SQL table format */
66 static char sql_create_table
[] = "CREATE TABLE cdr ("
67 " AcctId INTEGER PRIMARY KEY,"
71 " dcontext VARCHAR(80),"
72 " channel VARCHAR(80),"
73 " dstchannel VARCHAR(80),"
74 " lastapp VARCHAR(80),"
75 " lastdata VARCHAR(80),"
81 " disposition INTEGER,"
83 " accountcode VARCHAR(20)"
85 " ,uniqueid VARCHAR(32)"
88 " ,userfield VARCHAR(255)"
92 static int sqlite_log(struct ast_cdr
*cdr
)
98 char startstr
[80], answerstr
[80], endstr
[80];
101 ast_mutex_lock(&sqlite_lock
);
103 t
= cdr
->start
.tv_sec
;
104 ast_localtime(&t
, &tm
, NULL
);
105 strftime(startstr
, sizeof(startstr
), DATE_FORMAT
, &tm
);
107 t
= cdr
->answer
.tv_sec
;
108 ast_localtime(&t
, &tm
, NULL
);
109 strftime(answerstr
, sizeof(answerstr
), DATE_FORMAT
, &tm
);
112 ast_localtime(&t
, &tm
, NULL
);
113 strftime(endstr
, sizeof(endstr
), DATE_FORMAT
, &tm
);
115 for(count
=0; count
<5; count
++) {
116 res
= sqlite_exec_printf(db
,
118 "clid,src,dst,dcontext,"
119 "channel,dstchannel,lastapp,lastdata, "
121 "duration,billsec,disposition,amaflags, "
130 "'%q', '%q', '%q', '%q', "
131 "'%q', '%q', '%q', '%q', "
141 ")", NULL
, NULL
, &zErr
,
142 cdr
->clid
, cdr
->src
, cdr
->dst
, cdr
->dcontext
,
143 cdr
->channel
, cdr
->dstchannel
, cdr
->lastapp
, cdr
->lastdata
,
144 startstr
, answerstr
, endstr
,
145 cdr
->duration
, cdr
->billsec
, cdr
->disposition
, cdr
->amaflags
,
154 if (res
!= SQLITE_BUSY
&& res
!= SQLITE_LOCKED
)
160 ast_log(LOG_ERROR
, "cdr_sqlite: %s\n", zErr
);
164 ast_mutex_unlock(&sqlite_lock
);
168 static int unload_module(void)
172 ast_cdr_unregister(name
);
176 static int load_module(void)
182 /* is the database there? */
183 snprintf(fn
, sizeof(fn
), "%s/cdr.db", ast_config_AST_LOG_DIR
);
184 db
= sqlite_open(fn
, 0660, &zErr
);
186 ast_log(LOG_ERROR
, "cdr_sqlite: %s\n", zErr
);
191 /* is the table there? */
192 res
= sqlite_exec(db
, "SELECT COUNT(AcctId) FROM cdr;", NULL
, NULL
, NULL
);
194 res
= sqlite_exec(db
, sql_create_table
, NULL
, NULL
, &zErr
);
196 ast_log(LOG_ERROR
, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr
);
201 /* TODO: here we should probably create an index */
204 res
= ast_cdr_register(name
, ast_module_info
->description
, sqlite_log
);
206 ast_log(LOG_ERROR
, "Unable to register SQLite CDR handling\n");
217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "SQLite CDR Backend");