From f7ef6d9878b8a0ff2b353a6a35d09f140adbe86a Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Tue, 11 Aug 2020 11:14:45 +0430 Subject: [PATCH] Movable query (#107) Query and BulkInserter are now movable --- src/nut/abstractquery.cpp | 43 ----- src/nut/abstractquery.h | 53 ------ src/nut/abstractquery_p.h | 71 ------- src/nut/bulkinserter.cpp | 54 ++++-- src/nut/bulkinserter.h | 13 +- src/nut/bulkinserter_p.h | 23 +++ src/nut/database.cpp | 6 +- src/nut/nut.pro | 6 +- src/nut/query.h | 204 +++++++++++++-------- src/nut/tableset.h | 15 +- tests/auto/tst_basic/tst_basic.cpp | 85 +++++---- tests/auto/tst_datatypes/tst_datatypes.cpp | 10 +- tests/auto/tst_datetime/tst_datetime.cpp | 50 ++--- tests/auto/tst_json/tst_json.cpp | 6 +- tests/auto/tst_quuid/tst_uuid.cpp | 4 +- tests/auto/tst_upgrades/tst_upgrades.cpp | 4 +- 16 files changed, 290 insertions(+), 357 deletions(-) delete mode 100644 src/nut/abstractquery.cpp delete mode 100644 src/nut/abstractquery.h delete mode 100644 src/nut/abstractquery_p.h create mode 100644 src/nut/bulkinserter_p.h diff --git a/src/nut/abstractquery.cpp b/src/nut/abstractquery.cpp deleted file mode 100644 index 2ea87ed..0000000 --- a/src/nut/abstractquery.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/************************************************************************** -** -** This file is part of Nut project. -** https://github.com/HamedMasafi/Nut -** -** Nut is free software: you can redistribute it and/or modify -** it under the terms of the GNU Lesser General Public License as published by -** the Free Software Foundation, either version 3 of the License, or -** (at your option) any later version. -** -** Nut is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU Lesser General Public License for more details. -** -** You should have received a copy of the GNU Lesser General Public License -** along with Nut. If not, see . -** -**************************************************************************/ - -#include "abstractquery.h" -#include "abstractquery_p.h" - -NUT_BEGIN_NAMESPACE - -AbstractQuery::AbstractQuery(QObject *parent) : QObject(parent) - , d_ptr(new AbstractQueryPrivate(this)) -{ - -} - -Nut::AbstractQueryPrivate::AbstractQueryPrivate(Nut::AbstractQuery *parent) : - q_ptr(parent), skip(0), take(0) -{ - -} - -Nut::AbstractQueryPrivate::~AbstractQueryPrivate() -{ - -} - -NUT_END_NAMESPACE diff --git a/src/nut/abstractquery.h b/src/nut/abstractquery.h deleted file mode 100644 index 8618e72..0000000 --- a/src/nut/abstractquery.h +++ /dev/null @@ -1,53 +0,0 @@ -/************************************************************************** -** -** This file is part of Nut project. -** https://github.com/HamedMasafi/Nut -** -** Nut is free software: you can redistribute it and/or modify -** it under the terms of the GNU Lesser General Public License as published by -** the Free Software Foundation, either version 3 of the License, or -** (at your option) any later version. -** -** Nut is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU Lesser General Public License for more details. -** -** You should have received a copy of the GNU Lesser General Public License -** along with Nut. If not, see . -** -**************************************************************************/ - -#ifndef NUT_ABSTRACTQUERY_H -#define NUT_ABSTRACTQUERY_H - -#include -#include -#include - -#include "defines.h" -#include "abstractquery_p.h" - -NUT_BEGIN_NAMESPACE - -class AbstractQueryPrivate; -class NUT_EXPORT AbstractQuery : public QObject -{ - Q_OBJECT - -protected: - AbstractQueryPrivate *d_ptr; - Q_DECLARE_PRIVATE(AbstractQuery) - -public: - explicit AbstractQuery(QObject *parent = nullptr); - -protected: - // void addTableToSet(TableSetBase *set, Table *table); - -public slots: -}; - -NUT_END_NAMESPACE - -#endif // NUT_ABSTRACTQUERY_H diff --git a/src/nut/abstractquery_p.h b/src/nut/abstractquery_p.h deleted file mode 100644 index e432869..0000000 --- a/src/nut/abstractquery_p.h +++ /dev/null @@ -1,71 +0,0 @@ -/************************************************************************** -** -** This file is part of Nut project. -** https://github.com/HamedMasafi/Nut -** -** Nut is free software: you can redistribute it and/or modify -** it under the terms of the GNU Lesser General Public License as published by -** the Free Software Foundation, either version 3 of the License, or -** (at your option) any later version. -** -** Nut is distributed in the hope that it will be useful, -** but WITHOUT ANY WARRANTY; without even the implied warranty of -** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -** GNU Lesser General Public License for more details. -** -** You should have received a copy of the GNU Lesser General Public License -** along with Nut. If not, see . -** -**************************************************************************/ - -#ifndef NUT_QUERY_P_H -#define NUT_QUERY_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists for the convenience -// of qapplication_*.cpp, qwidget*.cpp and qfiledialog.cpp. This header -// file may change from version to version without notice, or even be removed. -// -// We mean it. -// - -#include "phrase.h" - -#include -#include -#include - -NUT_BEGIN_NAMESPACE - -class Database; -class AbstractTableSet; -class AbstractQuery; -struct RelationModel; -class NUT_EXPORT AbstractQueryPrivate { - AbstractQuery *q_ptr; - Q_DECLARE_PUBLIC(AbstractQuery) - -public: - explicit AbstractQueryPrivate(AbstractQuery *parent); - ~AbstractQueryPrivate(); - - QString sql; - QString className; - QString tableName; - QString select; - Database *database; - AbstractTableSet *tableSet; - QStringList joins; - QList relations; - int skip; - int take; - PhraseList orderPhrase, fieldPhrase; - ConditionalPhrase wherePhrase; -}; - -NUT_END_NAMESPACE - -#endif // NUT_QUERY_P_H diff --git a/src/nut/bulkinserter.cpp b/src/nut/bulkinserter.cpp index 2e09278..059bb87 100644 --- a/src/nut/bulkinserter.cpp +++ b/src/nut/bulkinserter.cpp @@ -1,4 +1,6 @@ #include "bulkinserter.h" +#include "bulkinserter_p.h" + #include "phrases/phraselist.h" #include "database.h" #include "abstractsqlgenerator.h" @@ -6,23 +8,47 @@ #include -Nut::BulkInserter::BulkInserter(Nut::Database *db, QString &className) - : _database(db), _fieldCount(0) +NUT_BEGIN_NAMESPACE + +BulkInserterPrivate::BulkInserterPrivate(Database *db) + : database(db), fieldCount(0) { + +} + +BulkInserter::BulkInserter(Database *db, QString &className) + : d_ptr(new BulkInserterPrivate(db)) +{ + Q_D(BulkInserter); + foreach (TableModel *m, db->model()) if (m->className() == className) - _className = m->name(); + d->className = m->name(); } -void Nut::BulkInserter::setFields(const Nut::PhraseList &ph) +BulkInserter::BulkInserter(const BulkInserter &other) { - _fields = ph; - _fieldCount = static_cast(ph.data.count()); + d_ptr = other.d_ptr; } -void Nut::BulkInserter::insert(std::initializer_list vars) +BulkInserter::BulkInserter(BulkInserter &&other) { - if (vars.size() != _fieldCount) { + d_ptr = other.d_ptr; + other.d_ptr = nullptr; +} + +void BulkInserter::setFields(const PhraseList &ph) +{ + Q_D(BulkInserter); + d->fields = ph; + d->fieldCount = static_cast(ph.data.count()); +} + +void BulkInserter::insert(std::initializer_list vars) +{ + Q_D(BulkInserter); + + if (vars.size() != d->fieldCount) { qInfo("Number of rows mistake"); return; } @@ -31,13 +57,17 @@ void Nut::BulkInserter::insert(std::initializer_list vars) std::initializer_list::iterator it; for (it = vars.begin(); it != vars.end(); ++it) list.append(*it); - variants.append(list); + d->variants.append(list); } -int Nut::BulkInserter::apply() +int BulkInserter::apply() { - auto sql = _database->sqlGenerator()->insertBulk(_className, _fields, variants); - QSqlQuery q = _database->exec(sql); + Q_D(BulkInserter); + auto sql = d->database->sqlGenerator()->insertBulk(d->className, + d->fields, + d->variants); + QSqlQuery q = d->database->exec(sql); return q.numRowsAffected(); } +NUT_END_NAMESPACE diff --git a/src/nut/bulkinserter.h b/src/nut/bulkinserter.h index 7ba47c6..ae837f6 100644 --- a/src/nut/bulkinserter.h +++ b/src/nut/bulkinserter.h @@ -12,16 +12,16 @@ NUT_BEGIN_NAMESPACE class PhraseList; class Database; +class BulkInserterPrivate; class NUT_EXPORT BulkInserter { - Database *_database; - QString _className; - Nut::PhraseList _fields; - QList variants; - size_t _fieldCount; + Q_DECLARE_PRIVATE(BulkInserter); public: BulkInserter(Database *db, QString &className); + BulkInserter(const BulkInserter &other); + BulkInserter(BulkInserter &&other); + void setFields(const PhraseList &ph); void insert(std::initializer_list vars); @@ -30,6 +30,9 @@ public: insert({args...}); } int apply(); + +private: + BulkInserterPrivate *d_ptr; }; NUT_END_NAMESPACE diff --git a/src/nut/bulkinserter_p.h b/src/nut/bulkinserter_p.h new file mode 100644 index 0000000..41e7cfa --- /dev/null +++ b/src/nut/bulkinserter_p.h @@ -0,0 +1,23 @@ +#ifndef BULKINSERTER_P_H +#define BULKINSERTER_P_H + +#include + +NUT_BEGIN_NAMESPACE + +class Database; +class BulkInserterPrivate +{ +public: + BulkInserterPrivate(Database *db); + + Database *database; + QString className; + PhraseList fields; + QList variants; + size_t fieldCount; +}; + +NUT_END_NAMESPACE + +#endif // BULKINSERTER_P_H diff --git a/src/nut/database.cpp b/src/nut/database.cpp index b9776fe..a7c8b22 100644 --- a/src/nut/database.cpp +++ b/src/nut/database.cpp @@ -281,10 +281,8 @@ bool DatabasePrivate::getCurrectSchema() DatabaseModel DatabasePrivate::getLastSchema() { Row u = changeLogs->query() - ->orderBy(!ChangeLogTable::idField()) - ->first(); - -// DatabaseModel ret(q->metaObject()->className()); + .orderBy(!ChangeLogTable::idField()) + .first(); if (u) { QJsonParseError e; diff --git a/src/nut/nut.pro b/src/nut/nut.pro index b6a4057..0b4bdeb 100644 --- a/src/nut/nut.pro +++ b/src/nut/nut.pro @@ -48,8 +48,7 @@ HEADERS += \ $$PWD/phrases/phraselist.h \ $$PWD/phrases/datephrase.h \ $$PWD/table_p.h \ - $$PWD/abstractquery.h \ - $$PWD/abstractquery_p.h + bulkinserter_p.h SOURCES += \ $$PWD/generators/abstractsqlgenerator.cpp \ @@ -78,8 +77,7 @@ SOURCES += \ $$PWD/phrases/phrasedata.cpp \ $$PWD/phrases/phrasedatalist.cpp \ $$PWD/phrases/phraselist.cpp \ - $$PWD/phrases/datephrase.cpp \ - $$PWD/abstractquery.cpp + $$PWD/phrases/datephrase.cpp load(qt_module) diff --git a/src/nut/query.h b/src/nut/query.h index 8cf076d..743b6c1 100644 --- a/src/nut/query.h +++ b/src/nut/query.h @@ -36,8 +36,6 @@ #endif #include -#include - #include #include #include @@ -48,34 +46,78 @@ NUT_BEGIN_NAMESPACE +struct NUT_EXPORT QueryData { + QString sql; + QString className; + QString tableName; + QString select; + Database *database; + AbstractTableSet *tableSet; + QStringList joins; + QList relations; + int skip; + int take; + PhraseList orderPhrase, fieldPhrase; + ConditionalPhrase wherePhrase; + + QueryData *clone() { + auto r = new QueryData; + r->sql = sql; + r->className = className; + r->tableName = tableName; + r->select = select; + r->database = database; + r->tableSet = tableSet; + r->joins = joins; + r->relations = relations; + r->skip = skip; + r->take = take; + r->orderPhrase = orderPhrase; + r->fieldPhrase = fieldPhrase; + r->wherePhrase = wherePhrase; + return r; + } + + QueryData() : skip(0), take(0) + { } + + QueryData(Database *db) : database(db), skip(0), take(0) + { } +}; + template -class Query : public AbstractQuery +class Query { - bool m_autoDelete; + QueryData *d; public: - explicit Query(Database *database, AbstractTableSet *tableSet, bool autoDelete); + explicit Query(Database *database, AbstractTableSet *tableSet); + Query (const Query &other); + Query (Query &&other); + ~Query(); + Query& operator=(const Query &q); + //ddl - Query *join(const QString &className); - Query *join(Table *c); + Query &join(const QString &className); + Query &join(Table *c); template - Query *join() + Query &join() { join(TABLE::staticMetaObject.className()); - return this; + return *this; } - // Query *orderBy(QString fieldName, QString type); - Query *skip(int n); - Query *take(int n); - Query *fields(const PhraseList &ph); - Query *orderBy(const PhraseList &ph); - Query *where(const ConditionalPhrase &ph); - Query *setWhere(const ConditionalPhrase &ph); + // Query &orderBy(QString fieldName, QString type); + Query &skip(int n); + Query &take(int n); + Query &fields(const PhraseList &ph); + Query &orderBy(const PhraseList &ph); + Query &where(const ConditionalPhrase &ph); + Query &setWhere(const ConditionalPhrase &ph); //data selecting Row first(); @@ -110,7 +152,7 @@ template template Q_OUTOFLINE_TEMPLATE QList Query::select(const std::function allocator) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); QList ret; d->joins.prepend(d->tableName); @@ -129,23 +171,20 @@ Q_OUTOFLINE_TEMPLATE QList Query::select(const std::function -//inline Query *createQuery(TableSet *tableSet) +//inline Query &createQuery(TableSet *tableSet) //{ // return tableSet->query(); //} template -Q_OUTOFLINE_TEMPLATE Query::Query(Database *database, AbstractTableSet *tableSet, - bool autoDelete) - : AbstractQuery(database), m_autoDelete(autoDelete) +Q_OUTOFLINE_TEMPLATE Query::Query(Database *database, AbstractTableSet *tableSet) + : d(new QueryData(database)) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->database = database; d->tableSet = tableSet; @@ -156,17 +195,40 @@ Q_OUTOFLINE_TEMPLATE Query::Query(Database *database, AbstractTableSet *table ->name(); } +template +Q_OUTOFLINE_TEMPLATE Query::Query(const Query &other) { + d = other.d->clone(); +} + +template +Q_OUTOFLINE_TEMPLATE Query::Query(Query &&other) { + d = std::move(other.d); + other.d = nullptr; +} + template Q_OUTOFLINE_TEMPLATE Query::~Query() { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); delete d; } +template +Q_OUTOFLINE_TEMPLATE Query &Query::operator=(const Query &q) +{ + if (this != &q) + { + QueryData *p = q.d ? q.d->clone() : nullptr; + delete d; + d = p; + } + return *this; +} + template Q_OUTOFLINE_TEMPLATE RowList Query::toList(int count) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); RowList returnList; d->select = QStringLiteral("*"); @@ -323,7 +385,7 @@ Q_OUTOFLINE_TEMPLATE RowList Query::toList(int count) } row->setStatus(Table::FetchedFromDB); - row->setParent(this); +// row->setParent(this); row->clear(); //set last created row @@ -331,10 +393,6 @@ Q_OUTOFLINE_TEMPLATE RowList Query::toList(int count) } //while } // while -#ifndef NUT_SHARED_POINTER - if (m_autoDelete) - deleteLater(); -#endif return returnList; } @@ -343,7 +401,7 @@ template template Q_OUTOFLINE_TEMPLATE QList Query::select(const FieldPhrase f) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); QList ret; d->joins.prepend(d->tableName); @@ -361,8 +419,6 @@ Q_OUTOFLINE_TEMPLATE QList Query::select(const FieldPhrase f) ret.append(v.value()); } - if (m_autoDelete) - deleteLater(); return ret; } @@ -381,7 +437,7 @@ Q_OUTOFLINE_TEMPLATE Row Query::first() template Q_OUTOFLINE_TEMPLATE int Query::count() { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->joins.prepend(d->tableName); d->select = QStringLiteral("COUNT(*)"); @@ -401,7 +457,7 @@ Q_OUTOFLINE_TEMPLATE int Query::count() template Q_OUTOFLINE_TEMPLATE QVariant Query::max(const FieldPhrase &f) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->joins.prepend(d->tableName); d->sql = d->database->sqlGenerator()->selectCommand( @@ -419,7 +475,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query::max(const FieldPhrase &f) template Q_OUTOFLINE_TEMPLATE QVariant Query::min(const FieldPhrase &f) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->joins.prepend(d->tableName); d->sql = d->database->sqlGenerator()->selectCommand( @@ -437,7 +493,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query::min(const FieldPhrase &f) template Q_OUTOFLINE_TEMPLATE QVariant Query::sum(const FieldPhrase &f) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->joins.prepend(d->tableName); d->sql = d->database->sqlGenerator()->selectCommand( @@ -455,7 +511,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query::sum(const FieldPhrase &f) template Q_OUTOFLINE_TEMPLATE QVariant Query::average(const FieldPhrase &f) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->joins.prepend(d->tableName); d->sql = d->database->sqlGenerator()->selectCommand( @@ -473,7 +529,7 @@ Q_OUTOFLINE_TEMPLATE QVariant Query::average(const FieldPhrase &f) template Q_OUTOFLINE_TEMPLATE QVariant Query::insert(const AssignmentPhraseList &p) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->sql = d->database->sqlGenerator() ->insertCommand(d->tableName, p); QSqlQuery q = d->database->exec(d->sql); @@ -482,9 +538,9 @@ Q_OUTOFLINE_TEMPLATE QVariant Query::insert(const AssignmentPhraseList &p) } template -Q_OUTOFLINE_TEMPLATE Query *Query::join(const QString &className) +Q_OUTOFLINE_TEMPLATE Query &Query::join(const QString &className) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); RelationModel *rel = d->database->model() .relationByClassNames(d->className, className); @@ -495,85 +551,85 @@ Q_OUTOFLINE_TEMPLATE Query *Query::join(const QString &className) if (!rel) { qDebug() << "No relation between" << d->className << "and" << className; - return this; + return *this; } d->relations.append(rel); d->joins.append(className); - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::join(Table *c) +Q_OUTOFLINE_TEMPLATE Query &Query::join(Table *c) { join(c->metaObject()->className()); - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::where(const ConditionalPhrase &ph) +Q_OUTOFLINE_TEMPLATE Query &Query::where(const ConditionalPhrase &ph) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); if (d->wherePhrase.data) d->wherePhrase = d->wherePhrase && ph; else d->wherePhrase = ph; - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::setWhere(const ConditionalPhrase &ph) +Q_OUTOFLINE_TEMPLATE Query &Query::setWhere(const ConditionalPhrase &ph) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->wherePhrase = ph; - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::skip(int n) +Q_OUTOFLINE_TEMPLATE Query &Query::skip(int n) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->skip = n; - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::take(int n) +Q_OUTOFLINE_TEMPLATE Query &Query::take(int n) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->take = n; - return this; + return *this; } template -Q_OUTOFLINE_TEMPLATE Query *Query::fields(const PhraseList &ph) +Q_OUTOFLINE_TEMPLATE Query &Query::fields(const PhraseList &ph) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->fieldPhrase = ph; - return this; + return *this; } //template -//Q_OUTOFLINE_TEMPLATE Query *Query::orderBy(QString fieldName, +//Q_OUTOFLINE_TEMPLATE Query &Query::orderBy(QString fieldName, // QString type) //{ -// Q_D(AbstractQuery); +// //Q_D(AbstractQuery); // d->orderPhrases.append(fieldName, type); -// return this; +// return *this; //} template -Q_OUTOFLINE_TEMPLATE Query *Query::orderBy(const PhraseList &ph) +Q_OUTOFLINE_TEMPLATE Query &Query::orderBy(const PhraseList &ph) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->orderPhrase = ph; - return this; + return *this; } template Q_OUTOFLINE_TEMPLATE int Query::update(const AssignmentPhraseList &ph) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->sql = d->database->sqlGenerator()->updateCommand( d->tableName, @@ -582,22 +638,18 @@ Q_OUTOFLINE_TEMPLATE int Query::update(const AssignmentPhraseList &ph) QSqlQuery q = d->database->exec(d->sql); - if (m_autoDelete) - deleteLater(); return q.numRowsAffected(); } template Q_OUTOFLINE_TEMPLATE int Query::remove() { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->sql = d->database->sqlGenerator()->deleteCommand( d->tableName, d->wherePhrase); QSqlQuery q = d->database->exec(d->sql); - if (m_autoDelete) - deleteLater(); return q.numRowsAffected(); } @@ -612,7 +664,7 @@ Q_OUTOFLINE_TEMPLATE QSqlQueryModel *Query::toModel() template Q_OUTOFLINE_TEMPLATE void Query::toModel(QSqlQueryModel *model) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->sql = d->database->sqlGenerator()->selectCommand( d->tableName, @@ -649,7 +701,7 @@ Q_OUTOFLINE_TEMPLATE void Query::toModel(QSqlQueryModel *model) template Q_OUTOFLINE_TEMPLATE void Query::toModel(SqlModel *model) { - Q_D(AbstractQuery); + //Q_D(AbstractQuery); d->sql = d->database->sqlGenerator()->selectCommand( d->tableName, @@ -686,7 +738,7 @@ Q_OUTOFLINE_TEMPLATE void Query::toModel(SqlModel *model) template Q_OUTOFLINE_TEMPLATE QString Query::sqlCommand() const { - Q_D(const AbstractQuery); + //Q_D(const AbstractQuery); return d->sql; } diff --git a/src/nut/tableset.h b/src/nut/tableset.h index 6fb9c7c..d28ddfb 100644 --- a/src/nut/tableset.h +++ b/src/nut/tableset.h @@ -63,8 +63,8 @@ public: Row at(int i) const; Row operator[](int i) const; - Query *query(bool autoDelete = true); - BulkInserter *bulkInserter(); + Query query(); + BulkInserter bulkInserter(); }; template @@ -80,18 +80,15 @@ Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Table *parent) : AbstractTableSet(par } template -Q_OUTOFLINE_TEMPLATE Query *TableSet::query(bool autoDelete) +Q_OUTOFLINE_TEMPLATE Query TableSet::query() { - Query *q = new Query(data->database, this, autoDelete); - - return q; + return Query(data->database, this); } template -Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet::bulkInserter() +Q_OUTOFLINE_TEMPLATE BulkInserter TableSet::bulkInserter() { - BulkInserter *bi = new BulkInserter(data->database, data->childClassName); - return bi; + return BulkInserter(data->database, data->childClassName); } template diff --git a/tests/auto/tst_basic/tst_basic.cpp b/tests/auto/tst_basic/tst_basic.cpp index 7e1a7a2..4ea84e8 100644 --- a/tests/auto/tst_basic/tst_basic.cpp +++ b/tests/auto/tst_basic/tst_basic.cpp @@ -38,10 +38,10 @@ void BasicTest::initTestCase() bool ok = db.open(); QVERIFY(ok); - db.comments()->query()->remove(); - db.posts()->query()->remove(); - db.users()->query()->remove(); - db.scores()->query()->remove(); + db.comments()->query().remove(); + db.posts()->query().remove(); + db.users()->query().remove(); + db.scores()->query().remove(); } void BasicTest::dataSchema() @@ -97,7 +97,7 @@ void BasicTest::createPost() void BasicTest::createPost2() { //create post on the fly - QVariant postIdVar = db.posts()->query()->insert( + QVariant postIdVar = db.posts()->query().insert( (Post::titleField() = QStringLiteral("This is a sample")) & (Post::isPublicField() = true)); @@ -123,15 +123,15 @@ void BasicTest::createPost2() void BasicTest::updatePostOnTheFly() { auto c = db.posts()->query() - ->where(Post::idField() == postId) - ->update(Post::titleField() = QStringLiteral("New title")); + .where(Post::idField() == postId) + .update(Post::titleField() = QStringLiteral("New title")); QCOMPARE(c, 1); auto titles = db.posts() ->query() - ->where(Post::idField() == postId) - ->select(Post::titleField()); + .where(Post::idField() == postId) + .select(Post::titleField()); QCOMPARE(titles.count(), 1); QCOMPARE(titles.at(0), QStringLiteral("New title")); @@ -140,12 +140,12 @@ void BasicTest::updatePostOnTheFly() void BasicTest::selectPublicts() { auto q = db.posts()->query() - ->where(Post::isPublicField()) - ->count(); + .where(Post::isPublicField()) + .count(); auto q2 = db.posts()->query() - ->where(!Post::isPublicField()) - ->count(); + .where(!Post::isPublicField()) + .count(); QCOMPARE(q, 1); QCOMPARE(q2, 1); @@ -154,11 +154,11 @@ void BasicTest::selectPublicts() void BasicTest::selectPosts() { auto q = db.posts()->query() - ->join() - ->orderBy((!Post::saveDateField()) | Post::bodyField()) - ->setWhere(Post::idField() == postId); + .join() + .orderBy((!Post::saveDateField()) | Post::bodyField()) + .where(Post::idField() == postId); - auto posts = q->toList(); + auto posts = q.toList(); post = posts.at(0); post->setBody(QStringLiteral("")); @@ -179,9 +179,9 @@ void BasicTest::selectScoreAverage() bool ok; auto avg = db.scores() ->query() - ->join() - ->where(Post::idField() == postId) - ->average(Score::scoreField()) + .join() + .where(Post::idField() == postId) + .average(Score::scoreField()) .toInt(&ok); QVERIFY(ok); @@ -190,21 +190,21 @@ void BasicTest::selectScoreAverage() void BasicTest::selectScoreSum() { - auto sum = db.scores()->query()->sum(Score::scoreField()); + auto sum = db.scores()->query().sum(Score::scoreField()); QCOMPARE(sum, 20); } void BasicTest::selectScoreCount() { - auto count = db.scores()->query()->count(); + auto count = db.scores()->query().count(); QCOMPARE(count, 10); } void BasicTest::selectFirst() { auto posts = db.posts()->query() - ->orderBy(Post::idField()) - ->first(); + .orderBy(Post::idField()) + .first(); QVERIFY(posts != Q_NULLPTR); } @@ -212,15 +212,15 @@ void BasicTest::selectFirst() void BasicTest::selectPostsWithoutTitle() { auto q = db.posts()->query(); - q->setWhere(Post::titleField().isNull()); - auto count = q->count(); + q.where(Post::titleField().isNull()); + auto count = q.count(); QCOMPARE(count, 0); } void BasicTest::selectPostIds() { auto q = db.posts()->query(); - auto ids = q->select(Post::idField()); + auto ids = q.select(Post::idField()); QCOMPARE(ids.count(), 2); } @@ -240,18 +240,17 @@ void BasicTest::testDate() db.saveChanges(true); auto q = db.posts()->query() - ->setWhere(Post::idField() == newPost->id()) - ->orderBy(Post::idField()) - ->first(); + .where(Post::idField() == newPost->id()) + .orderBy(Post::idField()) + .first(); QCOMPARE(q->saveDate(), d); } void BasicTest::testLimitedQuery() { - auto q = db.comments()->query(); - auto comments = q->toList(2); - qDebug() << q->sqlCommand(); + auto comments = db.comments()->query() + .toList(2); QCOMPARE(comments.length(), 2); } @@ -274,17 +273,17 @@ void BasicTest::join() void BasicTest::selectWithInvalidRelation() { auto q = db.posts()->query(); - q->join(QStringLiteral("Invalid_Class_Name")); - q->toList(); + q.join(QStringLiteral("Invalid_Class_Name")); + q.toList(); } void BasicTest::modifyPost() { auto q = db.posts()->query() - ->setWhere(Post::idField() == postId) - ->orderBy(Post::idField()); + .where(Post::idField() == postId) + .orderBy(Post::idField()); - Nut::Row post = q->first(); + Nut::Row post = q.first(); QTEST_ASSERT(post != nullptr); @@ -292,18 +291,18 @@ void BasicTest::modifyPost() db.saveChanges(); q = db.posts()->query() - ->setWhere(Post::idField() == postId) - ->orderBy(Post::idField()); + .where(Post::idField() == postId) + .orderBy(Post::idField()); - post = q->first(); + post = q.first(); PRINT(post->title()); QCOMPARE(post->title(), "new name"); } void BasicTest::emptyDatabase() { -// auto commentsCount = db.comments()->query()->remove(); -// auto postsCount = db.posts()->query()->remove(); +// auto commentsCount = db.comments()->query().remove(); +// auto postsCount = db.posts()->query().remove(); // QTEST_ASSERT(postsCount == 3); // QTEST_ASSERT(commentsCount == 6); } diff --git a/tests/auto/tst_datatypes/tst_datatypes.cpp b/tests/auto/tst_datatypes/tst_datatypes.cpp index a0503f6..293e940 100644 --- a/tests/auto/tst_datatypes/tst_datatypes.cpp +++ b/tests/auto/tst_datatypes/tst_datatypes.cpp @@ -80,7 +80,7 @@ void DataTypesTest::initTestCase() QTEST_ASSERT(ok); - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); } void DataTypesTest::insert() @@ -130,7 +130,7 @@ void DataTypesTest::insert() void DataTypesTest::retrive() { - Nut::RowList list = db.sampleTables()->query()->toList(); + Nut::RowList list = db.sampleTables()->query().toList(); QTEST_ASSERT(list.count() == 1); Nut::Row t = list.first(); @@ -179,8 +179,8 @@ void DataTypesTest::retrive() #define CHECK(name) \ c = db.sampleTables()->query() \ - ->where(SampleTable::f_ ## name ## Field() == f_ ## name) \ - ->count(); \ + .where(SampleTable::f_ ## name ## Field() == f_ ## name) \ + .count(); \ QTEST_ASSERT(c == 1); void DataTypesTest::check() @@ -224,7 +224,7 @@ void DataTypesTest::check() void DataTypesTest::cleanupTestCase() { - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); db.close(); PRINT_FORM(db); diff --git a/tests/auto/tst_datetime/tst_datetime.cpp b/tests/auto/tst_datetime/tst_datetime.cpp index 5f1fd53..212d3e1 100644 --- a/tests/auto/tst_datetime/tst_datetime.cpp +++ b/tests/auto/tst_datetime/tst_datetime.cpp @@ -32,7 +32,7 @@ void DateTimeTest::initTestCase() QTEST_ASSERT(db.open()); - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); } #define TEST_DATE(date, command, n) \ @@ -42,8 +42,8 @@ do { \ db.sampleTables()->append(s); \ db.saveChanges(); \ auto count = db.sampleTables()->query() \ - ->where(SampleTable::dField().command(n) == date.command(n)) \ - ->count(); \ + .where(SampleTable::dField().command(n) == date.command(n)) \ + .count(); \ QTEST_ASSERT(count); \ } while (false) @@ -54,8 +54,8 @@ do { \ db.sampleTables()->append(s); \ db.saveChanges(); \ auto count = db.sampleTables()->query() \ - ->where(SampleTable::tField().command(n) == time.addSecs(num)) \ - ->count(); \ + .where(SampleTable::tField().command(n) == time.addSecs(num)) \ + .count(); \ QTEST_ASSERT(count); \ } while (false) @@ -66,8 +66,8 @@ do { \ db.sampleTables()->append(s); \ db.saveChanges(); \ auto count = db.sampleTables()->query() \ - ->where(SampleTable::dtField().command(n) == datetime.command(n)) \ - ->count(); \ + .where(SampleTable::dtField().command(n) == datetime.command(n)) \ + .count(); \ QTEST_ASSERT(count); \ } while (false) @@ -78,8 +78,8 @@ do { \ db.sampleTables()->append(s); \ db.saveChanges(); \ auto count = db.sampleTables()->query() \ - ->where(SampleTable::dtField().command(n) == datetime.addSecs(num)) \ - ->count(); \ + .where(SampleTable::dtField().command(n) == datetime.addSecs(num)) \ + .count(); \ QTEST_ASSERT(count); \ } while (false) @@ -136,7 +136,7 @@ void DateTimeTest::dateTimeAdd() void DateTimeTest::datePart() { - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); QDate d = QDate::currentDate(); auto s = Nut::create(); @@ -146,18 +146,18 @@ void DateTimeTest::datePart() int count; - count = db.sampleTables()->query()->where(SampleTable::dField().year() == d.year())->count(); + count = db.sampleTables()->query().where(SampleTable::dField().year() == d.year()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dField().month() == d.month())->count(); + count = db.sampleTables()->query().where(SampleTable::dField().month() == d.month()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dField().day() == d.day())->count(); + count = db.sampleTables()->query().where(SampleTable::dField().day() == d.day()).count(); QTEST_ASSERT(count); } void DateTimeTest::timePart() { - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); QTime t = QTime::currentTime(); auto s = Nut::create(); @@ -167,17 +167,17 @@ void DateTimeTest::timePart() int count; - count = db.sampleTables()->query()->where(SampleTable::tField().hour() == t.hour())->count(); + count = db.sampleTables()->query().where(SampleTable::tField().hour() == t.hour()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::tField().minute() == t.minute())->count(); + count = db.sampleTables()->query().where(SampleTable::tField().minute() == t.minute()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::tField().second() == t.second())->count(); + count = db.sampleTables()->query().where(SampleTable::tField().second() == t.second()).count(); QTEST_ASSERT(count); } void DateTimeTest::dateTimePart() { - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); QDateTime dt = QDateTime::currentDateTime(); auto s = Nut::create(); @@ -187,24 +187,24 @@ void DateTimeTest::dateTimePart() int count; - count = db.sampleTables()->query()->where(SampleTable::dtField().year() == dt.date().year())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().year() == dt.date().year()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dtField().month() == dt.date().month())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().month() == dt.date().month()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dtField().day() == dt.date().day())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().day() == dt.date().day()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dtField().hour() == dt.time().hour())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().hour() == dt.time().hour()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dtField().minute() == dt.time().minute())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().minute() == dt.time().minute()).count(); QTEST_ASSERT(count); - count = db.sampleTables()->query()->where(SampleTable::dtField().second() == dt.time().second())->count(); + count = db.sampleTables()->query().where(SampleTable::dtField().second() == dt.time().second()).count(); QTEST_ASSERT(count); } void DateTimeTest::cleanupTestCase() { - db.sampleTables()->query()->remove(); + db.sampleTables()->query().remove(); db.close(); } diff --git a/tests/auto/tst_json/tst_json.cpp b/tests/auto/tst_json/tst_json.cpp index 8724d63..0f9f6ef 100644 --- a/tests/auto/tst_json/tst_json.cpp +++ b/tests/auto/tst_json/tst_json.cpp @@ -49,9 +49,9 @@ void TestJson::store() int id = t->id(); auto newObj = db.sampleTable()->query() - ->where(Table::idField() == id) - ->orderBy(Table::idField()) - ->first(); + .where(Table::idField() == id) + .orderBy(Table::idField()) + .first(); Q_ASSERT(newObj != nullptr); Q_ASSERT(newObj->doc() == t->doc()); diff --git a/tests/auto/tst_quuid/tst_uuid.cpp b/tests/auto/tst_quuid/tst_uuid.cpp index 21dadae..e77b39b 100644 --- a/tests/auto/tst_quuid/tst_uuid.cpp +++ b/tests/auto/tst_quuid/tst_uuid.cpp @@ -32,7 +32,7 @@ void UuidTest::initTestCase() bool ok = db.open(); - db.tests()->query()->remove(); + db.tests()->query().remove(); uuid = QUuid::createUuid(); QTEST_ASSERT(ok); @@ -54,7 +54,7 @@ void UuidTest::save() void UuidTest::restore() { TIC(); - auto test = db.tests()->query()->first(); + auto test = db.tests()->query().first(); TOC(); QTEST_ASSERT(!test->id().isNull()); QTEST_ASSERT(test->uuid() == uuid); diff --git a/tests/auto/tst_upgrades/tst_upgrades.cpp b/tests/auto/tst_upgrades/tst_upgrades.cpp index c91576b..345f09a 100644 --- a/tests/auto/tst_upgrades/tst_upgrades.cpp +++ b/tests/auto/tst_upgrades/tst_upgrades.cpp @@ -49,7 +49,7 @@ void Upgrades::version1() DB1 db; initDb(db); QTEST_ASSERT(db.open()); - db.sampleTable()->query()->remove(); + db.sampleTable()->query().remove(); } void Upgrades::version2() @@ -72,7 +72,7 @@ void Upgrades::version3() QTEST_ASSERT(db.open()); auto t = db.sampleTable()->query() - ->first(); + .first(); QTEST_ASSERT(id == t->id()); }