support PostGis types with equal operator [skip ci]

This commit is contained in:
Hamed Masafi 2019-06-08 12:50:52 +04:30
parent d2822e3ef3
commit 5f60b325aa
8 changed files with 61 additions and 11 deletions

View File

@ -62,6 +62,16 @@ bool PostgreSqlGenerator::readInsideParentese(QString &text, QString &out)
return false; return false;
} }
bool PostgreSqlGenerator::isPostGisType(const QVariant::Type &t) const
{
return t == QVariant::Point
|| t == QVariant::PointF
|| t == QVariant::Rect
|| t == QVariant::RectF
|| t == QVariant::Polygon
|| t == QVariant::PolygonF;
}
PostgreSqlGenerator::PostgreSqlGenerator(Database *parent) : SqlGeneratorBase (parent) PostgreSqlGenerator::PostgreSqlGenerator(Database *parent) : SqlGeneratorBase (parent)
{ {
@ -215,14 +225,14 @@ QString PostgreSqlGenerator::escapeValue(const QVariant &v) const
if (v.type() == QVariant::Point) { if (v.type() == QVariant::Point) {
QPoint pt = v.toPoint(); QPoint pt = v.toPoint();
return QString("'(%1, %2)'").arg(pt.x()).arg(pt.y()); return QString("point(%1, %2)").arg(pt.x()).arg(pt.y());
} }
if (v.type() == QVariant::PointF) { if (v.type() == QVariant::PointF) {
QPointF pt = v.toPointF(); QPointF pt = v.toPointF();
return QString("'(%1, %2)'").arg(pt.x()).arg(pt.y()); return QString("point(%1, %2)").arg(pt.x()).arg(pt.y());
} }
if (v.userType() == QMetaType::QJsonDocument) { if (v.userType() == QMetaType::QJsonDocument) {
return "'" + QString(v.toJsonDocument().toJson()) + "'"; return "'" + QString(v.toJsonDocument().toJson(QJsonDocument::Compact)) + "'";
} }
#ifdef QT_GUI_LIB #ifdef QT_GUI_LIB
@ -313,5 +323,21 @@ QVariant PostgreSqlGenerator::unescapeValue(const QMetaType::Type &type, const Q
return SqlGeneratorBase::unescapeValue(type, dbValue); return SqlGeneratorBase::unescapeValue(type, dbValue);
} }
QString PostgreSqlGenerator::createConditionalPhrase(const PhraseData *d) const
{
if (!d)
return QString();
if (d->type == PhraseData::WithVariant) {
if (isPostGisType(d->operand.type()) && d->operatorCond == PhraseData::Equal) {
return QString("%1 ~= %2")
.arg(SqlGeneratorBase::createConditionalPhrase(d->left),
escapeValue(d->operand));
}
}
return SqlGeneratorBase::createConditionalPhrase(d);
}
NUT_END_NAMESPACE NUT_END_NAMESPACE

View File

@ -30,18 +30,22 @@ class PostgreSqlGenerator : public SqlGeneratorBase
{ {
private: private:
bool readInsideParentese(QString &ref, QString &out); bool readInsideParentese(QString &ref, QString &out);
bool isPostGisType(const QVariant::Type &t) const;
public: public:
explicit PostgreSqlGenerator(Database *parent = nullptr); explicit PostgreSqlGenerator(Database *parent = nullptr);
QString fieldType(FieldModel *field); QString fieldType(FieldModel *field) override;
QString diff(FieldModel *oldField, FieldModel *newField); QString diff(FieldModel *oldField, FieldModel *newField) override;
// SqlGeneratorBase interface // SqlGeneratorBase interface
public: public:
QString escapeValue(const QVariant &v) const; QString escapeValue(const QVariant &v) const override;
QVariant unescapeValue(const QMetaType::Type &type, const QVariant &dbValue); QVariant unescapeValue(const QMetaType::Type &type, const QVariant &dbValue) override;
// SqlGeneratorBase interface
protected:
QString createConditionalPhrase(const PhraseData *d) const override;
}; };
NUT_END_NAMESPACE NUT_END_NAMESPACE

View File

@ -151,7 +151,7 @@ public:
virtual QString primaryKeyConstraint(const TableModel *table) const; virtual QString primaryKeyConstraint(const TableModel *table) const;
protected: protected:
QString createConditionalPhrase(const PhraseData *d) const; virtual QString createConditionalPhrase(const PhraseData *d) const;
QString createFieldPhrase(const PhraseList &ph); QString createFieldPhrase(const PhraseList &ph);
QString createOrderPhrase(const PhraseList &ph); QString createOrderPhrase(const PhraseList &ph);
void createInsertPhrase(const AssignmentPhraseList &ph, QString &fields, QString &values); void createInsertPhrase(const AssignmentPhraseList &ph, QString &fields, QString &values);

View File

@ -118,7 +118,7 @@ Q_OUTOFLINE_TEMPLATE Query<T>::Query(Database *database, TableSetBase *tableSet,
d->className = T::staticMetaObject.className(); d->className = T::staticMetaObject.className();
d->tableName = d->tableName =
d->database->model() d->database->model()
.tableByClassName(T::staticMetaObject.className()) .tableByClassName(d->className)
->name(); ->name();
} }

View File

@ -35,7 +35,6 @@ void TestJson::initTestCase()
void TestJson::store() void TestJson::store()
{ {
DB db;
initDb(db); initDb(db);
db.open(); db.open();
@ -57,5 +56,10 @@ void TestJson::store()
Q_ASSERT(newObj->doc() == t->doc()); Q_ASSERT(newObj->doc() == t->doc());
} }
void TestJson::cleanupTestCase()
{
PRINT_FORM(db);
}
QTEST_APPLESS_MAIN(TestJson) QTEST_APPLESS_MAIN(TestJson)

View File

@ -1,6 +1,8 @@
#ifndef TST_TESTJSON_H #ifndef TST_TESTJSON_H
#define TST_TESTJSON_H #define TST_TESTJSON_H
#include "db.h"
#include <QObject> #include <QObject>
namespace Nut { namespace Nut {
@ -10,6 +12,8 @@ class TestJson : public QObject
{ {
Q_OBJECT Q_OBJECT
DB db;
void initDb(Nut::Database &db); void initDb(Nut::Database &db);
int id; int id;
@ -22,6 +26,8 @@ private slots:
void store(); void store();
void cleanupTestCase();
}; };
#endif // TST_TESTJSON_H #endif // TST_TESTJSON_H

View File

@ -49,6 +49,7 @@ void Upgrades::version1()
DB1 db; DB1 db;
initDb(db); initDb(db);
QTEST_ASSERT(db.open()); QTEST_ASSERT(db.open());
db.sampleTable()->query()->remove();
} }
void Upgrades::version2() void Upgrades::version2()
@ -75,6 +76,13 @@ void Upgrades::version3()
QTEST_ASSERT(id == t->id()); QTEST_ASSERT(id == t->id());
} }
void Upgrades::cleanupTestCase()
{
DB1 db;
initDb(db);
PRINT_FORM(db);
}
QTEST_APPLESS_MAIN(Upgrades) QTEST_APPLESS_MAIN(Upgrades)

View File

@ -24,6 +24,8 @@ private slots:
void version2(); void version2();
void version3(); void version3();
void cleanupTestCase();
}; };
#endif // TST_UPGRADES_H #endif // TST_UPGRADES_H