wip:serializer
This commit is contained in:
parent
944e7ddf70
commit
afcfa39ada
|
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "3rdparty/serializer"]
|
||||||
|
path = 3rdparty/serializer
|
||||||
|
url = https://github.com/HamedMasafi/Serializer.git
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit dc55dc615a935b077b2cb0ba4d75257dfea1df8e
|
||||||
1
nut.pri
1
nut.pri
|
|
@ -3,6 +3,7 @@ QT += core sql
|
||||||
CONFIG += c++11
|
CONFIG += c++11
|
||||||
|
|
||||||
INCLUDEPATH += $$PWD/include
|
INCLUDEPATH += $$PWD/include
|
||||||
|
include(3rdparty/serializer/src/src.pri)
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
$$PWD/src/generators/sqlgeneratorbase_p.h \
|
$$PWD/src/generators/sqlgeneratorbase_p.h \
|
||||||
|
|
|
||||||
|
|
@ -143,15 +143,17 @@ bool DatabasePrivate::updateDatabase()
|
||||||
qDebug("Databse is changed");
|
qDebug("Databse is changed");
|
||||||
|
|
||||||
QStringList sql = sqlGenertor->diff(last, current);
|
QStringList sql = sqlGenertor->diff(last, current);
|
||||||
qDebug()<<"database Sql =\n"<<sql;
|
|
||||||
db.transaction();
|
db.transaction();
|
||||||
foreach (QString s, sql) {
|
foreach (QString s, sql) {
|
||||||
db.exec(s);
|
db.exec(s);
|
||||||
|
|
||||||
if (db.lastError().type() != QSqlError::NoError)
|
if (db.lastError().type() != QSqlError::NoError) {
|
||||||
qWarning("Error executing sql command `%s`, %s",
|
qWarning("Error executing sql command `%s`, %s",
|
||||||
qPrintable(s),
|
qPrintable(s),
|
||||||
db.lastError().text().toLatin1().data());
|
db.lastError().text().toLatin1().data());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
putModelToDatabase();
|
putModelToDatabase();
|
||||||
bool ok = db.commit();
|
bool ok = db.commit();
|
||||||
|
|
@ -160,6 +162,7 @@ qDebug()<<"database Sql =\n"<<sql;
|
||||||
|
|
||||||
q->databaseUpdated(last.version(), current.version());
|
q->databaseUpdated(last.version(), current.version());
|
||||||
|
|
||||||
|
//TODO: remove this
|
||||||
for (int i = 0; i < q->metaObject()->methodCount(); i++) {
|
for (int i = 0; i < q->metaObject()->methodCount(); i++) {
|
||||||
QMetaMethod m = q->metaObject()->method(i);
|
QMetaMethod m = q->metaObject()->method(i);
|
||||||
if (m.name() == "update" + current.version()) {
|
if (m.name() == "update" + current.version()) {
|
||||||
|
|
@ -202,7 +205,7 @@ bool DatabasePrivate::getCurrectScheema()
|
||||||
QString type;
|
QString type;
|
||||||
QString name;
|
QString name;
|
||||||
QString value;
|
QString value;
|
||||||
qDebug()<<q->metaObject()->className();
|
|
||||||
if (!nutClassInfoString(q->metaObject()->classInfo(i),
|
if (!nutClassInfoString(q->metaObject()->classInfo(i),
|
||||||
type, name, value)) {
|
type, name, value)) {
|
||||||
qDebug() << "No valid table in" << q->metaObject()->classInfo(i).value();
|
qDebug() << "No valid table in" << q->metaObject()->classInfo(i).value();
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,10 @@ QString SqlGeneratorBase::recordsPhrase(TableModel *table)
|
||||||
|
|
||||||
QString SqlGeneratorBase::fieldDeclare(FieldModel *field)
|
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)
|
QString SqlGeneratorBase::relationDeclare(const RelationModel *relation)
|
||||||
|
|
@ -199,7 +202,10 @@ QString SqlGeneratorBase::diff(TableModel *oldTable, TableModel *newTable)
|
||||||
if (!buffer.isNull())
|
if (!buffer.isNull())
|
||||||
columnSql << buffer;
|
columnSql << buffer;
|
||||||
} else {
|
} else {
|
||||||
columnSql << fieldDeclare(newField);
|
QString declare = fieldDeclare(newField);
|
||||||
|
if (declare.isEmpty())
|
||||||
|
return declare;
|
||||||
|
columnSql << declare;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// foreach (QString fieldName, relations) {
|
// foreach (QString fieldName, relations) {
|
||||||
|
|
@ -471,6 +477,7 @@ QString SqlGeneratorBase::agregateText(const AgregateType &t,
|
||||||
case SignleField:
|
case SignleField:
|
||||||
return arg;
|
return arg;
|
||||||
}
|
}
|
||||||
|
return QString(); // never reach
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SqlGeneratorBase::fromTableText(const QString &tableName,
|
QString SqlGeneratorBase::fromTableText(const QString &tableName,
|
||||||
|
|
@ -801,7 +808,6 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const
|
||||||
|
|
||||||
case QVariant::Char:
|
case QVariant::Char:
|
||||||
case QVariant::String:
|
case QVariant::String:
|
||||||
qDebug() << v.toString();
|
|
||||||
return "'" + v.toString().replace("'", "''") + "'";
|
return "'" + v.toString().replace("'", "''") + "'";
|
||||||
|
|
||||||
case QVariant::DateTime:
|
case QVariant::DateTime:
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ AbstractFieldPhrase::AbstractFieldPhrase(AbstractFieldPhrase &&other)
|
||||||
{
|
{
|
||||||
data = other.data;
|
data = other.data;
|
||||||
data->parents++;
|
data->parents++;
|
||||||
other.data = 0;
|
other.data = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
AbstractFieldPhrase::~AbstractFieldPhrase()
|
AbstractFieldPhrase::~AbstractFieldPhrase()
|
||||||
|
|
|
||||||
|
|
@ -92,11 +92,12 @@ public:
|
||||||
|
|
||||||
Type type;
|
Type type;
|
||||||
|
|
||||||
|
Condition operatorCond;
|
||||||
|
|
||||||
PhraseData *left;
|
PhraseData *left;
|
||||||
PhraseData *right;
|
PhraseData *right;
|
||||||
|
|
||||||
QVariant operand;
|
QVariant operand;
|
||||||
Condition operatorCond;
|
|
||||||
bool isNot;
|
bool isNot;
|
||||||
quint16 parents;
|
quint16 parents;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,14 +25,14 @@
|
||||||
|
|
||||||
NUT_BEGIN_NAMESPACE
|
NUT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
TableSetBase::TableSetBase(Database *parent) : QObject(parent), _database(parent), _table(0),
|
TableSetBase::TableSetBase(Database *parent) : QObject(parent),
|
||||||
_tableName(QString())
|
_database(parent), _table(nullptr), _tableName(QString())
|
||||||
{
|
{
|
||||||
parent->add(this);
|
parent->add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
TableSetBase::TableSetBase(Table *parent) : QObject(parent), _database(0), _table(parent),
|
TableSetBase::TableSetBase(Table *parent) : QObject(parent),
|
||||||
_tableName(QString())
|
_database(nullptr), _table(parent), _tableName(QString())
|
||||||
{
|
{
|
||||||
parent->add(this);
|
parent->add(this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,9 +48,9 @@ public:
|
||||||
protected:
|
protected:
|
||||||
QSet<Table*> _tables;
|
QSet<Table*> _tables;
|
||||||
QList<Table*> _tablesList;
|
QList<Table*> _tablesList;
|
||||||
QString _tableName;
|
|
||||||
Database *_database;
|
Database *_database;
|
||||||
Table *_table;
|
Table *_table;
|
||||||
|
QString _tableName;
|
||||||
QString _childClassName;
|
QString _childClassName;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,8 @@
|
||||||
#define DRIVER "QSQLITE"
|
#define DRIVER "QSQLITE"
|
||||||
#define HOST "127.0.0.1"
|
#define HOST "127.0.0.1"
|
||||||
#define DATABASE "nutdb1"
|
#define DATABASE "nutdb1"
|
||||||
#define USERNAME "root"
|
//#define USERNAME "root"
|
||||||
#define PASSWORD "onlyonlyi"
|
//#define PASSWORD "onlyonlyi"
|
||||||
|
|
||||||
//#define DRIVER "QODBC"
|
//#define DRIVER "QODBC"
|
||||||
//#define HOST "127.0.0.1"
|
//#define HOST "127.0.0.1"
|
||||||
|
|
@ -46,4 +46,9 @@
|
||||||
<< "\n\tTest:" << metaObject()->className() \
|
<< "\n\tTest:" << metaObject()->className() \
|
||||||
<< "\n****************************\n";
|
<< "\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
|
#endif // CONSTS_H
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,6 @@
|
||||||
#include "generators/sqlservergenerator.h"
|
#include "generators/sqlservergenerator.h"
|
||||||
|
|
||||||
#define PRINT(x) qDebug() << #x "=" << x;
|
#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*>()
|
#define REGISTER(x) qDebug() << #x << "type id:" << qRegisterMetaType<x*>()
|
||||||
|
|
||||||
|
|
@ -37,8 +33,8 @@ void MainTest::initTestCase()
|
||||||
db.setDriver(DRIVER);
|
db.setDriver(DRIVER);
|
||||||
db.setHostName(HOST);
|
db.setHostName(HOST);
|
||||||
db.setDatabaseName("nut_tst_basic");
|
db.setDatabaseName("nut_tst_basic");
|
||||||
db.setUserName(USERNAME);
|
// db.setUserName(USERNAME);
|
||||||
db.setPassword(PASSWORD);
|
// db.setPassword(PASSWORD);
|
||||||
|
|
||||||
bool ok = db.open();
|
bool ok = db.open();
|
||||||
QTEST_ASSERT(ok);
|
QTEST_ASSERT(ok);
|
||||||
|
|
@ -99,7 +95,7 @@ void MainTest::types()
|
||||||
// << QMetaType::QByteArrayList
|
// << QMetaType::QByteArrayList
|
||||||
;
|
;
|
||||||
|
|
||||||
Nut::SqlServerGenerator g;
|
Nut::SqliteGenerator g;
|
||||||
Nut::FieldModel m;
|
Nut::FieldModel m;
|
||||||
foreach (QMetaType::Type t, types) {
|
foreach (QMetaType::Type t, types) {
|
||||||
m.type = t;
|
m.type = t;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef SAMPLETABLE_H
|
#ifndef SAMPLETABLE_H
|
||||||
#define SAMPLETABLE_H
|
#define SAMPLETABLE_H
|
||||||
|
|
||||||
|
#include <QPoint>
|
||||||
|
#include <QPolygon>
|
||||||
#include "table.h"
|
#include "table.h"
|
||||||
|
|
||||||
#define FIELD_Q(type) NUT_DECLARE_FIELD(q##type, f##type, f##type, setF##type)
|
#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(qreal, freal, freal, setFreal)
|
||||||
NUT_DECLARE_FIELD(float, ffloat, ffloat, setFfloat)
|
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:
|
public:
|
||||||
Q_INVOKABLE SampleTable(QObject *parent = Q_NULLPTR);
|
Q_INVOKABLE SampleTable(QObject *parent = Q_NULLPTR);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue