diff --git a/nut.pri b/nut.pri index b0f235b..b0931d8 100644 --- a/nut.pri +++ b/nut.pri @@ -3,6 +3,7 @@ QT += core sql CONFIG += c++11 INCLUDEPATH += $$PWD/include +DEFINES += NUT_SHARED_POINTER include(3rdparty/serializer/src/src.pri) HEADERS += \ diff --git a/src/database.cpp b/src/database.cpp index 0c0b3bb..e804ef4 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -278,7 +278,7 @@ bool DatabasePrivate::getCurrectScheema() DatabaseModel DatabasePrivate::getLastScheema() { - typename TableType::Row u = changeLogs->query() + Row u = changeLogs->query() ->orderBy(!ChangeLogTable::idField()) ->first(); @@ -321,7 +321,7 @@ bool DatabasePrivate::putModelToDatabase() DatabaseModel current = currentModel; /*current.remove(__CHANGE_LOG_TABLE_NAME)*/; - auto *changeLog = new ChangeLogTable(); + auto changeLog = create(); changeLog->setData(QJsonDocument(current.toJson()).toJson(QJsonDocument::Compact)); changeLog->setVersion(current.version()); changeLogs->append(changeLog); diff --git a/src/defines.h b/src/defines.h index a26d7bb..86a96a3 100644 --- a/src/defines.h +++ b/src/defines.h @@ -45,6 +45,83 @@ Q_CLASSINFO(__nut_NAME_PERFIX type #name #value, \ type "\n" #name "\n" value) +#define NUT_FIELD_PERFIX +#define NUT_FIELD_POSTFIX Field + +// Database +#define NUT_DB_VERSION(version) \ + NUT_INFO(__nut_DB_VERSION, version, 0) + +#define NUT_DECLARE_TABLE(type, name) \ + NUT_INFO(__nut_TABLE, type, name) \ + Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet) name READ name) \ + NUT_WRAP_NAMESPACE(TableSet) *m_##name; \ + public: \ + static const type *_##name; \ + NUT_WRAP_NAMESPACE(TableSet) *name() const \ + { return m_##name; } \ + private: + +//Table +#define NUT_DECLARE_FIELD(type, name, read, write) \ + Q_PROPERTY(type name READ read WRITE write) \ + NUT_INFO(__nut_FIELD, name, 0) \ + type m_##name; \ +public: \ + static NUT_WRAP_NAMESPACE(FieldPhrase)& name ## Field(){ \ + static NUT_WRAP_NAMESPACE(FieldPhrase) f = \ + NUT_WRAP_NAMESPACE(FieldPhrase) \ + (staticMetaObject.className(), #name); \ + return f; \ + } \ + type read() const{ \ + return m_##name; \ + } \ + void write(type name){ \ + m_##name = name; \ + propertyChanged(#name); \ + } + +#define NUT_FOREGION_KEY(type, keytype, name, read, write) \ + Q_PROPERTY(Nut::Row name READ read WRITE write) \ + NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \ + NUT_INFO(__nut_FOREGION_KEY, name, type) \ + Nut::Row m_##name; \ +public: \ + Nut::Row read() const { return m_##name ; } \ + void write(Nut::Row name){ \ + m_##name = name; \ + } + +#define NUT_DECLARE_CHILD_TABLE(type, n) \ + private: \ + NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ + public: \ + static type *n##Table(); \ + NUT_WRAP_NAMESPACE(TableSet) *n(); + +#define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \ + type *class::n##Table(){ \ + static auto f = new type(); \ + return f; \ + } \ + NUT_WRAP_NAMESPACE(TableSet) *class::n(){ \ + return m_##n; \ + } + +#define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0) +#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) +#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0) +#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0) +#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name) +#define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0) +#define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len) +#define NUT_DEFAULT_VALUE(x, n) NUT_INFO(__nut_DEFAULT_VALUE, x, n) +#define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1) +#define NUT_INDEX(name, field, order) + +NUT_BEGIN_NAMESPACE + inline bool nutClassInfo(const QMetaClassInfo &classInfo, QString &type, QString &name, QVariant &value) { @@ -117,107 +194,37 @@ inline bool nutClassInfoInt(const QMetaClassInfo &classInfo, } } -#define NUT_FIELD_PERFIX -#define NUT_FIELD_POSTFIX Field - -// Database -#define NUT_DB_VERSION(version) \ - NUT_INFO(__nut_DB_VERSION, version, 0) - -#define NUT_DECLARE_TABLE(type, name) \ - NUT_INFO(__nut_TABLE, type, name) \ - Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet) name READ name) \ - NUT_WRAP_NAMESPACE(TableSet) *m_##name; \ - public: \ - static const type *_##name; \ - NUT_WRAP_NAMESPACE(TableSet) *name() const \ - { return m_##name; } \ - private: - -//Table -#define NUT_DECLARE_FIELD(type, name, read, write) \ - Q_PROPERTY(type name READ read WRITE write) \ - NUT_INFO(__nut_FIELD, name, 0) \ - type m_##name; \ -public: \ - static NUT_WRAP_NAMESPACE(FieldPhrase)& name ## Field(){ \ - static NUT_WRAP_NAMESPACE(FieldPhrase) f = \ - NUT_WRAP_NAMESPACE(FieldPhrase) \ - (staticMetaObject.className(), #name); \ - return f; \ - } \ - type read() const{ \ - return m_##name; \ - } \ - void write(type name){ \ - m_##name = name; \ - propertyChanged(#name); \ - } - -#define NUT_FOREGION_KEY(type, keytype, name, read, write) \ - Q_PROPERTY(type* name READ read WRITE write) \ - NUT_DECLARE_FIELD(keytype, name##Id, read##Id, write##Id) \ - NUT_INFO(__nut_FOREGION_KEY, name, type) \ - type *m_##name; \ -public: \ - type *read() const { return m_##name ; } \ - void write(type *name){ \ - m_##name = name; \ - } - -#define NUT_DECLARE_CHILD_TABLE(type, n) \ - private: \ - NUT_WRAP_NAMESPACE(TableSet) *m_##n; \ - public: \ - static type *n##Table(); \ - NUT_WRAP_NAMESPACE(TableSet) *n(); - -#define NUT_IMPLEMENT_CHILD_TABLE(class, type, n) \ - type *class::n##Table(){ \ - static auto f = new type(); \ - return f; \ - } \ - NUT_WRAP_NAMESPACE(TableSet) *class::n(){ \ - return m_##n; \ - } - -#define NUT_FIELD(name) NUT_INFO(__nut_FIELD, name, 0) -#define NUT_PRIMARY_KEY(x) NUT_INFO(__nut_PRIMARY_KEY, x, 0) -#define NUT_AUTO_INCREMENT(x) NUT_INFO(__nut_AUTO_INCREMENT, x, 0) -#define NUT_PRIMARY_AUTO_INCREMENT(x) NUT_INFO(__nut_PRIMARY_KEY_AI, x, 0) -#define NUT_DISPLAY_NAME(field, name) NUT_INFO(__nut_DISPLAY, field, name) -#define NUT_UNIQUE(x) NUT_INFO(__nut_UNIQUE, x, 0) -#define NUT_LEN(field, len) NUT_INFO(__nut_LEN, field, len) -#define NUT_DEFAULT_VALUE(x, n) NUT_INFO(__nut_DEFAULT_VALUE, x, n) -#define NUT_NOT_NULL(x) NUT_INFO(__nut_NOT_NULL, x, 1) -#define NUT_INDEX(name, field, order) +#ifdef NUT_SHARED_POINTER +template +using RowList = QList>; template -struct TableType -{ -#ifdef NUT_SHARED_POINTER - typedef QList> RowList; - typedef QSet> RowSet; - typedef QSharedPointer Row; +using RowSet = QSet>; + +template +using Row = QSharedPointer; + +template +inline Row create() { + return QSharedPointer(new T); +} + #else - typedef QList RowList; - typedef QSet RowSet; - typedef T* Row; +template +using RowList = QList; + +template +using RowSet = QSet; + +template +using Row = T*; + +template +inline Row create() { + return new T; +} #endif -}; -//#ifdef NUT_SHARED_POINTER -// template -// using RowList = typename QList>; - -// template -// using Row = typename QSharedPointer; -//#else -// template -// using RowList = typename QList; - -// template -// using Row = typename T* -//#endif +NUT_END_NAMESPACE #endif // SYNTAX_DEFINES_H diff --git a/src/query.h b/src/query.h index 8739eae..e4c0c9a 100644 --- a/src/query.h +++ b/src/query.h @@ -89,8 +89,8 @@ public: Query *setWhere(const ConditionalPhrase &ph); //data selecting - typename TableType::Row first(); - typename TableType::RowList toList(int count = -1); + Row first(); + RowList toList(int count = -1); template QList select(const FieldPhrase f); @@ -175,11 +175,11 @@ Q_OUTOFLINE_TEMPLATE Query::~Query() } template -Q_OUTOFLINE_TEMPLATE typename TableType::RowList Query::toList(int count) +Q_OUTOFLINE_TEMPLATE RowList Query::toList(int count) { Q_UNUSED(count); Q_D(Query); - typename TableType::RowList returnList; + RowList returnList; d->select = "*"; d->sql = d->database->sqlGenertor()->selectCommand( @@ -378,11 +378,11 @@ Q_OUTOFLINE_TEMPLATE QList Query::select(const FieldPhrase f) } template -Q_OUTOFLINE_TEMPLATE typename TableType::Row Query::first() +Q_OUTOFLINE_TEMPLATE Row Query::first() { skip(0); take(1); - typename TableType::RowList list = toList(1); + RowList list = toList(1); if (list.count()) return list.first(); diff --git a/src/querybase.cpp b/src/querybase.cpp index 5a9526d..f14f16e 100644 --- a/src/querybase.cpp +++ b/src/querybase.cpp @@ -11,9 +11,9 @@ QueryBase::QueryBase(QObject *parent) : QObject(parent) } -void QueryBase::addTableToSet(TableSetBase *set, Table *table) -{ - set->add(table); -} +//void QueryBase::addTableToSet(TableSetBase *set, Table *table) +//{ +// set->add(table); +//} NUT_END_NAMESPACE diff --git a/src/querybase_p.h b/src/querybase_p.h index 8607145..2ef0386 100644 --- a/src/querybase_p.h +++ b/src/querybase_p.h @@ -44,7 +44,7 @@ public: explicit QueryBase(QObject *parent = 0); protected: - void addTableToSet(TableSetBase *set, Table *table); +// void addTableToSet(TableSetBase *set, Table *table); public slots: }; diff --git a/src/sqlmodel.cpp b/src/sqlmodel.cpp index 331b180..7ee2dbd 100644 --- a/src/sqlmodel.cpp +++ b/src/sqlmodel.cpp @@ -75,7 +75,7 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const return QVariant("-"); if (role == Qt::DisplayRole) { - TableType::Row t = d->rows.at(index.row()); + Row
t = d->rows.at(index.row()); QVariant v = t->property(d->model->field(index.column())->name.toLocal8Bit().data()); // emit beforeShowText(index.column(), v); if (_renderer != nullptr) @@ -102,7 +102,7 @@ QVariant SqlModel::data(const QModelIndex &index, int role) const return QVariant(); } -void SqlModel::setRows(TableType
::RowList rows) +void SqlModel::setRows(RowList
rows) { Q_D(SqlModel); beginRemoveRows(QModelIndex(), 0, d->rows.count()); @@ -113,7 +113,7 @@ void SqlModel::setRows(TableType
::RowList rows) endInsertRows(); } -void SqlModel::append(TableType
::Row table) +void SqlModel::append(Row
table) { Q_D(SqlModel); beginInsertRows(QModelIndex(), d->rows.count(), d->rows.count()); diff --git a/src/sqlmodel.h b/src/sqlmodel.h index 061f56b..55ed774 100644 --- a/src/sqlmodel.h +++ b/src/sqlmodel.h @@ -51,8 +51,8 @@ public: template void setTable(QList> rows); - void setRows(TableType
::RowList rows); - void append(TableType
::Row table); + void setRows(RowList
rows); + void append(Row
table); // void append(Table *table); QVariant headerData(int section, Qt::Orientation orientation, int role) const; Table *at(const int &i) const; @@ -72,7 +72,7 @@ Q_OUTOFLINE_TEMPLATE void SqlModel::setTable(QList > rows) { Q_D(SqlModel); - TableType
::RowList tab; + RowList
tab; foreach (auto t, rows) tab.append(t); setRows(tab); diff --git a/src/sqlmodel_p.h b/src/sqlmodel_p.h index cf8bb93..1991d9c 100644 --- a/src/sqlmodel_p.h +++ b/src/sqlmodel_p.h @@ -18,7 +18,7 @@ public: QString tableName; - TableType
::RowList rows; + RowList
rows; TableModel *model; }; diff --git a/src/src.pri b/src/src.pri index 84d9a48..79319e3 100644 --- a/src/src.pri +++ b/src/src.pri @@ -1,9 +1,12 @@ +DEFINES += NUT_SHARED_POINTER + HEADERS += \ $$PWD/generators/sqlgeneratorbase_p.h \ $$PWD/generators/postgresqlgenerator.h \ $$PWD/generators/mysqlgenerator.h \ $$PWD/generators/sqlitegenerator.h \ $$PWD/generators/sqlservergenerator.h \ + $$PWD/tablesetbasedata.h \ $$PWD/types/dbgeography.h \ $$PWD/tableset.h \ $$PWD/defines_p.h \ diff --git a/src/tableset.h b/src/tableset.h index 7748614..af02003 100644 --- a/src/tableset.h +++ b/src/tableset.h @@ -32,6 +32,7 @@ #include "bulkinserter.h" //#include "database.h" #include "databasemodel.h" +#include "tablesetbasedata.h" NUT_BEGIN_NAMESPACE @@ -47,8 +48,8 @@ public: explicit TableSet(Database *parent); explicit TableSet(Table *parent); - void append(T *t); - void append(QList t); + void append(Row t); + void append(RowList t); void remove(T *t); void remove(QList t); @@ -65,19 +66,19 @@ public: template Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Database *parent) : TableSetBase(parent) { - _childClassName = T::staticMetaObject.className(); + data->childClassName = T::staticMetaObject.className(); } template Q_OUTOFLINE_TEMPLATE TableSet::TableSet(Table *parent) : TableSetBase(parent) { - _childClassName = T::staticMetaObject.className(); + data->childClassName = T::staticMetaObject.className(); } template Q_OUTOFLINE_TEMPLATE Query *TableSet::query(bool autoDelete) { - Query *q = new Query(_database, this, autoDelete); + Query *q = new Query(data->database, this, autoDelete); return q; } @@ -85,34 +86,34 @@ Q_OUTOFLINE_TEMPLATE Query *TableSet::query(bool autoDelete) template Q_OUTOFLINE_TEMPLATE BulkInserter *TableSet::bulkInserter() { - BulkInserter *bi = new BulkInserter(_database, _childClassName); + BulkInserter *bi = new BulkInserter(data->database, data->childClassName); return bi; - } template Q_OUTOFLINE_TEMPLATE int TableSet::length() const { - return _tables.count(); + return data->tables.count(); } template Q_OUTOFLINE_TEMPLATE T *TableSet::at(int i) const { - return reinterpret_cast(_childRows.at(i)); + //TODO: check + return reinterpret_cast(data->childRows.at(i)); } template Q_OUTOFLINE_TEMPLATE const T &TableSet::operator[](int i) const { - return _childRows[i]; + return data->childRows[i]; } template -Q_OUTOFLINE_TEMPLATE void TableSet::append(T *t) +Q_OUTOFLINE_TEMPLATE void TableSet::append(Row t) { - _tables.insert(t); - _childRows.append(t); + data->tables.insert(t.data()); + data->childRows.append(t.data()); // if (_database) // t->setModel(_database->model().tableByClassName(t->metaObject()->className())); @@ -123,16 +124,16 @@ Q_OUTOFLINE_TEMPLATE void TableSet::append(T *t) } template -Q_OUTOFLINE_TEMPLATE void TableSet::append(QList t) +Q_OUTOFLINE_TEMPLATE void TableSet::append(RowList t) { - foreach (T* i, t) + foreach (Row i, t) append(i); } template Q_OUTOFLINE_TEMPLATE void TableSet::remove(T *t) { - _tables.remove(t); + data->tables.remove(t); t->setStatus(Table::Deleted); } diff --git a/src/tablesetbase.cpp b/src/tablesetbase.cpp index 9b04a7a..15289ab 100644 --- a/src/tablesetbase.cpp +++ b/src/tablesetbase.cpp @@ -22,24 +22,25 @@ #include "database.h" #include "tablesetbase_p.h" #include "databasemodel.h" +#include "tablesetbasedata.h" NUT_BEGIN_NAMESPACE TableSetBase::TableSetBase(Database *parent) : QObject(parent), - _database(parent), _table(nullptr)//, _tableName(QString()) + data(new TableSetBaseData(parent)) { parent->add(this); } TableSetBase::TableSetBase(Table *parent) : QObject(parent), - _database(nullptr), _table(parent)//, _tableName(QString()) + data(new TableSetBaseData(parent)) { parent->add(this); } TableSetBase::~TableSetBase() { - foreach (Table *t, _tables) + foreach (Table *t, data->tables) t->setParentTableSet(nullptr); } @@ -47,12 +48,13 @@ int TableSetBase::save(Database *db, bool cleanUp) { int rowsAffected = 0; TableModel *masterModel = nullptr; - if (_table) - masterModel = db->model().tableByClassName(_table->metaObject()->className()); + if (data->table) + masterModel = db->model().tableByClassName(data->table->metaObject()->className()); - foreach (Table *t, _childRows) { - if(_table) - t->setParentTable(_table, masterModel, + foreach (Row
t, data->childRows) { + if(data->table) + t->setParentTable(data->table, + masterModel, db->model().tableByClassName(t->metaObject()->className())); if(t->status() == Table::Added @@ -66,45 +68,47 @@ int TableSetBase::save(Database *db, bool cleanUp) } if (cleanUp) - _childRows.clear(); + data->childRows.clear(); return rowsAffected; } void TableSetBase::clearChilds() { - foreach (Table *t, _childRows) +#ifndef NUT_SHARED_POINTER + foreach (Table *t, data->_childRows) t->deleteLater(); - _childRows.clear(); +#endif + data->childRows.clear(); } -void TableSetBase::add(Table *t) +void TableSetBase::add(Row
t) { - if(!_tables.contains(t)){ - _tables.insert(t); - _childRows.append(t); + if(!data->tables.contains(t.data())){ + data->tables.insert(t.data()); + data->childRows.append(t); } } -void TableSetBase::remove(Table *t) +void TableSetBase::remove(Row
t) { - _tables.remove(t); - _childRows.removeOne(t); + data->tables.remove(t.data()); + data->childRows.removeOne(t); } QString TableSetBase::childClassName() const { - return _childClassName; + return data->childClassName; } Database *TableSetBase::database() const { - return _database; + return data->database; } void TableSetBase::setDatabase(Database *database) { - _database = database; + data->database = database; } NUT_END_NAMESPACE diff --git a/src/tablesetbase_p.h b/src/tablesetbase_p.h index 114f458..c04072e 100644 --- a/src/tablesetbase_p.h +++ b/src/tablesetbase_p.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "defines.h" @@ -31,6 +32,7 @@ NUT_BEGIN_NAMESPACE class Table; class Database; +class TableSetBaseData; class TableSetBase : public QObject { @@ -47,16 +49,17 @@ public: void setDatabase(Database *database); protected: - QSet _tables; - QList _childRows; - Database *_database; - Table *_table; -// QString _tableName; - QString _childClassName; +// QSet _tables; +// RowList
_childRows; +// Database *_database; +// Table *_table; +//// QString _tableName; +// QString _childClassName; + QExplicitlySharedDataPointer data; private: - void add(Table* t); - void remove(Table* t); + void add(Row
t); + void remove(Row
t); friend class Table; friend class QueryBase; diff --git a/src/tablesetbasedata.h b/src/tablesetbasedata.h new file mode 100644 index 0000000..7e132ed --- /dev/null +++ b/src/tablesetbasedata.h @@ -0,0 +1,50 @@ +/************************************************************************** +** +** 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 TABLESETBASEDATA_H +#define TABLESETBASEDATA_H + +#include +#include "defines.h" + +NUT_BEGIN_NAMESPACE + +class Table; +class Database; +class TableSetBaseData : public QSharedData +{ +public: + TableSetBaseData(Database *parent) : + database(parent), table(nullptr) + { } + TableSetBaseData(Table *parent) : + database(nullptr), table(parent) + { } + + QSet tables; + QList childRows; + Database *database; + Table *table; + QString childClassName; +}; + +NUT_END_NAMESPACE + +#endif // TABLESETBASEDATA_H diff --git a/test/common/consts.h b/test/common/consts.h index 5b34315..ca49e2e 100644 --- a/test/common/consts.h +++ b/test/common/consts.h @@ -11,7 +11,7 @@ .arg(timer.elapsed() / 1000.) \ .arg(__func__) -#define DRIVER "QPSQL" +#define DRIVER "QSQLITE" #define DATABASE QString("nut_test_%1_db").arg(metaObject()->className()).toLower() #define HOST "127.0.0.1" #define USERNAME "postgres" diff --git a/test/common/nut-lib.pri b/test/common/nut-lib.pri index 47b0640..ade3e61 100644 --- a/test/common/nut-lib.pri +++ b/test/common/nut-lib.pri @@ -8,3 +8,5 @@ win32 { LIBS += -L$$LIBDIR -lnut INCLUDEPATH += $$PWD/../../src $$PWD/../common #include(../../src/src.pri) + +DEFINES += NUT_SHARED_POINTER diff --git a/test/tst_basic/tst_basic.cpp b/test/tst_basic/tst_basic.cpp index df3c568..55d59c3 100644 --- a/test/tst_basic/tst_basic.cpp +++ b/test/tst_basic/tst_basic.cpp @@ -56,7 +56,7 @@ void BasicTest::dataScheema() void BasicTest::createUser() { - user = new User; + user = Nut::create(); user->setUsername("admin"); user->setPassword("123456"); db.users()->append(user); @@ -66,7 +66,7 @@ void BasicTest::createUser() void BasicTest::createPost() { TIC(); - Post *newPost = new Post; + auto newPost = Nut::create(); newPost->setTitle("post title"); newPost->setSaveDate(QDateTime::currentDateTime()); newPost->setPublic(false); @@ -74,14 +74,14 @@ void BasicTest::createPost() db.posts()->append(newPost); for(int i = 0 ; i < 3; i++){ - auto *comment = new Comment; + auto comment = Nut::create(); comment->setMessage("comment #" + QString::number(i)); comment->setSaveDate(QDateTime::currentDateTime()); comment->setAuthorId(user->id()); newPost->comments()->append(comment); } for (int i = 0; i < 10; ++i) { - auto *score = new Score; + auto score = Nut::create(); score->setScore(i % 5); newPost->scores()->append(score); } @@ -106,7 +106,7 @@ void BasicTest::createPost2() int postId = postIdVar.toInt(); for(int i = 0 ; i < 3; i++){ - auto *comment = new Comment; + auto comment = Nut::create(); comment->setMessage("comment #" + QString::number(i + 2)); comment->setSaveDate(QDateTime::currentDateTime()); comment->setAuthor(user); @@ -205,7 +205,7 @@ void BasicTest::testDate() QTime t = QTime(d.time().hour(), d.time().minute(), d.time().second()); d.setTime(t); - Post *newPost = new Post; + auto newPost = Nut::create(); newPost->setTitle("post title"); newPost->setSaveDate(d); @@ -249,7 +249,7 @@ void BasicTest::modifyPost() auto q = db.posts()->query(); q->setWhere(Post::idField() == postId); - Post *post = q->first(); + Nut::Row post = q->first(); QTEST_ASSERT(post != nullptr); diff --git a/test/tst_basic/tst_basic.h b/test/tst_basic/tst_basic.h index d682b91..89b59a9 100644 --- a/test/tst_basic/tst_basic.h +++ b/test/tst_basic/tst_basic.h @@ -12,8 +12,8 @@ class BasicTest : public QObject Q_OBJECT WeblogDatabase db; int postId; - Post *post; - User *user; + Nut::Row post; + Nut::Row user; public: explicit BasicTest(QObject *parent = nullptr); diff --git a/test/tst_benckmark/tst_benchmark.cpp b/test/tst_benckmark/tst_benchmark.cpp index 157bb83..2618f14 100644 --- a/test/tst_benckmark/tst_benchmark.cpp +++ b/test/tst_benckmark/tst_benchmark.cpp @@ -45,7 +45,7 @@ void BenchmarkTest::insert1kPost() t.start(); for (int i = 0; i < 100; ++i) { - Post *newPost = new Post; + auto newPost = Nut::create(); newPost->setTitle("post title"); newPost->setSaveDate(QDateTime::currentDateTime()); diff --git a/test/tst_datatypes/tst_datatypes.cpp b/test/tst_datatypes/tst_datatypes.cpp index 4696244..2016137 100644 --- a/test/tst_datatypes/tst_datatypes.cpp +++ b/test/tst_datatypes/tst_datatypes.cpp @@ -85,53 +85,54 @@ void DataTypesTest::initTestCase() void DataTypesTest::insert() { - SampleTable t; - t.setInt8(f_int8); - t.setInt16(f_int16); - t.setInt32(f_int32); - t.setInt64(f_int64); + auto t = Nut::create(); - t.setUint8(f_uint8); - t.setUint16(f_uint16); - t.setUint32(f_uint32); - t.setUint64(f_uint64); + t->setInt8(f_int8); + t->setInt16(f_int16); + t->setInt32(f_int32); + t->setInt64(f_int64); - t.setReal(f_real); - t.setFloat(f_float); + t->setUint8(f_uint8); + t->setUint16(f_uint16); + t->setUint32(f_uint32); + t->setUint64(f_uint64); - t.setUrl(f_url); + t->setReal(f_real); + t->setFloat(f_float); - t.setTime(f_time); - t.setDate(f_date); - t.setDateTime(f_dateTime); - t.setUuid(f_uuid); + t->setUrl(f_url); - t.setJsonDoc(f_jsonDoc); - t.setJsonObj(f_jsonObj); - t.setJsonArray(f_jsonArray); - t.setJsonValue(f_jsonValue); + t->setTime(f_time); + t->setDate(f_date); + t->setDateTime(f_dateTime); + t->setUuid(f_uuid); - t.setString(f_string); - t.setStringList(f_stringList); - t.setQchar(f_qchar); + t->setJsonDoc(f_jsonDoc); + t->setJsonObj(f_jsonObj); + t->setJsonArray(f_jsonArray); + t->setJsonValue(f_jsonValue); + + t->setString(f_string); + t->setStringList(f_stringList); + t->setQchar(f_qchar); #ifdef QT_GUI_LIB - t.setColor(f_color); + t->setColor(f_color); - t.setPoint(f_point); - t.setPointf(f_pointf); + t->setPoint(f_point); + t->setPointf(f_pointf); - t.setPolygon(f_polygon); - t.setPolygonf(f_polygonf); + t->setPolygon(f_polygon); + t->setPolygonf(f_polygonf); #endif - db.sampleTables()->append(&t); + db.sampleTables()->append(t); db.saveChanges(); } void DataTypesTest::retrive() { - QList list = db.sampleTables()->query()->toList(); + Nut::RowList list = db.sampleTables()->query()->toList(); QTEST_ASSERT(list.count() == 1); - SampleTable *t = list.first(); + Nut::Row t = list.first(); QTEST_ASSERT(t->f_int8() == f_int8); QTEST_ASSERT(t->f_int16() == f_int16); diff --git a/test/tst_json/tst_json.cpp b/test/tst_json/tst_json.cpp index 4b56f3d..81c2977 100644 --- a/test/tst_json/tst_json.cpp +++ b/test/tst_json/tst_json.cpp @@ -39,7 +39,7 @@ void TestJson::store() db.open(); - Table *t = new Table; + auto t = Nut::create
(); QJsonParseError e; QJsonDocument doc = QJsonDocument::fromJson(R"({"a": 4, "b":3.14})", &e); qDebug() << e.errorString(); diff --git a/test/tst_quuid/tst_uuid.cpp b/test/tst_quuid/tst_uuid.cpp index 446ad38..3337e34 100644 --- a/test/tst_quuid/tst_uuid.cpp +++ b/test/tst_quuid/tst_uuid.cpp @@ -41,10 +41,10 @@ void UuidTest::initTestCase() void UuidTest::save() { TIC(); - Test t; - t.setId(QUuid::createUuid()); - t.setUuid(uuid); - db.tests()->append(&t); + auto t = Nut::create(); + t->setId(QUuid::createUuid()); + t->setUuid(uuid); + db.tests()->append(t); int n = db.saveChanges(); TOC(); diff --git a/test/tst_upgrades/tst_upgrades.cpp b/test/tst_upgrades/tst_upgrades.cpp index 602f9c6..c91576b 100644 --- a/test/tst_upgrades/tst_upgrades.cpp +++ b/test/tst_upgrades/tst_upgrades.cpp @@ -58,11 +58,11 @@ void Upgrades::version2() initDb(db); QTEST_ASSERT(db.open()); - Table2 t; - t.setStr("0"); - db.sampleTable()->append(&t); + auto t = Nut::create(); + t->setStr("0"); + db.sampleTable()->append(t); db.saveChanges(); - id = t.id(); + id = t->id(); } void Upgrades::version3()