Fix Query::first (#71)
The Query::first fetched all records. Now the limit number is properly propagated to the SQL generators. - Optimized limited but not offseted queries in SqliteGenerator - Implemented limit and offset generation in the MySqlGenerator - Added test for limited data query
This commit is contained in:
parent
2f439541a4
commit
89c362f20b
|
|
@ -345,4 +345,15 @@ QString MySqlGenerator::createConditionalPhrase(const PhraseData *d) const
|
||||||
return SqlGeneratorBase::createConditionalPhrase(d);
|
return SqlGeneratorBase::createConditionalPhrase(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MySqlGenerator::appendSkipTake(QString &sql, int skip, int take)
|
||||||
|
{
|
||||||
|
if (take > 0 && skip > 0) {
|
||||||
|
sql.append(QString(" LIMIT %1 OFFSET %2")
|
||||||
|
.arg(take)
|
||||||
|
.arg(skip));
|
||||||
|
} else if (take > 0) {
|
||||||
|
sql.append(QString(" LIMIT %1").arg(take));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NUT_END_NAMESPACE
|
NUT_END_NAMESPACE
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,8 @@ public:
|
||||||
// QString phrase(const PhraseData *d) const;
|
// QString phrase(const PhraseData *d) const;
|
||||||
// QString selectCommand(AgregateType t, QString agregateArg, QString tableName, QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QList<RelationModel *> joins, int skip, int take);
|
// QString selectCommand(AgregateType t, QString agregateArg, QString tableName, QList<WherePhrase> &wheres, QList<WherePhrase> &orders, QList<RelationModel *> joins, int skip, int take);
|
||||||
QString createConditionalPhrase(const PhraseData *d) const override;
|
QString createConditionalPhrase(const PhraseData *d) const override;
|
||||||
|
void appendSkipTake(QString &sql, int skip, int take) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool readInsideParentese(QString &text, QString &out);
|
bool readInsideParentese(QString &text, QString &out);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -617,8 +617,6 @@ QString SqlGeneratorBase::selectCommand(const QString &tableName,
|
||||||
const int skip,
|
const int skip,
|
||||||
const int take)
|
const int take)
|
||||||
{
|
{
|
||||||
Q_UNUSED(skip);
|
|
||||||
Q_UNUSED(take);
|
|
||||||
QStringList joinedOrders;
|
QStringList joinedOrders;
|
||||||
QString selectText = agregateText(t, agregateArg);
|
QString selectText = agregateText(t, agregateArg);
|
||||||
QString whereText = createConditionalPhrase(where.data);
|
QString whereText = createConditionalPhrase(where.data);
|
||||||
|
|
|
||||||
|
|
@ -187,10 +187,13 @@ QStringList SqliteGenerator::diff(TableModel *oldTable, TableModel *newTable)
|
||||||
}
|
}
|
||||||
void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take)
|
void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take)
|
||||||
{
|
{
|
||||||
if (take != -1 && skip != -1)
|
if (take > 0 && skip > 0) {
|
||||||
sql.append(QString(" LIMIT %1 OFFSET %2")
|
sql.append(QString(" LIMIT %1 OFFSET %2")
|
||||||
.arg(take)
|
.arg(take)
|
||||||
.arg(skip));
|
.arg(skip));
|
||||||
|
} else if (take > 0) {
|
||||||
|
sql.append(QString(" LIMIT %1").arg(take));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqliteGenerator::primaryKeyConstraint(const TableModel *table) const
|
QString SqliteGenerator::primaryKeyConstraint(const TableModel *table) const
|
||||||
|
|
|
||||||
|
|
@ -169,14 +169,13 @@ Q_OUTOFLINE_TEMPLATE Query<T>::~Query()
|
||||||
template <class T>
|
template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
|
Q_OUTOFLINE_TEMPLATE RowList<T> Query<T>::toList(int count)
|
||||||
{
|
{
|
||||||
Q_UNUSED(count);
|
|
||||||
Q_D(Query);
|
Q_D(Query);
|
||||||
RowList<T> returnList;
|
RowList<T> returnList;
|
||||||
d->select = "*";
|
d->select = "*";
|
||||||
|
|
||||||
d->sql = d->database->sqlGenertor()->selectCommand(
|
d->sql = d->database->sqlGenertor()->selectCommand(
|
||||||
d->tableName, d->fieldPhrase, d->wherePhrase, d->orderPhrase,
|
d->tableName, d->fieldPhrase, d->wherePhrase, d->orderPhrase,
|
||||||
d->relations, d->skip, d->take);
|
d->relations, d->skip, count);
|
||||||
|
|
||||||
QSqlQuery q = d->database->exec(d->sql);
|
QSqlQuery q = d->database->exec(d->sql);
|
||||||
if (q.lastError().isValid()) {
|
if (q.lastError().isValid()) {
|
||||||
|
|
@ -372,7 +371,6 @@ template <class T>
|
||||||
Q_OUTOFLINE_TEMPLATE Row<T> Query<T>::first()
|
Q_OUTOFLINE_TEMPLATE Row<T> Query<T>::first()
|
||||||
{
|
{
|
||||||
skip(0);
|
skip(0);
|
||||||
take(1);
|
|
||||||
RowList<T> list = toList(1);
|
RowList<T> list = toList(1);
|
||||||
|
|
||||||
if (list.count())
|
if (list.count())
|
||||||
|
|
|
||||||
|
|
@ -225,6 +225,12 @@ void BasicTest::testDate()
|
||||||
QTEST_ASSERT(q->saveDate() == d);
|
QTEST_ASSERT(q->saveDate() == d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BasicTest::testLimitedQuery()
|
||||||
|
{
|
||||||
|
auto comments = db.comments()->query()->toList(2);
|
||||||
|
QTEST_ASSERT(comments.length() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
void BasicTest::join()
|
void BasicTest::join()
|
||||||
{
|
{
|
||||||
// TIC();
|
// TIC();
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ private slots:
|
||||||
void selectPostIds();
|
void selectPostIds();
|
||||||
void updatePostOnTheFly();
|
void updatePostOnTheFly();
|
||||||
void testDate();
|
void testDate();
|
||||||
|
void testLimitedQuery();
|
||||||
void selectWithInvalidRelation();
|
void selectWithInvalidRelation();
|
||||||
void modifyPost();
|
void modifyPost();
|
||||||
void emptyDatabase();
|
void emptyDatabase();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue