data types test

This commit is contained in:
Hamed Masafi 2019-01-20 19:59:54 +03:30
parent 301895131f
commit e75f4f1da7
10 changed files with 204 additions and 34 deletions

2
.gitignore vendored
View File

@ -32,3 +32,5 @@ Makefile*
#QtCtreator Qml
*.qmlproject.user
*.qmlproject.user.*
build

View File

@ -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<QMetaType::Type>(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)

View File

@ -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);

9
test/datatypes/db.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "db.h"
#include "sampletable.h"
DB::DB() : Nut::Database (),
m_sampleTables(new Nut::TableSet<SampleTable>(this))
{
}

19
test/datatypes/db.h Normal file
View File

@ -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

View File

@ -0,0 +1,59 @@
#include <QtTest>
#include <QDebug>
#include <QSqlError>
#include <QElapsedTimer>
#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<x*>()
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<QMetaType::Type>();
// 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)

26
test/datatypes/maintest.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef MAINTEST_H
#define MAINTEST_H
#include <QtCore/QObject>
#include <QtCore/qglobal.h>
#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

View File

@ -0,0 +1,6 @@
#include "sampletable.h"
SampleTable::SampleTable(QObject *parent) : Nut::Table (parent)
{
}

View File

@ -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

View File

@ -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