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"
|
2017-10-07 18:26:05 +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;
|
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;
|
|
|
|
|
|
2017-07-24 16:27:52 +08:00
|
|
|
case QVariant::Uuid:
|
|
|
|
|
dbType = "VARCHAR(64)";
|
|
|
|
|
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);
|
2018-04-03 13:27:11 +08:00
|
|
|
dbType = QString();
|
2016-05-12 14:08:58 +08:00
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 17:35:35 +08:00
|
|
|
QVariant MySqlGenerator::readValue(const QMetaType::Type &type, const QVariant &dbValue)
|
2017-06-12 00:50:43 +08:00
|
|
|
{
|
2019-02-10 17:35:35 +08:00
|
|
|
if (type == QMetaType::QPointF) {
|
2017-06-12 00:50:43 +08:00
|
|
|
qDebug() << "QVariant::PointF" << dbValue;
|
|
|
|
|
}
|
2017-09-10 22:18:20 +08:00
|
|
|
return SqlGeneratorBase::readValue(type, dbValue);
|
2017-06-12 00:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
2018-02-17 23:44:39 +08:00
|
|
|
//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);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//QString MySqlGenerator::selectCommand(SqlGeneratorBase::AgregateType t,
|
|
|
|
|
// QString agregateArg,
|
|
|
|
|
// QString tableName,
|
|
|
|
|
// QList<WherePhrase> &wheres,
|
|
|
|
|
// QList<WherePhrase> &orders,
|
|
|
|
|
// QList<RelationModel*> joins,
|
|
|
|
|
// int skip, int take)
|
|
|
|
|
//{
|
|
|
|
|
// QString command = SqlGeneratorBase::selectCommand(t, agregateArg,
|
|
|
|
|
// tableName,
|
|
|
|
|
// wheres, orders,
|
|
|
|
|
// joins, skip, take);
|
|
|
|
|
|
|
|
|
|
// if (take != -1 && skip != -1)
|
|
|
|
|
// command.append(QString(" LIMIT %1 OFFSET %2")
|
|
|
|
|
// .arg(take)
|
|
|
|
|
// .arg(skip));
|
|
|
|
|
// return command;
|
|
|
|
|
//}
|
2018-01-11 00:18:49 +08:00
|
|
|
|
2017-02-01 18:01:21 +08:00
|
|
|
NUT_END_NAMESPACE
|