Class: QgsSQLStatement¶
Class for parsing SQL statements.
Class Hierarchy¶
Subclasses¶
Class for parsing fragments of SQL statements, such as an expression or where clause.  | 
Methods
Entry function for the visitor pattern  | 
|
Performs basic validity checks.  | 
|
Returns the statement string, constructed from the internal abstract syntax tree.  | 
|
Returns   | 
|
Returns parser error  | 
|
Returns the root node of the statement.  | 
|
Returns the original, unmodified statement string.  | 
Static Methods
Returns a quoted column reference (in double quotes)  | 
|
Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.  | 
|
Returns a quoted version of a string (in single quotes)  | 
|
Remove double quotes from an Microsoft style identifier.  | 
|
Remove double quotes from an identifier.  | 
Attributes
- class qgis.core.QgsSQLStatement[source]¶
 Bases:
object- __init__(statement: str | None)
 Creates a new statement based on the provided string.
- Parameters:
 statement (Optional[str])
- __init__(other: QgsSQLStatement)
 - Parameters:
 other (QgsSQLStatement)
- __init__(statement: str | None, allowFragments: bool)
 Constructor for QgsSQLStatement, with the specified
statement.If
allowFragmentsisTruethen the parser will allow SQL fragments, such as a expression or filter where clause alone.Added in version 3.16.
- Parameters:
 statement (Optional[str])
allowFragments (bool)
- class BinaryOperator¶
 Bases:
int
- class JoinType¶
 Bases:
int
- class Node[source]¶
 Bases:
objectAbstract node class
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 Support the visitor pattern.
For any implementation this should look like
v.visit(self)
- Parameters:
 v (QgsSQLStatement.Visitor) – A visitor that visits this node.
- clone(self) QgsSQLStatement.Node | None[source]¶
 Generate a clone of this node. Make sure that the clone does not contain any information which is generated in prepare and context related. Ownership is transferred to the caller.
- Return type:
 Optional[QgsSQLStatement.Node]
- Returns:
 a deep copy of this node.
- dump(self) str[source]¶
 Abstract virtual dump method
- Return type:
 str
- Returns:
 A statement which represents this node as string
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 Abstract virtual that returns the type of this node.
- Return type:
 - Returns:
 The type of this node
- class NodeBetweenOperator[source]¶
 Bases:
Node‘X BETWEEN y and z’ operator
QgsSQLStatement.NodeBetweenOperator(node: Optional[QgsSQLStatement.Node], minVal: Optional[QgsSQLStatement.Node], maxVal: Optional[QgsSQLStatement.Node], notBetween: bool = False) Constructor
QgsSQLStatement.NodeBetweenOperator(a0: QgsSQLStatement.NodeBetweenOperator)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- maxVal(self) QgsSQLStatement.Node | None[source]¶
 Maximum bound
- Return type:
 Optional[QgsSQLStatement.Node]
- minVal(self) QgsSQLStatement.Node | None[source]¶
 Minimum bound
- Return type:
 Optional[QgsSQLStatement.Node]
- node(self) QgsSQLStatement.Node | None[source]¶
 Variable at the left of BETWEEN
- Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeBinaryOperator[source]¶
 Bases:
NodeBinary logical/arithmetical operator (AND, OR, =, +, …)
QgsSQLStatement.NodeBinaryOperator(op: QgsSQLStatement.BinaryOperator, opLeft: Optional[QgsSQLStatement.Node], opRight: Optional[QgsSQLStatement.Node]) Constructor
QgsSQLStatement.NodeBinaryOperator(a0: QgsSQLStatement.NodeBinaryOperator)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- opLeft(self) QgsSQLStatement.Node | None[source]¶
 Left operand
- Return type:
 Optional[QgsSQLStatement.Node]
- opRight(self) QgsSQLStatement.Node | None[source]¶
 Right operand
- Return type:
 Optional[QgsSQLStatement.Node]
- class NodeCast[source]¶
 Bases:
NodeCAST operator
QgsSQLStatement.NodeCast(node: Optional[QgsSQLStatement.Node], type: Optional[str]) Constructor
QgsSQLStatement.NodeCast(a0: QgsSQLStatement.NodeCast)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- node(self) QgsSQLStatement.Node | None[source]¶
 Node that is referred to
- Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeColumnRef[source]¶
 Bases:
NodeReference to a column
QgsSQLStatement.NodeColumnRef(name: Optional[str], star: bool) Constructor with column name only
QgsSQLStatement.NodeColumnRef(tableName: Optional[str], name: Optional[str], star: bool) Constructor with table and column name
QgsSQLStatement.NodeColumnRef(a0: QgsSQLStatement.NodeColumnRef)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- cloneThis(self) QgsSQLStatement.NodeColumnRef | None[source]¶
 Clone with same type return
