Nut/src/mysqlgenerator.cpp

137 lines
3.4 KiB
C++
Raw Normal View History

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:
2017-07-24 16:27:52 +08:00
dbType = "BOOLEAN";
2016-05-12 14:08:58 +08:00
break;
case QVariant::ByteArray:
2017-07-24 16:27:52 +08:00
dbType = "BLOB";
2016-05-12 14:08:58 +08:00
break;
2016-05-21 16:09:03 +08:00
case QVariant::DateTime:
2017-07-24 16:27:52 +08:00
dbType = "DATETIME";
2016-05-21 16:09:03 +08:00
break;
case QVariant::Date:
2017-07-24 16:27:52 +08:00
dbType = "DATE";
2016-05-21 16:09:03 +08:00
break;
case QVariant::Time:
2017-07-24 16:27:52 +08:00
dbType = "TIME";
2016-05-21 16:09:03 +08:00
break;
2016-05-12 14:08:58 +08:00
case QVariant::Double:
2017-07-24 16:27:52 +08:00
dbType = "REAL";
2016-05-12 14:08:58 +08:00
break;
case QVariant::Int:
2017-07-24 16:27:52 +08:00
dbType = "INT(4)";
2016-05-12 14:08:58 +08:00
if(field->isAutoIncrement)
2017-07-24 16:27:52 +08:00
dbType += " AUTO_INCREMENT";
2016-05-12 14:08:58 +08:00
break;
case QVariant::String:
if(field->length)
2017-07-24 16:27:52 +08:00
dbType = QString("VARCHAR(%1)").arg(field->length);
2016-05-12 14:08:58 +08:00
else
2017-07-24 16:27:52 +08:00
dbType = "TEXT";
2016-05-12 14:08:58 +08:00
break;
case QVariant::Point:
case QVariant::PointF:
dbType = "POINT";
break;
case QVariant::Polygon:
case QVariant::PolygonF:
dbType = "POLYGON";
break;
2017-07-24 16:27:52 +08:00
case QVariant::Uuid:
dbType = "VARCHAR(64)";
break;
2016-05-12 14:08:58 +08:00
default:
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
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