QTBUG-1363: fix QSqlField.length() for ODBC

Applied Bill King's suggestion in QTBUG-1363. Columns of
hStmt must be accessed in order.

Verified using ODBC driver on SQL Server 2005 on Windows 7.

Added test for length of text field for MS SQL Server over ODBC.

Task-Id: QTBUG-1363
Change-Id: I6673dafe75e3ef394d41e439adb45096c1421068
Reviewed-by: Bill King <bill.king@nokia.com>
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
Mark Brand 2012-04-12 12:49:03 +02:00 committed by Qt by Nokia
parent 13ebff81d0
commit cf52540dfb
2 changed files with 9 additions and 5 deletions

View File

@ -573,6 +573,11 @@ static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, const QODBCDriverPrivate*
QString fname = qGetStringData(hStmt, 3, -1, p->unicode);
int type = qGetIntData(hStmt, 4).toInt(); // column type
QSqlField f(fname, qDecodeODBCType(type, p));
QVariant var = qGetIntData(hStmt, 6);
f.setLength(var.isNull() ? -1 : var.toInt()); // column size
var = qGetIntData(hStmt, 8).toInt();
f.setPrecision(var.isNull() ? -1 : var.toInt()); // precision
f.setSqlType(type);
int required = qGetIntData(hStmt, 10).toInt(); // nullable-flag
// required can be SQL_NO_NULLS, SQL_NULLABLE or SQL_NULLABLE_UNKNOWN
if (required == SQL_NO_NULLS)
@ -580,11 +585,6 @@ static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, const QODBCDriverPrivate*
else if (required == SQL_NULLABLE)
f.setRequired(false);
// else we don't know
QVariant var = qGetIntData(hStmt, 6);
f.setLength(var.isNull() ? -1 : var.toInt()); // column size
var = qGetIntData(hStmt, 8).toInt();
f.setPrecision(var.isNull() ? -1 : var.toInt()); // precision
f.setSqlType(type);
return f;
}

View File

@ -129,6 +129,10 @@ void tst_QSqlDriver::record()
QSqlRecord rec = db.driver()->record(tablename);
QCOMPARE(rec.count(), 4);
// QTBUG-1363: QSqlField::length() always return -1 when using QODBC3 driver and QSqlDatabase::record()
if (db.driverName().startsWith("QODBC") && tst_Databases::isSqlServer(db))
QCOMPARE(rec.field(1).length(), 20);
if (db.driverName().startsWith("QIBASE")|| db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
for(int i = 0; i < fields.count(); ++i)
fields[i] = fields[i].toUpper();