25#include <QRegularExpression>
28using namespace Qt::StringLiterals;
38 "OR",
"AND",
"=",
"<>",
"<=",
">=",
"<",
">",
"LIKE",
"NOT LIKE",
"ILIKE",
"NOT ILIKE",
"IS",
"IS NOT",
"+",
"-",
"*",
"/",
"//",
"%",
"^",
"||"
72 return tr(
"(no root)" );
79 return u
"\"%1\""_s.arg( name.replace(
'\"',
"\"\""_L1 ) );
85 static const char *
const RESERVED_KEYWORDS[] = {
"AND",
"OR",
"NOT",
"LIKE",
"IN",
"IS",
"BETWEEN",
"NULL",
"SELECT",
"ALL",
"DISTINCT",
"CAST",
86 "AS",
"FROM",
"JOIN",
"ON",
"USING",
"WHERE",
"ORDER",
"BY",
"ASC",
"DESC",
"LEFT",
"RIGHT",
87 "INNER",
"OUTER",
"CROSS",
"FULL",
"NATURAL",
"UNION",
"OFFSET",
"LIMIT",
"GROUP",
"HAVING" };
89 for (
size_t i = 0; i <
sizeof( RESERVED_KEYWORDS ) /
sizeof( RESERVED_KEYWORDS[0] ); ++i )
91 if ( name.compare( QString( RESERVED_KEYWORDS[i] ), Qt::CaseInsensitive ) == 0 )
96 const thread_local QRegularExpression IDENTIFIER_RE(
"^[A-Za-z_\\x80-\\xff][A-Za-z0-9_\\x80-\\xff]*$" );
97 return IDENTIFIER_RE.match( name ).hasMatch() ? name :
quotedIdentifier( name );
102 if ( text.length() >= 2 && text[0] ==
'"' && text[text.length() - 1] ==
'"' )
105 text = text.mid( 1, text.length() - 2 );
108 text.replace(
"\"\""_L1,
"\""_L1 );
115 if ( text.length() >= 2 && text[0] ==
'[' && text[text.length() - 1] ==
']' )
118 text = text.mid( 1, text.length() - 2 );
125 text.replace(
'\'',
"''"_L1 );
126 text.replace(
'\\',
"\\\\"_L1 );
127 text.replace(
'\n',
"\\n"_L1 );
128 text.replace(
'\t',
"\\t"_L1 );
129 return u
"'%1'"_s.arg( text );
152 if ( &other !=
this )
188 const auto constTables = n.
tables();
191 table->accept( *
this );
193 const auto constColumns = n.
columns();
196 column->accept( *
this );
198 const auto constJoins = n.
joins();
201 join->accept( *
this );
206 const auto constOrderBy = n.
orderBy();
209 column->accept( *
this );
262 errorMsgOut = tr(
"No root node" );
272 if ( !errorMsgOut.isEmpty() )
273 errorMsgOut +=
' '_L1;
274 errorMsgOut += tr(
"Table %1 is referenced by column %2, but not selected in FROM / JOIN." ).arg( pair.first, pair.second );
278 return errorMsgOut.isEmpty();
295 const auto constMList =
mList;
296 for (
Node *node : constMList )
298 nl->
mList.append( node->clone() );
308 const auto constMList =
mList;
309 for (
Node *n : constMList )
376 Q_ASSERT(
false &&
"unexpected binary operator" );
411 Q_ASSERT(
false &&
"unexpected binary operator" );
426 rdump.prepend(
'(' ).append(
')' );
455 return u
"%1 %2IN (%3)"_s.arg(
mNode->dump(),
mNotIn ?
"NOT " :
"",
mList->dump() );
494 switch (
mValue.userType() )
496 case QMetaType::Type::Int:
497 return QString::number(
mValue.toInt() );
498 case QMetaType::Type::LongLong:
499 return QString::number(
mValue.toLongLong() );
500 case QMetaType::Type::Double:
501 return QString::number(
mValue.toDouble() );
502 case QMetaType::Type::QString:
504 case QMetaType::Type::Bool:
505 return mValue.toBool() ?
"TRUE" :
"FALSE";
507 return tr(
"[unsupported type: %1; value: %2]" ).arg(
mValue.typeName(),
mValue.toString() );
522 ret +=
"DISTINCT "_L1;
608 QString ret = u
"SELECT "_s;
610 ret +=
"DISTINCT "_L1;
611 bool bFirstColumn =
true;
612 const auto constMColumns =
mColumns;
617 bFirstColumn =
false;
618 ret += column->dump();
621 bool bFirstTable =
true;
628 ret += table->dump();
630 const auto constMJoins =
mJoins;
643 ret +=
" ORDER BY "_L1;
645 const auto constMOrderBy =
mOrderBy;
659 QList<QgsSQLStatement::NodeSelectedColumn *> newColumnList;
660 const auto constMColumns =
mColumns;
663 newColumnList.push_back( column->cloneThis() );
665 QList<QgsSQLStatement::NodeTableDef *> newTableList;
669 newTableList.push_back( table->cloneThis() );
672 const auto constMJoins =
mJoins;
681 QList<QgsSQLStatement::NodeColumnSorted *> newOrderByList;
682 const auto constMOrderBy =
mOrderBy;
685 newOrderByList.push_back( columnSorted->cloneThis() );
710 ret +=
" USING ("_L1;
713 for ( QString column : constMUsingColumns )
763 QString ret( u
"CAST("_s );
764 ret +=
mNode->dump();
QgsSQLStatementCollectTableNames()=default
Constructor for QgsSQLStatementCollectTableNames.
QSet< TableColumnPair > tableNamesReferenced
QPair< QString, QString > TableColumnPair
QSet< QString > tableNamesDeclared
void visit(const QgsSQLStatement::NodeColumnRef &n) override
Visit NodeColumnRef.
QgsSQLStatementFragment(const QString &fragment)
Constructor for QgsSQLStatementFragment of the specified fragment.
std::unique_ptr< Node > mMaxVal
std::unique_ptr< Node > mNode
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
std::unique_ptr< Node > mMinVal
NodeBetweenOperator(QgsSQLStatement::Node *node, QgsSQLStatement::Node *minVal, QgsSQLStatement::Node *maxVal, bool notBetween=false)
Constructor.
QString dump() const override
Abstract virtual dump method.
Binary logical/arithmetical operator (AND, OR, =, +, ...).
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
std::unique_ptr< Node > mOpLeft
int precedence() const
Precedence.
QString dump() const override
Abstract virtual dump method.
NodeBinaryOperator(QgsSQLStatement::BinaryOperator op, QgsSQLStatement::Node *opLeft, QgsSQLStatement::Node *opRight)
Constructor.
std::unique_ptr< Node > mOpRight
bool leftAssociative() const
Is left associative ?
NodeCast(QgsSQLStatement::Node *node, const QString &type)
Constructor.
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
std::unique_ptr< Node > mNode
QString name() const
The name of the column.
NodeColumnRef(const QString &name, bool star)
Constructor with column name only.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QString tableName() const
The name of the table. May be empty.
QString dump() const override
Abstract virtual dump method.
void setDistinct(bool distinct=true)
Sets whether this is prefixed by DISTINCT.
QgsSQLStatement::NodeColumnRef * cloneThis() const
Clone with same type return.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QgsSQLStatement::NodeColumnSorted * cloneThis() const
Clone with same type return.
QString dump() const override
Abstract virtual dump method.
std::unique_ptr< NodeColumnRef > mColumn
NodeColumnSorted(QgsSQLStatement::NodeColumnRef *column, bool asc)
Constructor.
NodeFunction(const QString &name, QgsSQLStatement::NodeList *args)
Constructor.
QString dump() const override
Abstract virtual dump method.
std::unique_ptr< NodeList > mArgs
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
std::unique_ptr< NodeList > mList
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
std::unique_ptr< Node > mNode
NodeInOperator(QgsSQLStatement::Node *node, QgsSQLStatement::NodeList *list, bool notin=false)
Constructor.
QString dump() const override
Abstract virtual dump method.
QList< QString > mUsingColumns
QgsSQLStatement::NodeTableDef * tableDef() const
Table definition.
std::unique_ptr< Node > mOnExpr
QgsSQLStatement::Node * onExpr() const
On expression. Will be nullptr if usingColumns() is not empty.
NodeJoin(QgsSQLStatement::NodeTableDef *tabledef, QgsSQLStatement::Node *onExpr, QgsSQLStatement::JoinType type)
Constructor with table definition, ON expression.
std::unique_ptr< NodeTableDef > mTableDef
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::NodeJoin * cloneThis() const
Clone with same type return.
QgsSQLStatement::NodeList * clone() const
Creates a deep copy of this list. Ownership is transferred to the caller.
void accept(QgsSQLStatement::Visitor &v) const
Accept visitor.
virtual QString dump() const
Dump list.
QString dump() const override
Abstract virtual dump method.
NodeLiteral(const QVariant &value)
Constructor.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
NodeSelect(const QList< QgsSQLStatement::NodeTableDef * > &tableList, const QList< QgsSQLStatement::NodeSelectedColumn * > &columns, bool distinct)
Constructor.
QList< QgsSQLStatement::NodeColumnSorted * > orderBy() const
Returns the list of order by columns.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
void setWhere(QgsSQLStatement::Node *where)
Sets where clause.
QList< NodeColumnSorted * > mOrderBy
QList< QgsSQLStatement::NodeSelectedColumn * > columns() const
Returns the list of columns.
QList< NodeSelectedColumn * > mColumns
void appendJoin(QgsSQLStatement::NodeJoin *join)
Append a join.
void setOrderBy(const QList< QgsSQLStatement::NodeColumnSorted * > &orderBy)
Sets order by columns.
std::unique_ptr< Node > mWhere
QList< QgsSQLStatement::NodeJoin * > joins() const
Returns the list of joins.
QList< NodeJoin * > mJoins
QList< NodeTableDef * > mTableList
QgsSQLStatement::Node * where() const
Returns the where clause.
QString dump() const override
Abstract virtual dump method.
QList< QgsSQLStatement::NodeTableDef * > tables() const
Returns the list of tables.
NodeSelectedColumn(QgsSQLStatement::Node *node)
Constructor.
void setAlias(const QString &alias)
Sets alias name.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
QString dump() const override
Abstract virtual dump method.
QgsSQLStatement::NodeSelectedColumn * cloneThis() const
Clone with same type return.
std::unique_ptr< Node > mColumnNode
QString name() const
Table name.
QString dump() const override
Abstract virtual dump method.
NodeTableDef(const QString &name)
Constructor with table name.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QString alias() const
Table alias.
QString schema() const
Returns the schema name.
QgsSQLStatement::NodeTableDef * cloneThis() const
Clone with same type return.
Unary logical/arithmetical operator ( NOT, - ).
QString dump() const override
Abstract virtual dump method.
NodeUnaryOperator(QgsSQLStatement::UnaryOperator op, QgsSQLStatement::Node *operand)
Constructor.
std::unique_ptr< Node > mOperand
QgsSQLStatement::UnaryOperator op() const
Operator.
QgsSQLStatement::Node * clone() const override
Generate a clone of this node.
Abstract node class for SQL statement nodes.
virtual void accept(QgsSQLStatement::Visitor &v) const =0
Support the visitor pattern.
A visitor that recursively explores all children.
void visit(const QgsSQLStatement::NodeUnaryOperator &n) override
Visit NodeUnaryOperator.
Support for visitor pattern - algorithms dealing with the statement may be implemented without modify...
bool doBasicValidationChecks(QString &errorMsgOut) const
Performs basic validity checks.
static QString stripQuotedIdentifier(QString text)
Remove double quotes from an identifier.
static QString quotedIdentifierIfNeeded(const QString &name)
Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.
QString mParserErrorString
static QString quotedIdentifier(QString name)
Returns a quoted column reference (in double quotes).
QString parserErrorString() const
Returns parser error.
static const char * JOIN_TYPE_TEXT[]
QgsSQLStatement & operator=(const QgsSQLStatement &other)
static QString stripMsQuotedIdentifier(QString text)
Remove double quotes from an Microsoft style identifier.
static const char * BINARY_OPERATOR_TEXT[]
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes).
QString dump() const
Returns the statement string, constructed from the internal abstract syntax tree.
virtual ~QgsSQLStatement()
void acceptVisitor(QgsSQLStatement::Visitor &v) const
Entry function for the visitor pattern.
std::unique_ptr< QgsSQLStatement::Node > mRootNode
bool hasParserError() const
Returns true if an error occurred when parsing the input statement.
const QgsSQLStatement::Node * rootNode() const
Returns the root node of the statement.
QString statement() const
Returns the original, unmodified statement string.
QgsSQLStatement(const QString &statement)
Creates a new statement based on the provided string.
static const char * UNARY_OPERATOR_TEXT[]
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QgsSQLStatement::Node * parse(const QString &str, QString &parserErrorMsg, bool allowFragments)