Class: QgsSQLStatement¶
Parses SQL statements.
List of all members, including inherited members
Class Hierarchy¶
Subclasses¶
Parses fragments of SQL statements, such as an expression or where clause. |
Classes
Abstract node class for SQL statement nodes. |
|
An 'X BETWEEN y and z' operator. |
|
Binary logical/arithmetical operator (AND, OR, =, +, ...). |
|
CAST operator. |
|
Reference to a column. |
|
Column in a ORDER BY. |
|
Function with a name and arguments node. |
|
An 'x IN (y, z)' operator. |
|
Join definition. |
|
A list of nodes. |
|
Literal value (integer, integer64, double, string). |
|
SELECT node. |
|
Selected column. |
|
Table definition. |
|
Unary logical/arithmetical operator ( NOT, - ). |
|
A visitor that recursively explores all children. |
|
Support for visitor pattern - algorithms dealing with the statement may be implemented without modifying the Node classes. |
Enums
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. |
- 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(*values)¶
Bases:
IntEnum- 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¶
- class JoinType(*values)¶
Bases:
IntEnum- jtCross = 5¶
- jtDefault = 0¶
- jtFull = 7¶
- jtInner = 6¶
- jtLeft = 1¶
- jtLeftOuter = 2¶
- jtRight = 3¶
- jtRightOuter = 4¶
- class Node[source]¶
Bases:
objectAbstract node class for SQL statement nodes.
- abstract 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.
- abstract 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:
- Returns:
a deep copy of this node.
- abstract dump(self) str[source]¶
Abstract virtual dump method
- Return type:
str
- Returns:
A statement which represents this node as string
- abstract 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:
NodeAn ‘X BETWEEN y and z’ operator.
- __init__(node: QgsSQLStatement.Node | None, minVal: QgsSQLStatement.Node | None, maxVal: QgsSQLStatement.Node | None, notBetween: bool = False)
Constructor
- Parameters:
node (Optional[QgsSQLStatement.Node])
minVal (Optional[QgsSQLStatement.Node])
maxVal (Optional[QgsSQLStatement.Node])
notBetween (bool = False)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- maxVal(self) QgsSQLStatement.Node | None[source]¶
Maximum bound
- Return type:
- minVal(self) QgsSQLStatement.Node | None[source]¶
Minimum bound
- Return type:
- node(self) QgsSQLStatement.Node | None[source]¶
Variable at the left of BETWEEN
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeBinaryOperator[source]¶
Bases:
NodeBinary logical/arithmetical operator (AND, OR, =, +, …).
- __init__(op: QgsSQLStatement.BinaryOperator, opLeft: QgsSQLStatement.Node | None, opRight: QgsSQLStatement.Node | None)
Constructor
- Parameters:
opLeft (Optional[QgsSQLStatement.Node])
opRight (Optional[QgsSQLStatement.Node])
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- op(self) QgsSQLStatement.BinaryOperator[source]¶
Operator
- Return type:
- opLeft(self) QgsSQLStatement.Node | None[source]¶
Left operand
- Return type:
- opRight(self) QgsSQLStatement.Node | None[source]¶
Right operand
- Return type:
- class NodeCast[source]¶
Bases:
NodeCAST operator.
- __init__(node: QgsSQLStatement.Node | None, type: str | None)
Constructor
- Parameters:
node (Optional[QgsSQLStatement.Node])
type (Optional[str])
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- node(self) QgsSQLStatement.Node | None[source]¶
Node that is referred to
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeColumnRef[source]¶
Bases:
NodeReference to a column.
- __init__(name: str | None, star: bool)
Constructor with column name only
- Parameters:
name (Optional[str])
star (bool)
- __init__(tableName: str | None, name: str | None, star: bool)
Constructor with table and column name
- __init__(a0: QgsSQLStatement.NodeColumnRef)
- Parameters:
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeColumnRef | None[source]¶
Clone with same type return
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeColumnSorted[source]¶
Bases:
NodeColumn in a ORDER BY.
- __init__(column: QgsSQLStatement.NodeColumnRef | None, asc: bool)
Constructor
- Parameters:
column (Optional[QgsSQLStatement.NodeColumnRef])
asc (bool)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeColumnSorted | None[source]¶
Clone with same type return
- Return type:
- column(self) QgsSQLStatement.NodeColumnRef | None[source]¶
The name of the column.
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeFunction[source]¶
Bases:
NodeFunction with a name and arguments node.
- __init__(name: str | None, args: QgsSQLStatement.NodeList | None)
Constructor
- Parameters:
name (Optional[str])
args (Optional[QgsSQLStatement.NodeList])
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- args(self) QgsSQLStatement.NodeList | None[source]¶
Returns arguments
- Return type:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeInOperator[source]¶
Bases:
NodeAn ‘x IN (y, z)’ operator.
- __init__(node: QgsSQLStatement.Node | None, list: QgsSQLStatement.NodeList | None, notin: bool = False)
Constructor
- Parameters:
node (Optional[QgsSQLStatement.Node])
list (Optional[QgsSQLStatement.NodeList])
notin (bool = False)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- list(self) QgsSQLStatement.NodeList | None[source]¶
Values list
- Return type:
- node(self) QgsSQLStatement.Node | None[source]¶
Variable at the left of IN
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeJoin[source]¶
Bases:
NodeJoin definition.
- __init__(tabledef: QgsSQLStatement.NodeTableDef | None, onExpr: QgsSQLStatement.Node | None, type: QgsSQLStatement.JoinType)
Constructor with table definition, ON expression
- Parameters:
tabledef (Optional[QgsSQLStatement.NodeTableDef])
onExpr (Optional[QgsSQLStatement.Node])
type (QgsSQLStatement.JoinType)
- __init__(tabledef: QgsSQLStatement.NodeTableDef | None, usingColumns: Iterable[str | None], type: QgsSQLStatement.JoinType)
Constructor with table definition and USING columns
- Parameters:
tabledef (Optional[QgsSQLStatement.NodeTableDef])
usingColumns (Iterable[Optional[str]])
type (QgsSQLStatement.JoinType)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeJoin | None[source]¶
Clone with same type return
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- onExpr(self) QgsSQLStatement.Node | None[source]¶
On expression. Will be
NoneifusingColumns()is not empty- Return type:
- tableDef(self) QgsSQLStatement.NodeTableDef | None[source]¶
Table definition
- Return type:
- 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:
- list(self) list[QgsSQLStatement.Node]¶
Returns list
- Return type:
list[QgsSQLStatement.Node]
- class NodeLiteral[source]¶
Bases:
NodeLiteral value (integer, integer64, double, string).
- __init__(a0: QgsSQLStatement.NodeLiteral)
- Parameters:
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeSelect[source]¶
Bases:
NodeSELECT node.
- __init__(tableList: Iterable[QgsSQLStatement.NodeTableDef], columns: Iterable[QgsSQLStatement.NodeSelectedColumn], distinct: bool)
Constructor
- Parameters:
tableList (Iterable[QgsSQLStatement.NodeTableDef])
columns (Iterable[QgsSQLStatement.NodeSelectedColumn])
distinct (bool)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- appendJoin(self, join: QgsSQLStatement.NodeJoin | None)[source]¶
Append a join
- Parameters:
join (Optional[QgsSQLStatement.NodeJoin])
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- 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]
- abstract 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:
- class NodeSelectedColumn[source]¶
Bases:
NodeSelected column.
- __init__(node: QgsSQLStatement.Node | None)
Constructor
- Parameters:
node (Optional[QgsSQLStatement.Node])
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeSelectedColumn | None[source]¶
Clone with same type return
- Return type:
- column(self) QgsSQLStatement.Node | None[source]¶
Column that is referred to
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeTableDef[source]¶
Bases:
NodeTable definition.
- __init__(name: str | None)
Constructor with table name
- Parameters:
name (Optional[str])
- __init__(name: str | None, alias: str | None)
Constructor with table name and alias
- __init__(schema: str | None, name: str | None, alias: str | None)
Constructor with schema, table name and alias
Added in version 3.28.
- __init__(a0: QgsSQLStatement.NodeTableDef)
- Parameters:
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- cloneThis(self) QgsSQLStatement.NodeTableDef | None[source]¶
Clone with same type return
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- class NodeType(*values)¶
Bases:
IntEnum- 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¶
- class NodeUnaryOperator[source]¶
Bases:
NodeUnary logical/arithmetical operator ( NOT, - ).
- __init__(op: QgsSQLStatement.UnaryOperator, operand: QgsSQLStatement.Node | None)
Constructor
- Parameters:
operand (Optional[QgsSQLStatement.Node])
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None[source]¶
- Return type:
- abstract nodeType(self) QgsSQLStatement.NodeType[source]¶
- Return type:
- op(self) QgsSQLStatement.UnaryOperator[source]¶
Operator
- Return type:
- operand(self) QgsSQLStatement.Node | None[source]¶
Operand
- Return type:
- class RecursiveVisitor[source]¶
Bases:
VisitorA visitor that recursively explores all children.
- abstract visit(self, n: QgsSQLStatement.NodeUnaryOperator)[source]¶
- abstract visit(self, n: QgsSQLStatement.NodeBinaryOperator)
- abstract visit(self, n: QgsSQLStatement.NodeInOperator)
- abstract visit(self, n: QgsSQLStatement.NodeBetweenOperator)
- abstract visit(self, n: QgsSQLStatement.NodeFunction)
- abstract visit(self, a0: QgsSQLStatement.NodeLiteral)
- abstract visit(self, a0: QgsSQLStatement.NodeColumnRef)
- abstract visit(self, n: QgsSQLStatement.NodeSelectedColumn)
- abstract visit(self, a0: QgsSQLStatement.NodeTableDef)
- abstract visit(self, n: QgsSQLStatement.NodeSelect)
- abstract visit(self, n: QgsSQLStatement.NodeJoin)
- abstract visit(self, n: QgsSQLStatement.NodeColumnSorted)
- abstract visit(self, n: QgsSQLStatement.NodeCast)
- Parameters:
- class Visitor[source]¶
Bases:
objectSupport for visitor pattern - algorithms dealing with the statement may be implemented without modifying the Node classes.
- abstract visit(self, n: QgsSQLStatement.NodeUnaryOperator)[source]¶
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)[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- Return type:
(bool, str)
- 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:
- 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¶