From 7f93a2e6d827924b1ecae81f33ef2b5acd4ab9b3 Mon Sep 17 00:00:00 2001 From: tilghman Date: Fri, 13 Jun 2008 21:50:28 +0000 Subject: Properly detect the size of char/varchar fields git-svn-id: http://svn.digium.com/svn/asterisk/trunk@122716 f38db490-d61c-443f-a65b-d21fe96a405b --- res/res_config_pgsql.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'res/res_config_pgsql.c') diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 79c9c1937..fe582f9d8 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -123,7 +123,7 @@ static struct tables *find_table(const char *tablename) ast_debug(1, "Table '%s' not found in cache, querying now\n", tablename); /* Not found, scan the table */ - ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc FROM pg_class c, pg_type t, pg_attribute a LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", tablename); + ast_str_set(&sql, 0, "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM pg_class c, pg_type t, pg_attribute a LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", tablename); result = PQexec(pgsqlConn, sql->str); ast_debug(1, "Query of table structure complete. Now retrieving results.\n"); if (PQresultStatus(result) != PGRES_TUPLES_OK) { @@ -159,7 +159,14 @@ static struct tables *find_table(const char *tablename) return NULL; } - sscanf(flen, "%d", &column->len); + if (strcmp(flen, "-1") == 0) { + /* Some types, like chars, have the length stored in a different field */ + flen = PQgetvalue(result, i, 5); + sscanf(flen, "%d", &column->len); + column->len -= 4; + } else { + sscanf(flen, "%d", &column->len); + } column->name = (char *)column + sizeof(*column); column->type = (char *)column + sizeof(*column) + strlen(fname) + 1; strcpy(column->name, fname); @@ -954,7 +961,14 @@ static int require_pgsql(const char *database, const char *tablename, va_list ap ast_log(LOG_WARNING, "Column '%s' may not be large enough for the required data length: %d\n", column->name, size); res = -1; } else if (type == RQ_CHAR || type == RQ_DATETIME || type == RQ_FLOAT || type == RQ_DATE) { - ast_log(LOG_WARNING, "Column '%s' is of the incorrect type: (need %s(%d) but saw %s)\n", column->name, type == RQ_CHAR ? "char" : type == RQ_DATETIME ? "datetime" : type == RQ_DATE ? "date" : type == RQ_FLOAT ? "float" : "a rather stiff drink ", size, column->type); + ast_log(LOG_WARNING, "Column '%s' is of the incorrect type: (need %s(%d) but saw %s)\n", + column->name, + type == RQ_CHAR ? "char" : + type == RQ_DATETIME ? "datetime" : + type == RQ_DATE ? "date" : + type == RQ_FLOAT ? "float" : + "a rather stiff drink ", + size, column->type); res = -1; } } else if (strncmp(column->type, "float", 5) == 0 && !ast_rq_is_int(type) && type != RQ_FLOAT) { -- cgit v1.2.3