aboutsummaryrefslogtreecommitdiffstats
path: root/common.py
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem@fripost.org>2026-03-05 16:00:08 +0100
committerGuilhem Moulin <guilhem@fripost.org>2026-03-05 22:56:29 +0100
commit4bb4d381f193f14260fc9f56679588d8e455dc93 (patch)
tree5e695654382cda35e3f7bc4786f209ece80e72fb /common.py
parent05ac5b52a224d6eddb23d748a9d8e3ee5571341b (diff)
Use PostgreSQL schemas to partition layers.
Rather than putting everything in the 'postgis' schema using ':' as hierarchy separator. When the destination dataset is not PostgreSQL, the layers names are prefixed using '$SCHEMA.' instead of '$SCHEMA:'
Diffstat (limited to 'common.py')
-rw-r--r--common.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/common.py b/common.py
index cbdc43c..040b90f 100644
--- a/common.py
+++ b/common.py
@@ -259,6 +259,26 @@ def escape_literal_str(literal : str) -> str:
# SQL:1999 character string literal
return '\'' + literal.replace('\'', '\'\'') + '\''
+# pylint: disable-next=invalid-name
+def getEscapedTableNamePG(layername : str, extract_schema_from_layer_name : bool = True) -> str:
+ """Return the layer name as an escaped identifier, suitable for injection into SQL queries.
+ The optional boolean (default: True) indicates whether the first dot character is used as
+ the separator between the schema and the table name, like the EXTRACT_SCHEMA_FROM_LAYER_NAME
+ layer creation option, see
+ https://gdal.org/en/stable/drivers/vector/pg.html#layer-creation-options"""
+ if extract_schema_from_layer_name:
+ (schema_name, table_name) = getQualifiedLayerName(layername)
+ if schema_name is not None:
+ return escape_identifier(schema_name) + '.' + escape_identifier(table_name)
+ return escape_identifier(layername)
+
+# pylint: disable-next=invalid-name
+def getQualifiedLayerName(layername : str) -> tuple[str|None,str]:
+ """Return the qualified pair (schema_name, table_name) of a PostGIS layer name."""
+ if '.' in layername:
+ return tuple(layername.split('.', 1))
+ return (None, layername)
+
######
# The function definitions below are taken from cpython's source code