Skip to content

Commit dd22022

Browse files
israelins85The Qt Project
authored and
The Qt Project
committed
ODBC: fix loss of milliseconds when binding QDateTime
Caused by operator precedence error. Follow-up to commit b8b79a0f37ec74fd5b4ad829e522a384ba3622ae Task-number: QTBUG-2192 Change-Id: I17decd18c469b48a0bc938ae05c16cced8042219 Reviewed-by: Mark Brand <[email protected]> (cherry-picked from qtbase commit 36b6d4afc970a328cced87af3d39b70d327eb3ad) Reviewed-by: Andy Shaw <[email protected]>
1 parent 488cb66 commit dd22022

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/sql/drivers/odbc/qsql_odbc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ bool QODBCResult::exec()
14131413

14141414
// (How many leading digits do we want to keep? With SQL Server 2005, this should be 3: 123000000)
14151415
int keep = (int)qPow(10.0, 9 - qMin(9, precision));
1416-
dt->fraction /= keep * keep;
1416+
dt->fraction = (dt->fraction / keep) * keep;
14171417
}
14181418

14191419
r = SQLBindParameter(d->hStmt,

tests/auto/qsqlquery/tst_qsqlquery.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,14 +3294,19 @@ void tst_QSqlQuery::QTBUG_2192()
32943294
QSqlQuery q(db);
32953295
QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (dt DATETIME)"));
32963296

3297+
QDateTime dt = QDateTime(QDate(2012, 7, 4), QTime(23, 59, 59, 999));
32973298
QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " (dt) VALUES (?)"));
3298-
q.bindValue(0, QVariant(QDateTime(QDate(2012, 7, 4), QTime(23, 59, 59, 999))));
3299+
q.bindValue(0, dt);
32993300
QVERIFY_SQL(q, exec());
33003301

3301-
// Check if value was stored with at least second precision.
33023302
QVERIFY_SQL(q, exec("SELECT dt FROM " + tableName));
33033303
QVERIFY_SQL(q, next());
3304-
QVERIFY(q.value(0).toDateTime().msecsTo(QDateTime(QDate(2012, 7, 4), QTime(23, 59, 59, 999))) < 1000 );
3304+
3305+
// Check if retrieved value preserves reported precision
3306+
int precision = qMax(0, q.record().field("dt").precision());
3307+
int diff = qAbs(q.value(0).toDateTime().msecsTo(dt));
3308+
int keep = qMin(1000, (int)qPow(10.0, precision));
3309+
QVERIFY(diff <= 1000 - keep);
33053310
}
33063311
}
33073312

0 commit comments

Comments
 (0)