make table configurable for cdr_odbc (bug #2755)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@4416 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2004-12-10 11:59:43 +00:00
parent 998621f76a
commit 4bbdbf3a79
2 changed files with 39 additions and 8 deletions

View File

@@ -37,8 +37,8 @@
static char *desc = "ODBC CDR Backend"; static char *desc = "ODBC CDR Backend";
static char *name = "ODBC"; static char *name = "ODBC";
static char *config = "cdr_odbc.conf"; static char *config = "cdr_odbc.conf";
static char *dsn = NULL, *username = NULL, *password = NULL, *loguniqueid = NULL; static char *dsn = NULL, *username = NULL, *password = NULL, *loguniqueid = NULL, *table = NULL;
static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, loguniqueid_alloc = 0; static int dsn_alloc = 0, username_alloc = 0, password_alloc = 0, loguniqueid_alloc = 0, table_alloc = 0;
static int connected = 0; static int connected = 0;
AST_MUTEX_DEFINE_STATIC(odbc_lock); AST_MUTEX_DEFINE_STATIC(odbc_lock);
@@ -66,15 +66,15 @@ static int odbc_log(struct ast_cdr *cdr)
strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm); strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
memset(sqlcmd,0,2048); memset(sqlcmd,0,2048);
if ((loguniqueid != NULL) && (ast_true(loguniqueid))) { if ((loguniqueid != NULL) && (ast_true(loguniqueid))) {
snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO cdr " snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp," "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
"lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) " "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
} else { } else {
snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO cdr " snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
"(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata," "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
"duration,billsec,disposition,amaflags,accountcode) " "duration,billsec,disposition,amaflags,accountcode) "
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table);
} }
if (!connected) { if (!connected) {
@@ -211,6 +211,13 @@ static int odbc_unload_module(void)
loguniqueid = NULL; loguniqueid = NULL;
loguniqueid_alloc = 0; loguniqueid_alloc = 0;
} }
if (table && table_alloc) {
if (option_verbose > 10)
ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: free table\n");
free(table);
table = NULL;
table_alloc = 0;
}
ast_cdr_unregister(name); ast_cdr_unregister(name);
ast_mutex_unlock(&odbc_lock); ast_mutex_unlock(&odbc_lock);
return 0; return 0;
@@ -302,11 +309,28 @@ static int odbc_load_module(void)
loguniqueid = NULL; loguniqueid = NULL;
} }
tmp = ast_variable_retrieve(cfg,"global","table");
if (tmp) {
table = malloc(strlen(tmp) + 1);
if (table != NULL) {
memset(table, 0, strlen(tmp) + 1);
table_alloc = 1;
strncpy(table, tmp, strlen(tmp));
} else {
ast_log(LOG_ERROR,"cdr_odbc: Out of memory error.\n");
return -1;
}
} else {
ast_log(LOG_WARNING,"cdr_odbc: table not specified. Assuming cdr\n");
table = "cdr";
}
ast_destroy(cfg); ast_destroy(cfg);
if (option_verbose > 3) { if (option_verbose > 3) {
ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn); ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: dsn is %s\n",dsn);
ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username); ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: username is %s\n",username);
ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n"); ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: password is [secret]\n");
ast_verbose( VERBOSE_PREFIX_4 "cdr_odbc: table is %s\n",table);
} }
res = odbc_init(); res = odbc_init();
@@ -420,7 +444,13 @@ int reload(void)
int usecount(void) int usecount(void)
{ {
return connected; /* Simplistic use count */
if (ast_mutex_trylock(&odbc_lock)) {
return 1;
} else {
ast_mutex_unlock(&odbc_lock);
return 0;
}
} }
char *key() char *key()

View File

@@ -1,5 +1,5 @@
; ;
; cdr_unixodbc.conf ; cdr_odbc.conf
; ;
;[global] ;[global]
@@ -7,3 +7,4 @@
;username=username ;username=username
;password=password ;password=password
;loguniqueid=yes ;loguniqueid=yes
;table=cdr ;"cdr" is default table name