[asterisk-commits] tilghman: branch tilghman/cdr_custom_odbc r49338 - in /team/tilghman/cdr_cust...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Jan 3 12:02:11 MST 2007


Author: tilghman
Date: Wed Jan  3 13:02:11 2007
New Revision: 49338

URL: http://svn.digium.com/view/asterisk?view=rev&rev=49338
Log:
Initialized merge tracking via "svnmerge" with revisions "1-49336" from 
https://origsvn.digium.com/svn/asterisk/trunk

Added:
    team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c   (with props)
Modified:
    team/tilghman/cdr_custom_odbc/   (props changed)

Propchange: team/tilghman/cdr_custom_odbc/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/tilghman/cdr_custom_odbc/
------------------------------------------------------------------------------
    automerge-email = tilghman at mail.jeffandtilghman.com

Propchange: team/tilghman/cdr_custom_odbc/
------------------------------------------------------------------------------
    svnmerge-integrated = /trunk:1-49336

Added: team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c?view=auto&rev=49338
==============================================================================
--- team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c (added)
+++ team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c Wed Jan  3 13:02:11 2007
@@ -1,0 +1,327 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Tilghman Lesher
+ *
+ * Tilghman Lesher <cdr_custom_odbc__v1 at the-tilghman.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Custom ODBC CDR backend
+ * 
+ * \author Tilghman Lesher <cdr_custom_odbc__v1 at the-tilghman.com>
+ * \ingroup cdr_drivers
+ */
+
+/*** MODULEINFO
+	<depend>unixodbc</depend>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <sql.h>
+#include <sqlext.h>
+#include <sqltypes.h>
+
+#include "asterisk/config.h"
+#include "asterisk/options.h"
+#include "asterisk/channel.h"
+#include "asterisk/lock.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/res_odbc.h"
+#include "asterisk/cdr.h"
+#include "asterisk/module.h"
+#include "asterisk/logger.h"
+
+#define	CONFIG	"custom_odbc.conf"
+
+static char *name = "Custom ODBC";
+static char connection[40];
+static char table[40];
+
+struct columns {
+	char *name;
+	SQLSMALLINT type;
+	SQLINTEGER size;
+	SQLSMALLINT decimals;
+	SQLSMALLINT radix;
+	SQLSMALLINT nullable;
+	SQLINTEGER octetlen;
+	AST_LIST_ENTRY(columns) list;
+};
+
+static AST_RWLIST_HEAD_STATIC(odbc_columns, columns);
+
+static int load_config(void)
+{
+	struct ast_config *cfg;
+	struct ast_variable *var;
+	const char *tmp;
+	struct columns *entry;
+	struct odbc_obj *obj;
+	char name[80];
+	SQLLEN sqlptr;
+	int res;
+	SQLHSTMT stmt;
+
+	cfg = ast_config_load(CONFIG);
+	if (!cfg) {
+		ast_log(LOG_WARNING, "Unable to load " CONFIG ".  No custom ODBC CDRs.\n");
+		return -1;
+	}
+
+	var = ast_variable_browse(cfg, "default");
+	if (!var) {
+		ast_log(LOG_WARNING, "Could not find 'default' context within " CONFIG ".  No custom ODBC CDRs.\n");
+		return -1;
+	}
+
+	tmp = ast_variable_retrieve(cfg, "default", "connection");
+	if (ast_strlen_zero(tmp)) {
+		ast_log(LOG_WARNING, "No connection found.  No custom ODBC CDRs.\n");
+		return -1;
+	}
+	ast_copy_string(connection, tmp, sizeof(connection));
+
+	obj = ast_odbc_request_obj(connection, 1);
+	if (!obj) {
+		ast_log(LOG_WARNING, "No such connection '%s' in the 'default' section of " CONFIG ".  No custom ODBC CDRs.\n", connection);
+		return -1;
+	}
+
+	tmp = ast_variable_retrieve(cfg, "default", "table");
+	if (ast_strlen_zero(tmp)) {
+		ast_log(LOG_NOTICE, "No table name found.  Assuming 'cdr'.\n");
+		tmp = "cdr";
+	}
+	ast_copy_string(table, tmp, sizeof(table));
+
+	res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
+	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+		ast_log(LOG_ERROR, "Unable to query database columns.  No custom ODBC CDRs.\n");
+		ast_odbc_release_obj(obj);
+		return -1;
+	}
+
+	while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
+		SQLGetData(stmt,  4, SQL_C_CHAR, name, sizeof(name), &sqlptr);
+		entry = ast_calloc(1, sizeof(*entry) + strlen(name) + 1);
+		entry->name = (char *)entry + sizeof(entry);
+		strcpy((char *)entry + sizeof(entry), name);
+
+		SQLGetData(stmt,  5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
+		SQLGetData(stmt,  7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
+		SQLGetData(stmt,  9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
+		SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
+		SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
+		SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
+
+		/* Insert column info into column list */
+		AST_RWLIST_INSERT_TAIL(&odbc_columns, entry, list);
+	}
+
+	ast_odbc_release_obj(obj);
+	return 0;
+}
+
+static int free_config(void)
+{
+	struct columns *entry;
+	while ((entry = AST_RWLIST_REMOVE_HEAD(&odbc_columns, list))) {
+		ast_free(entry);
+	}
+	return 0;
+}
+
+static int odbc_log(struct ast_cdr *cdr)
+{
+	struct columns *entry;
+	struct odbc_obj *obj;
+	/* Allocated, so we can realloc() */
+	char *sql = ast_calloc(1, 512), *sql2 = ast_calloc(1, 512), *tmp;
+	int lensql, lensql2, sizesql = 512, sizesql2 = 512, newsize;
+	char colbuf[1024], *colptr;
+
+	lensql = snprintf(sql, sizesql, "INSERT INTO %s (", table);
+	lensql2 = snprintf(sql2, sizesql2, "VALUES (");
+
+	if (!AST_RWLIST_RDLOCK(&odbc_columns)) {
+		ast_log(LOG_ERROR, "Unable to lock column list.  Insert CDR failed.\n");
+		return -1;
+	}
+
+	AST_LIST_TRAVERSE(&odbc_columns, entry, list) {
+		/* Check if we have a similarly named variable */
+		ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 1);
+
+		if (colptr) {
+			switch (entry->type) {
+			case SQL_CHAR:
+			case SQL_VARCHAR:
+			case SQL_LONGVARCHAR:
+			case SQL_BINARY:
+			case SQL_VARBINARY:
+			case SQL_LONGVARBINARY:
+			case SQL_GUID:
+				/* Truncate too-long fields */
+				if (strlen(colptr) > entry->octetlen)
+					colptr[entry->octetlen] = '\0';
+
+				/* Lengthen buffer, if necessary */
+				if ((newsize = lensql + strlen(entry->name) + 3) > sizesql) {
+					if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) {
+						sql = tmp;
+						sizesql = (newsize / 512 + 1) * 512;
+					} else {
+						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n");
+						ast_free(sql);
+						ast_free(sql2);
+						AST_RWLIST_UNLOCK(&odbc_columns);
+						return -1;
+					}
+				}
+				lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+
+				if ((newsize = lensql2 + 3) > sizesql2) {
+					if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) {
+						sql2 = tmp;
+						sizesql2 = (newsize / 512 + 1) * 512;
+					} else {
+						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n");
+						ast_free(sql);
+						ast_free(sql2);
+						AST_RWLIST_UNLOCK(&odbc_columns);
+						return -1;
+					}
+				}
+
+				/* Encode value, with escaping */
+				strcpy(sql2 + lensql2, "'");
+				lensql2++;
+				for (tmp = colptr; *tmp; tmp++) {
+					if (*tmp == '\'') {
+						strcpy(sql2 + lensql2, "''");
+						lensql2 += 2;
+					} else if (*tmp == '\\') {
+						strcpy(sql2 + lensql2, "\\\\");
+						lensql2 += 2;
+					} else {
+						sql2[lensql2++] = *tmp;
+						sql2[lensql2] = '\0';
+					}
+				}
+				strcpy(sql2 + lensql2, "',");
+				lensql2 += 2;
+				break;
+			case SQL_INTEGER:
+			case SQL_SMALLINT:
+			case SQL_BIGINT:
+			case SQL_TINYINT:
+			case SQL_BIT:
+
+			case SQL_NUMERIC:
+			case SQL_DECIMAL:
+			case SQL_FLOAT:
+			case SQL_REAL:
+			case SQL_DOUBLE:
+
+			case SQL_DATETIME:
+			case SQL_INTERVAL:
+			case SQL_TIMESTAMP:
+			case SQL_INTERVAL_YEAR:
+			case SQL_INTERVAL_MONTH:
+			case SQL_INTERVAL_YEAR_TO_MONTH:
+			case SQL_INTERVAL_DAY:
+			case SQL_INTERVAL_HOUR:
+			case SQL_INTERVAL_MINUTE:
+			case SQL_INTERVAL_SECOND:
+			case SQL_INTERVAL_DAY_TO_HOUR:
+			case SQL_INTERVAL_DAY_TO_MINUTE:
+			case SQL_INTERVAL_DAY_TO_SECOND:
+			case SQL_INTERVAL_HOUR_TO_MINUTE:
+			case SQL_INTERVAL_HOUR_TO_SECOND:
+			case SQL_INTERVAL_MINUTE_TO_SECOND:
+
+			case SQL_UNICODE:
+			case SQL_UNICODE_VARCHAR:
+			case SQL_UNICODE_LONGVARCHAR:
+			default:
+				ast_log(LOG_WARNING, "Column type %d (field '%s') unsupported at this time.\n", entry->type, entry->name);
+			}
+		}
+	}
+
+	obj = ast_odbc_request_obj(connection, 1);
+
+	ast_odbc_release_obj(obj);
+	AST_RWLIST_UNLOCK(&odbc_columns);
+	return 0;
+}
+
+static int unload_module(void)
+{
+	ast_cdr_unregister(name);
+	usleep(1);
+	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+		ast_cdr_register(name, ast_module_info->description, odbc_log);
+		ast_log(LOG_ERROR, "Unable to lock column list.  Unload failed.\n");
+		return -1;
+	}
+
+	free_config();
+	AST_RWLIST_UNLOCK(&odbc_columns);
+	return 0;
+}
+
+static int load_module(void)
+{
+	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+		ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
+		return 0;
+	}
+
+	load_config();
+	AST_RWLIST_UNLOCK(&odbc_columns);
+	ast_cdr_register(name, ast_module_info->description, odbc_log);
+	return 0;
+}
+
+static int reload(void)
+{
+	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+		ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
+		return -1;
+	}
+
+	free_config();
+	load_config();
+	AST_RWLIST_UNLOCK(&odbc_columns);
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Custom ODBC CDR backend",
+	.load = load_module,
+	.unload = unload_module,
+	.reload = reload,
+);
+

Propchange: team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list