[svn-commits] tilghman: trunk r650 - /trunk/res/res_config_mysql.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jul 31 15:42:46 CDT 2008


Author: tilghman
Date: Thu Jul 31 15:42:46 2008
New Revision: 650

URL: http://svn.digium.com/view/asterisk-addons?view=rev&rev=650
Log:
Permit altering column sizes while running, if so configured (so that columns aren't
trimmed).

Modified:
    trunk/res/res_config_mysql.c

Modified: trunk/res/res_config_mysql.c
URL: http://svn.digium.com/view/asterisk-addons/trunk/res/res_config_mysql.c?view=diff&rev=650&r1=649&r2=650
==============================================================================
--- trunk/res/res_config_mysql.c (original)
+++ trunk/res/res_config_mysql.c Thu Jul 31 15:42:46 2008
@@ -100,6 +100,7 @@
 static char *handle_cli_realtime_mysql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *handle_cli_realtime_mysql_cache(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static int load_mysql_config(struct ast_config *config, const char *category, struct mysql_conn *conn);
+static int require_mysql(const char *database, const char *tablename, va_list ap);
 
 static enum { RQ_WARN, RQ_CREATECLOSE, RQ_CREATECHAR } requirements = RQ_WARN;
 
@@ -107,6 +108,16 @@
 	AST_CLI_DEFINE(handle_cli_realtime_mysql_status, "Shows connection information for the MySQL RealTime driver"),
 	AST_CLI_DEFINE(handle_cli_realtime_mysql_cache, "Shows cached tables within the MySQL realtime driver"),
 };
+
+static int internal_require(const char *database, const char *table, ...)
+{
+	va_list ap;
+	int res;
+	va_start(ap, table);
+	res = require_mysql(database, table, ap);
+	va_end(ap);
+	return res;
+}
 
 static void destroy_table(struct tables *table)
 {
@@ -209,6 +220,19 @@
 	ast_mutex_lock(&table->lock);
 	AST_LIST_UNLOCK(&mysql_tables);
 	return table;
+}
+
+static struct columns *find_column(struct tables *table, const char *colname)
+{
+	struct columns *column;
+
+	AST_LIST_TRAVERSE(&table->columns, column, list) {
+		if (strcmp(column->name, colname) == 0) {
+			break;
+		}
+	}
+
+	return column;
 }
 
 static struct ast_variable *realtime_mysql(const char *database, const char *table, va_list ap)
@@ -489,13 +513,7 @@
 	}
 
 	/* Check that the column exists in the table */
-	AST_LIST_TRAVERSE(&table->columns, column, list) {
-		if (strcmp(column->name, newparam) == 0) {
-			break;
-		}
-	}
-
-	if (!column) {
+	if (!(column = find_column(table, newparam))) {
 		ast_log(LOG_ERROR, "PostgreSQL RealTime: Updating on column '%s', but that column does not exist within the table '%s'!\n", newparam, tablename);
 		ast_mutex_unlock(&table->lock);
 		ast_free(sql);
@@ -522,17 +540,16 @@
 	mysql_real_escape_string(&dbwrite.handle, buf->str, newval, valsz);
 	ast_str_set(&sql, 0, "UPDATE %s SET %s = '%s'", tablename, newparam, buf->str);
 
+	/* If the column length isn't long enough, give a chance to lengthen it. */
+	if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
+		internal_require(database, tablename, newparam, RQ_CHAR, buf->used);
+	}
+
 	while ((newparam = va_arg(ap, const char *))) {
 		newval = va_arg(ap, const char *);
 
 		/* If the column is not within the table, then skip it */
-		AST_LIST_TRAVERSE(&table->columns, column, list) {
-			if (strcmp(column->name, newparam) == 0) {
-				break;
-			}
-		}
-
-		if (!column) {
+		if (!(column = find_column(table, newparam))) {
 			ast_log(LOG_WARNING, "Attempted to update column '%s' in table '%s', but column does not exist!\n", newparam, tablename);
 			continue;
 		}
@@ -542,6 +559,11 @@
 		}
 		mysql_real_escape_string(&dbwrite.handle, buf->str, newval, valsz);
 		ast_str_append(&sql, 0, ", %s = '%s'", newparam, buf->str);
+
+		/* If the column length isn't long enough, give a chance to lengthen it. */
+		if (strncmp(column->type, "char", 4) == 0 || strncmp(column->type, "varchar", 7) == 0) {
+			internal_require(database, tablename, newparam, RQ_CHAR, buf->used);
+		}
 	}
 	va_end(ap);
 	ast_mutex_unlock(&table->lock);




More information about the svn-commits mailing list