18#ifndef QGSSQLSTATEMENT_H
19#define QGSSQLSTATEMENT_H
26#include <QCoreApplication>
34using namespace Qt::StringLiterals;
187 static const char *BINARY_OPERATOR_TEXT[]
SIP_SKIP;
190 static const char *UNARY_OPERATOR_TEXT[]
SIP_SKIP;
193 static const char *JOIN_TYPE_TEXT[]
SIP_SKIP;
225 switch ( sipCpp->nodeType() )
228 sipType = sipType_QgsSQLStatement_NodeUnaryOperator;
231 sipType = sipType_QgsSQLStatement_NodeBinaryOperator;
234 sipType = sipType_QgsSQLStatement_NodeInOperator;
237 sipType = sipType_QgsSQLStatement_NodeBetweenOperator;
240 sipType = sipType_QgsSQLStatement_NodeFunction;
243 sipType = sipType_QgsSQLStatement_NodeLiteral;
246 sipType = sipType_QgsSQLStatement_NodeColumnRef;
249 sipType = sipType_QgsSQLStatement_NodeSelectedColumn;
252 sipType = sipType_QgsSQLStatement_NodeSelect;
255 sipType = sipType_QgsSQLStatement_NodeTableDef;
258 sipType = sipType_QgsSQLStatement_NodeJoin;
261 sipType = sipType_QgsSQLStatement_NodeColumnSorted;
264 sipType = sipType_QgsSQLStatement_NodeCast;
288 virtual QString
dump()
const = 0;
346 virtual QString
dump() const;
366 SIP_PYOBJECT __repr__();
367 % MethodCode QString str = u
"<QgsSQLStatement.NodeUnaryOperator: %1>"_s.arg( sipCpp->text() );
368 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
382 QString
dump()
const override;
393 QString text() const;
423 SIP_PYOBJECT __repr__();
424 % MethodCode QString str = u
"<QgsSQLStatement.NodeBinaryOperator: %1>"_s.arg( sipCpp->text() );
425 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
442 QString
dump()
const override;
448 int precedence() const;
451 bool leftAssociative() const;
459 QString text() const;
499 QString
dump()
const override;
546 QString
dump()
const override;
581 SIP_PYOBJECT __repr__();
582 % MethodCode QString function;
583 QString str = u
"<QgsSQLStatement.NodeFunction: %1>"_s.arg( sipCpp->name() );
584 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
598 QString
dump()
const override;
629 SIP_PYOBJECT __repr__();
630 % MethodCode QString str = u
"<QgsSQLStatement.NodeLiteral: %1>"_s.arg( sipCpp->valueAsString() );
631 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
642 QString
dump()
const override;
651 QString valueAsString() const;
680 SIP_PYOBJECT __repr__();
681 % MethodCode QString columnRef;
682 if ( sipCpp->star() )
688 columnRef = sipCpp->name();
690 if ( !sipCpp->tableName().isEmpty() )
692 columnRef = u
"\"%1.%2\""_s.arg( sipCpp->tableName(), columnRef );
694 if ( sipCpp->distinct() )
696 columnRef = u
"DISTINCT %1"_s.arg( columnRef );
698 QString str = u
"<QgsSQLStatement.NodeColumnRef: %1>"_s.arg( columnRef );
699 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
722 QString
dump()
const override;
758 QString
dump()
const override;
792 SIP_PYOBJECT __repr__();
793 % MethodCode QString str = u
"<QgsSQLStatement.NodeCast: AS %1>"_s.arg( sipCpp->type() );
794 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
808 QString
dump()
const override;
854 SIP_PYOBJECT __repr__();
855 % MethodCode QString table;
856 if ( !sipCpp->schema().isEmpty() )
858 table = u
"%1.%2"_s.arg( sipCpp->schema(), sipCpp->name() );
862 table = sipCpp->name();
864 QString str = u
"<QgsSQLStatement.NodeTableDef: %1>"_s.arg( table );
865 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
886 QString
dump()
const override;
932 QString
dump()
const override;
974 QString
dump()
const override;
1040 QString
dump()
const override;
QgsSQLStatementFragment(const QString &fragment)
Constructor for QgsSQLStatementFragment of the specified fragment.
An 'X BETWEEN y and z' operator.
std::unique_ptr< Node > mMaxVal
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::Node * node() const
Variable at the left of BETWEEN.
QgsSQLStatement::Node * minVal() const
Minimum bound.
bool isNotBetween() const
Whether this is a NOT BETWEEN operator.
std::unique_ptr< Node > mNode
QgsSQLStatement::Node * maxVal() const
Maximum bound.
std::unique_ptr< Node > mMinVal
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeBetweenOperator(QgsSQLStatement::Node *node, QgsSQLStatement::Node *minVal, QgsSQLStatement::Node *maxVal, bool notBetween=false)
Constructor.
Binary logical/arithmetical operator (AND, OR, =, +, ...).
std::unique_ptr< Node > mOpLeft
QgsSQLStatement::Node * opLeft() const
Left operand.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeBinaryOperator(QgsSQLStatement::BinaryOperator op, QgsSQLStatement::Node *opLeft, QgsSQLStatement::Node *opRight)
Constructor.
std::unique_ptr< Node > mOpRight
QgsSQLStatement::BinaryOperator op() const
Operator.
QgsSQLStatement::Node * opRight() const
Right operand.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QgsSQLStatement::Node * node() const
Node that is referred to.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeCast(QgsSQLStatement::Node *node, const QString &type)
Constructor.
QString type() const
Type.
std::unique_ptr< Node > mNode
QString name() const
The name of the column.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
NodeColumnRef(const QString &name, bool star)
Constructor with column name only.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
bool star() const
Whether this is the * column.
NodeColumnRef(const QString &tableName, const QString &name, bool star)
Constructor with table and column name.
QString tableName() const
The name of the table. May be empty.
void setDistinct(bool distinct=true)
Sets whether this is prefixed by DISTINCT.
bool distinct() const
Whether this is prefixed by DISTINCT.
bool ascending() const
Whether the column is sorted in ascending order.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeColumnRef > mColumn
QgsSQLStatement::NodeColumnRef * column() const
The name of the column.
NodeColumnSorted(QgsSQLStatement::NodeColumnRef *column, bool asc)
Constructor.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
Function with a name and arguments node.
NodeFunction(const QString &name, QgsSQLStatement::NodeList *args)
Constructor.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeList > mArgs
QgsSQLStatement::NodeList * args() const
Returns arguments.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QString name() const
Returns function name.
An 'x IN (y, z)' operator.
std::unique_ptr< NodeList > mList
bool isNotIn() const
Whether this is a NOT IN operator.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QgsSQLStatement::Node * node() const
Variable at the left of IN.
std::unique_ptr< Node > mNode
NodeInOperator(QgsSQLStatement::Node *node, QgsSQLStatement::NodeList *list, bool notin=false)
Constructor.
QgsSQLStatement::NodeList * list() const
Values list.
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.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
std::unique_ptr< NodeTableDef > mTableDef
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeJoin(QgsSQLStatement::NodeTableDef *tabledef, const QList< QString > &usingColumns, QgsSQLStatement::JoinType type)
Constructor with table definition and USING columns.
QList< QString > usingColumns() const
Columns referenced by USING.
QgsSQLStatement::JoinType type() const
Join type.
void accept(QgsSQLStatement::Visitor &v) const
Accept visitor.
QList< QgsSQLStatement::Node * > list()
Returns list.
int count() const
Returns the number of nodes in the list.
void append(QgsSQLStatement::Node *node)
Takes ownership of the provided node.
Literal value (integer, integer64, double, string).
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
NodeLiteral(const QVariant &value)
Constructor.
QVariant value() const
The value of the literal.
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.
void setJoins(const QList< QgsSQLStatement::NodeJoin * > &joins)
Sets joins.
void setWhere(QgsSQLStatement::Node *where)
Sets where clause.
QList< NodeColumnSorted * > mOrderBy
QList< QgsSQLStatement::NodeSelectedColumn * > columns() const
Returns the list of columns.
bool distinct() const
Returns if the SELECT is DISTINCT.
QList< NodeSelectedColumn * > mColumns
void appendJoin(QgsSQLStatement::NodeJoin *join)
Append a join.
void setOrderBy(const QList< QgsSQLStatement::NodeColumnSorted * > &orderBy)
Sets order by columns.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
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.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QList< QgsSQLStatement::NodeTableDef * > tables() const
Returns the list of tables.
NodeSelectedColumn(QgsSQLStatement::Node *node)
Constructor.
void setAlias(const QString &alias)
Sets alias name.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::Node * column() const
Column that is referred to.
QString alias() const
Alias name.
std::unique_ptr< Node > mColumnNode
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QString name() const
Table name.
NodeTableDef(const QString &schema, const QString &name, const QString &alias)
Constructor with schema, table name and alias.
NodeTableDef(const QString &name)
Constructor with table name.
NodeTableDef(const QString &name, const QString &alias)
Constructor with table name and alias.
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QString alias() const
Table alias.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
QString schema() const
Returns the schema name.
Unary logical/arithmetical operator ( NOT, - ).
NodeUnaryOperator(QgsSQLStatement::UnaryOperator op, QgsSQLStatement::Node *operand)
Constructor.
std::unique_ptr< Node > mOperand
void accept(QgsSQLStatement::Visitor &v) const override
Support the visitor pattern.
QgsSQLStatement::UnaryOperator op() const
Operator.
QgsSQLStatement::Node * operand() const
Operand.
QgsSQLStatement::NodeType nodeType() const override
Abstract virtual that returns the type of this node.
Abstract node class for SQL statement nodes.
virtual QgsSQLStatement::Node * clone() const =0
Generate a clone of this node.
virtual QString dump() const =0
Abstract virtual dump method.
virtual QgsSQLStatement::NodeType nodeType() const =0
Abstract virtual that returns the type of this node.
virtual void accept(QgsSQLStatement::Visitor &v) const =0
Support the visitor pattern.
void visit(const QgsSQLStatement::NodeFunction &n) override
Visit NodeFunction.
void visit(const QgsSQLStatement::NodeSelectedColumn &n) override
Visit NodeSelectedColumn.
void visit(const QgsSQLStatement::NodeUnaryOperator &n) override
Visit NodeUnaryOperator.
void visit(const QgsSQLStatement::NodeLiteral &) override
Visit NodeLiteral.
RecursiveVisitor()=default
void visit(const QgsSQLStatement::NodeBetweenOperator &n) override
Visit NodeBetweenOperator.
void visit(const QgsSQLStatement::NodeBinaryOperator &n) override
Visit NodeBinaryOperator.
void visit(const QgsSQLStatement::NodeColumnRef &) override
Visit NodeColumnRef.
void visit(const QgsSQLStatement::NodeTableDef &) override
Visit NodeTableDef.
void visit(const QgsSQLStatement::NodeColumnSorted &n) override
Visit NodeColumnSorted.
void visit(const QgsSQLStatement::NodeInOperator &n) override
Visit NodeInOperator.
void visit(const QgsSQLStatement::NodeCast &n) override
Visit NodeCast.
Support for visitor pattern - algorithms dealing with the statement may be implemented without modify...
virtual void visit(const QgsSQLStatement::NodeBetweenOperator &n)=0
Visit NodeBetweenOperator.
virtual ~Visitor()=default
virtual void visit(const QgsSQLStatement::NodeFunction &n)=0
Visit NodeFunction.
virtual void visit(const QgsSQLStatement::NodeColumnRef &n)=0
Visit NodeColumnRef.
virtual void visit(const QgsSQLStatement::NodeBinaryOperator &n)=0
Visit NodeBinaryOperator.
virtual void visit(const QgsSQLStatement::NodeSelect &n)=0
Visit NodeSelect.
virtual void visit(const QgsSQLStatement::NodeCast &n)=0
Visit NodeCast.
virtual void visit(const QgsSQLStatement::NodeSelectedColumn &n)=0
Visit NodeSelectedColumn.
virtual void visit(const QgsSQLStatement::NodeJoin &n)=0
Visit NodeJoin.
virtual void visit(const QgsSQLStatement::NodeUnaryOperator &n)=0
Visit NodeUnaryOperator.
virtual void visit(const QgsSQLStatement::NodeInOperator &n)=0
Visit NodeInOperator.
virtual void visit(const QgsSQLStatement::NodeLiteral &n)=0
Visit NodeLiteral.
virtual void visit(const QgsSQLStatement::NodeColumnSorted &n)=0
Visit NodeColumnSorted.
virtual void visit(const QgsSQLStatement::NodeTableDef &n)=0
Visit NodeTableDef.
bool doBasicValidationChecks(QString &errorMsgOut) const
Performs basic validity checks.
JoinType
list of join types
BinaryOperator
list of binary operators
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.
QgsSQLStatement & operator=(const QgsSQLStatement &other)
static QString stripMsQuotedIdentifier(QString text)
Remove double quotes from an Microsoft style identifier.
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.
std::unique_ptr< QgsSQLStatement::Node > mRootNode
UnaryOperator
list of unary operators
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.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)