From cd1bf906086617fb8edf8cbe9aabeab7c19bb7b3 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Thu, 12 Jul 2018 11:17:41 +0430 Subject: [PATCH] create model without extra property --- src/database.cpp | 12 ++++++++++-- src/databasemodel.cpp | 14 +++++++------- src/defines.h | 5 +++-- src/generators/sqlgeneratorbase.cpp | 8 ++++---- src/generators/sqlitegenerator.cpp | 3 +++ src/table.cpp | 5 +++-- test/quuid/test.cpp | 6 ++++++ test/quuid/test.h | 25 +++++++++++++++++++++++++ test/quuid/testdatabase.cpp | 8 ++++++++ test/quuid/testdatabase.h | 18 ++++++++++++++++++ 10 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 test/quuid/test.cpp create mode 100644 test/quuid/test.h create mode 100644 test/quuid/testdatabase.cpp create mode 100644 test/quuid/testdatabase.h diff --git a/src/database.cpp b/src/database.cpp index 1bbd3f8..e7b52b7 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -205,12 +205,20 @@ bool DatabasePrivate::getCurrectScheema() if (!nutClassInfoString(q->metaObject()->classInfo(i), type, name, value)) { + qDebug() << "No valid table in" << q->metaObject()->classInfo(i).value(); continue; } - - if (type == __nut_TABLE) + if (type == __nut_TABLE) { + //name: table class name + //value: table variable name (table name in db) tables.insert(name, value); + int typeId = QMetaType::type(name.toLocal8Bit() + "*"); + qDebug() << type << name << value << typeId; + TableModel *sch = new TableModel(typeId, value); + currentModel.append(sch); + } + if (type == __nut_DB_VERSION) currentModel.setVersion(name); diff --git a/src/databasemodel.cpp b/src/databasemodel.cpp index 5aad118..89d5ede 100644 --- a/src/databasemodel.cpp +++ b/src/databasemodel.cpp @@ -229,13 +229,13 @@ DatabaseModel *DatabaseModel::modelByName(const QString &name) void DatabaseModel::deleteAllModels() { - QMapIterator i(_models); - while (i.hasNext()) { - i.next(); -// cout << i.key() << ": " << i.value() << endl; - delete i.value(); - } -// qDeleteAll(_models.values()); +// QMapIterator i(_models); +// while (i.hasNext()) { +// i.next(); +//// cout << i.key() << ": " << i.value() << endl; +// delete i.value(); +// } + qDeleteAll(_models.values()); _models.clear(); } diff --git a/src/defines.h b/src/defines.h index 699e5b7..bf1bb7b 100644 --- a/src/defines.h +++ b/src/defines.h @@ -126,10 +126,11 @@ inline bool nutClassInfoInt(const QMetaClassInfo &classInfo, NUT_INFO(__nut_TABLE, type, name) \ Q_PROPERTY(NUT_WRAP_NAMESPACE(TableSet) name READ name) \ NUT_WRAP_NAMESPACE(TableSet) *m_##name; \ -public: \ + public: \ static const type *_##name; \ NUT_WRAP_NAMESPACE(TableSet) *name() const \ - { return m_##name; } + { return m_##name; } \ + private: //Table #define NUT_DECLARE_FIELD(type, name, read, write) \ diff --git a/src/generators/sqlgeneratorbase.cpp b/src/generators/sqlgeneratorbase.cpp index 51812cf..3a8ae1a 100644 --- a/src/generators/sqlgeneratorbase.cpp +++ b/src/generators/sqlgeneratorbase.cpp @@ -395,13 +395,13 @@ QString SqlGeneratorBase::join(const QStringList &list, QStringList *order) QString SqlGeneratorBase::insertRecord(Table *t, QString tableName) { QString sql = QString(); - QString key = t->primaryKey(); + QString key = t->isPrimaryKeyAutoIncrement() ? t->primaryKey() : QString(); + QStringList values; foreach (QString f, t->changedProperties()) if (f != key) - values.append("'" + t->property(f.toLatin1().data()).toString() - + "'"); + values.append(escapeValue(t->property(f.toLatin1().data()))); QString changedPropertiesText = QString(); QSet props = t->changedProperties(); @@ -785,7 +785,7 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const break; case QVariant::Uuid: - return v.toUuid().toString(); + return "'" + v.toUuid().toString() + "'"; break; case QVariant::Char: diff --git a/src/generators/sqlitegenerator.cpp b/src/generators/sqlitegenerator.cpp index 6e8d774..fa18978 100644 --- a/src/generators/sqlitegenerator.cpp +++ b/src/generators/sqlitegenerator.cpp @@ -64,6 +64,9 @@ QString SqliteGenerator::fieldType(FieldModel *field) else dbType = "text"; break; + case QVariant::Uuid: + dbType = "text"; + break; default: dbType = QString(); } diff --git a/src/table.cpp b/src/table.cpp index e3deae5..afb4033 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -74,8 +74,9 @@ void Table::propertyChanged(QString propName) if (!myModel) qFatal ("model for this class not found"); - if (propName == primaryKey()) - return; + foreach (FieldModel *f, myModel->fields()) + if(f->isPrimaryKey && propName == f->name && f->isAutoIncrement) + return; _changedProperties.insert(propName); if (_status == FeatchedFromDB) diff --git a/test/quuid/test.cpp b/test/quuid/test.cpp new file mode 100644 index 0000000..69ddded --- /dev/null +++ b/test/quuid/test.cpp @@ -0,0 +1,6 @@ +#include "test.h" + +Test::Test(QObject *parentTableSet) +{ + +} diff --git a/test/quuid/test.h b/test/quuid/test.h new file mode 100644 index 0000000..edff96c --- /dev/null +++ b/test/quuid/test.h @@ -0,0 +1,25 @@ +#ifndef TEST_H +#define TEST_H + +#include + +#include "table.h" + +class Test : public Nut::Table +{ + Q_OBJECT + + NUT_PRIMARY_KEY(id) + NUT_DECLARE_FIELD(QUuid, id, id, setId) + + NUT_NOT_NULL(username) + NUT_LEN(username, 50) + NUT_DECLARE_FIELD(QString, username, username, setUsername) + +public: + Q_INVOKABLE Test(QObject *parentTableSet = 0); +}; + +Q_DECLARE_METATYPE(Test*) + +#endif // TEST_H diff --git a/test/quuid/testdatabase.cpp b/test/quuid/testdatabase.cpp new file mode 100644 index 0000000..336119e --- /dev/null +++ b/test/quuid/testdatabase.cpp @@ -0,0 +1,8 @@ +#include "testdatabase.h" +#include "test.h" + +TestDatabase::TestDatabase(QObject *parent) + : Database(parent), m_tests(new Nut::TableSet(this)) +{ + +} diff --git a/test/quuid/testdatabase.h b/test/quuid/testdatabase.h new file mode 100644 index 0000000..1e257e6 --- /dev/null +++ b/test/quuid/testdatabase.h @@ -0,0 +1,18 @@ +#ifndef TESTDATABASE_H +#define TESTDATABASE_H + +#include + +class Test; +class TestDatabase : public Nut::Database +{ + Q_OBJECT + + NUT_DB_VERSION(1) + + NUT_DECLARE_TABLE(Test, tests) +public: + TestDatabase(QObject *parent = nullptr); +}; + +#endif // TESTDATABASE_H