stratified_packager.toolbelt.sql
Plugin-agnostic SQL string building for the SQLite/GeoPackage dialect.
Pure standard library: this module imports neither qgis nor osgeo (GDAL/OGR), so its
helpers stay usable from background threads, scripts/ and osgeo-free tests. It owns the SQL
text concerns — identifier quoting and reserved-table-name guarding — while
gpkg owns the OGR and sqlite3 work that consumes the quoting.
Functions
|
Collect the identifiers an SQL query compares with |
|
Quote an SQL identifier (table or column name) with double quotes. |
|
Prefix |
|
Collect the table names an SQL query reads ( |
|
Report why SQLite could not compile where as a |
- stratified_packager.toolbelt.sql.equality_operands(query, /)[source]
Collect the identifiers an SQL query compares with
=, table qualifier dropped.Answers “which columns could an index serve here?” for a caller that intersects the result with a table’s real column names. That intersection is what lets this stay a scan rather than a parser: an operand that is a literal, a function name or a table alias has no matching column and drops out, so over-matching costs nothing.
- stratified_packager.toolbelt.sql.quote_identifier(name, /)[source]
Quote an SQL identifier (table or column name) with double quotes.
- stratified_packager.toolbelt.sql.safe_table_name(name, /)[source]
Prefix
_to dodge GeoPackage/SQLite reserved table-name prefixes.OGR refuses to create a GeoPackage layer whose name begins with
gpkg, and SQLite reserves thesqlite_prefix for its own tables. A name that begins with either (case-insensitively, since both dialects fold table-name case) gets a leading_; every other name is returned unchanged. Idempotent:_gpkg…no longer matches.
- stratified_packager.toolbelt.sql.source_tables(query, /)[source]
Collect the table names an SQL query reads (
FROM/JOIN), quoting stripped.Answers “which tables does this query expect to exist?” for a caller that checks them against the tables it is about to create.
- stratified_packager.toolbelt.sql.sqlite_where_error(columns, where, /)[source]
Report why SQLite could not compile where as a
WHEREclause over columns.A filter written for another provider’s dialect (a PostgreSQL
::cast, a schema-qualified table, a function SQLite lacks) is accepted by the layer API yet fails when the SQLite/GeoPackage backend prepares the statement — where the failure surfaces as a bareCPLErrorrather than a caller-visible one. Compiling it here answers the same question up front.The statement is compiled with
EXPLAIN, which resolves every table, column and function name without executing anything. Compilation runs against a throwaway in-memory table, so no GeoPackage is touched and the caller needs only the column names. Extension functions a real GeoPackage connection would register (SpatiaLite’s, GDAL’s) are absent here, so treat a complaint as advisory rather than proof the filter is unusable.
- stratified_packager.toolbelt.sql._EQUALITY_OPERAND: Final = re.compile('(?:\\w+\\s*\\.\\s*)?(\\w+)\\s*=\\s*(?:\\w+\\s*\\.\\s*)?(\\w+)')
Both operands of an
=comparison, with anytable./alias.qualifier dropped.A word character never precedes the
=of<=,>=,!=or<>, so none of those operators can match.
- stratified_packager.toolbelt.sql._RESERVED_TABLE_PREFIXES: Final = ('gpkg', 'sqlite_')
Reserved table-name prefixes: OGR rejects
gpkg; SQLite reservessqlite_.
- stratified_packager.toolbelt.sql._SOURCE_TABLE: Final = re.compile('\\b(?:from|join)\\s+ # the keyword introducing a table reference\n (?:"([^"]+)" # "quoted"\n |`([^`]+)` # `backquoted`\n, re.IGNORECASE|re.VERBOSE)
The table name following each
FROM/JOIN, in whichever way it is spelled.