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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Jan 9 16:59:33 MST 2007


Author: tilghman
Date: Tue Jan  9 17:59:32 2007
New Revision: 50222

URL: http://svn.digium.com/view/asterisk?view=rev&rev=50222
Log:
Add config file, add multiple tables

Added:
    team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample   (with props)
Modified:
    team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c

Modified: 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=diff&rev=50222&r1=50221&r2=50222
==============================================================================
--- team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c (original)
+++ team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c Tue Jan  9 17:59:32 2007
@@ -56,8 +56,6 @@
 #define	CONFIG	"custom_odbc.conf"
 
 static char *name = "Custom ODBC";
-static char connection[40];
-static char table[40];
 
 struct columns {
 	char *name;
@@ -70,16 +68,27 @@
 	AST_LIST_ENTRY(columns) list;
 };
 
-static AST_RWLIST_HEAD_STATIC(odbc_columns, columns);
+struct tables {
+	char *connection;
+	char *table;
+	AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
+	AST_RWLIST_ENTRY(tables) list;
+};
+
+static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
 
 static int load_config(void)
 {
 	struct ast_config *cfg;
 	struct ast_variable *var;
-	const char *tmp;
+	const char *tmp, *catg;
+	struct tables *tableptr;
 	struct columns *entry;
 	struct odbc_obj *obj;
-	char name[80];
+	char columnname[80];
+	char connection[40];
+	char table[40];
+	int lenconnection, lentable;
 	SQLLEN sqlptr;
 	int res;
 	SQLHSTMT stmt = NULL;
@@ -90,72 +99,100 @@
 		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 = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
-	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-		ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+	for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
+		var = ast_variable_browse(cfg, catg);
+		if (!var)
+			continue;
+
+		if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
+			ast_log(LOG_WARNING, "No connection parameter found in '%s'.  Skipping.\n", catg);
+			continue;
+		}
+		ast_copy_string(connection, tmp, sizeof(connection));
+		lenconnection = strlen(connection);
+
+		obj = ast_odbc_request_obj(connection, 1);
+		if (!obj) {
+			ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ".  Check res_odbc.conf.\n", connection, catg);
+			continue;
+		}
+
+		if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
+			ast_log(LOG_NOTICE, "No table name found.  Assuming 'cdr'.\n");
+			tmp = "cdr";
+		}
+		ast_copy_string(table, tmp, sizeof(table));
+		lentable = strlen(table);
+
+		res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
+		if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+			ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
+			ast_odbc_release_obj(obj);
+			continue;
+		}
+
+		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 on connection '%s'.  Skipping.\n", connection);
+			ast_odbc_release_obj(obj);
+			continue;
+		}
+
+		tableptr = ast_calloc(1, sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
+		if (!tableptr) {
+			ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
+			ast_odbc_release_obj(obj);
+			res = -1;
+			break;
+		}
+
+		tableptr->connection = (char *)tableptr + sizeof(*tableptr);
+		tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
+		ast_copy_string(tableptr->connection, connection, lenconnection + 1);
+		ast_copy_string(tableptr->table, table, lentable + 1);
+
+		while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
+			SQLGetData(stmt,  4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
+			entry = ast_calloc(1, sizeof(*entry) + strlen(columnname) + 1);
+			if (!entry) {
+				ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
+				res = -1;
+				break;
+			}
+			entry->name = (char *)entry + sizeof(*entry);
+			strcpy((char *)entry + sizeof(*entry), columnname);
+
+			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_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
+			res = 0;
+		}
+
 		ast_odbc_release_obj(obj);
-		return -1;
-	}
-
-	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;
+
+		if (AST_LIST_FIRST(&(tableptr->columns)))
+			AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
+		else
+			free(tableptr);
+	}
+	return res;
 }
 
 static int free_config(void)
 {
+	struct tables *table;
 	struct columns *entry;
-	while ((entry = AST_RWLIST_REMOVE_HEAD(&odbc_columns, list))) {
-		ast_free(entry);
+	while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
+		while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
+			ast_free(entry);
+		}
+		ast_free(table);
 	}
 	return 0;
 }
@@ -190,10 +227,10 @@
 						sql = tmp;												\
 						sizesql = (newsize / 512 + 1) * 512;					\
 					} else {													\
-						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
+						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
 						ast_free(sql);											\
 						ast_free(sql2);											\
-						AST_RWLIST_UNLOCK(&odbc_columns);						\
+						AST_RWLIST_UNLOCK(&odbc_tables);						\
 						return -1;												\
 					}															\
 				}																\
