From e75f4f1da78be4664e1fb6e89c6bb3757165e027 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Sun, 20 Jan 2019 19:59:54 +0330 Subject: [PATCH] data types test --- .gitignore | 2 + src/generators/sqlitegenerator.cpp | 60 ++++++++++++++---------------- src/tablemodel.h | 5 ++- test/datatypes/db.cpp | 9 +++++ test/datatypes/db.h | 19 ++++++++++ test/datatypes/maintest.cpp | 59 +++++++++++++++++++++++++++++ test/datatypes/maintest.h | 26 +++++++++++++ test/datatypes/sampletable.cpp | 6 +++ test/datatypes/sampletable.h | 33 ++++++++++++++++ test/datatypes/tst_datatypes.pro | 19 ++++++++++ 10 files changed, 204 insertions(+), 34 deletions(-) create mode 100644 test/datatypes/db.cpp create mode 100644 test/datatypes/db.h create mode 100644 test/datatypes/maintest.cpp create mode 100644 test/datatypes/maintest.h create mode 100644 test/datatypes/sampletable.cpp create mode 100644 test/datatypes/sampletable.h create mode 100644 test/datatypes/tst_datatypes.pro diff --git a/.gitignore b/.gitignore index 04b7a50..a150acc 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ Makefile* #QtCtreator Qml *.qmlproject.user *.qmlproject.user.* + +build diff --git a/src/generators/sqlitegenerator.cpp b/src/generators/sqlitegenerator.cpp index fa18978..2e6b529 100644 --- a/src/generators/sqlitegenerator.cpp +++ b/src/generators/sqlitegenerator.cpp @@ -34,44 +34,40 @@ QString SqliteGenerator::fieldType(FieldModel *field) QString ret = field->name + " "; QString dbType; - switch (field->type) { - case QVariant::Bool: - dbType = "int"; - break; - case QVariant::ByteArray: - dbType = "blob"; - break; - case QVariant::Date: - dbType = "date"; - break; - case QVariant::DateTime: - dbType = "datetime"; - break; - case QVariant::Time: - dbType = "time"; - break; - case QVariant::Double: - dbType = "real"; - break; - case QVariant::Int: - dbType = "integer"; + switch (static_cast(field->type)) { + case QMetaType::Bool: return "BOOLEAN"; + case QMetaType::QByteArray: return "BLOB"; + case QMetaType::QDate: return "DATE"; + case QMetaType::QDateTime: return "DATETIME"; + case QMetaType::QTime: return "TIME"; + case QMetaType::Double: return "DOUBLE"; + case QMetaType::Float: return "FLOAT"; + + case QMetaType::SChar: + case QMetaType::Char: return "TINYINT"; + case QMetaType::UChar: return "TINYINT UNSIGNED"; + case QMetaType::Short: return "SMALLINT"; + case QMetaType::UShort: return "SMALLINT UNSIGNED"; + case QMetaType::Int: return "INT"; + case QMetaType::UInt: return "INT UNSIGNED"; + case QMetaType::Long: return "MEDIUMINT"; + case QMetaType::ULong: return "MEDIUMINT UNSIGNED"; + case QMetaType::LongLong: return "BIGINT"; + case QMetaType::ULongLong: return "BIGINT UNSIGNED"; + + case QMetaType::QUuid: return "text"; + // if (field->isAutoIncrement) // dbType.append(" PRIMARY KEY AUTOINCREMENT"); - break; - case QVariant::String: + case QMetaType::QString: if(field->length) - dbType = QString("varchar(%1)").arg(field->length); + return QString("VARCHAR(%1)").arg(field->length); else - dbType = "text"; - break; - case QVariant::Uuid: - dbType = "text"; - break; + return "TEXT"; default: - dbType = QString(); + qDebug() << "The type (" << field->type << ") does not supported"; + return QString(); } - - return dbType; } void SqliteGenerator::appendSkipTake(QString &sql, int skip, int take) diff --git a/src/tablemodel.h b/src/tablemodel.h index 345374f..084f5de 100644 --- a/src/tablemodel.h +++ b/src/tablemodel.h @@ -70,8 +70,9 @@ struct FieldModel{ }; struct RelationModel{ - RelationModel() : localColumn(QString()), localProperty(QString()), slaveTable(0), - foreignColumn(QString()), masterTable(0), masterClassName(QString()) + RelationModel() : localColumn(QString()), localProperty(QString()), + slaveTable(nullptr), foreignColumn(QString()), masterTable(nullptr), + masterClassName(QString()) {} explicit RelationModel(const QJsonObject &obj); diff --git a/test/datatypes/db.cpp b/test/datatypes/db.cpp new file mode 100644 index 0000000..e570806 --- /dev/null +++ b/test/datatypes/db.cpp @@ -0,0 +1,9 @@ +#include "db.h" + +#include "sampletable.h" + +DB::DB() : Nut::Database (), + m_sampleTables(new Nut::TableSet(this)) +{ + +} diff --git a/test/datatypes/db.h b/test/datatypes/db.h new file mode 100644 index 0000000..6beb344 --- /dev/null +++ b/test/datatypes/db.h @@ -0,0 +1,19 @@ +#ifndef DB_H +#define DB_H + +#include "database.h" + +class SampleTable; +class DB : public Nut::Database +{ + Q_OBJECT + + NUT_DB_VERSION(1) + + NUT_DECLARE_TABLE(SampleTable, sampleTables) + +public: + DB(); +}; + +#endif // DB_H diff --git a/test/datatypes/maintest.cpp b/test/datatypes/maintest.cpp new file mode 100644 index 0000000..6fa792a --- /dev/null +++ b/test/datatypes/maintest.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "consts.h" + +#include "maintest.h" +#include "query.h" +#include "tableset.h" +#include "tablemodel.h" +#include "databasemodel.h" + +#include "sampletable.h" + +#define PRINT(x) qDebug() << #x "=" << x; +#define TIC() QElapsedTimer timer; timer.start() +#define TOC() qDebug() << QString("Elapsed time: %1ms for %2") \ + .arg(timer.elapsed() / 1000.) \ + .arg(__func__) + +#define REGISTER(x) qDebug() << #x << "type id:" << qRegisterMetaType() + +MainTest::MainTest(QObject *parent) : QObject(parent) +{ +} + +void MainTest::initTestCase() +{ + //register all entities with Qt-MetaType mechanism + REGISTER(SampleTable); + REGISTER(DB); + + db.setDriver(DRIVER); + db.setHostName(HOST); + db.setDatabaseName("nut_tst_basic"); + db.setUserName(USERNAME); + db.setPassword(PASSWORD); + + bool ok = db.open(); + QTEST_ASSERT(ok); +} + +void MainTest::types() +{ +// QMetaEnum en = QMetaEnum::fromType(); +// for (int i = 0; i < en.keyCount(); i++) +// qDebug() << en.value(i); +} + +void MainTest::cleanupTestCase() +{ + db.close(); + QFile::remove("nut_tst_basic"); + + PRINT_FORM(db); +} + +QTEST_MAIN(MainTest) diff --git a/test/datatypes/maintest.h b/test/datatypes/maintest.h new file mode 100644 index 0000000..3b0925e --- /dev/null +++ b/test/datatypes/maintest.h @@ -0,0 +1,26 @@ +#ifndef MAINTEST_H +#define MAINTEST_H + +#include +#include + +#include "db.h" +class MainTest : public QObject +{ + Q_OBJECT + DB db; + +public: + explicit MainTest(QObject *parent = nullptr); + +signals: + +private slots: + void initTestCase(); + + void types(); + + void cleanupTestCase(); +}; + +#endif // MAINTEST_H diff --git a/test/datatypes/sampletable.cpp b/test/datatypes/sampletable.cpp new file mode 100644 index 0000000..68a9c9d --- /dev/null +++ b/test/datatypes/sampletable.cpp @@ -0,0 +1,6 @@ +#include "sampletable.h" + +SampleTable::SampleTable(QObject *parent) : Nut::Table (parent) +{ + +} diff --git a/test/datatypes/sampletable.h b/test/datatypes/sampletable.h new file mode 100644 index 0000000..2091ec7 --- /dev/null +++ b/test/datatypes/sampletable.h @@ -0,0 +1,33 @@ +#ifndef SAMPLETABLE_H +#define SAMPLETABLE_H + +#include "table.h" + +#define FIELD_Q(type) NUT_DECLARE_FIELD(q##type, f##type, f##type, setF##type) + +class SampleTable : public Nut::Table +{ + Q_OBJECT + + NUT_PRIMARY_AUTO_INCREMENT(id) + NUT_DECLARE_FIELD(int, id, id, setId) + + NUT_DECLARE_FIELD(qint8, fint8, fint8, setFint8) + NUT_DECLARE_FIELD(qint16, fint16, fint16, setFint16) + NUT_DECLARE_FIELD(qint32, fint32, fint32, setFint32) + NUT_DECLARE_FIELD(qint64, fint64, fint64, setFint64) + + NUT_DECLARE_FIELD(quint8, fuint8, fuint8, setFuint8) + NUT_DECLARE_FIELD(quint16, fuint16, fuint16, setFuint16) + NUT_DECLARE_FIELD(quint32, fuint32, fuint32, setFuint32) + NUT_DECLARE_FIELD(quint64, fuint64, fuint64, setFuint64) + + NUT_DECLARE_FIELD(qreal, freal, freal, setFreal) + NUT_DECLARE_FIELD(float, ffloat, ffloat, setFfloat) +// NUT_DECLARE_FIELD(long double, fldouble, fldouble, setFldouble) + +public: + Q_INVOKABLE SampleTable(QObject *parent = Q_NULLPTR); +}; + +#endif // SAMPLETABLE_H diff --git a/test/datatypes/tst_datatypes.pro b/test/datatypes/tst_datatypes.pro new file mode 100644 index 0000000..d7a6f66 --- /dev/null +++ b/test/datatypes/tst_datatypes.pro @@ -0,0 +1,19 @@ +QT += qml quick testlib sql +QT -= gui + +TARGET = tst_nut +TEMPLATE = app + +CONFIG += warn_on qmltestcase c++11 +INCLUDEPATH += $$PWD/../../src $$PWD/../common +include(../../nut.pri) +IMPORTPATH += $$OUT_PWD/../src/imports +SOURCES += \ + maintest.cpp \ + db.cpp \ + sampletable.cpp + +HEADERS += \ + maintest.h \ + db.h \ + sampletable.h