From 9a10c53beeaa8f16aa83c0ce1d3237bb9ae4fa8c Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Mon, 10 Aug 2020 12:21:43 +0430 Subject: [PATCH] :-( --- src/nut/3rdparty/serializer | 2 +- src/nut/database.cpp | 2 +- src/nut/query.h | 22 ++++-- src/nut/tableset.h | 8 +-- tests/auto/tst_basic/tst_basic.cpp | 84 +++++++++++----------- 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 +- 10 files changed, 102 insertions(+), 90 deletions(-) diff --git a/src/nut/3rdparty/serializer b/src/nut/3rdparty/serializer index 2dc11b3..acbd689 160000 --- a/src/nut/3rdparty/serializer +++ b/src/nut/3rdparty/serializer @@ -1 +1 @@ -Subproject commit 2dc11b380a07424a4face3046cc6b5e50aefb700 +Subproject commit acbd68924feb9ee412b4d69bbf24cb3a3cffdfd6 diff --git a/src/nut/database.cpp b/src/nut/database.cpp index 205b895..de16341 100644 --- a/src/nut/database.cpp +++ b/src/nut/database.cpp @@ -281,7 +281,7 @@ bool DatabasePrivate::getCurrectSchema() DatabaseModel DatabasePrivate::getLastSchema() { Row u = changeLogs->query() - ->orderBy(!ChangeLogTable::idField()) + .orderBy(!ChangeLogTable::idField()) .first(); // DatabaseModel ret(q->metaObject()->className()); diff --git a/src/nut/query.h b/src/nut/query.h index fbd36da..d83fd16 100644 --- a/src/nut/query.h +++ b/src/nut/query.h @@ -63,7 +63,21 @@ struct NUT_EXPORT QueryData { ConditionalPhrase wherePhrase; QueryData *clone() { - return new QueryData; + 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; } }; @@ -189,7 +203,7 @@ Q_OUTOFLINE_TEMPLATE Query::Query(const Query &other) { template Q_OUTOFLINE_TEMPLATE Query::Query(Query &&other) { d = std::move(other.d); - other._data = nullptr; + other.d = nullptr; } template @@ -204,7 +218,7 @@ Q_OUTOFLINE_TEMPLATE Query &Query::operator=(const Query &q) { if (this != &q) { - T *p = q._data ? q._data->detach() : nullptr; + QueryData *p = q.d ? q.d->clone() : nullptr; delete d; d = p; } @@ -371,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 diff --git a/src/nut/tableset.h b/src/nut/tableset.h index 6fb9c7c..8cd813a 100644 --- a/src/nut/tableset.h +++ b/src/nut/tableset.h @@ -63,7 +63,7 @@ public: Row at(int i) const; Row operator[](int i) const; - Query *query(bool autoDelete = true); + Query query(bool autoDelete = true); BulkInserter *bulkInserter(); }; @@ -80,11 +80,9 @@ Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Table *parent) : AbstractTableSet(par } template -Q_OUTOFLINE_TEMPLATE Query *TableSet::query(bool autoDelete) +Q_OUTOFLINE_TEMPLATE Query TableSet::query(bool autoDelete) { - Query *q = new Query(data->database, this, autoDelete); - - return q; + return Query(data->database, this, autoDelete); } template diff --git a/tests/auto/tst_basic/tst_basic.cpp b/tests/auto/tst_basic/tst_basic.cpp index 7e1a7a2..a188f80 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,9 +240,9 @@ 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); } @@ -250,8 +250,8 @@ void BasicTest::testDate() void BasicTest::testLimitedQuery() { auto q = db.comments()->query(); - auto comments = q->toList(2); - qDebug() << q->sqlCommand(); + auto comments = q.toList(2); + qDebug() << q.sqlCommand(); QCOMPARE(comments.length(), 2); } @@ -274,17 +274,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 +292,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()); }