another bunch of include removals (errno.h and asterisk/logger.h)
[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
51 #define LOG_UNIQUEID    0
52 #define LOG_USERFIELD   0
53
54 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
55 #define DATE_FORMAT "%Y-%m-%d %T"
56
57 static char *name = "sqlite";
58 static sqlite* db = NULL;
59
60 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
61
62 /*! \brief SQL table format */
63 static char sql_create_table[] = "CREATE TABLE cdr ("
64 "       AcctId          INTEGER PRIMARY KEY,"
65 "       clid            VARCHAR(80),"
66 "       src             VARCHAR(80),"
67 "       dst             VARCHAR(80),"
68 "       dcontext        VARCHAR(80),"
69 "       channel         VARCHAR(80),"
70 "       dstchannel      VARCHAR(80),"
71 "       lastapp         VARCHAR(80),"
72 "       lastdata        VARCHAR(80),"
73 "       start           CHAR(19),"
74 "       answer          CHAR(19),"
75 "       end             CHAR(19),"
76 "       duration        INTEGER,"
77 "       billsec         INTEGER,"
78 "       disposition     INTEGER,"
79 "       amaflags        INTEGER,"
80 "       accountcode     VARCHAR(20)"
81 #if LOG_UNIQUEID
82 "       ,uniqueid       VARCHAR(32)"
83 #endif
84 #if LOG_USERFIELD
85 "       ,userfield      VARCHAR(255)"
86 #endif
87 ");";
88
89 static int sqlite_log(struct ast_cdr *cdr)
90 {
91         int res = 0;
92         char *zErr = 0;
93         struct ast_tm tm;
94         char startstr[80], answerstr[80], endstr[80];
95         int count;
96
97         ast_mutex_lock(&sqlite_lock);
98
99         ast_localtime(&cdr->start, &tm, NULL);
100         ast_strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
101
102         ast_localtime(&cdr->answer, &tm, NULL);
103         ast_strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
104
105         ast_localtime(&cdr->end, &tm, NULL);
106         ast_strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
107
108         for(count=0; count<5; count++) {
109                 res = sqlite_exec_printf(db,
110                         "INSERT INTO cdr ("
111                                 "clid,src,dst,dcontext,"
112                                 "channel,dstchannel,lastapp,lastdata, "
113                                 "start,answer,end,"
114                                 "duration,billsec,disposition,amaflags, "
115                                 "accountcode"
116 #                               if LOG_UNIQUEID
117                                 ",uniqueid"
118 #                               endif
119 #                               if LOG_USERFIELD
120                                 ",userfield"
121 #                               endif
122                         ") VALUES ("
123                                 "'%q', '%q', '%q', '%q', "
124                                 "'%q', '%q', '%q', '%q', "
125                                 "'%q', '%q', '%q', "
126                                 "%d, %d, %d, %d, "
127                                 "'%q'"
128 #                               if LOG_UNIQUEID
129                                 ",'%q'"
130 #                               endif
131 #                               if LOG_USERFIELD
132                                 ",'%q'"
133 #                               endif
134                         ")", NULL, NULL, &zErr,
135                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
136                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
137                                 startstr, answerstr, endstr,
138                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
139                                 cdr->accountcode
140 #                               if LOG_UNIQUEID
141                                 ,cdr->uniqueid
142 #                               endif
143 #                               if LOG_USERFIELD
144                                 ,cdr->userfield
145 #                               endif
146                         );
147                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
148                         break;
149                 usleep(200);
150         }
151         
152         if (zErr) {
153                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
154                 ast_free(zErr);
155         }
156
157         ast_mutex_unlock(&sqlite_lock);
158         return res;
159 }
160
161 static int unload_module(void)
162 {
163         if (db)
164                 sqlite_close(db);
165         ast_cdr_unregister(name);
166         return 0;
167 }
168
169 static int load_module(void)
170 {
171         char *zErr;
172         char fn[PATH_MAX];
173         int res;
174
175         ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
176                 "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
177
178         /* is the database there? */
179         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
180         db = sqlite_open(fn, AST_FILE_MODE, &zErr);
181         if (!db) {
182                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
183                 ast_free(zErr);
184                 return -1;
185         }
186
187         /* is the table there? */
188         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
189         if (res) {
190                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
191                 if (res) {
192                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
193                         ast_free(zErr);
194                         goto err;
195                 }
196
197                 /* TODO: here we should probably create an index */
198         }
199         
200         res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
201         if (res) {
202                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
203                 return -1;
204         }
205         return 0;
206
207 err:
208         if (db)
209                 sqlite_close(db);
210         return -1;
211 }
212
213 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");