From fcfb9ebccd9bad16777b092909881a2c4d035544 Mon Sep 17 00:00:00 2001 From: gambas Date: Wed, 13 Sep 2017 17:21:44 +0200 Subject: [PATCH] Correctly detect the datatype of query columns that are expressions. [GB.DB.SQLITE3] * BUG: Correctly detect the datatype of query columns that are expressions. --- gb.db.sqlite3/src/helper.c | 45 +++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/gb.db.sqlite3/src/helper.c b/gb.db.sqlite3/src/helper.c index 9b71bc947..c50610390 100644 --- a/gb.db.sqlite3/src/helper.c +++ b/gb.db.sqlite3/src/helper.c @@ -153,16 +153,15 @@ GB_TYPE sqlite_get_type(const char *type, int *length) if (length) *length = 0; + if (!type || !*type) + return GB_T_STRING; + upper = GB.NewZeroString(type); for (i = 0; i < GB.StringLength(upper); i++) upper[i] = toupper(upper[i]); - type = upper; - if (!type || !*type) - { - gtype = GB_T_STRING; - } - else if (strstr(type, "CHAR(") /* note the opening bracket */ + + if (strstr(type, "CHAR(") /* note the opening bracket */ || strstr(type, "CLOB") || strstr(type, "TEXT") /* also catches TINYTEXT */ || strstr(type, "VARCHAR") || strstr(type, "VARYING CHAR") || strstr(type, "ENUM") || strstr(type, "SET") || strstr(type, "YEAR")) @@ -259,6 +258,9 @@ static int my_sqlite3_exec( int nRetry = 0; int nCallback; int i; + const char *decltype; + int n_unknown = 0; + int type; //fprintf(stderr, "my_sqlite3_exec: %s\n", zSql); @@ -312,7 +314,19 @@ static int my_sqlite3_exec( if (need_types) { for (i = 0; i < ncol; i++) - result->types[i] = sqlite_get_type(sqlite3_column_decltype(pStmt, i), &result->lengths[i]); + { + decltype = sqlite3_column_decltype(pStmt, i); + if (!decltype) + { + result->types[i] = 0; + result->lengths[i] = 0; + n_unknown++; + } + else + { + result->types[i] = (int)sqlite_get_type(decltype, &result->lengths[i]); + } + } } nCallback++; @@ -328,7 +342,22 @@ static int my_sqlite3_exec( for (i = 0; i < ncol; i++) { - if (sqlite3_column_type(pStmt, i) == SQLITE_BLOB) + type = sqlite3_column_type(pStmt, i); + + if (n_unknown && result->types[i] == 0) + { + switch(type) + { + case SQLITE_INTEGER: result->types[i] = GB_T_INTEGER; break; + case SQLITE_FLOAT: result->types[i] = GB_T_FLOAT; break; + case SQLITE_BLOB: result->types[i] = (int)DB_T_BLOB; break; + default: result->types[i] = GB_T_STRING; + } + + n_unknown--; + } + + if (type == SQLITE_BLOB) value = (char *)sqlite3_column_blob(pStmt, i); else value = (char *)sqlite3_column_text(pStmt, i);