diff options
author | Andy Shaw <andy.shaw@digia.com> | 2012-02-03 22:35:14 (GMT) |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-12 11:37:25 (GMT) |
commit | a570ad0ced6f91eb878f90df2731ceba8f8cfe9f (patch) | |
tree | ac202ac6475c711d6d01200f72728bbd4619d70a /src/sql/drivers/sqlite | |
parent | bb2f051d7443a462bec8c47225aeb62bc0f76bf8 (diff) | |
download | Qt-a570ad0ced6f91eb878f90df2731ceba8f8cfe9f.zip Qt-a570ad0ced6f91eb878f90df2731ceba8f8cfe9f.tar.gz Qt-a570ad0ced6f91eb878f90df2731ceba8f8cfe9f.tar.bz2 |
Improve type detection for query results
When an aggregate function is used for a column in a SQL resultset then
it should ensure that the right data type is reported for that column.
This also concerns expressions when the returned column does not map
directly to a table column.
Test included for this.
Task-number: QTBUG-22038
Change-Id: I681297accc979081d14b44d190ab9d5f83aac215
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com>
(cherry picked from commit 678ab52ccba0b6a6903e9aee404dbe84fb74c91d)
Diffstat (limited to 'src/sql/drivers/sqlite')
-rw-r--r-- | src/sql/drivers/sqlite/qsql_sqlite.cpp | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/src/sql/drivers/sqlite/qsql_sqlite.cpp b/src/sql/drivers/sqlite/qsql_sqlite.cpp index cc11770..97d3531 100644 --- a/src/sql/drivers/sqlite/qsql_sqlite.cpp +++ b/src/sql/drivers/sqlite/qsql_sqlite.cpp @@ -170,12 +170,37 @@ void QSQLiteResultPrivate::initColumns(bool emptyResultset) // must use typeName for resolving the type to match QSqliteDriver::record QString typeName = QString(reinterpret_cast<const QChar *>( sqlite3_column_decltype16(stmt, i))); - - int dotIdx = colName.lastIndexOf(QLatin1Char('.')); - QSqlField fld(colName.mid(dotIdx == -1 ? 0 : dotIdx + 1), qGetColumnType(typeName)); - // sqlite3_column_type is documented to have undefined behavior if the result set is empty int stp = emptyResultset ? -1 : sqlite3_column_type(stmt, i); + + QVariant::Type fieldType; + + if (!typeName.isEmpty()) { + fieldType = qGetColumnType(typeName); + } else { + // Get the proper type for the field based on stp value + switch (stp) { + case SQLITE_INTEGER: + fieldType = QVariant::Int; + break; + case SQLITE_FLOAT: + fieldType = QVariant::Double; + break; + case SQLITE_BLOB: + fieldType = QVariant::ByteArray; + break; + case SQLITE_TEXT: + fieldType = QVariant::String; + break; + case SQLITE_NULL: + default: + fieldType = QVariant::Invalid; + break; + } + } + + int dotIdx = colName.lastIndexOf(QLatin1Char('.')); + QSqlField fld(colName.mid(dotIdx == -1 ? 0 : dotIdx + 1), fieldType); fld.setSqlType(stp); rInf.append(fld); } |