Fix a bug I noticed while doing the previous merge
[asterisk/asterisk.git] / cdr / cdr_sqlite.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005, Holger Schurig
5  *
6  *
7  * Ideas taken from other cdr_*.c files
8  *
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.
14  *
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.
18  */
19
20 /*! \file
21  *
22  * \brief Store CDR records in a SQLite database.
23  * 
24  * \author Holger Schurig <hs4233@mail.mn-solutions.de>
25  * \extref SQLite http://www.sqlite.org/
26  *
27  * See also
28  * \arg \ref Config_cdr
29  * \arg http://www.sqlite.org/
30  * 
31  * Creates the database and table on-the-fly
32  * \ingroup cdr_drivers
33  *
34  * \note This module has been marked deprecated in favor for cdr_sqlite3_custom
35  */
36
37 /*** MODULEINFO
38         <depend>sqlite</depend>
39  ***/
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include <sqlite.h>
46
47 #include "asterisk/channel.h"
48 #include "asterisk/module.h"
49 #include "asterisk/utils.h"
50 #include "asterisk/paths.h"
51
52 #define LOG_UNIQUEID    0
53 #define LOG_USERFIELD   0
54
55 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
56 #define DATE_FORMAT "%Y-%m-%d %T"
57
58 static char *name = "sqlite";
59 static sqlite* db = NULL;
60
61 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
62
63 /*! \brief SQL table format */
64 static char sql_create_table[] = "CREATE TABLE cdr ("
65 "       AcctId          INTEGER PRIMARY KEY,"
66 "       clid            VARCHAR(80),"
67 "       src             VARCHAR(80),"
68 "       dst             VARCHAR(80),"
69 "       dcontext        VARCHAR(80),"
70 "       channel         VARCHAR(80),"
71 "       dstchannel      VARCHAR(80),"
72 "       lastapp         VARCHAR(80),"
73 "       lastdata        VARCHAR(80),"
74 "       start           CHAR(19),"
75 "       answer          CHAR(19),"
76 "       end             CHAR(19),"
77 "       duration        INTEGER,"
78 "       billsec         INTEGER,"
79 "       disposition     INTEGER,"
80 "       amaflags        INTEGER,"
81 "       accountcode     VARCHAR(20)"
82 #if LOG_UNIQUEID
83 "       ,uniqueid       VARCHAR(32)"
84 #endif
85 #if LOG_USERFIELD
86 "       ,userfield      VARCHAR(255)"
87 #endif
88 ");";
89
90 static int sqlite_log(struct ast_cdr *cdr)
91 {
92         int res = 0;
93         char *zErr = 0;
94         struct ast_tm tm;
95         char startstr[80], answerstr[80], endstr[80];
96         int count;
97
98         ast_mutex_lock(&sqlite_lock);
99
100         ast_localtime(&cdr->start, &tm, NULL);
101         ast_strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
102
103         ast_localtime(&cdr->answer, &tm, NULL);
104         ast_strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
105
106         ast_localtime(&cdr->end, &tm, NULL);
107         ast_strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
108
109         for(count=0; count<5; count++) {
110                 res = sqlite_exec_printf(db,
111                         "INSERT INTO cdr ("
112                                 "clid,src,dst,dcontext,"
113                                 "channel,dstchannel,lastapp,lastdata, "
114                                 "start,answer,end,"
115                                 "duration,billsec,disposition,amaflags, "
116                                 "accountcode"
117 #                               if LOG_UNIQUEID
118                                 ",uniqueid"
119 #                               endif
120 #                               if LOG_USERFIELD
121                                 ",userfield"
122 #                               endif
123                         ") VALUES ("
124                                 "'%q', '%q', '%q', '%q', "
125                                 "'%q', '%q', '%q', '%q', "
126                                 "'%q', '%q', '%q', "
127                                 "%d, %d, %d, %d, "
128                                 "'%q'"
129 #                               if LOG_UNIQUEID
130                                 ",'%q'"
131 #                               endif
132 #                               if LOG_USERFIELD
133                                 ",'%q'"
134 #                               endif
135                         ")", NULL, NULL, &zErr,
136                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
137                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
138                                 startstr, answerstr, endstr,
139                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
140                                 cdr->accountcode
141 #                               if LOG_UNIQUEID
142                                 ,cdr->uniqueid
143 #                               endif
144 #                               if LOG_USERFIELD
145                                 ,cdr->userfield
146 #                               endif
147                         );
148                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
149                         break;
150                 usleep(200);
151         }
152         
153         if (zErr) {
154                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
155                 ast_free(zErr);
156         }
157
158         ast_mutex_unlock(&sqlite_lock);
159         return res;
160 }
161
162 static int unload_module(void)
163 {
164         if (db)
165                 sqlite_close(db);
166         ast_cdr_unregister(name);
167         return 0;
168 }
169
170 static int load_module(void)
171 {
172         char *zErr;
173         char fn[PATH_MAX];
174         int res;
175
176         ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
177                 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
178
179         /* is the database there? */
180         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
181         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
182         if (!db) {
183                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
184                 ast_free(zErr);
185                 return -1;
186         }
187
188         /* is the table there? */
189         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
190         if (res) {
191                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
192                 if (res) {
193                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
194                         ast_free(zErr);
195                         goto err;
196                 }
197
198                 /* TODO: here we should probably create an index */
199         }
200         
201         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
202         if (res) {
203                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
204                 return -1;
205         }
206         return 0;
207
208 err:
209         if (db)
210                 sqlite_close(db);
211         return -1;
212 }
213
214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");