(closes issue #11594)
[asterisk-bristuff.git] / cdr / cdr_sqlite.c
blob5aa8a515489b863f0854384c12fcdaf362f3cf4c
1 /*
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.
20 /*! \file
22 * \brief Store CDR records in a SQLite database.
24 * \author Holger Schurig <hs4233@mail.mn-solutions.de>
26 * See also
27 * \arg \ref Config_cdr
28 * \arg http://www.sqlite.org/
30 * Creates the database and table on-the-fly
31 * \ingroup cdr_drivers
34 /*** MODULEINFO
35 <depend>sqlite</depend>
36 ***/
38 #include "asterisk.h"
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <sqlite.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,"
68 " clid VARCHAR(80),"
69 " src VARCHAR(80),"
70 " dst VARCHAR(80),"
71 " dcontext VARCHAR(80),"
72 " channel VARCHAR(80),"
73 " dstchannel VARCHAR(80),"
74 " lastapp VARCHAR(80),"
75 " lastdata VARCHAR(80),"
76 " start CHAR(19),"
77 " answer CHAR(19),"
78 " end CHAR(19),"
79 " duration INTEGER,"
80 " billsec INTEGER,"
81 " disposition INTEGER,"
82 " amaflags INTEGER,"
83 " accountcode VARCHAR(20)"
84 #if LOG_UNIQUEID
85 " ,uniqueid VARCHAR(32)"
86 #endif
87 #if LOG_USERFIELD
88 " ,userfield VARCHAR(255)"
89 #endif
90 ");";
92 static int sqlite_log(struct ast_cdr *cdr)
94 int res = 0;
95 char *zErr = 0;
96 struct tm tm;
97 time_t t;
98 char startstr[80], answerstr[80], endstr[80];
99 int count;
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);
111 t = cdr->end.tv_sec;
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,
117 "INSERT INTO cdr ("
118 "clid,src,dst,dcontext,"
119 "channel,dstchannel,lastapp,lastdata, "
120 "start,answer,end,"
121 "duration,billsec,disposition,amaflags, "
122 "accountcode"
123 # if LOG_UNIQUEID
124 ",uniqueid"
125 # endif
126 # if LOG_USERFIELD
127 ",userfield"
128 # endif
129 ") VALUES ("
130 "'%q', '%q', '%q', '%q', "
131 "'%q', '%q', '%q', '%q', "
132 "'%q', '%q', '%q', "
133 "%d, %d, %d, %d, "
134 "'%q'"
135 # if LOG_UNIQUEID
136 ",'%q'"
137 # endif
138 # if LOG_USERFIELD
139 ",'%q'"
140 # endif
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,
146 cdr->accountcode
147 # if LOG_UNIQUEID
148 ,cdr->uniqueid
149 # endif
150 # if LOG_USERFIELD
151 ,cdr->userfield
152 # endif
154 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
155 break;
156 usleep(200);
159 if (zErr) {
160 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
161 free(zErr);
164 ast_mutex_unlock(&sqlite_lock);
165 return res;
168 static int unload_module(void)
170 if (db)
171 sqlite_close(db);
172 ast_cdr_unregister(name);
173 return 0;
176 static int load_module(void)
178 char *zErr;
179 char fn[PATH_MAX];
180 int res;
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);
185 if (!db) {
186 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
187 free(zErr);
188 return -1;
191 /* is the table there? */
192 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
193 if (res) {
194 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
195 if (res) {
196 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
197 free(zErr);
198 goto err;
201 /* TODO: here we should probably create an index */
204 res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
205 if (res) {
206 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
207 return -1;
209 return 0;
211 err:
212 if (db)
213 sqlite_close(db);
214 return -1;
217 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");