wip:serializer

This commit is contained in:
Hamed Masafi 2019-02-07 19:22:57 +03:30
parent 944e7ddf70
commit afcfa39ada
12 changed files with 44 additions and 23 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "3rdparty/serializer"]
path = 3rdparty/serializer
url = https://github.com/HamedMasafi/Serializer.git

1
3rdparty/serializer vendored Submodule

@ -0,0 +1 @@
Subproject commit dc55dc615a935b077b2cb0ba4d75257dfea1df8e

View File

@ -3,6 +3,7 @@ QT += core sql
CONFIG += c++11
INCLUDEPATH += $$PWD/include
include(3rdparty/serializer/src/src.pri)
HEADERS += \
$$PWD/src/generators/sqlgeneratorbase_p.h \

View File

@ -143,15 +143,17 @@ bool DatabasePrivate::updateDatabase()
qDebug("Databse is changed");
QStringList sql = sqlGenertor->diff(last, current);
qDebug()<<"database Sql =\n"<<sql;
db.transaction();
foreach (QString s, sql) {
db.exec(s);
if (db.lastError().type() != QSqlError::NoError)
if (db.lastError().type() != QSqlError::NoError) {
qWarning("Error executing sql command `%s`, %s",
qPrintable(s),
db.lastError().text().toLatin1().data());
return false;
}
}
putModelToDatabase();
bool ok = db.commit();
@ -160,6 +162,7 @@ qDebug()<<"database Sql =\n"<<sql;
q->databaseUpdated(last.version(), current.version());
//TODO: remove this
for (int i = 0; i < q->metaObject()->methodCount(); i++) {
QMetaMethod m = q->metaObject()->method(i);
if (m.name() == "update" + current.version()) {
@ -202,7 +205,7 @@ bool DatabasePrivate::getCurrectScheema()
QString type;
QString name;
QString value;
qDebug()<<q->metaObject()->className();
if (!nutClassInfoString(q->metaObject()->classInfo(i),
type, name, value)) {
qDebug() << "No valid table in" << q->metaObject()->classInfo(i).value();

View File

@ -111,7 +111,10 @@ QString SqlGeneratorBase::recordsPhrase(TableModel *table)
QString SqlGeneratorBase::fieldDeclare(FieldModel *field)
{
return field->name + " " + fieldType(field) + (field->notNull ? " NOT NULL" : "");
QString type = fieldType(field);
if (type.isEmpty())
return type;
return field->name + " " + type + (field->notNull ? " NOT NULL" : "");
}
QString SqlGeneratorBase::relationDeclare(const RelationModel *relation)
@ -199,7 +202,10 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
if (!buffer.isNull())
columnSql << buffer;
} else {
columnSql << fieldDeclare(newField);
QString declare = fieldDeclare(newField);
if (declare.isEmpty())
return declare;
columnSql << declare;
}
}
// foreach (QString fieldName, relations) {
@ -471,6 +477,7 @@ QString SqlGeneratorBase::agregateText(const AgregateType &t,
case SignleField:
return arg;
}
return QString(); // never reach
}
QString SqlGeneratorBase::fromTableText(const QString &tableName,
@ -801,7 +808,6 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const
case QVariant::Char:
case QVariant::String:
qDebug() << v.toString();
return "'" + v.toString().replace("'", "''") + "'";
case QVariant::DateTime:

View File

@ -115,7 +115,7 @@ AbstractFieldPhrase::AbstractFieldPhrase(AbstractFieldPhrase &&other)
{
data = other.data;
data->parents++;
other.data = 0;
other.data = nullptr;
}
AbstractFieldPhrase::~AbstractFieldPhrase()

View File

@ -92,11 +92,12 @@ public:
Type type;
Condition operatorCond;
PhraseData *left;
PhraseData *right;
QVariant operand;
Condition operatorCond;
bool isNot;
quint16 parents;

View File

@ -25,14 +25,14 @@
NUT_BEGIN_NAMESPACE
TableSetBase::TableSetBase(Database *parent) : QObject(parent), _database(parent), _table(0),
_tableName(QString())
TableSetBase::TableSetBase(Database *parent) : QObject(parent),
_database(parent), _table(nullptr), _tableName(QString())
{
parent->add(this);
}
TableSetBase::TableSetBase(Table *parent) : QObject(parent), _database(0), _table(parent),
_tableName(QString())
TableSetBase::TableSetBase(Table *parent) : QObject(parent),
_database(nullptr), _table(parent), _tableName(QString())
{
parent->add(this);
}

View File

@ -48,9 +48,9 @@ public:
protected:
QSet<Table*> _tables;
QList<Table*> _tablesList;
QString _tableName;
Database *_database;
Table *_table;
QString _tableName;
QString _childClassName;
private:

View File

@ -16,8 +16,8 @@
#define DRIVER "QSQLITE"
#define HOST "127.0.0.1"
#define DATABASE "nutdb1"
#define USERNAME "root"
#define PASSWORD "onlyonlyi"
//#define USERNAME "root"
//#define PASSWORD "onlyonlyi"
//#define DRIVER "QODBC"
//#define HOST "127.0.0.1"
@ -46,4 +46,9 @@
<< "\n\tTest:" << metaObject()->className() \
<< "\n****************************\n";
#define TIC() QElapsedTimer timer; timer.start()
#define TOC() qDebug() << QString("Elapsed time: %1ms for %2") \
.arg(timer.elapsed() / 1000.) \
.arg(__func__)
#endif // CONSTS_H

View File

@ -17,10 +17,6 @@
#include "generators/sqlservergenerator.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*>()
@ -37,8 +33,8 @@ void MainTest::initTestCase()
db.setDriver(DRIVER);
db.setHostName(HOST);
db.setDatabaseName("nut_tst_basic");
db.setUserName(USERNAME);
db.setPassword(PASSWORD);
// db.setUserName(USERNAME);
// db.setPassword(PASSWORD);
bool ok = db.open();
QTEST_ASSERT(ok);
@ -99,7 +95,7 @@ void MainTest::types()
// << QMetaType::QByteArrayList
;
Nut::SqlServerGenerator g;
Nut::SqliteGenerator g;
Nut::FieldModel m;
foreach (QMetaType::Type t, types) {
m.type = t;

View File

@ -1,6 +1,8 @@
#ifndef SAMPLETABLE_H
#define SAMPLETABLE_H
#include <QPoint>
#include <QPolygon>
#include "table.h"
#define FIELD_Q(type) NUT_DECLARE_FIELD(q##type, f##type, f##type, setF##type)
@ -24,7 +26,10 @@ class SampleTable : public Nut::Table
NUT_DECLARE_FIELD(qreal, freal, freal, setFreal)
NUT_DECLARE_FIELD(float, ffloat, ffloat, setFfloat)
// NUT_DECLARE_FIELD(long double, fldouble, fldouble, setFldouble)
NUT_DECLARE_FIELD(long double, fldouble, fldouble, setFldouble)
NUT_DECLARE_FIELD(QString, string, string, setString)
NUT_DECLARE_FIELD(QPoint, point, point, setPoint)
NUT_DECLARE_FIELD(QPolygon, polygon, polygon, setPolygon)
public:
Q_INVOKABLE SampleTable(QObject *parent = Q_NULLPTR);