- Return type:
 Optional[QgsSQLStatement.NodeColumnRef]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeColumnSorted[source]¶
 Bases:
NodeColumn in a ORDER BY
QgsSQLStatement.NodeColumnSorted(column: Optional[QgsSQLStatement.NodeColumnRef], asc: bool) Constructor
QgsSQLStatement.NodeColumnSorted(a0: QgsSQLStatement.NodeColumnSorted)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- cloneThis(self) QgsSQLStatement.NodeColumnSorted | None[source]¶
 Clone with same type return
- Return type:
 Optional[QgsSQLStatement.NodeColumnSorted]
- column(self) QgsSQLStatement.NodeColumnRef | None[source]¶
 The name of the column.
- Return type:
 Optional[QgsSQLStatement.NodeColumnRef]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeFunction[source]¶
 Bases:
NodeFunction with a name and arguments node
QgsSQLStatement.NodeFunction(name: Optional[str], args: Optional[QgsSQLStatement.NodeList]) Constructor
QgsSQLStatement.NodeFunction(a0: QgsSQLStatement.NodeFunction)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- args(self) QgsSQLStatement.NodeList | None[source]¶
 Returns arguments
- Return type:
 Optional[QgsSQLStatement.NodeList]
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeInOperator[source]¶
 Bases:
Node‘x IN (y, z)’ operator
QgsSQLStatement.NodeInOperator(node: Optional[QgsSQLStatement.Node], list: Optional[QgsSQLStatement.NodeList], notin: bool = False) Constructor
QgsSQLStatement.NodeInOperator(a0: QgsSQLStatement.NodeInOperator)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- list(self) QgsSQLStatement.NodeList | None[source]¶
 Values list
- Return type:
 Optional[QgsSQLStatement.NodeList]
- node(self) QgsSQLStatement.Node | None[source]¶
 Variable at the left of IN
- Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeJoin[source]¶
 Bases:
NodeJoin definition
QgsSQLStatement.NodeJoin(tabledef: Optional[QgsSQLStatement.NodeTableDef], onExpr: Optional[QgsSQLStatement.Node], type: QgsSQLStatement.JoinType) Constructor with table definition, ON expression
QgsSQLStatement.NodeJoin(tabledef: Optional[QgsSQLStatement.NodeTableDef], usingColumns: Iterable[Optional[str]], type: QgsSQLStatement.JoinType) Constructor with table definition and USING columns
QgsSQLStatement.NodeJoin(a0: QgsSQLStatement.NodeJoin)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- cloneThis(self) QgsSQLStatement.NodeJoin | None[source]¶
 Clone with same type return
- Return type:
 Optional[QgsSQLStatement.NodeJoin]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- onExpr(self) QgsSQLStatement.Node | None[source]¶
 On expression. Will be
NoneifusingColumns()is not empty- Return type:
 Optional[QgsSQLStatement.Node]
- tableDef(self) QgsSQLStatement.NodeTableDef | None[source]¶
 Table definition
- Return type:
 Optional[QgsSQLStatement.NodeTableDef]
- type(self) QgsSQLStatement.JoinType[source]¶
 Join type
- Return type:
 
- usingColumns(self) List[str]¶
 Columns referenced by USING
- Return type:
 List[str]
- class NodeList[source]¶
 Bases:
objectA list of nodes.
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 Accept visitor
- Parameters:
 
- append(self, node: QgsSQLStatement.Node | None)[source]¶
 Takes ownership of the provided node
- Parameters:
 node (Optional[QgsSQLStatement.Node])
- clone(self) QgsSQLStatement.NodeList | None[source]¶
 Creates a deep copy of this list. Ownership is transferred to the caller
- Return type:
 Optional[QgsSQLStatement.NodeList]
- list(self) List[QgsSQLStatement.Node]¶
 Returns list
- Return type:
 List[QgsSQLStatement.Node]
- class NodeLiteral[source]¶
 Bases:
NodeLiteral value (integer, integer64, double, string)
QgsSQLStatement.NodeLiteral(value: Any) Constructor
QgsSQLStatement.NodeLiteral(a0: QgsSQLStatement.NodeLiteral)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeSelect[source]¶
 Bases:
NodeSELECT node
QgsSQLStatement.NodeSelect(tableList: Iterable[QgsSQLStatement.NodeTableDef], columns: Iterable[QgsSQLStatement.NodeSelectedColumn], distinct: bool) Constructor
QgsSQLStatement.NodeSelect(a0: QgsSQLStatement.NodeSelect)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- appendJoin(self, join: QgsSQLStatement.NodeJoin | None)[source]¶
 Append a join
- Parameters:
 join (Optional[QgsSQLStatement.NodeJoin])
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- columns(self) List[QgsSQLStatement.NodeSelectedColumn]¶
 Returns the list of columns
- Return type:
 
