store and destroy implementations for sqlite (closes issue #10446) and odbc (closes...
[asterisk/asterisk.git] / cdr / cdr_sqlite3_custom.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com> and others.
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Custom SQLite3 CDR records.
22  *
23  * \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
24  *  Russell Bryant <russell@digium.com> from 
25  *  cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
26  *      and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
27  *      
28  *
29  * \arg See also \ref AstCDR
30  *
31  *
32  * \ingroup cdr_drivers
33  */
34
35 /*** MODULEINFO
36         <depend>sqlite3</depend>
37  ***/
38
39 #include "asterisk.h"
40
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <time.h>
49 #include <sys/types.h>
50 #include <sqlite3.h>
51
52 #include "asterisk/channel.h"
53 #include "asterisk/cdr.h"
54 #include "asterisk/module.h"
55 #include "asterisk/config.h"
56 #include "asterisk/pbx.h"
57 #include "asterisk/logger.h"
58 #include "asterisk/utils.h"
59 #include "asterisk/cli.h"
60 #include "asterisk/options.h"
61
62 AST_MUTEX_DEFINE_STATIC(lock);
63
64 static const char config_file[] = "cdr_sqlite3_custom.conf";
65
66 static char *desc = "Customizable SQLite3 CDR Backend";
67 static char *name = "cdr_sqlite3_custom";
68 static sqlite3 *db = NULL;
69
70 static char table[80];
71 static char columns[1024];
72 static char values[1024];
73
74 static int load_config(int reload)
75 {
76         struct ast_config *cfg;
77         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
78         struct ast_variable *mappingvar;
79         const char *tmp;
80
81         if (!(cfg = ast_config_load(config_file, config_flags))) {
82                 if (reload)
83                         ast_log(LOG_WARNING, "%s: Failed to reload configuration file.\n", name);
84                 else {
85                         ast_log(LOG_WARNING,
86                                         "%s: Failed to load configuration file. Module not activated.\n",
87                                         name);
88                 }
89                 return -1;
90         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
91                 return 0;
92
93         ast_mutex_lock(&lock);
94
95         if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
96                 /* nothing configured */
97                 ast_config_destroy(cfg);
98                 return 0;
99         }
100         
101         /* Mapping must have a table name */
102         tmp = ast_variable_retrieve(cfg, "master", "table");
103         if (!ast_strlen_zero(tmp))
104                 ast_copy_string(table, tmp, sizeof(table));
105         else {
106                 ast_log(LOG_WARNING, "%s: Table name not specified.  Assuming cdr.\n", name);
107                 strcpy(table, "cdr");
108         }
109
110         tmp = ast_variable_retrieve(cfg, "master", "columns");
111         if (!ast_strlen_zero(tmp))
112                 ast_copy_string(columns, tmp, sizeof(columns));
113         else {
114                 ast_log(LOG_WARNING, "%s: Column names not specified. Module not loaded.\n",
115                                 name);
116                 ast_config_destroy(cfg);
117                 return -1;
118         }
119
120         tmp = ast_variable_retrieve(cfg, "master", "values");
121         if (!ast_strlen_zero(tmp))
122                 ast_copy_string(values, tmp, sizeof(values));
123         else {
124                 ast_log(LOG_WARNING, "%s: Values not specified. Module not loaded.\n", name);
125                 ast_config_destroy(cfg);
126                 return -1;
127         }
128
129         ast_mutex_unlock(&lock);
130
131         ast_config_destroy(cfg);
132
133         return 0;
134 }
135
136 /* assumues 'to' buffer is at least strlen(from) * 2 + 1 bytes */
137 static int do_escape(char *to, const char *from)
138 {
139         char *out = to;
140
141         for (; *from; from++) {
142                 if (*from == '\'' || *from == '\\')
143                         *out++ = *from;
144                 *out++ = *from;
145         }
146         *out = '\0';
147
148         return 0;
149 }
150
151 static int sqlite3_log(struct ast_cdr *cdr)
152 {
153         int res = 0;
154         char *zErr = 0;
155         char *sql_cmd;
156         struct ast_channel dummy = { 0, };
157         int count;
158
159         { /* Make it obvious that only sql_cmd should be used outside of this block */
160                 char *sql_tmp_cmd;
161                 char sql_insert_cmd[2048] = "";
162                 sql_tmp_cmd = sqlite3_mprintf("INSERT INTO %q (%q) VALUES (%q)", table, columns, values);
163                 dummy.cdr = cdr;
164                 pbx_substitute_variables_helper(&dummy, sql_tmp_cmd, sql_insert_cmd, sizeof(sql_insert_cmd) - 1);
165                 sqlite3_free(sql_tmp_cmd);
166                 sql_cmd = alloca(strlen(sql_insert_cmd) * 2 + 1);
167                 do_escape(sql_cmd, sql_insert_cmd);
168         }
169
170         ast_mutex_lock(&lock);
171
172         for (count = 0; count < 5; count++) {
173                 res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
174                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
175                         break;
176                 usleep(200);
177         }
178
179         if (zErr) {
180                 ast_log(LOG_ERROR, "%s: %s. sentence: %s.\n", name, zErr, sql_cmd);
181                 sqlite3_free(zErr);
182         }
183
184         ast_mutex_unlock(&lock);
185
186         return res;
187 }
188
189 static int unload_module(void)
190 {
191         if (db)
192                 sqlite3_close(db);
193
194         ast_cdr_unregister(name);
195
196         return 0;
197 }
198
199 static int load_module(void)
200 {
201         char *zErr;
202         char fn[PATH_MAX];
203         int res;
204         char *sql_cmd;
205
206         if (!load_config(0)) {
207                 res = ast_cdr_register(name, desc, sqlite3_log);
208                 if (res) {
209                         ast_log(LOG_ERROR, "%s: Unable to register custom SQLite3 CDR handling\n", name);
210                         return AST_MODULE_LOAD_DECLINE;
211                 }
212         }
213
214         /* is the database there? */
215         snprintf(fn, sizeof(fn), "%s/master.db", ast_config_AST_LOG_DIR);
216         res = sqlite3_open(fn, &db);
217         if (!db) {
218                 ast_log(LOG_ERROR, "%s: Could not open database %s.\n", name, fn);
219                 sqlite3_free(zErr);
220                 return AST_MODULE_LOAD_DECLINE;
221         }
222
223         /* is the table there? */
224         sql_cmd = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
225         res = sqlite3_exec(db, sql_cmd, NULL, NULL, NULL);
226         sqlite3_free(sql_cmd);
227         if (res) {
228                 sql_cmd = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY,%q)", table, columns);
229                 res = sqlite3_exec(db, sql_cmd, NULL, NULL, &zErr);
230                 sqlite3_free(sql_cmd);
231                 if (zErr) {
232                         ast_log(LOG_WARNING, "%s: %s.\n", name, zErr);
233                         sqlite3_free(zErr);
234                         return 0;
235                 }
236
237                 if (res) {
238                         ast_log(LOG_ERROR, "%s: Unable to create table '%s': %s.\n", name, table, zErr);
239                         sqlite3_free(zErr);
240                         if (db)
241                                 sqlite3_close(db);
242                         return AST_MODULE_LOAD_DECLINE;
243                 }
244         }
245
246         return 0;
247 }
248
249 static int reload(void)
250 {
251         return load_config(1);
252 }
253
254 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SQLite3 Custom CDR Module",
255         .load = load_module,
256         .unload = unload_module,
257         .reload = reload,
258 );