Herunterladen als
root/src/queryinterpreter.cpp @ 9e2d71d6
38838b5b | Christian Ehringfeld | /*
|
|
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|||
*
|
|||
* This program 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.
|
|||
*
|
|||
* This program 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 General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
2ee5022f | Christian Ehringfeld | #include "queryinterpreter.h"
|
|
3160499c | Christian Ehringfeld | #include "join.h"
|
|
#include "query.h"
|
|||
#include "querybuilder.h"
|
|||
#include "orderby.h"
|
|||
#include "expression.h"
|
|||
#include "schema.h"
|
|||
2ee5022f | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
QueryInterpreter::QueryInterpreter(QSharedPointer<QueryBuilder> builder) {
|
|||
this->builder = builder;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QSqlQuery QueryInterpreter::build(Query &q) {
|
|
3160499c | Christian Ehringfeld | QList<QString> clauses = QList<QString>();
|
|
1b167b6c | Christian Ehringfeld | clauses.append(this->buildSelect(q, q.getSelect(), q.getDistinct(),
|
|
3160499c | Christian Ehringfeld | q.getSelectOption()));
|
|
clauses.append(this->buildFrom(q.getFrom()));
|
|||
clauses.append(this->buildJoin(q.getJoins()));
|
|||
1b167b6c | Christian Ehringfeld | clauses.append(this->buildWhere(q, q.getWhere()));
|
|
3160499c | Christian Ehringfeld | clauses.append(this->buildGroupBy(q.getGroupBy()));
|
|
1b167b6c | Christian Ehringfeld | clauses.append(this->buildHaving(q, q.getHaving()));
|
|
3160499c | Christian Ehringfeld | QString sql = "";
|
|
bool first = true;
|
|||
for (int i = 0; i < clauses.size(); ++i) {
|
|||
QString clause = clauses.at(i);
|
|||
if (!clause.isEmpty()) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
sql += this->builder->getSeparator();
|
|||
}
|
|||
sql += clause;
|
|||
}
|
|||
}
|
|||
sql = this->buildOrderByAndLimit(sql, q.getOrderBy(), q.getLimit(),
|
|||
q.getOffset());
|
|||
QSqlQuery sqlQuery = this->builder->getQuery();
|
|||
sqlQuery.prepare(sql);
|
|||
this->builder->bindValues(q.getParams(), sqlQuery, false);
|
|||
return sqlQuery;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QString QueryInterpreter::buildSelect(Query &q,
|
|
const QList<Expression> &columns,
|
|||
3160499c | Christian Ehringfeld | const bool &distinct, const QString &selectOption) const {
|
|
38335e94 | Christian Ehringfeld | QString sqlSelect = distinct ? ("SELECT " + this->builder->distinct() + " ") :
|
|
87739ae0 | Christian Ehringfeld | "SELECT ";
|
|
3160499c | Christian Ehringfeld | if (!selectOption.isEmpty()) {
|
|
38335e94 | Christian Ehringfeld | sqlSelect += selectOption + " ";
|
|
3160499c | Christian Ehringfeld | }
|
|
if (columns.isEmpty()) {
|
|||
87739ae0 | Christian Ehringfeld | return sqlSelect + "*";
|
|
3160499c | Christian Ehringfeld | }
|
|
bool first = true;
|
|||
for (int i = 0; i < columns.size(); ++i) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
sqlSelect += ", ";
|
|||
}
|
|||
Expression e = columns.at(i);
|
|||
1b167b6c | Christian Ehringfeld | q.appendParams(e.getParams());
|
|
3160499c | Christian Ehringfeld | QString nExp = e.getExpression();
|
|
if (e.getOnlyColumn()) {
|
|||
sqlSelect += this->builder->getSchema()->quoteColumnName(e.getExpression());
|
|||
} else if (!nExp.contains("(")) {
|
|||
QRegularExpression re =
|
|||
QRegularExpression(
|
|||
QRegularExpression::escape("/^(.*?)(?i:\\s+as\\s+|\\s+)([\\w\\-_\\.]+)$/"));
|
|||
ae872f6f | Christian Ehringfeld | re.optimize();
|
|
3160499c | Christian Ehringfeld | QRegularExpressionMatchIterator iterator = re.globalMatch(nExp, 0,
|
|
QRegularExpression::PartialPreferFirstMatch);
|
|||
if (iterator.hasNext()) {
|
|||
for (int var = 0; var < 2; ++var) {
|
|||
QRegularExpressionMatch match = iterator.next();
|
|||
if (var == 1) {
|
|||
sqlSelect += this->builder->getSchema()->quoteColumnName(
|
|||
match.captured()) + " AS " + this->builder->getSchema()->quoteColumnName("a" +
|
|||
QString::number(i));
|
|||
}
|
|||
}
|
|||
} else {
|
|||
nExp = this->builder->getSchema()->quoteColumnName(nExp);
|
|||
}
|
|||
} else {
|
|||
sqlSelect += nExp + " AS " + this->builder->getSchema()->quoteColumnName("a" +
|
|||
QString::number(i));
|
|||
}
|
|||
}
|
|||
return sqlSelect;
|
|||
}
|
|||
QString QueryInterpreter::buildFrom(const QStringList &from) const {
|
|||
if (from.isEmpty()) {
|
|||
return "";
|
|||
}
|
|||
auto tables = this->builder->quoteTableNames(from);
|
|||
QString clause = "FROM ";
|
|||
bool first = true;
|
|||
for (int var = 0; var < tables.size(); ++var) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
clause += ", ";
|
|||
}
|
|||
f89ad1ad | Christian Ehringfeld | clause += tables.at(var);
|
|
3160499c | Christian Ehringfeld | }
|
|
return clause;
|
|||
}
|
|||
QString QueryInterpreter::buildJoin(const QList<Join> &joins) const {
|
|||
if (joins.isEmpty()) {
|
|||
return "";
|
|||
}
|
|||
QString sqlJoin = "";
|
|||
for (int i = 0; i < joins.size(); ++i) {
|
|||
Join j = joins.at(i);
|
|||
sqlJoin += j.getType() + this->builder->getSeparator() +
|
|||
this->builder->getSchema()->quoteTableName(j.getForeignTable());
|
|||
506067a2 | Christian Ehringfeld | if (!j.getExpression().getExpression().isEmpty()) {
|
|
6cfbc25e | Christian Ehringfeld | QString expression = j.getExpression().getExpression();
|
|
int count = expression.count("=");
|
|||
if (count < 1) {
|
|||
expression = this->builder->getSchema()->quoteTableName(expression);
|
|||
} else if (count == 1) {
|
|||
QStringList list = expression.split("=");
|
|||
9e44a59b | Christian Ehringfeld | expression = this->builder->getSchema()->quoteColumnName(list.at(
|
|
6cfbc25e | Christian Ehringfeld | 0).trimmed()) + " = ";
|
|
9e44a59b | Christian Ehringfeld | expression += this->builder->getSchema()->quoteColumnName(list.at(1).trimmed());
|
|
6cfbc25e | Christian Ehringfeld | }
|
|
sqlJoin += " ON " + expression;
|
|||
3160499c | Christian Ehringfeld | }
|
|
}
|
|||
return sqlJoin;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QString QueryInterpreter::buildWhere(Query &q,
|
|
const QList<Expression> &conditions)
|
|||
3160499c | Christian Ehringfeld | const {
|
|
1b167b6c | Christian Ehringfeld | QString where = this->buildCondition(q, conditions);
|
|
3160499c | Christian Ehringfeld | return where.isEmpty() ? "" : ("WHERE " + where);
|
|
}
|
|||
2ee5022f | Christian Ehringfeld | ||
3160499c | Christian Ehringfeld | QString QueryInterpreter::buildGroupBy(const QStringList &groupBy) const {
|
|
return groupBy.isEmpty() ? "" : "GROUP BY " + this->builder->buildColumns(
|
|||
groupBy);
|
|||
2ee5022f | Christian Ehringfeld | }
|
|
3160499c | Christian Ehringfeld | ||
1b167b6c | Christian Ehringfeld | QString QueryInterpreter::buildHaving(Query &q,
|
|
19cf5548 | Christian Ehringfeld | const QList<Expression> &conditions) const {
|
|
1b167b6c | Christian Ehringfeld | QString having = this->buildCondition(q, conditions);
|
|
3160499c | Christian Ehringfeld | return having.isEmpty() ? "" : ("HAVING " + having);
|
|
}
|
|||
QString QueryInterpreter::buildOrderByAndLimit(QString sql,
|
|||
const QList<OrderBy> &orderBy, const quint64 &limit,
|
|||
const quint64 &offset) const {
|
|||
QString sqlOrderBy = this->buildOrderBy(orderBy);
|
|||
if (!sqlOrderBy.isEmpty()) {
|
|||
sql += this->builder->getSeparator() + sqlOrderBy;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QString sqlLimit = this->builder->limit(limit, offset, false);
|
|
3160499c | Christian Ehringfeld | if (!sqlLimit.isEmpty()) {
|
|
sql += this->builder->getSeparator() + sqlLimit;
|
|||
}
|
|||
return sql;
|
|||
}
|
|||
QString QueryInterpreter::buildOrderBy(const QList<OrderBy> &columns) const {
|
|||
if (columns.isEmpty()) {
|
|||
return "";
|
|||
}
|
|||
bool first = true;
|
|||
QString sqlOrder = "ORDER BY ";
|
|||
for (int i = 0; i < columns.size(); ++i) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
sqlOrder += ", ";
|
|||
}
|
|||
OrderBy order = columns.at(i);
|
|||
if (order.getColumn().isEmpty()) {
|
|||
sqlOrder += order.getExpressedDirection().getExpression();
|
|||
} else {
|
|||
sqlOrder += this->builder->getSchema()->quoteColumnName(order.getColumn());
|
|||
switch (order.getDirection()) {
|
|||
case Direction::SORT_ASC:
|
|||
sqlOrder += " ASC";
|
|||
break;
|
|||
case Direction::SORT_DESC:
|
|||
sqlOrder += " DESC";
|
|||
break;
|
|||
default:
|
|||
break;
|
|||
}
|
|||
}
|
|||
}
|
|||
return sqlOrder;
|
|||
}
|
|||
1b167b6c | Christian Ehringfeld | QString QueryInterpreter::buildCondition(Query &q,
|
|
19cf5548 | Christian Ehringfeld | const QList<Expression> &conditions) const {
|
|
3160499c | Christian Ehringfeld | if (conditions.isEmpty()) {
|
|
return "";
|
|||
}
|
|||
506067a2 | Christian Ehringfeld | QString sqlCondition = "";
|
|
bool first = true;
|
|||
for (int i = 0; i < conditions.size(); ++i) {
|
|||
Expression exp = conditions.at(i);
|
|||
QString expression = exp.getExpression();
|
|||
if (!expression.isEmpty()) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else if (expression.at(0) != ' ') {
|
|||
sqlCondition += this->builder->getSeparator();
|
|||
}
|
|||
}
|
|||
sqlCondition += expression;
|
|||
dc618bda | Christian Ehringfeld | q.appendParams(exp.getParams());
|
|
506067a2 | Christian Ehringfeld | }
|
|
return sqlCondition;
|
|||
3160499c | Christian Ehringfeld | }
|