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 specified statement.

If allowFragments is True then the parser will allow SQL fragments, such as a expression or filter where clause alone.

Methods

acceptVisitor

Entry function for the visitor pattern

doBasicValidationChecks

Performs basic validity checks.

dump

Returns the statement string, constructed from the internal abstract syntax tree.

hasParserError

Returns True if an error occurred when parsing the input statement

parserErrorString

Returns parser error

quotedIdentifier

Returns a quoted column reference (in double quotes)

quotedIdentifierIfNeeded

Returns a quoted column reference (in double quotes) if needed, or otherwise the original string.

quotedString

Returns a quoted version of a string (in single quotes)

rootNode

Returns the root node of the statement.

statement

Returns the original, unmodified statement string.

stripMsQuotedIdentifier

Remove double quotes from an Microsoft style identifier.

stripQuotedIdentifier

Remove double quotes from an identifier.

Attributes

boAnd

boConcat

boDiv

boEQ

boGE

boGT

boILike

boIntDiv

boIs

boIsNot

boLE

boLT

boLike

boMinus

boMod

boMul

boNE

boNotILike

boNotLike

boOr

boPlus

boPow

jtCross

jtDefault

jtFull

jtInner

jtLeft

jtLeftOuter

jtRight

jtRightOuter

ntBetweenOperator

ntBinaryOperator

ntCast

ntColumnRef

ntColumnSorted

ntFunction

ntInOperator

ntJoin

ntLiteral

ntSelect

ntSelectedColumn

ntTableDef

ntUnaryOperator

uoMinus

uoNot

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

QgsSQLStatement.Node

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

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

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

QgsSQLStatement.Node

minVal(self)QgsSQLStatement.Node

Minimum bound

Return type

QgsSQLStatement.Node

node(self)QgsSQLStatement.Node

Variable at the left of BETWEEN

Return type

QgsSQLStatement.Node

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

dump(self)str
Return type

str

leftAssociative(self)bool

Is left associative ?

Return type

bool

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

op(self)QgsSQLStatement.BinaryOperator

Operator

Return type

QgsSQLStatement.BinaryOperator

opLeft(self)QgsSQLStatement.Node

Left operand

Return type

QgsSQLStatement.Node

opRight(self)QgsSQLStatement.Node

Right operand

Return type

QgsSQLStatement.Node

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

QgsSQLStatement.Node

dump(self)str
Return type

str

node(self)QgsSQLStatement.Node

Node that is referred to

Return type

QgsSQLStatement.Node

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

cloneThis(self)QgsSQLStatement.NodeColumnRef

Clone with same type return

Return type

QgsSQLStatement.NodeColumnRef

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

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

cloneThis(self)QgsSQLStatement.NodeColumnSorted

Clone with same type return

Return type

QgsSQLStatement.NodeColumnSorted

column(self)QgsSQLStatement.NodeColumnRef

The name of the column.

Return type

QgsSQLStatement.NodeColumnRef

dump(self)str
Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.NodeList

clone(self)QgsSQLStatement.Node
Return type

QgsSQLStatement.Node

dump(self)str
Return type

str

name(self)str

Returns function name

Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

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

QgsSQLStatement.NodeList

node(self)QgsSQLStatement.Node

Variable at the left of IN

Return type

QgsSQLStatement.Node

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

cloneThis(self)QgsSQLStatement.NodeJoin

Clone with same type return

Return type

QgsSQLStatement.NodeJoin

dump(self)str
Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

onExpr(self)QgsSQLStatement.Node

On expression. Will be None if usingColumns() is not empty

Return type

QgsSQLStatement.Node

tableDef(self)QgsSQLStatement.NodeTableDef

Table definition

Return type

QgsSQLStatement.NodeTableDef

type(self)QgsSQLStatement.JoinType

Join type

Return type

QgsSQLStatement.JoinType

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

QgsSQLStatement.NodeList

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

QgsSQLStatement.Node

dump(self)str
Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

columns(self)List[QgsSQLStatement.NodeSelectedColumn]

Returns the list of columns

Return type

List[QgsSQLStatement.NodeSelectedColumn]

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

QgsSQLStatement.NodeType

orderBy(self)List[QgsSQLStatement.NodeColumnSorted]

Returns the list of order by columns

Return type

List[QgsSQLStatement.NodeColumnSorted]

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

List[QgsSQLStatement.NodeTableDef]

where(self)QgsSQLStatement.Node

Returns the where clause

Return type

QgsSQLStatement.Node

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

QgsSQLStatement.Node

cloneThis(self)QgsSQLStatement.NodeSelectedColumn

Clone with same type return

Return type

QgsSQLStatement.NodeSelectedColumn

column(self)QgsSQLStatement.Node

Column that is referred to

Return type

QgsSQLStatement.Node

dump(self)str
Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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(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

QgsSQLStatement.Node

cloneThis(self)QgsSQLStatement.NodeTableDef

Clone with same type return

Return type

QgsSQLStatement.NodeTableDef

dump(self)str
Return type

str

name(self)str

Table name

Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

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

QgsSQLStatement.Node

dump(self)str
Return type

str

nodeType(self)QgsSQLStatement.NodeType
Return type

QgsSQLStatement.NodeType

op(self)QgsSQLStatement.UnaryOperator

Operator

Return type

QgsSQLStatement.UnaryOperator

operand(self)QgsSQLStatement.Node

Operand

Return type

QgsSQLStatement.Node

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) visit(self, n: QgsSQLStatement.NodeInOperator) visit(self, n: QgsSQLStatement.NodeBetweenOperator) visit(self, n: QgsSQLStatement.NodeFunction) visit(self, QgsSQLStatement.NodeLiteral) visit(self, QgsSQLStatement.NodeColumnRef) visit(self, n: QgsSQLStatement.NodeSelectedColumn) visit(self, QgsSQLStatement.NodeTableDef) visit(self, n: QgsSQLStatement.NodeSelect) visit(self, n: QgsSQLStatement.NodeJoin) visit(self, n: QgsSQLStatement.NodeColumnSorted) visit(self, n: QgsSQLStatement.NodeCast)

Parameters

n (QgsSQLStatement.NodeUnaryOperator) –

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

n (QgsSQLStatement.NodeUnaryOperator) –

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

quotedString()

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

quotedString()

Parameters

name (str) –

Return type

str

quotedString(text: str)str

Returns a quoted version of a string (in single quotes)

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

QgsSQLStatement.Node

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.

Parameters

text (str) –

Return type

str

stripQuotedIdentifier(text: str)str

Remove double quotes from an identifier.

Parameters

text (str) –

Return type

str

uoMinus = 1
uoNot = 0