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