- joins(self) List[QgsSQLStatement.NodeJoin]¶
 Returns the list of joins
- Return type:
 List[QgsSQLStatement.NodeJoin]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- orderBy(self) List[QgsSQLStatement.NodeColumnSorted]¶
 Returns the list of order by columns
- Return type:
 
- setJoins(self, joins: Iterable[QgsSQLStatement.NodeJoin])[source]¶
 Sets joins
- Parameters:
 joins (Iterable[QgsSQLStatement.NodeJoin])
- setOrderBy(self, orderBy: Iterable[QgsSQLStatement.NodeColumnSorted])[source]¶
 Sets order by columns
- Parameters:
 orderBy (Iterable[QgsSQLStatement.NodeColumnSorted])
- setWhere(self, where: QgsSQLStatement.Node | None)[source]¶
 Sets where clause
- Parameters:
 where (Optional[QgsSQLStatement.Node])
- tables(self) List[QgsSQLStatement.NodeTableDef]¶
 Returns the list of tables
- Return type:
 
- where(self) QgsSQLStatement.Node | None[source]¶
 Returns the where clause
- Return type:
 Optional[QgsSQLStatement.Node]
- class NodeSelectedColumn[source]¶
 Bases:
NodeSelected column
QgsSQLStatement.NodeSelectedColumn(node: Optional[QgsSQLStatement.Node]) Constructor
QgsSQLStatement.NodeSelectedColumn(a0: QgsSQLStatement.NodeSelectedColumn)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- cloneThis(self) QgsSQLStatement.NodeSelectedColumn | None[source]¶
 Clone with same type return
- Return type:
 Optional[QgsSQLStatement.NodeSelectedColumn]
- column(self) QgsSQLStatement.Node | None[source]¶
 Column that is referred to
- Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeTableDef[source]¶
 Bases:
NodeTable definition
QgsSQLStatement.NodeTableDef(name: Optional[str]) Constructor with table name
QgsSQLStatement.NodeTableDef(name: Optional[str], alias: Optional[str]) Constructor with table name and alias
QgsSQLStatement.NodeTableDef(schema: Optional[str], name: Optional[str], alias: Optional[str]) Constructor with schema, table name and alias
Added in version 3.28.
QgsSQLStatement.NodeTableDef(a0: QgsSQLStatement.NodeTableDef)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- cloneThis(self) QgsSQLStatement.NodeTableDef | None[source]¶
 Clone with same type return
- Return type:
 Optional[QgsSQLStatement.NodeTableDef]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- class NodeType¶
 Bases:
int
- class NodeUnaryOperator[source]¶
 Bases:
NodeUnary logicial/arithmetical operator ( NOT, - )
QgsSQLStatement.NodeUnaryOperator(op: QgsSQLStatement.UnaryOperator, operand: Optional[QgsSQLStatement.Node]) Constructor
QgsSQLStatement.NodeUnaryOperator(a0: QgsSQLStatement.NodeUnaryOperator)
- accept(self, v: QgsSQLStatement.Visitor)[source]¶
 - Parameters:
 
- clone(self) QgsSQLStatement.Node | None[source]¶
 - Return type:
 Optional[QgsSQLStatement.Node]
- nodeType(self) QgsSQLStatement.NodeType[source]¶
 - Return type:
 
- operand(self) QgsSQLStatement.Node | None[source]¶
 
- class RecursiveVisitor[source]¶
 Bases:
VisitorA visitor that recursively explores all children
- visit(self, n: QgsSQLStatement.NodeUnaryOperator)[source]¶
 - visit(self, n: QgsSQLStatement.NodeBinaryOperator) None
 - visit(self, n: QgsSQLStatement.NodeInOperator) None
 - visit(self, n: QgsSQLStatement.NodeBetweenOperator) None
 - visit(self, n: QgsSQLStatement.NodeFunction) None
 - visit(self, a0: QgsSQLStatement.NodeLiteral) None
 - visit(self, a0: QgsSQLStatement.NodeColumnRef) None
 - visit(self, n: QgsSQLStatement.NodeSelectedColumn) None
 - visit(self, a0: QgsSQLStatement.NodeTableDef) None
 - visit(self, n: QgsSQLStatement.NodeSelect) None
 - visit(self, n: QgsSQLStatement.NodeJoin) None
 - visit(self, n: QgsSQLStatement.NodeColumnSorted) None
 - visit(self, n: QgsSQLStatement.NodeCast) None
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeBinaryOperator)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeInOperator)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeBetweenOperator)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeFunction)[source]
 - Parameters:
 
- visit(self, a0: QgsSQLStatement.NodeLiteral)[source]
 - Parameters:
 
- visit(self, a0: QgsSQLStatement.NodeColumnRef)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeSelectedColumn)[source]
 - Parameters:
 
