a5eb54f22bf60b42db1c5270b0013ba409b24cad
[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 const 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 void format_date(char *buffer, size_t length, struct timeval *when)
91 {
92         struct ast_tm tm;
93
94         ast_localtime(when, &tm, NULL);
95         ast_strftime(buffer, length, DATE_FORMAT, &tm);
96 }
97
98 static int sqlite_log(struct ast_cdr *cdr)
99 {
100         int res = 0;
101         char *zErr = 0;
102         char startstr[80], answerstr[80], endstr[80];
103         int count;
104
105         ast_mutex_lock(&sqlite_lock);
106
107         format_date(startstr, sizeof(startstr), &cdr->start);
108         format_date(answerstr, sizeof(answerstr), &cdr->answer);
109         format_date(endstr, sizeof(endstr), &cdr->end);
110
111         for(count=0; count<5; count++) {
112                 res = sqlite_exec_printf(db,
113                         "INSERT INTO cdr ("
114                                 "clid,src,dst,dcontext,"
115                                 "channel,dstchannel,lastapp,lastdata, "
116                                 "start,answer,end,"
117                                 "duration,billsec,disposition,amaflags, "
118                                 "accountcode"
119 #                               if LOG_UNIQUEID
120                                 ",uniqueid"
121 #                               endif
122 #                               if LOG_USERFIELD
123                                 ",userfield"
124 #                               endif
125                         ") VALUES ("
126                                 "'%q', '%q', '%q', '%q', "
127                                 "'%q', '%q', '%q', '%q', "
128                                 "'%q', '%q', '%q', "
129                                 "%d, %d, %d, %d, "
130                                 "'%q'"
131 #                               if LOG_UNIQUEID
132                                 ",'%q'"
133 #                               endif
134 #                               if LOG_USERFIELD
135                                 ",'%q'"
136 #                               endif
137                         ")", NULL, NULL, &zErr,
138                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
139                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
140                                 startstr, answerstr, endstr,
141                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
142                                 cdr->accountcode
143 #                               if LOG_UNIQUEID
144                                 ,cdr->uniqueid
145 #                               endif
146 #                               if LOG_USERFIELD
147                                 ,cdr->userfield
148 #                               endif
149                         );
150                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
151                         break;
152                 usleep(200);
153         }
154
155         if (zErr) {
156                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
157                 ast_free(zErr);
158         }
159
160         ast_mutex_unlock(&sqlite_lock);
161         return res;
162 }
163
164 static int unload_module(void)
165 {
166         if (db)
167                 sqlite_close(db);
168         ast_cdr_unregister(name);
169         return 0;
170 }
171
172 static int load_module(void)
173 {
174         char *zErr;
175         char fn[PATH_MAX];
176         int res;
177
178         ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
179                 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
180
181         /* is the database there? */
182         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
183         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
184         if (!db) {
185                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
186                 ast_free(zErr);
187                 return AST_MODULE_LOAD_DECLINE;
188         }
189
190         /* is the table there? */
191         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
192         if (res) {
193                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
194                 if (res) {
195                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
196                         ast_free(zErr);
197                         goto err;
198                 }
199
200                 /* TODO: here we should probably create an index */
201         }
202
203         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
204         if (res) {
205                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
206                 return AST_MODULE_LOAD_DECLINE;
207         }
208         return AST_MODULE_LOAD_SUCCESS;
209
210 err:
211         if (db)
212                 sqlite_close(db);
213         return AST_MODULE_LOAD_DECLINE;
214 }
215
216 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");