Lizmap Web Client 3.11.0

SqlTools
in package

Table of Contents

Constants

GEOMETRY_PREDICATES  : array<string, string> = array('intersects' => 'ST_Intersects', 'contain...
QGIS geometry predicate functions mapped to their PostGIS ST_* equivalents.
PARSER_STATE_BETWEEN_PARAMETERS  : mixed = 0
PARSER_STATE_BETWEEN_TOKENS  : mixed = 0
PARSER_STATE_COMMENT_SQL_1  : mixed = 12
PARSER_STATE_COMMENT_SQL_2  : mixed = 13
PARSER_STATE_IN_VALUE  : mixed = 3
PARSER_STATE_PARAM_EQUAL  : mixed = 2
PARSER_STATE_PARAM_NAME  : mixed = 1
PARSER_STATE_SQL_VALUE  : mixed = 10
PARSER_STATE_STRING_VALUE  : mixed = 11
PARSER_STATE_TABLE_NAME  : mixed = 8
PARSER_STATE_TABLE_NAME_QUOTE  : mixed = 7
PARSER_STATE_TABLE_NAME_SEPARATOR  : mixed = 6
PARSER_STATE_TABLE_SCHEMA  : mixed = 5
PARSER_STATE_TABLE_SQL  : mixed = 9
PARSER_STATE_TABLE_VALUE  : mixed = 4
QGIS_EXPRESSION_TOKENS  : mixed = array( // Operators '<>', '||', '+', '-', '*',...
Tokens of the QGIS expressions without risk, added to the whitelist for the expressions evaluated only by QGIS Server, e.g. the filter of the features table.
ALLOWED_SQL_TOKENS  : mixed = array( // operators ',', '=', '<', '>', '<=', '...
Allowed SQL tokens.
NUMERIC_LITERAL_PATTERN  : mixed = '[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+...
Regular expression of a numeric literal, without delimiters.
SQL_KEYWORDS  : mixed = array('is', 'not', 'null', 'and', 'or', 'in', '...
Allowed SQL tokens which are keywords, not column names, when they are not followed by an opening parenthesis.

Methods

getSQLStringColumnNames()  : array<string|int, string>
Get the column names referenced by a SQL filter.
parseAndValidateSQLString()  : bool
Parse and validate the provided SQL query string against the defined whitelist of tokens and functions.
parseQgisConnectionString()  : array<string|int, mixed>
Parse a Qgis connection string.
replaceIdVariable()  : null|string
Replace the $id variable of a QGIS expression filter.
translateExpressionToPostgis()  : string
Translate a QGIS expression filter into a PostGIS compatible SQL filter.
validateExpressionFilter()  : bool
Validate if the given string is valid.
getNextSignificantToken()  : null|string
Get the next significant token of a list of SQL tokens.
replaceSignificantTokens()  : string
Replace the significant tokens of a SQL string.
tokenizeSQLString()  : array<string|int, string>
Split a SQL string into tokens.

Constants

GEOMETRY_PREDICATES

QGIS geometry predicate functions mapped to their PostGIS ST_* equivalents.

public array<string, string> GEOMETRY_PREDICATES = array('intersects' => 'ST_Intersects', 'contains' => 'ST_Contains', 'within' => 'ST_Within', 'crosses' => 'ST_Crosses', 'overlaps' => 'ST_Overlaps', 'touches' => 'ST_Touches', 'disjoint' => 'ST_Disjoint')

These are the operators offered by the selection tool and the filter form.

PARSER_STATE_BETWEEN_PARAMETERS

public mixed PARSER_STATE_BETWEEN_PARAMETERS = 0

PARSER_STATE_BETWEEN_TOKENS

public mixed PARSER_STATE_BETWEEN_TOKENS = 0

PARSER_STATE_COMMENT_SQL_1

public mixed PARSER_STATE_COMMENT_SQL_1 = 12

PARSER_STATE_COMMENT_SQL_2

public mixed PARSER_STATE_COMMENT_SQL_2 = 13

PARSER_STATE_IN_VALUE

public mixed PARSER_STATE_IN_VALUE = 3

PARSER_STATE_PARAM_EQUAL

public mixed PARSER_STATE_PARAM_EQUAL = 2

PARSER_STATE_PARAM_NAME

public mixed PARSER_STATE_PARAM_NAME = 1

PARSER_STATE_SQL_VALUE

public mixed PARSER_STATE_SQL_VALUE = 10

PARSER_STATE_STRING_VALUE

public mixed PARSER_STATE_STRING_VALUE = 11

PARSER_STATE_TABLE_NAME

public mixed PARSER_STATE_TABLE_NAME = 8

PARSER_STATE_TABLE_NAME_QUOTE

public mixed PARSER_STATE_TABLE_NAME_QUOTE = 7

PARSER_STATE_TABLE_NAME_SEPARATOR

public mixed PARSER_STATE_TABLE_NAME_SEPARATOR = 6

PARSER_STATE_TABLE_SCHEMA

public mixed PARSER_STATE_TABLE_SCHEMA = 5

PARSER_STATE_TABLE_SQL

public mixed PARSER_STATE_TABLE_SQL = 9

PARSER_STATE_TABLE_VALUE

public mixed PARSER_STATE_TABLE_VALUE = 4

QGIS_EXPRESSION_TOKENS

Tokens of the QGIS expressions without risk, added to the whitelist for the expressions evaluated only by QGIS Server, e.g. the filter of the features table.

public mixed QGIS_EXPRESSION_TOKENS = array( // Operators '<>', '||', '+', '-', '*', '/', '%', '~', // Booleans 'true', 'false', // Conditionals 'if', 'try', // Conversions 'to_string', 'to_int', 'to_real', 'to_date', 'to_datetime', 'to_time', // Strings 'substr', 'strpos', 'title', 'lpad', 'rpad', 'regexp_match', 'regexp_replace', 'regexp_substr', // Mathematics 'abs', 'round', 'floor', 'ceil', 'min', 'max', // Dates 'year', 'month', 'week', 'day', 'hour', 'minute', 'second', 'day_of_week', 'format_date', // Arrays 'array', 'array_contains', 'array_length', 'array_to_string', 'string_to_array', // Current feature 'attribute', '$x', '$y', 'x', 'y', 'perimeter', 'centroid', 'geom_from_wkt', 'geom_to_wkt', )

Only operators and functions using the values of the current feature are listed: no function reading other layers, the project, the files or the server environment. A more complex expression can be used in a virtual field of the layer.

These tokens are not added to ALLOWED_SQL_TOKENS: they are not valid in the SQL run by Lizmap, e.g. the WFS filter queried directly in the database, which would fail instead of being sent to QGIS Server, or the filter of the filter module. Please only in lowercase.

ALLOWED_SQL_TOKENS

Allowed SQL tokens.

protected mixed ALLOWED_SQL_TOKENS = array( // operators ',', '=', '<', '>', '<=', '>=', '!=', 'is', 'not', 'null', 'and', 'or', 'in', 'like', 'ilike', 'as', 'case', 'when', 'then', 'else', 'end', 'between', 'dmetaphone', 'soundex', // QGIS Expression tokens '$id', '$geometry', '$area', '$length', // Lizmap tokens '@lizmap_user', '@lizmap_user_groups', // Spatial functions 'st_intersects', 'st_contains', 'st_within', 'st_crosses', 'st_overlaps', 'st_touches', 'st_disjoint', 'st_geomfromgml', 'st_geomfromtext', 'st_geomfromwkb', 'st_geomfromewkb', 'st_geomfromewkt', 'st_geomfromgeojson', 'st_geomfromkml', 'st_makepoint', 'st_makeline', 'st_makepolygon', 'st_makeenvelope', 'st_buffer', 'st_transform', 'st_setsrid', 'st_srid', 'st_astext', 'st_asbinary', 'st_asewkt', 'st_asewkb', 'st_asgeojson', 'st_askml', 'st_asgml', 'st_distance', 'st_length', 'st_area', 'st_area2d', 'st_intersection', 'st_union', 'st_difference', 'st_symdifference', 'st_convexhull', 'st_envelope', 'st_centroid', 'st_x', 'st_y', 'st_exteriorring', 'st_interiorringn', 'st_numinteriorrings', 'st_numgeometries', 'st_geometryn', // QGIS Expression functions, used by the filters built by the web client, // e.g. the selection tool, which are also sent to QGIS Server. // When the filter is run as SQL by Lizmap, translateExpressionToPostgis() // translates the spatial predicates and geom_from_gml to their PostGIS // ST_* equivalents, and $geometry to the geometry column. 'buffer', 'distance', 'area', 'length', 'intersects', 'contains', 'within', 'crosses', 'overlaps', 'touches', 'disjoint', 'geom_from_gml', // String functions 'length', 'lower', 'upper', 'trim', 'ltrim', 'rtrim', 'substring', 'position', 'replace', 'concat', 'left', 'right', 'initcap', // Accents removal, with the unaccent PostgreSQL extension, and the immutable // f_unaccent function advised by the Lizmap documentation, used by the quick // search of the attribute table only when they exist in the database 'unaccent', 'f_unaccent', // Date and time functions 'now', 'current_date', 'current_time', 'current_timestamp', 'extract', 'date_part', 'date_trunc', 'age', 'to_date', // Miscellaneous functions 'coalesce', 'nullif', )

Please only in lowercase.

NUMERIC_LITERAL_PATTERN

Regular expression of a numeric literal, without delimiters.

protected mixed NUMERIC_LITERAL_PATTERN = '[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?'

SQL_KEYWORDS

Allowed SQL tokens which are keywords, not column names, when they are not followed by an opening parenthesis.

protected mixed SQL_KEYWORDS = array('is', 'not', 'null', 'and', 'or', 'in', 'like', 'ilike', 'as', 'case', 'when', 'then', 'else', 'end', 'between', 'current_date', 'current_time', 'current_timestamp')

Please only in lowercase.

Methods

getSQLStringColumnNames()

Get the column names referenced by a SQL filter.

public static getSQLStringColumnNames(string $sql) : array<string|int, string>

The filter must have been validated with parseAndValidateSQLString(). The column names are the double-quoted identifiers, and the bare words which are neither SQL keywords nor function calls, nor QGIS or Lizmap variables like $geometry or @lizmap_user, e.g. "age" in "age > 3". The bare words are returned in lowercase, as the database folds them. Strings and comments are ignored.

Parameters
$sql : string

the validated SQL filter

Tags
throws
InvalidArgumentException

if the string cannot be split

Return values
array<string|int, string> —

the unique column names

parseAndValidateSQLString()

Parse and validate the provided SQL query string against the defined whitelist of tokens and functions.

public static parseAndValidateSQLString(string $sql[, array<string|int, mixed> $allowedSqlTokens = array() ][, array<string|int, string> $columnNames = array() ]) : bool

The column names are accepted without double quotes, but not as a function, followed by an opening parenthesis: a column with the name of a function, e.g. env, does not allow this function.

Parameters
$sql : string

the SQL query string to be parsed and validated

$allowedSqlTokens : array<string|int, mixed> = array()

allowed SQL tokens to be merged with the default whitelist

$columnNames : array<string|int, string> = array()

column names allowed without double quotes, not as functions

Tags
throws
Exception

if the SQL query contains disallowed tokens, functions, or syntax errors

Return values
bool —

true when no exceptions

parseQgisConnectionString()

Parse a Qgis connection string.

public static parseQgisConnectionString(string $connection_string) : array<string|int, mixed>

It supports table and sql parameters, as well as geometry tags like (geom), in addition of other database connection parameters.

It is compliant with the Qgis 4.2 parser.

Parameters
$connection_string : string
Tags
see
https://api.qgis.org/api/4.2/qgsdatasourceuri_8cpp_source.html
throws
QgisConnectionStringParserException
Return values
array<string|int, mixed> —

the list of parameters

replaceIdVariable()

Replace the $id variable of a QGIS expression filter.

public static replaceIdVariable(string $filter, null|string $idColumn) : null|string

The variable is replaced on the tokens of the filter: a $id in a string, a double quoted identifier or a comment is left untouched.

Parameters
$filter : string

The filter, validated by parseAndValidateSQLString()

$idColumn : null|string

The primary key column, enclosed for the database, or null if the layer has no single primary key

Return values
null|string —

The filter with the primary key column, or null if the filter uses $id and there is no single primary key column

translateExpressionToPostgis()

Translate a QGIS expression filter into a PostGIS compatible SQL filter.

public static translateExpressionToPostgis(string $filter, string $geometryColumn) : string

Rewrites the geometry predicate functions used by the selection tool and the filter form (intersects, contains, within, crosses, overlaps, touches, disjoint) to their PostGIS ST_* equivalents, translates geom_from_gml() and replaces the $geometry variable with the given geometry column.

The filter is translated on its tokens, as it has been validated: the strings, the double quoted identifiers and the comments are left untouched. The function names are matched case-insensitively and only when followed by an opening parenthesis, after the whitespaces and the comments.

Parameters
$filter : string

The QGIS expression filter, validated by parseAndValidateSQLString()

$geometryColumn : string

The layer geometry column name

Return values
string —

The PostGIS compatible filter

validateExpressionFilter()

Validate if the given string is valid.

public static validateExpressionFilter(string $sql[, array<string|int, string> $allowedSqlTokens = array() ][, array<string|int, string> $columnNames = array() ]) : bool

The string is checked against the whitelist of tokens, extended with the given tokens, e.g. the QGIS expression tokens, and with the unquoted column names, e.g. the field names of a layer, which can not be used as functions.

Parameters
$sql : string

The SQL string to test

$allowedSqlTokens : array<string|int, string> = array()

Allowed SQL tokens to be merged with the default whitelist

$columnNames : array<string|int, string> = array()

Column names allowed without double quotes, not as functions

Return values
bool —

True if the SQL string is valid

getNextSignificantToken()

Get the next significant token of a list of SQL tokens.

protected static getNextSignificantToken(array<string|int, string> $tokens, int $start) : null|string

The whitespaces, line breaks and comments are skipped, as done by the databases. A block comment ends at the first closing mark, as in SQLite and in parseAndValidateSQLString(): with the nested comments of PostgreSQL, the extra closing mark is then rejected by the whitelist.

Parameters
$tokens : array<string|int, string>

the tokens returned by tokenizeSQLString()

$start : int

the index of the first token to look at

Return values
null|string —

the next significant token, or null if there is none

replaceSignificantTokens()

Replace the significant tokens of a SQL string.

protected static replaceSignificantTokens(string $sql, callable $callback) : string

The tokens of the strings, of the double quoted identifiers and of the comments are kept unchanged: only the other tokens are given to the callback, e.g. the function names and the variables.

Parameters
$sql : string

the SQL string, validated by parseAndValidateSQLString()

$callback : callable

called with the token, all the tokens and the index of the token; it returns the replacement of the token, or null to keep it

Tags
throws
InvalidArgumentException

if the string cannot be split

Return values
string —

the SQL string with the replaced tokens

tokenizeSQLString()

Split a SQL string into tokens.

protected static tokenizeSQLString(string $sql) : array<string|int, string>

The string is split on the syntactic elements: numeric literals, operators, punctuation, comments, quotes and whitespace, which are kept as tokens.

Parameters
$sql : string

the SQL string

Tags
throws
InvalidArgumentException

if the string cannot be split

Return values
array<string|int, string> —

the tokens

On this page

Search results