Class: QgsSQLStatement¶
- class qgis.core.QgsSQLStatement¶
Bases:
sip.wrapper
Class for parsing SQL statements.
New in version 2.16.
QgsSQLStatement(statement: str) Creates a new statement based on the provided string.
QgsSQLStatement(other:
QgsSQLStatement
) Create a copy of this statement.QgsSQLStatement(statement: str, allowFragments: bool) Constructor for
QgsSQLStatement
, with the specifiedstatement
.If
allowFragments
isTrue
then the parser will allow SQL fragments, such as a expression or filter where clause alone.New in version 3.16:
Methods
Entry function for the visitor pattern
Performs basic validity checks.
Returns the statement string, constructed from the internal abstract syntax tree.
Returns
True
if an error occurred when parsing the input statementReturns parser error
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)
Returns the root node of the statement.
Returns the original, unmodified statement string.
Remove double quotes from an Microsoft style identifier.
Remove double quotes from an identifier.
Attributes
- class BinaryOperator¶
Bases:
int
- class JoinType¶
Bases:
int
- class Node¶
Bases:
sip.wrapper
Abstract node class
- accept(self, v: QgsSQLStatement.Visitor)¶
Support the visitor pattern.
For any implementation this should look like
C++:
v.visit( *this );
Python:
v.visit( self)
- Parameters:
v (QgsSQLStatement.Visitor) – A visitor that visits this node.
- clone(self) QgsSQLStatement.Node ¶
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:
- Returns:
a deep copy of this node.
- dump(self) str ¶
Abstract virtual dump method
- Return type:
str
- Returns:
A statement which represents this node as string
- nodeType(self) QgsSQLStatement.NodeType ¶
Abstract virtual that returns the type of this node.
- Return type:
- Returns:
The type of this node
- class NodeBetweenOperator¶
Bases:
Node
‘X BETWEEN y and z’ operator
QgsSQLStatement.NodeBetweenOperator(node: QgsSQLStatement.Node, minVal: QgsSQLStatement.Node, maxVal: QgsSQLStatement.Node, notBetween: bool = False) Constructor
QgsSQLStatement.NodeBetweenOperator(QgsSQLStatement.NodeBetweenOperator)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- isNotBetween(self) bool ¶
Whether this is a NOT BETWEEN operator
- Return type:
bool
- maxVal(self) QgsSQLStatement.Node ¶
Maximum bound
- Return type:
- minVal(self) QgsSQLStatement.Node ¶
Minimum bound
- Return type:
- node(self) QgsSQLStatement.Node ¶
Variable at the left of BETWEEN
- Return type:
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- class NodeBinaryOperator¶
Bases:
Node
Binary logical/arithmetical operator (AND, OR, =, +, …)
QgsSQLStatement.NodeBinaryOperator(op: QgsSQLStatement.BinaryOperator, opLeft: QgsSQLStatement.Node, opRight: QgsSQLStatement.Node) Constructor
QgsSQLStatement.NodeBinaryOperator(QgsSQLStatement.NodeBinaryOperator)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- leftAssociative(self) bool ¶
Is left associative ?
- Return type:
bool
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- op(self) QgsSQLStatement.BinaryOperator ¶
Operator
- Return type:
- opLeft(self) QgsSQLStatement.Node ¶
Left operand
- Return type:
- opRight(self) QgsSQLStatement.Node ¶
Right operand
- Return type:
- precedence(self) int ¶
Precedence
- Return type:
int
- class NodeCast¶
Bases:
Node
CAST operator
QgsSQLStatement.NodeCast(node: QgsSQLStatement.Node, type: str) Constructor
QgsSQLStatement.NodeCast(QgsSQLStatement.NodeCast)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- node(self) QgsSQLStatement.Node ¶
Node that is referred to
- Return type:
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- type(self) str ¶
Type
- Return type:
str
- class NodeColumnRef¶
Bases:
Node
Reference to a column
QgsSQLStatement.NodeColumnRef(name: str, star: bool) Constructor with column name only
QgsSQLStatement.NodeColumnRef(tableName: str, name: str, star: bool) Constructor with table and column name
QgsSQLStatement.NodeColumnRef(QgsSQLStatement.NodeColumnRef)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeColumnRef ¶
Clone with same type return
- Return type:
- distinct(self) bool ¶
Whether this is prefixed by DISTINCT
- Return type:
bool
- dump(self) str ¶
- Return type:
str
- name(self) str ¶
The name of the column.
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- setDistinct(self, distinct: bool = True)¶
Sets whether this is prefixed by DISTINCT
- Parameters:
distinct (bool = True) –
- star(self) bool ¶
Whether this is the * column
- Return type:
bool
- tableName(self) str ¶
The name of the table. May be empty.
- Return type:
str
- class NodeColumnSorted¶
Bases:
Node
Column in a ORDER BY
QgsSQLStatement.NodeColumnSorted(column: QgsSQLStatement.NodeColumnRef, asc: bool) Constructor
QgsSQLStatement.NodeColumnSorted(QgsSQLStatement.NodeColumnSorted)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- ascending(self) bool ¶
Whether the column is sorted in ascending order
- Return type:
bool
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeColumnSorted ¶
Clone with same type return
- Return type:
- column(self) QgsSQLStatement.NodeColumnRef ¶
The name of the column.
- Return type:
- dump(self) str ¶
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- class NodeFunction¶
Bases:
Node
Function with a name and arguments node
QgsSQLStatement.NodeFunction(name: str, args: QgsSQLStatement.NodeList) Constructor
QgsSQLStatement.NodeFunction(QgsSQLStatement.NodeFunction)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- args(self) QgsSQLStatement.NodeList ¶
Returns arguments
- Return type:
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- name(self) str ¶
Returns function name
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- class NodeInOperator¶
Bases:
Node
‘x IN (y, z)’ operator
QgsSQLStatement.NodeInOperator(node: QgsSQLStatement.Node, list: QgsSQLStatement.NodeList, notin: bool = False) Constructor
QgsSQLStatement.NodeInOperator(QgsSQLStatement.NodeInOperator)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- isNotIn(self) bool ¶
Whether this is a NOT IN operator
- Return type:
bool
- list(self) QgsSQLStatement.NodeList ¶
Values list
- Return type:
- node(self) QgsSQLStatement.Node ¶
Variable at the left of IN
- Return type:
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- class NodeJoin¶
Bases:
Node
Join definition
QgsSQLStatement.NodeJoin(tabledef: QgsSQLStatement.NodeTableDef, onExpr: QgsSQLStatement.Node, type: QgsSQLStatement.JoinType) Constructor with table definition, ON expression
QgsSQLStatement.NodeJoin(tabledef: QgsSQLStatement.NodeTableDef, usingColumns: Iterable[str], type: QgsSQLStatement.JoinType) Constructor with table definition and USING columns
QgsSQLStatement.NodeJoin(QgsSQLStatement.NodeJoin)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeJoin ¶
Clone with same type return
- Return type:
- dump(self) str ¶
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- onExpr(self) QgsSQLStatement.Node ¶
On expression. Will be
None
ifusingColumns()
is not empty- Return type:
- tableDef(self) QgsSQLStatement.NodeTableDef ¶
Table definition
- Return type:
- type(self) QgsSQLStatement.JoinType ¶
Join type
- Return type:
- usingColumns(self) List[str] ¶
Columns referenced by USING
- Return type:
List[str]
- class NodeList¶
Bases:
sip.wrapper
A list of nodes.
QgsSQLStatement.NodeList() Constructor
QgsSQLStatement.NodeList(QgsSQLStatement.NodeList)
- accept(self, v: QgsSQLStatement.Visitor)¶
Accept visitor
- Parameters:
v (QgsSQLStatement.Visitor) –
- append(self, node: QgsSQLStatement.Node)¶
Takes ownership of the provided node
- Parameters:
node (QgsSQLStatement.Node) –
- clone(self) QgsSQLStatement.NodeList ¶
Creates a deep copy of this list. Ownership is transferred to the caller
- Return type:
- count(self) int ¶
Returns the number of nodes in the list.
- Return type:
int
- dump(self) str ¶
Dump list
- Return type:
str
- list(self) List[QgsSQLStatement.Node] ¶
Returns list
- Return type:
List[QgsSQLStatement.Node]
- class NodeLiteral¶
Bases:
Node
Literal value (integer, integer64, double, string)
QgsSQLStatement.NodeLiteral(value: Any) Constructor
QgsSQLStatement.NodeLiteral(QgsSQLStatement.NodeLiteral)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- value(self) Any ¶
The value of the literal.
- Return type:
Any
- class NodeSelect¶
Bases:
Node
SELECT node
QgsSQLStatement.NodeSelect(tableList: Iterable[QgsSQLStatement.NodeTableDef], columns: Iterable[QgsSQLStatement.NodeSelectedColumn], distinct: bool) Constructor
QgsSQLStatement.NodeSelect(QgsSQLStatement.NodeSelect)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- appendJoin(self, join: QgsSQLStatement.NodeJoin)¶
Append a join
- Parameters:
join (QgsSQLStatement.NodeJoin) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- columns(self) List[QgsSQLStatement.NodeSelectedColumn] ¶
Returns the list of columns
- Return type:
- distinct(self) bool ¶
Returns if the SELECT is DISTINCT
- Return type:
bool
- dump(self) str ¶
- Return type:
str
- joins(self) List[QgsSQLStatement.NodeJoin] ¶
Returns the list of joins
- Return type:
List[QgsSQLStatement.NodeJoin]
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- orderBy(self) List[QgsSQLStatement.NodeColumnSorted] ¶
Returns the list of order by columns
- Return type:
- setJoins(self, joins: Iterable[QgsSQLStatement.NodeJoin])¶
Sets joins
- Parameters:
joins (Iterable[QgsSQLStatement.NodeJoin]) –
- setOrderBy(self, orderBy: Iterable[QgsSQLStatement.NodeColumnSorted])¶
Sets order by columns
- Parameters:
orderBy (Iterable[QgsSQLStatement.NodeColumnSorted]) –
- setWhere(self, where: QgsSQLStatement.Node)¶
Sets where clause
- Parameters:
where (QgsSQLStatement.Node) –
- tables(self) List[QgsSQLStatement.NodeTableDef] ¶
Returns the list of tables
- Return type:
- where(self) QgsSQLStatement.Node ¶
Returns the where clause
- Return type:
- class NodeSelectedColumn¶
Bases:
Node
Selected column
QgsSQLStatement.NodeSelectedColumn(node: QgsSQLStatement.Node) Constructor
QgsSQLStatement.NodeSelectedColumn(QgsSQLStatement.NodeSelectedColumn)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- alias(self) str ¶
Alias name
- Return type:
str
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeSelectedColumn ¶
Clone with same type return
- Return type:
- column(self) QgsSQLStatement.Node ¶
Column that is referred to
- Return type:
- dump(self) str ¶
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- setAlias(self, alias: str)¶
Sets alias name
- Parameters:
alias (str) –
- class NodeTableDef¶
Bases:
Node
Table definition
QgsSQLStatement.NodeTableDef(name: str) Constructor with table name
QgsSQLStatement.NodeTableDef(name: str, alias: str) Constructor with table name and alias
QgsSQLStatement.NodeTableDef(schema: str, name: str, alias: str) Constructor with schema, table name and alias
New in version 3.28.
QgsSQLStatement.NodeTableDef(QgsSQLStatement.NodeTableDef)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- alias(self) str ¶
Table alias
- Return type:
str
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeTableDef ¶
Clone with same type return
- Return type:
- dump(self) str ¶
- Return type:
str
- name(self) str ¶
Table name
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- schema(self) str ¶
Returns the schema name.
New in version 3.28.
- Return type:
str
- class NodeType¶
Bases:
int
- class NodeUnaryOperator¶
Bases:
Node
Unary logicial/arithmetical operator ( NOT, - )
QgsSQLStatement.NodeUnaryOperator(op: QgsSQLStatement.UnaryOperator, operand: QgsSQLStatement.Node) Constructor
QgsSQLStatement.NodeUnaryOperator(QgsSQLStatement.NodeUnaryOperator)
- accept(self, v: QgsSQLStatement.Visitor)¶
- Parameters:
v (QgsSQLStatement.Visitor) –
- clone(self) QgsSQLStatement.Node ¶
- Return type:
- dump(self) str ¶
- Return type:
str
- nodeType(self) QgsSQLStatement.NodeType ¶
- Return type:
- op(self) QgsSQLStatement.UnaryOperator ¶
Operator
- Return type:
- operand(self) QgsSQLStatement.Node ¶
Operand
- Return type:
- class RecursiveVisitor¶
Bases:
Visitor
A visitor that recursively explores all children
QgsSQLStatement.RecursiveVisitor() Constructor
QgsSQLStatement.RecursiveVisitor(QgsSQLStatement.RecursiveVisitor)
- visit(self, n: QgsSQLStatement.NodeUnaryOperator)¶
- 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, QgsSQLStatement.NodeLiteral) None
- visit(self, QgsSQLStatement.NodeColumnRef) None
- visit(self, n: QgsSQLStatement.NodeSelectedColumn) None
- visit(self, 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:
- class UnaryOperator¶
Bases:
int
- class Visitor¶
Bases:
sip.wrapper
Support for visitor pattern - algorithms dealing with the statement may be implemented without modifying the Node classes
- visit(self, n: QgsSQLStatement.NodeUnaryOperator)¶
Visit NodeUnaryOperator
visit(self, n: QgsSQLStatement.NodeBinaryOperator) Visit NodeBinaryOperator
visit(self, n: QgsSQLStatement.NodeInOperator) Visit NodeInOperator
visit(self, n: QgsSQLStatement.NodeBetweenOperator) Visit NodeBetweenOperator
visit(self, n: QgsSQLStatement.NodeFunction) Visit NodeFunction
visit(self, n: QgsSQLStatement.NodeLiteral) Visit NodeLiteral
visit(self, n: QgsSQLStatement.NodeColumnRef) Visit NodeColumnRef
visit(self, n: QgsSQLStatement.NodeSelectedColumn) Visit NodeSelectedColumn
visit(self, n: QgsSQLStatement.NodeTableDef) Visit NodeTableDef
visit(self, n: QgsSQLStatement.NodeSelect) Visit NodeSelect
visit(self, n: QgsSQLStatement.NodeJoin) Visit NodeJoin
visit(self, n: QgsSQLStatement.NodeColumnSorted) Visit NodeColumnSorted
visit(self, n: QgsSQLStatement.NodeCast) Visit NodeCast
- Parameters:
- acceptVisitor(self, v: QgsSQLStatement.Visitor)¶
Entry function for the visitor pattern
- Parameters:
v (QgsSQLStatement.Visitor) –
- 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) Tuple[bool, str] ¶
Performs basic validity checks. Basically checking that columns referencing a table, references a specified table. Returns
True
if the validation is successful- Return type:
Tuple[bool, str]
- dump(self) str ¶
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 ¶
Returns
True
if 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¶
- parserErrorString(self) str ¶
Returns parser error
- Return type:
str
- quotedIdentifier(name: str) str ¶
Returns a quoted column reference (in double quotes)
See also
- Parameters:
name (str) –
- Return type:
str
- quotedIdentifierIfNeeded(name: str) str ¶
Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.
See also
- Parameters:
name (str) –
- Return type:
str
- quotedString(text: str) str ¶
Returns a quoted version of a string (in single quotes)
See also
- Parameters:
text (str) –
- Return type:
str
- rootNode(self) QgsSQLStatement.Node ¶
Returns the root node of the statement. The root node is
None
if parsing has failed.- Return type:
- statement(self) str ¶
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
- stripMsQuotedIdentifier(text: str) str ¶
Remove double quotes from an Microsoft style identifier.
See also
- Parameters:
text (str) –
- Return type:
str
- stripQuotedIdentifier(text: str) str ¶
Remove double quotes from an identifier.
See also
- Parameters:
text (str) –
- Return type:
str
- uoMinus = 1¶
- uoNot = 0¶