- visit(self, a0: QgsSQLStatement.NodeTableDef)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeSelect)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeJoin)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeColumnSorted)[source]
 - Parameters:
 
- visit(self, n: QgsSQLStatement.NodeCast)[source]
 - Parameters:
 
- class UnaryOperator¶
 Bases:
int
- class Visitor[source]¶
 Bases:
objectSupport for visitor pattern - algorithms dealing with the statement may be implemented without modifying the Node classes
- visit(self, n: QgsSQLStatement.NodeUnaryOperator)[source]¶
 Visit NodeUnaryOperator
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeBinaryOperator)[source]
 Visit NodeBinaryOperator
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeInOperator)[source]
 Visit NodeInOperator
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeBetweenOperator)[source]
 Visit NodeBetweenOperator
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeFunction)[source]
 Visit NodeFunction
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeLiteral)[source]
 Visit NodeLiteral
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeColumnRef)[source]
 Visit NodeColumnRef
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeSelectedColumn)[source]
 Visit NodeSelectedColumn
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeTableDef)[source]
 Visit NodeTableDef
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeSelect)[source]
 Visit NodeSelect
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeJoin)[source]
 Visit NodeJoin
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeColumnSorted)[source]
 Visit NodeColumnSorted
- Parameters:
 
- visit(self, n: QgsSQLStatement.NodeCast)[source]
 Visit NodeCast
- Parameters:
 
- acceptVisitor(self, v: QgsSQLStatement.Visitor)[source]¶
 Entry function for the visitor pattern
- Parameters:
 
- boAnd = 1¶
 
- boConcat = 21¶
 
- boDiv = 17¶
 
- boEQ = 2¶
 
- boGE = 5¶
 
- boGT = 7¶
 
- boILike = 10¶
 
- boIntDiv = 18¶
 
- boIs = 12¶
 
- boIsNot = 13¶
 
- boLE = 4¶
 
- boLT = 6¶
 
- boLike = 8¶
 
- boMinus = 15¶
 
- boMod = 19¶
 
- boMul = 16¶
 
- boNE = 3¶
 
- boNotILike = 11¶
 
- boNotLike = 9¶
 
- boOr = 0¶
 
- boPlus = 14¶
 
- boPow = 20¶
 
- doBasicValidationChecks(self)[source]¶
 Performs basic validity checks. Basically checking that columns referencing a table, references a specified table. Returns
Trueif the validation is successful
- dump(self) str[source]¶
 Returns the statement string, constructed from the internal abstract syntax tree. This does not contain any nice whitespace formatting or comments. In general it is preferable to use
statement()instead.- Return type:
 str
- hasParserError(self) bool[source]¶
 Returns
Trueif an error occurred when parsing the input statement- Return type:
 bool
- jtCross = 5¶
 
- jtDefault = 0¶
 
- jtFull = 7¶
 
- jtInner = 6¶
 
- jtLeft = 1¶
 
- jtLeftOuter = 2¶
 
- jtRight = 3¶
 
- jtRightOuter = 4¶
 
- ntBetweenOperator = 3¶
 
- ntBinaryOperator = 1¶
 
- ntCast = 12¶
 
- ntColumnRef = 6¶
 
- ntColumnSorted = 11¶
 
- ntFunction = 4¶
 
- ntInOperator = 2¶
 
- ntJoin = 10¶
 
- ntLiteral = 5¶
 
- ntSelect = 8¶
 
- ntSelectedColumn = 7¶
 
- ntTableDef = 9¶
 
- ntUnaryOperator = 0¶
 
- static quotedIdentifier(name: str | None) str[source]¶
 Returns a quoted column reference (in double quotes)
See also
See also
- Parameters:
 name (Optional[str])
- Return type:
 str
- static quotedIdentifierIfNeeded(name: str | None) str[source]¶
 Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.
See also
See also
- Parameters:
 name (Optional[str])
- Return type:
 str
- static quotedString(text: str | None) str[source]¶
 Returns a quoted version of a string (in single quotes)
See also
See also
- Parameters:
 text (Optional[str])
- Return type:
 str
- rootNode(self) QgsSQLStatement.Node | None[source]¶
 Returns the root node of the statement. The root node is
Noneif parsing has failed.- Return type:
 Optional[QgsSQLStatement.Node]
- statement(self) str[source]¶
 Returns the original, unmodified statement string. If there was none supplied because it was constructed by sole API calls,
dump()will be used to create one instead.- Return type:
 str
- static stripMsQuotedIdentifier(text: str | None) str[source]¶
 Remove double quotes from an Microsoft style identifier.
See also
- Parameters:
 text (Optional[str])
- Return type:
 str
- static stripQuotedIdentifier(text: str | None) str[source]¶
 Remove double quotes from an identifier.
See also
- Parameters:
 text (Optional[str])
- Return type:
 str
- uoMinus = 1¶
 
- uoNot = 0¶