[QSqlQuery] misbehavior of seek in special query positions
When QSqlQuery::at() == QSql::BeforeFirstRow and seek(1, true) (seek to next record) is called the expected result is go to first row. When QSqlQuery::at() == QSql::AfterLastRow and seek(-1, true) (seek to previous record) is called the expected result is go to last row. But in all cases the first and last are skipped. Change-Id: I584138b3d397ce1c790bf89688ee92289a99611c Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
parent
a2ad5cf1aa
commit
3e6e70bddd
@ -511,12 +511,23 @@ const QSqlResult* QSqlQuery::result() const
|
||||
|
||||
\list
|
||||
|
||||
\li If the result is currently positioned before the first record or
|
||||
on the first record, and \a index is negative, there is no change,
|
||||
and false is returned.
|
||||
\li If the result is currently positioned before the first record and:
|
||||
\list
|
||||
\li \a index is negative or zero, there is no change, and false is
|
||||
returned.
|
||||
\li \a index is positive, an attempt is made to position the result
|
||||
at absolute position \a index - 1, following the sames rule for non
|
||||
relative seek, above.
|
||||
\endlist
|
||||
|
||||
\li If the result is currently located after the last record, and \a
|
||||
index is positive, there is no change, and false is returned.
|
||||
\li If the result is currently positioned after the last record and:
|
||||
\list
|
||||
\li \a index is positive or zero, there is no change, and false is
|
||||
returned.
|
||||
\li \a index is negative, an attempt is made to position the result
|
||||
at \a index + 1 relative position from last record, following the
|
||||
rule below.
|
||||
\endlist
|
||||
|
||||
\li If the result is currently located somewhere in the middle, and
|
||||
the relative offset \a index moves the result below zero, the result
|
||||
@ -549,7 +560,7 @@ bool QSqlQuery::seek(int index, bool relative)
|
||||
switch (at()) { // relative seek
|
||||
case QSql::BeforeFirstRow:
|
||||
if (index > 0)
|
||||
actualIdx = index;
|
||||
actualIdx = index - 1;
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
@ -557,7 +568,7 @@ bool QSqlQuery::seek(int index, bool relative)
|
||||
case QSql::AfterLastRow:
|
||||
if (index < 0) {
|
||||
d->sqlResult->fetchLast();
|
||||
actualIdx = at() + index;
|
||||
actualIdx = at() + index + 1;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
@ -1247,6 +1247,26 @@ void tst_QSqlQuery::seek()
|
||||
QVERIFY( q.seek( 0 ) );
|
||||
QCOMPARE( q.at(), 0 );
|
||||
QCOMPARE( q.value( 0 ).toInt(), 1 );
|
||||
|
||||
QVERIFY(!q.seek(QSql::BeforeFirstRow));
|
||||
QCOMPARE(q.at(), int(QSql::BeforeFirstRow));
|
||||
QVERIFY(q.seek(1, true));
|
||||
QCOMPARE(q.at(), 0);
|
||||
QCOMPARE(q.value(0).toInt(), 1);
|
||||
|
||||
qint32 count = 1;
|
||||
while (q.next()) ++count;
|
||||
|
||||
QCOMPARE(q.at(), int(QSql::AfterLastRow));
|
||||
|
||||
if (!q.isForwardOnly()) {
|
||||
QVERIFY(q.seek(-1, true));
|
||||
QCOMPARE(q.at(), count - 1);
|
||||
QCOMPARE(q.value(0).toInt(), count);
|
||||
} else {
|
||||
QVERIFY(!q.seek(-1, true));
|
||||
QCOMPARE(q.at(), int(QSql::AfterLastRow));
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QSqlQuery::seekForwardOnlyQuery()
|
||||
|
Loading…
x
Reference in New Issue
Block a user