[asterisk-commits] tilghman: branch tilghman/cdr_custom_odbc r49629 - /team/tilghman/cdr_custom_...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jan 4 18:26:34 MST 2007


Author: tilghman
Date: Thu Jan  4 19:26:33 2007
New Revision: 49629

URL: http://svn.digium.com/view/asterisk?view=rev&rev=49629
Log:
Flesh out the various types into SQL

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=49629&r1=49628&r2=49629
==============================================================================
--- team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c (original)
+++ team/tilghman/cdr_custom_odbc/cdr/cdr_custom_odbc.c Thu Jan  4 19:26:33 2007
@@ -82,7 +82,7 @@
 	char name[80];
 	SQLLEN sqlptr;
 	int res;
-	SQLHSTMT stmt;
+	SQLHSTMT stmt = NULL;
 
 	cfg = ast_config_load(CONFIG);
 	if (!cfg) {
@@ -115,6 +115,13 @@
 		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");
+		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)) {
@@ -153,6 +160,61 @@
 	return 0;
 }
 
+static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
+{
+	int res;
+	char *sql = data;
+	SQLHSTMT stmt;
+
+	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");
+		return NULL;
+	}
+
+	res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
+	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+		ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
+		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		return NULL;
+	}
+
+	return stmt;
+}
+
+#define LENGTHEN_BUF1(size)														\
+			do {																\
+				/* Lengthen buffer, if necessary */								\
+				if ((newsize = lensql + (size) + 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;												\
+					}															\
+				}																\
+			} while (0)
+
+#define LENGTHEN_BUF2(size)														\
+			do {																\
+				if ((newsize = lensql2 + (size) + 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;												\
+					}															\
+				}																\
+			} while (0)
+
 static int odbc_log(struct ast_cdr *cdr)
 {
 	struct columns *entry;
@@ -161,9 +223,11 @@
 	char *sql = ast_calloc(1, 512), *sql2 = ast_calloc(1, 512), *tmp;
 	int lensql, lensql2, sizesql = 512, sizesql2 = 512, newsize;
 	char colbuf[1024], *colptr;
+	SQLHSTMT stmt = NULL;
+	SQLINTEGER rows = 0;
 
 	lensql = snprintf(sql, sizesql, "INSERT INTO %s (", table);
-	lensql2 = snprintf(sql2, sizesql2, "VALUES (");
+	lensql2 = snprintf(sql2, sizesql2, " VALUES (");
 
 	if (!AST_RWLIST_RDLOCK(&odbc_columns)) {
 		ast_log(LOG_ERROR, "Unable to lock column list.  Insert CDR failed.\n");
@@ -175,6 +239,8 @@
 		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:
@@ -184,36 +250,13 @@
 			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;
-					}
-				}
+				if (entry->type != SQL_GUID) {
+					if (strlen(colptr) > entry->octetlen)
+						colptr[entry->octetlen] = '\0';
+				}
+
 				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;
-					}
-				}
+				LENGTHEN_BUF2(strlen(colptr));
 
 				/* Encode value, with escaping */
 				strcpy(sql2 + lensql2, "'");
@@ -233,48 +276,194 @@
 				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:
-			case SQL_BIGINT:
+				{
+					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:
-
-			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:
+				{
+					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') unsupported at this time.\n", entry->type, entry->name);
+				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);
+	ast_free(sql2);
 
 	obj = ast_odbc_request_obj(connection, 1);
-
-	ast_odbc_release_obj(obj);
-	AST_RWLIST_UNLOCK(&odbc_columns);
+	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;
 }
 



More information about the asterisk-commits mailing list