Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / cdr / cdr_odbc.c
blob6b5b1b77b1115a8751838e5c23eb1b71df7365d9
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2003-2005, Digium, Inc.
6 * Brian K. West <brian@bkw.org>
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.
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.
19 /*! \file
21 * \brief ODBC CDR Backend
23 * \author Brian K. West <brian@bkw.org>
25 * See also:
26 * \arg http://www.unixodbc.org
27 * \arg \ref Config_cdr
28 * \ingroup cdr_drivers
31 /*** MODULEINFO
32 <depend>unixodbc</depend>
33 <depend>ltdl</depend>
34 ***/
36 #include "asterisk.h"
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include <time.h>
42 #include "asterisk/config.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/cdr.h"
45 #include "asterisk/module.h"
46 #include "asterisk/res_odbc.h"
48 #define DATE_FORMAT "%Y-%m-%d %T"
50 static char *name = "ODBC";
51 static char *config_file = "cdr_odbc.conf";
52 static char *dsn = NULL, *table = NULL;
54 enum {
55 CONFIG_LOGUNIQUEID = 1 << 0,
56 CONFIG_USEGMTIME = 1 << 1,
57 CONFIG_DISPOSITIONSTRING = 1 << 2,
60 static struct ast_flags config = { 0 };
62 static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data)
64 struct ast_cdr *cdr = data;
65 SQLRETURN ODBC_res;
66 char sqlcmd[2048] = "", timestr[128];
67 struct ast_tm tm;
68 SQLHSTMT stmt;
70 ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL);
71 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
73 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
74 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
75 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
76 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
77 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
78 } else {
79 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
80 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
81 "duration,billsec,disposition,amaflags,accountcode) "
82 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
85 ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
87 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
88 ast_verb(11, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
89 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
90 return NULL;
93 SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
94 SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
95 SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
96 SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
97 SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
98 SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
99 SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
100 SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
101 SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
102 SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
103 if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING))
104 SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ast_cdr_disp2str(cdr->disposition)) + 1, 0, ast_cdr_disp2str(cdr->disposition), 0, NULL);
105 else
106 SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
107 SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
108 SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
110 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
111 SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
112 SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
115 ODBC_res = SQLExecDirect(stmt, (unsigned char *)sqlcmd, SQL_NTS);
117 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
118 ast_verb(11, "cdr_odbc: Error in ExecDirect: %d\n", ODBC_res);
119 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
120 return NULL;
123 return stmt;
127 static int odbc_log(struct ast_cdr *cdr)
129 struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
130 SQLHSTMT stmt;
132 if (!obj) {
133 ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n");
134 return -1;
137 stmt = ast_odbc_direct_execute(obj, execute_cb, cdr);
138 if (stmt) {
139 SQLLEN rows = 0;
141 SQLRowCount(stmt, &rows);
142 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
144 if (rows == 0)
145 ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n");
146 } else
147 ast_log(LOG_ERROR, "CDR direct execute failed\n");
148 ast_odbc_release_obj(obj);
149 return 0;
152 static int odbc_load_module(int reload)
154 int res = 0;
155 struct ast_config *cfg;
156 struct ast_variable *var;
157 const char *tmp;
158 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
160 do {
161 cfg = ast_config_load(config_file, config_flags);
162 if (!cfg) {
163 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file);
164 res = AST_MODULE_LOAD_DECLINE;
165 break;
166 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
167 break;
169 var = ast_variable_browse(cfg, "global");
170 if (!var) {
171 /* nothing configured */
172 break;
175 if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) {
176 ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n");
177 tmp = "asteriskdb";
179 if (dsn)
180 ast_free(dsn);
181 dsn = ast_strdup(tmp);
182 if (dsn == NULL) {
183 res = -1;
184 break;
187 if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp))
188 ast_set_flag(&config, CONFIG_DISPOSITIONSTRING);
189 else
190 ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING);
192 if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) {
193 ast_set_flag(&config, CONFIG_LOGUNIQUEID);
194 ast_debug(1, "cdr_odbc: Logging uniqueid\n");
195 } else {
196 ast_clear_flag(&config, CONFIG_LOGUNIQUEID);
197 ast_debug(1, "cdr_odbc: Not logging uniqueid\n");
200 if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) {
201 ast_set_flag(&config, CONFIG_USEGMTIME);
202 ast_debug(1, "cdr_odbc: Logging in GMT\n");
203 } else {
204 ast_clear_flag(&config, CONFIG_USEGMTIME);
205 ast_debug(1, "cdr_odbc: Logging in local time\n");
208 if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) {
209 ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n");
210 tmp = "cdr";
212 if (table)
213 ast_free(table);
214 table = ast_strdup(tmp);
215 if (table == NULL) {
216 res = -1;
217 break;
220 ast_verb(3, "cdr_odbc: dsn is %s\n", dsn);
221 ast_verb(3, "cdr_odbc: table is %s\n", table);
223 res = ast_cdr_register(name, ast_module_info->description, odbc_log);
224 if (res) {
225 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
227 } while (0);
229 if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED)
230 ast_config_destroy(cfg);
231 return res;
234 static int load_module(void)
236 return odbc_load_module(0);
239 static int unload_module(void)
241 ast_cdr_unregister(name);
243 if (dsn) {
244 ast_verb(11, "cdr_odbc: free dsn\n");
245 ast_free(dsn);
247 if (table) {
248 ast_verb(11, "cdr_odbc: free table\n");
249 ast_free(table);
252 return 0;
255 static int reload(void)
257 return odbc_load_module(1);
260 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC CDR Backend",
261 .load = load_module,
262 .unload = unload_module,
263 .reload = reload,