2016-05-21 16:09:03 +08:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Nut project.
|
|
|
|
|
** https://github.com/HamedMasafi/Nut
|
|
|
|
|
**
|
|
|
|
|
** Nut is free software: you can redistribute it and/or modify
|
|
|
|
|
** it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
** (at your option) any later version.
|
|
|
|
|
**
|
|
|
|
|
** Nut is distributed in the hope that it will be useful,
|
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
** GNU Lesser General Public License for more details.
|
|
|
|
|
**
|
|
|
|
|
** You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
** along with Nut. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
#include "mysqlgenerator.h"
|
2016-05-21 16:09:03 +08:00
|
|
|
#include "tablemodel.h"
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2017-06-12 00:50:43 +08:00
|
|
|
#include <QPoint>
|
|
|
|
|
#include <QPointF>
|
|
|
|
|
|
2017-02-01 18:01:21 +08:00
|
|
|
NUT_BEGIN_NAMESPACE
|
2016-05-12 14:08:58 +08:00
|
|
|
|
2016-05-24 14:47:37 +08:00
|
|
|
MySqlGenerator::MySqlGenerator(Database *parent) : SqlGeneratorBase(parent)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
QString MySqlGenerator::fieldType(FieldModel *field)
|
2016-05-12 14:08:58 +08:00
|
|
|
{
|
|
|
|
|
QString dbType;
|
|
|
|
|
|
|
|
|
|
switch (field->type) {
|
|
|
|
|
case QVariant::Bool:
|
|
|
|
|
dbType = "boolean";
|
|
|
|
|
break;
|
|
|
|
|
case QVariant::ByteArray:
|
|
|
|
|
dbType = "blob";
|
|
|
|
|
break;
|
2016-05-21 16:09:03 +08:00
|
|
|
case QVariant::DateTime:
|
|
|
|
|
dbType = "datetime";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case QVariant::Date:
|
|
|
|
|
dbType = "date";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case QVariant::Time:
|
|
|
|
|
dbType = "time";
|
|
|
|
|
break;
|
2016-05-12 14:08:58 +08:00
|
|
|
case QVariant::Double:
|
|
|
|
|
dbType = "real";
|
|
|
|
|
break;
|
|
|
|
|
case QVariant::Int:
|
|
|
|
|
dbType = "int(4)";
|
|
|
|
|
if(field->isAutoIncrement)
|
|
|
|
|
dbType += " auto_increment";
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case QVariant::String:
|
|
|
|
|
if(field->length)
|
|
|
|
|
dbType = QString("varchar(%1)").arg(field->length);
|
|
|
|
|
else
|
|
|
|
|
dbType = "text";
|
|
|
|
|
break;
|
2017-05-27 23:40:10 +08:00
|
|
|
|
|
|
|
|
case QVariant::Point:
|
|
|
|
|
case QVariant::PointF:
|
|
|
|
|
dbType = "POINT";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case QVariant::Polygon:
|
|
|
|
|
case QVariant::PolygonF:
|
|
|
|
|
dbType = "POLYGON";
|
|
|
|
|
break;
|
|
|
|
|
|
2016-05-12 14:08:58 +08:00
|
|
|
default:
|
2017-05-27 23:40:10 +08:00
|
|
|
qWarning("Type %s::%s(%d) is not supported",
|
|
|
|
|
qPrintable(field->name),
|
|
|
|
|
QMetaType::typeName(field->type),
|
|
|
|
|
field->type);
|
2016-05-12 14:08:58 +08:00
|
|
|
dbType = "";
|
|
|
|
|
}
|
2016-05-21 16:09:03 +08:00
|
|
|
|
2017-05-27 23:40:10 +08:00
|
|
|
if(field->typeName == QStringLiteral("Nut::DbGeography"))
|
|
|
|
|
dbType = "GEOMETRY";
|
|
|
|
|
|
2016-05-21 16:09:03 +08:00
|
|
|
return dbType;
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
|
|
|
|
|
2017-06-12 00:50:43 +08:00
|
|
|
QString MySqlGenerator::escapeValue(const QVariant &v) const
|
|
|
|
|
{
|
|
|
|
|
switch (v.type()) {
|
|
|
|
|
case QVariant::Point: {
|
|
|
|
|
QPoint pt = v.toPoint();
|
|
|
|
|
return QString("GeomFromText('POINT(%1 %2)',0)").arg(pt.x()).arg(pt.y());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case QVariant::PointF: {
|
|
|
|
|
QPointF pt = v.toPointF();
|
|
|
|
|
return QString("GeomFromText('POINT(%1 %2)',0)").arg(pt.x()).arg(pt.y());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return SqlGeneratorBase::escapeValue(v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant MySqlGenerator::readValue(const QVariant::Type &type, const QVariant &dbValue)
|
|
|
|
|
{
|
|
|
|
|
if (type == QVariant::PointF) {
|
|
|
|
|
qDebug() << "QVariant::PointF" << dbValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString MySqlGenerator::phrase(const PhraseData *d) const
|
|
|
|
|
{
|
|
|
|
|
if (d->operatorCond == PhraseData::Distance) {
|
|
|
|
|
return QString("ST_Distance(%1, %2)")
|
|
|
|
|
.arg(d->left->text)
|
|
|
|
|
.arg(escapeValue(d->operand.toPointF()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SqlGeneratorBase::phrase(d);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 18:01:21 +08:00
|
|
|
NUT_END_NAMESPACE
|