From f5c10e8759ae31db6bebf845108de2b5019292b7 Mon Sep 17 00:00:00 2001 From: Hamed Masafi Date: Fri, 8 Feb 2019 12:09:24 +0330 Subject: [PATCH] subdirs --- nut.pro | 55 ++----------------- src/database.cpp | 11 ++++ src/generators/sqlgeneratorbase.cpp | 24 +++++--- src/generators/sqlgeneratorbase_p.h | 4 ++ src/generators/sqlitegenerator.cpp | 8 ++- src/src.pro | 55 +++++++++++++++++++ test/common/consts.h | 4 +- test/common/post.cpp | 4 +- test/test.pro | 11 ++++ test/{basic => tst_basic}/maintest.cpp | 0 test/{basic => tst_basic}/maintest.h | 0 test/{basic => tst_basic}/tst_basic.pro | 0 .../{benckmark => tst_benckmark}/maintest.cpp | 0 test/{benckmark => tst_benckmark}/maintest.h | 0 .../tst_benckmark.pro} | 0 test/{commands => tst_commands}/maintest.cpp | 0 test/{commands => tst_commands}/maintest.h | 0 .../tst_commands.pro | 0 test/{datatypes => tst_datatypes}/db.cpp | 0 test/{datatypes => tst_datatypes}/db.h | 0 .../{datatypes => tst_datatypes}/maintest.cpp | 19 ++++++- test/{datatypes => tst_datatypes}/maintest.h | 2 +- .../sampletable.cpp | 0 .../sampletable.h | 2 +- .../tst_datatypes.pro | 0 test/{join => tst_join}/jointest.cpp | 0 test/{join => tst_join}/jointest.h | 0 test/{join => tst_join}/tst_join.pro | 0 test/{phrases => tst_phrases}/maintest.cpp | 0 test/{phrases => tst_phrases}/maintest.h | 0 test/{phrases => tst_phrases}/tst_phrases.pro | 0 test/{quuid => tst_quuid}/maintest.cpp | 0 test/{quuid => tst_quuid}/maintest.h | 0 test/{quuid => tst_quuid}/test.cpp | 0 test/{quuid => tst_quuid}/test.h | 0 test/{quuid => tst_quuid}/testdatabase.cpp | 0 test/{quuid => tst_quuid}/testdatabase.h | 0 test/{quuid => tst_quuid}/tst_quuid.pro | 0 38 files changed, 130 insertions(+), 69 deletions(-) create mode 100644 src/src.pro create mode 100644 test/test.pro rename test/{basic => tst_basic}/maintest.cpp (100%) rename test/{basic => tst_basic}/maintest.h (100%) rename test/{basic => tst_basic}/tst_basic.pro (100%) rename test/{benckmark => tst_benckmark}/maintest.cpp (100%) rename test/{benckmark => tst_benckmark}/maintest.h (100%) rename test/{benckmark/tst_benchmark.pro => tst_benckmark/tst_benckmark.pro} (100%) rename test/{commands => tst_commands}/maintest.cpp (100%) rename test/{commands => tst_commands}/maintest.h (100%) rename test/{commands => tst_commands}/tst_commands.pro (100%) rename test/{datatypes => tst_datatypes}/db.cpp (100%) rename test/{datatypes => tst_datatypes}/db.h (100%) rename test/{datatypes => tst_datatypes}/maintest.cpp (88%) rename test/{datatypes => tst_datatypes}/maintest.h (95%) rename test/{datatypes => tst_datatypes}/sampletable.cpp (100%) rename test/{datatypes => tst_datatypes}/sampletable.h (94%) rename test/{datatypes => tst_datatypes}/tst_datatypes.pro (100%) rename test/{join => tst_join}/jointest.cpp (100%) rename test/{join => tst_join}/jointest.h (100%) rename test/{join => tst_join}/tst_join.pro (100%) rename test/{phrases => tst_phrases}/maintest.cpp (100%) rename test/{phrases => tst_phrases}/maintest.h (100%) rename test/{phrases => tst_phrases}/tst_phrases.pro (100%) rename test/{quuid => tst_quuid}/maintest.cpp (100%) rename test/{quuid => tst_quuid}/maintest.h (100%) rename test/{quuid => tst_quuid}/test.cpp (100%) rename test/{quuid => tst_quuid}/test.h (100%) rename test/{quuid => tst_quuid}/testdatabase.cpp (100%) rename test/{quuid => tst_quuid}/testdatabase.h (100%) rename test/{quuid => tst_quuid}/tst_quuid.pro (100%) diff --git a/nut.pro b/nut.pro index 6bc6b01..5a35529 100644 --- a/nut.pro +++ b/nut.pro @@ -1,53 +1,6 @@ -QT += sql -QT -= gui +TEMPLATE = subdirs -TARGET = nut -TEMPLATE = lib -CONFIG += c++11 +SUBDIRS += \ + src \ + test -DEFINES += QT_DEPRECATED_WARNINGS - -HEADERS += \ - $$PWD/src/generators/sqlgeneratorbase_p.h \ - $$PWD/src/generators/postgresqlgenerator.h \ - $$PWD/src/generators/mysqlgenerator.h \ - $$PWD/src/generators/sqlitegenerator.h \ - $$PWD/src/generators/sqlservergenerator.h \ - $$PWD/src/types/dbgeography.h \ - $$PWD/src/tableset.h \ - $$PWD/src/defines_p.h \ - $$PWD/src/defines.h \ - $$PWD/src/query.h \ - $$PWD/src/databasemodel.h \ - $$PWD/src/changelogtable.h \ - $$PWD/src/tablesetbase_p.h \ - $$PWD/src/querybase_p.h \ - $$PWD/src/tablemodel.h \ - $$PWD/src/query_p.h \ - $$PWD/src/table.h \ - $$PWD/src/database.h \ - $$PWD/src/database_p.h \ - $$PWD/src/serializableobject.h \ - $$PWD/src/sqlmodel.h \ - $$PWD/src/sqlmodel_p.h \ - $$PWD/src/phrase.h - -SOURCES += \ - $$PWD/src/generators/sqlgeneratorbase.cpp \ - $$PWD/src/generators/postgresqlgenerator.cpp \ - $$PWD/src/generators/mysqlgenerator.cpp \ - $$PWD/src/generators/sqlitegenerator.cpp \ - $$PWD/src/generators/sqlservergenerator.cpp \ - $$PWD/src/types/dbgeography.cpp \ - $$PWD/src/tableset.cpp \ - $$PWD/src/query.cpp \ - $$PWD/src/databasemodel.cpp \ - $$PWD/src/tablesetbase.cpp \ - $$PWD/src/changelogtable.cpp \ - $$PWD/src/querybase.cpp \ - $$PWD/src/tablemodel.cpp \ - $$PWD/src/table.cpp \ - $$PWD/src/database.cpp \ - $$PWD/src/serializableobject.cpp \ - $$PWD/src/sqlmodel.cpp \ - $$PWD/src/phrase.cpp diff --git a/src/database.cpp b/src/database.cpp index 8e68031..6d61c30 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -137,6 +137,17 @@ bool DatabasePrivate::updateDatabase() return true; } + foreach (TableModel *tm, current) { + foreach (FieldModel *fm, tm->fields()) { + if (sqlGenertor->fieldType(fm).isEmpty()) { + qWarning("The type (%s) is not supported for field %s::%s", + QMetaType::typeName(fm->type), + qPrintable(tm->className()), + qPrintable(fm->name)); + return false; + } + } + } if (!last.count()) qDebug("Databse is new"); else diff --git a/src/generators/sqlgeneratorbase.cpp b/src/generators/sqlgeneratorbase.cpp index f99f110..7ecb036 100644 --- a/src/generators/sqlgeneratorbase.cpp +++ b/src/generators/sqlgeneratorbase.cpp @@ -31,6 +31,7 @@ #include "../table.h" #include "../databasemodel.h" #include "../tablemodel.h" +#include "stringserializer.h" NUT_BEGIN_NAMESPACE @@ -56,6 +57,8 @@ SqlGeneratorBase::SqlGeneratorBase(Database *parent) { if (parent) _database = parent; + + _serializer = new StringSerializer; } SqlGeneratorBase::~SqlGeneratorBase() @@ -792,6 +795,13 @@ void SqlGeneratorBase::removeTableNames(QString &command) QString SqlGeneratorBase::escapeValue(const QVariant &v) const { + QString serialized = _serializer->toString(v); + if (serialized.isEmpty()) { + qWarning("No field escape rule for: %s", v.typeName()); + return QString(); + } + return "'" + serialized + "'"; + switch (v.type()) { case QVariant::Bool: return v.toBool() ? "1" : "0"; @@ -823,15 +833,11 @@ QString SqlGeneratorBase::escapeValue(const QVariant &v) const case QVariant::List: return "('" + v.toStringList().join("', '") + "')"; - case QVariant::Point: { - QPoint pt = v.toPoint(); - return QString("POINT(%1 %2)").arg(pt.x()).arg(pt.y()); - } - - case QVariant::PointF: { - QPointF pt = v.toPointF(); - return QString("POINT(%1 %2)").arg(pt.x()).arg(pt.y()); - } + case QVariant::Point: + case QVariant::PointF: + case QVariant::Polygon: + case QVariant::PolygonF: + return "'" + _serializer->toString(v) + "'"; case QVariant::Invalid: qFatal("Invalud field value"); diff --git a/src/generators/sqlgeneratorbase_p.h b/src/generators/sqlgeneratorbase_p.h index 821ba40..c3bfd45 100644 --- a/src/generators/sqlgeneratorbase_p.h +++ b/src/generators/sqlgeneratorbase_p.h @@ -27,6 +27,8 @@ #include "../phrase.h" //#include "../wherephrase.h" +class StringSerializer; + NUT_BEGIN_NAMESPACE class Table; @@ -40,6 +42,8 @@ class SqlGeneratorBase : public QObject // Q_OBJECT Database *_database; + StringSerializer *_serializer; + public: //TODO: remove this enum enum CommandType{ diff --git a/src/generators/sqlitegenerator.cpp b/src/generators/sqlitegenerator.cpp index 8396e01..374e683 100644 --- a/src/generators/sqlitegenerator.cpp +++ b/src/generators/sqlitegenerator.cpp @@ -67,6 +67,10 @@ QString SqliteGenerator::fieldType(FieldModel *field) case QMetaType::QJsonValue: case QMetaType::QJsonObject: case QMetaType::QJsonDocument: + case QMetaType::QPoint: + case QMetaType::QPointF: + case QMetaType::QPolygon: + case QMetaType::QPolygonF: case QMetaType::QUuid: return "text"; // if (field->isAutoIncrement) @@ -78,8 +82,8 @@ QString SqliteGenerator::fieldType(FieldModel *field) else return "TEXT"; default: - qWarning("The type (%s) does not supported", - QMetaType::typeName(field->type)); +// qWarning("The type (%s) does not supported", +// QMetaType::typeName(field->type)); return QString(); } } diff --git a/src/src.pro b/src/src.pro new file mode 100644 index 0000000..33e1f26 --- /dev/null +++ b/src/src.pro @@ -0,0 +1,55 @@ +QT += sql +QT -= gui + +TARGET = nut +TEMPLATE = lib +CONFIG += c++11 + +DEFINES += QT_DEPRECATED_WARNINGS + +HEADERS += \ + $$PWD/generators/sqlgeneratorbase_p.h \ + $$PWD/generators/postgresqlgenerator.h \ + $$PWD/generators/mysqlgenerator.h \ + $$PWD/generators/sqlitegenerator.h \ + $$PWD/generators/sqlservergenerator.h \ + $$PWD/types/dbgeography.h \ + $$PWD/tableset.h \ + $$PWD/defines_p.h \ + $$PWD/defines.h \ + $$PWD/query.h \ + $$PWD/databasemodel.h \ + $$PWD/changelogtable.h \ + $$PWD/tablesetbase_p.h \ + $$PWD/querybase_p.h \ + $$PWD/tablemodel.h \ + $$PWD/query_p.h \ + $$PWD/table.h \ + $$PWD/database.h \ + $$PWD/database_p.h \ + $$PWD/serializableobject.h \ + $$PWD/sqlmodel.h \ + $$PWD/sqlmodel_p.h \ + $$PWD/phrase.h + +SOURCES += \ + $$PWD/generators/sqlgeneratorbase.cpp \ + $$PWD/generators/postgresqlgenerator.cpp \ + $$PWD/generators/mysqlgenerator.cpp \ + $$PWD/generators/sqlitegenerator.cpp \ + $$PWD/generators/sqlservergenerator.cpp \ + $$PWD/types/dbgeography.cpp \ + $$PWD/tableset.cpp \ + $$PWD/query.cpp \ + $$PWD/databasemodel.cpp \ + $$PWD/tablesetbase.cpp \ + $$PWD/changelogtable.cpp \ + $$PWD/querybase.cpp \ + $$PWD/tablemodel.cpp \ + $$PWD/table.cpp \ + $$PWD/database.cpp \ + $$PWD/serializableobject.cpp \ + $$PWD/sqlmodel.cpp \ + $$PWD/phrase.cpp + +include($$PWD/../3rdparty/serializer/src/src.pri) diff --git a/test/common/consts.h b/test/common/consts.h index 18c1788..d0f6a7e 100644 --- a/test/common/consts.h +++ b/test/common/consts.h @@ -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" diff --git a/test/common/post.cpp b/test/common/post.cpp index 9aa932a..13123af 100644 --- a/test/common/post.cpp +++ b/test/common/post.cpp @@ -11,5 +11,5 @@ Post::Post(QObject *parent) : Table(parent), } -NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments) -NUT_IMPLEMENT_CHILD_TABLE(Post, Score, scores) +//NUT_IMPLEMENT_CHILD_TABLE(Post, Comment, comments) +//NUT_IMPLEMENT_CHILD_TABLE(Post, Score, scores) diff --git a/test/test.pro b/test/test.pro new file mode 100644 index 0000000..11dbe29 --- /dev/null +++ b/test/test.pro @@ -0,0 +1,11 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + tst_basic \ + tst_benckmark \ + tst_commands \ + tst_datatypes \ + #tst_join \ + tst_phrases \ + tst_quuid + diff --git a/test/basic/maintest.cpp b/test/tst_basic/maintest.cpp similarity index 100% rename from test/basic/maintest.cpp rename to test/tst_basic/maintest.cpp diff --git a/test/basic/maintest.h b/test/tst_basic/maintest.h similarity index 100% rename from test/basic/maintest.h rename to test/tst_basic/maintest.h diff --git a/test/basic/tst_basic.pro b/test/tst_basic/tst_basic.pro similarity index 100% rename from test/basic/tst_basic.pro rename to test/tst_basic/tst_basic.pro diff --git a/test/benckmark/maintest.cpp b/test/tst_benckmark/maintest.cpp similarity index 100% rename from test/benckmark/maintest.cpp rename to test/tst_benckmark/maintest.cpp diff --git a/test/benckmark/maintest.h b/test/tst_benckmark/maintest.h similarity index 100% rename from test/benckmark/maintest.h rename to test/tst_benckmark/maintest.h diff --git a/test/benckmark/tst_benchmark.pro b/test/tst_benckmark/tst_benckmark.pro similarity index 100% rename from test/benckmark/tst_benchmark.pro rename to test/tst_benckmark/tst_benckmark.pro diff --git a/test/commands/maintest.cpp b/test/tst_commands/maintest.cpp similarity index 100% rename from test/commands/maintest.cpp rename to test/tst_commands/maintest.cpp diff --git a/test/commands/maintest.h b/test/tst_commands/maintest.h similarity index 100% rename from test/commands/maintest.h rename to test/tst_commands/maintest.h diff --git a/test/commands/tst_commands.pro b/test/tst_commands/tst_commands.pro similarity index 100% rename from test/commands/tst_commands.pro rename to test/tst_commands/tst_commands.pro diff --git a/test/datatypes/db.cpp b/test/tst_datatypes/db.cpp similarity index 100% rename from test/datatypes/db.cpp rename to test/tst_datatypes/db.cpp diff --git a/test/datatypes/db.h b/test/tst_datatypes/db.h similarity index 100% rename from test/datatypes/db.h rename to test/tst_datatypes/db.h diff --git a/test/datatypes/maintest.cpp b/test/tst_datatypes/maintest.cpp similarity index 88% rename from test/datatypes/maintest.cpp rename to test/tst_datatypes/maintest.cpp index 31bfeea..cba0b83 100644 --- a/test/datatypes/maintest.cpp +++ b/test/tst_datatypes/maintest.cpp @@ -103,7 +103,24 @@ void MainTest::types() Q_ASSERT(!fn.isEmpty()); } // for (int i = 0; i < en.keyCount(); i++) -// qDebug() << en.value(i); + // qDebug() << en.value(i); +} + +void MainTest::insert() +{ + SampleTable t; + t.setFint8(1); + t.setFreal(1.2); + t.setFfloat(4.5f); + t.setFint16(16); + t.setFint32(65000); + t.setFint64(3255465232); + t.setFuint8(2); + t.setPoint(QPoint(1, 2)); + t.setPolygon(QPolygon() << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6)); + + db.sampleTables()->append(&t); + db.saveChanges(); } void MainTest::cleanupTestCase() diff --git a/test/datatypes/maintest.h b/test/tst_datatypes/maintest.h similarity index 95% rename from test/datatypes/maintest.h rename to test/tst_datatypes/maintest.h index 0c2d1d4..455af12 100644 --- a/test/datatypes/maintest.h +++ b/test/tst_datatypes/maintest.h @@ -20,7 +20,7 @@ private slots: void initTestCase(); void types(); - + void insert(); void cleanupTestCase(); }; diff --git a/test/datatypes/sampletable.cpp b/test/tst_datatypes/sampletable.cpp similarity index 100% rename from test/datatypes/sampletable.cpp rename to test/tst_datatypes/sampletable.cpp diff --git a/test/datatypes/sampletable.h b/test/tst_datatypes/sampletable.h similarity index 94% rename from test/datatypes/sampletable.h rename to test/tst_datatypes/sampletable.h index de92b87..ae0a362 100644 --- a/test/datatypes/sampletable.h +++ b/test/tst_datatypes/sampletable.h @@ -26,7 +26,7 @@ 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) diff --git a/test/datatypes/tst_datatypes.pro b/test/tst_datatypes/tst_datatypes.pro similarity index 100% rename from test/datatypes/tst_datatypes.pro rename to test/tst_datatypes/tst_datatypes.pro diff --git a/test/join/jointest.cpp b/test/tst_join/jointest.cpp similarity index 100% rename from test/join/jointest.cpp rename to test/tst_join/jointest.cpp diff --git a/test/join/jointest.h b/test/tst_join/jointest.h similarity index 100% rename from test/join/jointest.h rename to test/tst_join/jointest.h diff --git a/test/join/tst_join.pro b/test/tst_join/tst_join.pro similarity index 100% rename from test/join/tst_join.pro rename to test/tst_join/tst_join.pro diff --git a/test/phrases/maintest.cpp b/test/tst_phrases/maintest.cpp similarity index 100% rename from test/phrases/maintest.cpp rename to test/tst_phrases/maintest.cpp diff --git a/test/phrases/maintest.h b/test/tst_phrases/maintest.h similarity index 100% rename from test/phrases/maintest.h rename to test/tst_phrases/maintest.h diff --git a/test/phrases/tst_phrases.pro b/test/tst_phrases/tst_phrases.pro similarity index 100% rename from test/phrases/tst_phrases.pro rename to test/tst_phrases/tst_phrases.pro diff --git a/test/quuid/maintest.cpp b/test/tst_quuid/maintest.cpp similarity index 100% rename from test/quuid/maintest.cpp rename to test/tst_quuid/maintest.cpp diff --git a/test/quuid/maintest.h b/test/tst_quuid/maintest.h similarity index 100% rename from test/quuid/maintest.h rename to test/tst_quuid/maintest.h diff --git a/test/quuid/test.cpp b/test/tst_quuid/test.cpp similarity index 100% rename from test/quuid/test.cpp rename to test/tst_quuid/test.cpp diff --git a/test/quuid/test.h b/test/tst_quuid/test.h similarity index 100% rename from test/quuid/test.h rename to test/tst_quuid/test.h diff --git a/test/quuid/testdatabase.cpp b/test/tst_quuid/testdatabase.cpp similarity index 100% rename from test/quuid/testdatabase.cpp rename to test/tst_quuid/testdatabase.cpp diff --git a/test/quuid/testdatabase.h b/test/tst_quuid/testdatabase.h similarity index 100% rename from test/quuid/testdatabase.h rename to test/tst_quuid/testdatabase.h diff --git a/test/quuid/tst_quuid.pro b/test/tst_quuid/tst_quuid.pro similarity index 100% rename from test/quuid/tst_quuid.pro rename to test/tst_quuid/tst_quuid.pro