Class: QgsSQLStatement¶
Parses SQL statements.
Class Hierarchy¶
Subclasses¶
Parses 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
allowFragments
isTrue
then 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:
object
Abstract 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:
Optional[QgsSQLStatement.Node]
- 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:
Node
An ‘X BETWEEN y and z’ operator.
QgsSQLStatement.NodeBetweenOperator(node: Optional[QgsSQLStatement.Node], minVal: Optional[QgsSQLStatement.Node], maxVal: Optional[QgsSQLStatement.Node], notBetween: bool = False) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeBinaryOperator[source]¶
Bases:
Node
Binary logical/arithmetical operator (AND, OR, =, +, …).
QgsSQLStatement.NodeBinaryOperator(op: QgsSQLStatement.BinaryOperator, opLeft: Optional[QgsSQLStatement.Node], opRight: Optional[QgsSQLStatement.Node]) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None [source]¶
- Return type:
Optional[QgsSQLStatement.Node]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- op(self) QgsSQLStatement.BinaryOperator [source]¶
- 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:
Node
CAST operator.
QgsSQLStatement.NodeCast(node: Optional[QgsSQLStatement.Node], type: Optional[str]) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeColumnRef[source]¶
Bases:
Node
Reference 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)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeColumnSorted[source]¶
Bases:
Node
Column in a ORDER BY.
QgsSQLStatement.NodeColumnSorted(column: Optional[QgsSQLStatement.NodeColumnRef], asc: bool) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeFunction[source]¶
Bases:
Node
Function with a name and arguments node.
QgsSQLStatement.NodeFunction(name: Optional[str], args: Optional[QgsSQLStatement.NodeList]) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- args(self) QgsSQLStatement.NodeList | None [source]¶
Returns arguments
- Return type:
Optional[QgsSQLStatement.NodeList]
- abstract clone(self) QgsSQLStatement.Node | None [source]¶
- Return type:
Optional[QgsSQLStatement.Node]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeInOperator[source]¶
Bases:
Node
An ‘x IN (y, z)’ operator.
QgsSQLStatement.NodeInOperator(node: Optional[QgsSQLStatement.Node], list: Optional[QgsSQLStatement.NodeList], notin: bool = False) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeJoin[source]¶
Bases:
Node
Join 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
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- onExpr(self) QgsSQLStatement.Node | None [source]¶
On expression. Will be
None
ifusingColumns()
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:
object
A 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:
Node
Literal value (integer, integer64, double, string).
QgsSQLStatement.NodeLiteral(value: Any) Constructor
QgsSQLStatement.NodeLiteral(a0: QgsSQLStatement.NodeLiteral)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None [source]¶
- Return type:
Optional[QgsSQLStatement.Node]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeSelect[source]¶
Bases:
Node
SELECT node.
QgsSQLStatement.NodeSelect(tableList: Iterable[QgsSQLStatement.NodeTableDef], columns: Iterable[QgsSQLStatement.NodeSelectedColumn], distinct: bool) Constructor
- 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:
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]
- 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:
Optional[QgsSQLStatement.Node]
- class NodeSelectedColumn[source]¶
Bases:
Node
Selected column.
QgsSQLStatement.NodeSelectedColumn(node: Optional[QgsSQLStatement.Node]) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeTableDef[source]¶
Bases:
Node
Table 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)
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract 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]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- class NodeType¶
Bases:
int
- class NodeUnaryOperator[source]¶
Bases:
Node
Unary logical/arithmetical operator ( NOT, - ).
QgsSQLStatement.NodeUnaryOperator(op: QgsSQLStatement.UnaryOperator, operand: Optional[QgsSQLStatement.Node]) Constructor
- abstract accept(self, v: QgsSQLStatement.Visitor)[source]¶
- Parameters:
- abstract clone(self) QgsSQLStatement.Node | None [source]¶
- Return type:
Optional[QgsSQLStatement.Node]
- abstract nodeType(self) QgsSQLStatement.NodeType [source]¶
- Return type:
- op(self) QgsSQLStatement.UnaryOperator [source]¶
- operand(self) QgsSQLStatement.Node | None [source]¶
- class RecursiveVisitor[source]¶
Bases:
Visitor
A visitor that recursively explores all children.
- abstract visit(self, n: QgsSQLStatement.NodeUnaryOperator)[source]¶
- abstract visit(self, n: QgsSQLStatement.NodeBinaryOperator) None
- abstract visit(self, n: QgsSQLStatement.NodeInOperator) None
- abstract visit(self, n: QgsSQLStatement.NodeBetweenOperator) None
- abstract visit(self, n: QgsSQLStatement.NodeFunction) None
- abstract visit(self, a0: QgsSQLStatement.NodeLiteral) None
- abstract visit(self, a0: QgsSQLStatement.NodeColumnRef) None
- abstract visit(self, n: QgsSQLStatement.NodeSelectedColumn) None
- abstract visit(self, a0: QgsSQLStatement.NodeTableDef) None
- abstract visit(self, n: QgsSQLStatement.NodeSelect) None
- abstract visit(self, n: QgsSQLStatement.NodeJoin) None
- abstract visit(self, n: QgsSQLStatement.NodeColumnSorted) None
- abstract visit(self, n: QgsSQLStatement.NodeCast) None
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeBinaryOperator)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeInOperator)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeBetweenOperator)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeFunction)[source]
- Parameters:
- abstract visit(self, a0: QgsSQLStatement.NodeLiteral)[source]
- Parameters:
- abstract visit(self, a0: QgsSQLStatement.NodeColumnRef)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeSelectedColumn)[source]
- Parameters:
- abstract visit(self, a0: QgsSQLStatement.NodeTableDef)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeSelect)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeJoin)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeColumnSorted)[source]
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeCast)[source]
- Parameters:
- class UnaryOperator¶
Bases:
int
- class Visitor[source]¶
Bases:
object
Support 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
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeBinaryOperator)[source]
Visit NodeBinaryOperator
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeInOperator)[source]
Visit NodeInOperator
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeBetweenOperator)[source]
Visit NodeBetweenOperator
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeFunction)[source]
Visit NodeFunction
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeLiteral)[source]
Visit NodeLiteral
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeColumnRef)[source]
Visit NodeColumnRef
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeSelectedColumn)[source]
Visit NodeSelectedColumn
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeTableDef)[source]
Visit NodeTableDef
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeSelect)[source]
Visit NodeSelect
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeJoin)[source]
Visit NodeJoin
- Parameters:
- abstract visit(self, n: QgsSQLStatement.NodeColumnSorted)[source]
Visit NodeColumnSorted
- Parameters:
- abstract 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
True
if 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
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¶
- 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
None
if 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¶