@@ -206,10 +243,10 @@
 						sql2 = tmp;												\
 						sizesql2 = (newsize / 512 + 1) * 512;					\
 					} else {													\
-						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n");	\
+						ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table);	\
 						ast_free(sql);											\
 						ast_free(sql2);											\
-						AST_RWLIST_UNLOCK(&odbc_columns);						\
+						AST_RWLIST_UNLOCK(&odbc_tables);						\
 						return -1;												\
 					}															\
 				}																\
@@ -217,6 +254,7 @@
 
 static int odbc_log(struct ast_cdr *cdr)
 {
+	struct tables *tableptr;
 	struct columns *entry;
 	struct odbc_obj *obj;
 	/* Allocated, so we can realloc() */
@@ -226,252 +264,254 @@
 	SQLHSTMT stmt = NULL;
 	SQLINTEGER rows = 0;
 
-	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");
+	if (!AST_RWLIST_RDLOCK(&odbc_tables)) {
+		ast_log(LOG_ERROR, "Unable to lock table list.  Insert CDR(s) 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) {
-			LENGTHEN_BUF1(strlen(entry->name));
-
-			switch (entry->type) {
-			case SQL_CHAR:
-			case SQL_VARCHAR:
-			case SQL_LONGVARCHAR:
-			case SQL_BINARY:
-			case SQL_VARBINARY:
-			case SQL_LONGVARBINARY:
-			case SQL_GUID:
-				/* For these two field names, get the rendered form, instead of the raw
-				 * form (but only when we're dealing with a character-based field).
-				 */
-				if (strcasecmp(entry->name, "disposition") == 0)
-					ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
-				else if (strcasecmp(entry->name, "amaflags") == 0)
-					ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
-
-				/* Truncate too-long fields */
-				if (entry->type != SQL_GUID) {
-					if (strlen(colptr) > entry->octetlen)
-						colptr[entry->octetlen] = '\0';
+	AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
+		lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
+		lensql2 = snprintf(sql2, sizesql2, " VALUES (");
+
+		AST_LIST_TRAVERSE(&(tableptr->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) {
+				LENGTHEN_BUF1(strlen(entry->name));
+
+				switch (entry->type) {
+				case SQL_CHAR:
+				case SQL_VARCHAR:
+				case SQL_LONGVARCHAR:
+				case SQL_BINARY:
+				case SQL_VARBINARY:
+				case SQL_LONGVARBINARY:
+				case SQL_GUID:
+					/* For these two field names, get the rendered form, instead of the raw
+					 * form (but only when we're dealing with a character-based field).
+					 */
+					if (strcasecmp(entry->name, "disposition") == 0)
+						ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
+					else if (strcasecmp(entry->name, "amaflags") == 0)
+						ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
+
+					/* Truncate too-long fields */
+					if (entry->type != SQL_GUID) {
+						if (strlen(colptr) > entry->octetlen)
+							colptr[entry->octetlen] = '\0';
+					}
+
+					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+					LENGTHEN_BUF2(strlen(colptr));
+
+					/* 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_TYPE_DATE:
+					{
+						int year = 0, month = 0, day = 0;
+						if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
+							month <= 0 || month > 12 || day < 0 || day > 31 ||
+							((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
+							(month == 2 && year % 400 == 0 && day > 29) ||
+							(month == 2 && year % 100 == 0 && day > 28) ||
+							(month == 2 && year % 4 == 0 && day > 29) ||
+							(month == 2 && year % 4 != 0 && day > 28)) {
+							ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
+							break;
+						}
+
+						if (year > 0 && year < 100)
+							year += 2000;
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(10);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
+					}
+					break;
+				case SQL_TYPE_TIME:
+					{
+						int hour = 0, minute = 0, second = 0;
+						int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
+
+						if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
+							ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(8);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
+					}
+					break;
+				case SQL_TYPE_TIMESTAMP:
+					{
+						int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
+						int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
+
+						if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
+							month <= 0 || month > 12 || day < 0 || day > 31 ||
+							((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
+							(month == 2 && year % 400 == 0 && day > 29) ||
+							(month == 2 && year % 100 == 0 && day > 28) ||
+							(month == 2 && year % 4 == 0 && day > 29) ||
+							(month == 2 && year % 4 != 0 && day > 28) ||
+							hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
+							ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
+							break;
+						}
+
+						if (year > 0 && year < 100)
+							year += 2000;
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(19);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
+					}
+					break;
+				case SQL_INTEGER:
+					{
+						int integer = 0;
+						if (sscanf(colptr, "%d", &integer) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(12);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
+					}
+					break;
+				case SQL_BIGINT:
+					{
+						long long integer = 0;
+						if (sscanf(colptr, "%lld", &integer) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(24);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
+					}
+					break;
+				case SQL_SMALLINT:
+					{
+						short integer = 0;
+						if (sscanf(colptr, "%hd", &integer) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(6);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
+					}
+					break;
+				case SQL_TINYINT:
+					{
+						char integer = 0;
+						if (sscanf(colptr, "%hhd", &integer) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(4);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
+					}
+					break;
+				case SQL_BIT:
+					{
+						char integer = 0;
+						if (sscanf(colptr, "%hhd", &integer) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
+							break;
+						}
+						if (integer != 0)
+							integer = 1;
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(2);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
+					}
+					break;
+				case SQL_NUMERIC:
+				case SQL_DECIMAL:
+					{
+						double number = 0.0;
+						if (sscanf(colptr, "%lf", &number) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(entry->decimals);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
+					}
+					break;
+				case SQL_FLOAT:
+				case SQL_REAL:
+				case SQL_DOUBLE:
+					{
+						double number = 0.0;
+						if (sscanf(colptr, "%lf", &number) != 1) {
+							ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
+							break;
+						}
+
+						lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
+						LENGTHEN_BUF2(entry->decimals);
+						lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
+					}
+					break;
+				default:
+					ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
 				}
-
-				lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-				LENGTHEN_BUF2(strlen(colptr));
-
-				/* 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_TYPE_DATE:
-				{
-					int year = 0, month = 0, day = 0;
-					if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
-						month <= 0 || month > 12 || day < 0 || day > 31 ||
-						((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
-						(month == 2 && year % 400 == 0 && day > 29) ||
-						(month == 2 && year % 100 == 0 && day > 28) ||
-						(month == 2 && year % 4 == 0 && day > 29) ||
-						(month == 2 && year % 4 != 0 && day > 28)) {
-						ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
-						break;
-					}
-
-					if (year > 0 && year < 100)
-						year += 2000;
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(10);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
-				}
-				break;
-			case SQL_TYPE_TIME:
-				{
-					int hour = 0, minute = 0, second = 0;
-					int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
-
-					if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
-						ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(8);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
-				}
-				break;
-			case SQL_TYPE_TIMESTAMP:
-				{
-					int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
-					int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
-
-					if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
-						month <= 0 || month > 12 || day < 0 || day > 31 ||
-						((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
-						(month == 2 && year % 400 == 0 && day > 29) ||
-						(month == 2 && year % 100 == 0 && day > 28) ||
-						(month == 2 && year % 4 == 0 && day > 29) ||
-						(month == 2 && year % 4 != 0 && day > 28) ||
-						hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
-						ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
-						break;
-					}
-
-					if (year > 0 && year < 100)
-						year += 2000;
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(19);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
-				}
-				break;
-			case SQL_INTEGER:
-				{
-					int integer = 0;
-					if (sscanf(colptr, "%d", &integer) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(12);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
-				}
-				break;
-			case SQL_BIGINT:
-				{
-					long long integer = 0;
-					if (sscanf(colptr, "%lld", &integer) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(24);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
-				}
-				break;
-			case SQL_SMALLINT:
-				{
-					short integer = 0;
-					if (sscanf(colptr, "%hd", &integer) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(6);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
-				}
-				break;
-			case SQL_TINYINT:
-				{
-					char integer = 0;
-					if (sscanf(colptr, "%hhd", &integer) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(4);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
-				}
-				break;
-			case SQL_BIT:
-				{
-					char integer = 0;
-					if (sscanf(colptr, "%hhd", &integer) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
-						break;
-					}
-					if (integer != 0)
-						integer = 1;
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(2);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
-				}
-				break;
-			case SQL_NUMERIC:
-			case SQL_DECIMAL:
-				{
-					double number = 0.0;
-					if (sscanf(colptr, "%lf", &number) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(entry->decimals);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
-				}
-				break;
-			case SQL_FLOAT:
-			case SQL_REAL:
-			case SQL_DOUBLE:
-				{
-					double number = 0.0;
-					if (sscanf(colptr, "%lf", &number) != 1) {
-						ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
-						break;
-					}
-
-					lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
-					LENGTHEN_BUF2(entry->decimals);
-					lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
-				}
-				break;
-			default:
-				ast_log(LOG_WARNING, "Column type %d (field '%s') is unsupported at this time.\n", entry->type, entry->name);
 			}
 		}
-	}
-	AST_RWLIST_UNLOCK(&odbc_columns);
-
-	/* Concatenate the two constructed buffers */
-	LENGTHEN_BUF1(lensql2);
-	sql[lensql - 1] = ')';
-	sql2[lensql2 - 1] = ')';
-	strcat(sql + lensql, sql2);
+
+		/* Concatenate the two constructed buffers */
+		LENGTHEN_BUF1(lensql2);
+		sql[lensql - 1] = ')';
+		sql2[lensql2 - 1] = ')';
+		strcat(sql + lensql, sql2);
+
+		obj = ast_odbc_request_obj(tableptr->connection, 1);
+		if (obj) {
+			stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
+			if (stmt) {
+				SQLRowCount(stmt, &rows);
+				SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+			}
+			if (rows == 0) {
+				ast_log(LOG_WARNING, "cdr_custom_odbc: Insert failed on '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
+			}
+			ast_odbc_release_obj(obj);
+		} else {
+			ast_log(LOG_WARNING, "cdr_custom_odbc: Unable to retrieve database handle for '%s:%s'.  CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
+		}
+	}
+	AST_RWLIST_UNLOCK(&odbc_tables);
+
+	ast_free(sql);
 	ast_free(sql2);
-
-	obj = ast_odbc_request_obj(connection, 1);
-	if (obj) {
-		stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
-		if (stmt) {
-			SQLRowCount(stmt, &rows);
-			SQLFreeHandle(SQL_HANDLE_STMT, stmt);
-		}
-		if (rows == 0) {
-			ast_log(LOG_WARNING, "cdr_custom_odbc: Insert failed: %s\n", sql);
-		}
-		ast_odbc_release_obj(obj);
-	} else {
-		ast_log(LOG_WARNING, "cdr_custom_odbc: Unable to retrieve database handle.  CDR failed: %s\n", sql);
-	}
-
-	ast_free(sql);
 	return 0;
 }
 
@@ -479,40 +519,40 @@
 {
 	ast_cdr_unregister(name);
 	usleep(1);
-	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+	if (!AST_RWLIST_WRLOCK(&odbc_tables)) {
 		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);
+	AST_RWLIST_UNLOCK(&odbc_tables);
 	return 0;
 }
 
 static int load_module(void)
 {
-	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+	if (!AST_RWLIST_WRLOCK(&odbc_tables)) {
 		ast_log(LOG_ERROR, "Unable to lock column list.  Load failed.\n");
 		return 0;
 	}
 
 	load_config();
-	AST_RWLIST_UNLOCK(&odbc_columns);
+	AST_RWLIST_UNLOCK(&odbc_tables);
 	ast_cdr_register(name, ast_module_info->description, odbc_log);
 	return 0;
 }
 
 static int reload(void)
 {
-	if (!AST_RWLIST_WRLOCK(&odbc_columns)) {
+	if (!AST_RWLIST_WRLOCK(&odbc_tables)) {
 		ast_log(LOG_ERROR, "Unable to lock column list.  Reload failed.\n");
 		return -1;
 	}
 
 	free_config();
 	load_config();
-	AST_RWLIST_UNLOCK(&odbc_columns);
+	AST_RWLIST_UNLOCK(&odbc_tables);
 	return 0;
 }
 

Added: team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample
URL: http://svn.digium.com/view/asterisk/team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample?view=auto&rev=50222
==============================================================================
--- team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample (added)
+++ team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample Tue Jan  9 17:59:32 2007
@@ -1,0 +1,30 @@
+;
+; This configuration defines the connections and tables for which CDRs may
+; be populated.  Each context specifies a different CDR table to be used.
+;
+; The columns in the tables should match up word-for-word (case-insensitive)
+; to the CDR variables set in the dialplan.  The natural advantage to this
+; system is that beyond setting up the configuration file to tell you what
+; tables to look at, there isn't anything more to do beyond creating the
+; columns for the fields that you want, and populating the corresponding
+; CDR variables in the dialplan.
+;
+; Please note that after adding columns to the database, it is necessary to
+; reload this module to get the new column names and types read.
+;
+; Warning: if you specify two contexts with exactly the same connection and
+; table names, you will get duplicate records in that table.  So be careful.
+;
+
+[first]
+connection=mysql1
+table=cdr
+
+[second]
+connection=mysql1
+table=extracdr
+
+[third]
+connection=sqlserver
+table=AsteriskCDR
+

Propchange: team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/tilghman/cdr_custom_odbc/configs/cdr_custom_odbc.conf.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list