Nut/src/generators/sqlservergenerator.cpp

156 lines
4.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-16 15:42:38 +08:00
#include "sqlservergenerator.h"
2017-10-07 18:26:05 +08:00
#include "../table.h"
#include "../tablemodel.h"
2016-05-21 16:09:03 +08:00
2017-06-02 02:49:44 +08:00
#include <QPoint>
2016-05-21 16:09:03 +08:00
#include <QRegularExpression>
2017-02-01 18:01:21 +08:00
NUT_BEGIN_NAMESPACE
2016-05-21 16:09:03 +08:00
2017-08-24 23:35:23 +08:00
SqlServerGenerator::SqlServerGenerator(Database *parent)
: SqlGeneratorBase(parent)
2016-05-21 16:09:03 +08:00
{
}
QString SqlServerGenerator::masterDatabaseName(QString databaseName)
{
2017-08-24 23:35:23 +08:00
return databaseName.replace(
QRegularExpression("DATABASE\\=(\\w+)",
QRegularExpression::CaseInsensitiveOption),
"DATABASE=");
2016-05-21 16:09:03 +08:00
}
2016-05-16 15:42:38 +08:00
2016-05-21 16:09:03 +08:00
QString SqlServerGenerator::fieldType(FieldModel *field)
2016-05-16 15:42:38 +08:00
{
2016-05-21 16:09:03 +08:00
QString dbType;
switch (field->type) {
case QVariant::Bool:
2017-07-24 16:27:52 +08:00
dbType = "BIT";
2016-05-21 16:09:03 +08:00
break;
case QVariant::ByteArray:
2017-07-24 16:27:52 +08:00
dbType = "VARBINARY";
2016-05-21 16:09:03 +08:00
2017-08-24 23:35:23 +08:00
if (field->length)
2016-05-21 16:09:03 +08:00
dbType.append(" (" + QString::number(field->length) + ")");
else
dbType.append(" (MAX)");
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::DateTime:
2017-07-24 16:27:52 +08:00
dbType = "DATETIME";
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;
case QVariant::Double:
2017-07-24 16:27:52 +08:00
dbType = "REAL";
2016-05-21 16:09:03 +08:00
break;
case QVariant::Int:
2017-07-24 16:27:52 +08:00
dbType = "INT";
2017-08-24 23:35:23 +08:00
if (field->isAutoIncrement)
2017-07-24 16:27:52 +08:00
dbType += " IDENTITY(1,1)";
2016-05-21 16:09:03 +08:00
break;
2017-06-02 02:45:40 +08:00
case QVariant::Point:
case QVariant::PointF:
2017-06-02 14:17:31 +08:00
dbType = "GEOMETRY";
2017-06-02 02:45:40 +08:00
break;
2016-05-21 16:09:03 +08:00
case QVariant::String:
2017-08-24 23:35:23 +08:00
if (field->length)
2017-07-24 16:27:52 +08:00
dbType = QString("NVARCHAR(%1)").arg(field->length);
2016-05-21 16:09:03 +08:00
else
2017-07-24 16:27:52 +08:00
dbType = "NVARCHAR(MAX)";
2016-05-21 16:09:03 +08:00
break;
2017-07-24 16:27:52 +08:00
case QVariant::Uuid:
dbType = "UNIQUEIDENTIFIER";
break;
2016-05-21 16:09:03 +08:00
default:
dbType = "";
}
2016-05-16 15:42:38 +08:00
2016-05-21 16:09:03 +08:00
return dbType;
2016-05-16 15:42:38 +08:00
}
2016-05-21 16:09:03 +08:00
QString SqlServerGenerator::diff(FieldModel *oldField, FieldModel *newField)
{
QString sql = "";
2017-08-24 23:35:23 +08:00
if (oldField && newField)
if (*oldField == *newField)
2016-05-21 16:09:03 +08:00
return QString::null;
2017-08-24 23:35:23 +08:00
if (!newField) {
2016-05-21 16:09:03 +08:00
sql = "DROP COLUMN " + oldField->name;
2017-08-24 23:35:23 +08:00
} else {
if (oldField)
2016-05-21 16:09:03 +08:00
sql = "MODIFY COLUMN ";
else
sql = "ADD ";
sql.append(fieldDeclare(newField));
}
return sql;
}
2016-06-05 20:22:26 +08:00
QString SqlServerGenerator::escapeValue(const QVariant &v) const
{
2017-08-24 23:35:23 +08:00
if (v.type() == QVariant::String || v.type() == QVariant::Char)
2016-06-05 20:22:26 +08:00
return "N'" + v.toString() + "'";
2017-06-02 02:49:44 +08:00
else if (v.type() == QVariant::Point) {
QPoint pt = v.toPoint();
2017-08-24 23:35:23 +08:00
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
pt.y());
2017-06-02 02:49:44 +08:00
} else if (v.type() == QVariant::Point) {
QPointF pt = v.toPointF();
2017-08-24 23:35:23 +08:00
return QString("geography::POINT(%1, %2, 4326)").arg(pt.x()).arg(
pt.y());
2017-06-02 02:49:44 +08:00
}
2017-08-24 23:35:23 +08:00
return SqlGeneratorBase::escapeValue(v);
}
2018-01-13 23:59:55 +08:00
QString SqlServerGenerator::selectCommand(SqlGeneratorBase::AgregateType t,
QString agregateArg,
QString tableName,
QList<WherePhrase> &wheres,
QList<WherePhrase> &orders,
QList<RelationModel*> joins, int skip, int take)
2017-08-24 23:35:23 +08:00
{
2018-01-13 23:59:55 +08:00
QString command = SqlGeneratorBase::selectCommand(t, agregateArg,
tableName,
wheres, orders,
joins, skip, take);
2017-08-24 23:35:23 +08:00
if (take != -1 && skip != -1)
2017-10-07 19:45:20 +08:00
command.append(QString("OFFSET %1 ROWS FETCH NEXT %2 ROWS ONLY")
2017-08-24 23:35:23 +08:00
.arg(skip)
.arg(take));
return command;
2016-06-05 20:22:26 +08:00
}
2017-02-01 18:01:21 +08:00
NUT_END_NAMESPACE