aboutsummaryrefslogtreecommitdiffstats
path: root/webmap-cgi
diff options
context:
space:
mode:
Diffstat (limited to 'webmap-cgi')
-rwxr-xr-xwebmap-cgi26
1 files changed, 10 insertions, 16 deletions
diff --git a/webmap-cgi b/webmap-cgi
index b5e5f98..96dba33 100755
--- a/webmap-cgi
+++ b/webmap-cgi
@@ -23,12 +23,12 @@
import sys
from os import path as os_path
-from json import load as json_load, dumps as json_dumps, JSONDecodeError
+from json import load as json_load, JSONDecodeError
import logging
from typing import Final, Iterator
import atexit
-from psycopg import connect, Cursor # pylint: disable=import-error
+from psycopg import connect, RawCursor # pylint: disable=import-error
import common
@@ -62,7 +62,7 @@ def get_query_map(layernames : set[str]) -> dict[str,bytes]:
for layername in layernames:
cur.execute('SELECT f_geometry_column, coord_dimension, srid, type '
'FROM ' + common.escape_identifier(SCHEMA_NAME) + '.geometry_columns '
- 'WHERE f_table_schema = %s AND f_table_name = %s',
+ 'WHERE f_table_schema = $1 AND f_table_name = $2',
params=(SCHEMA_NAME, layername),
prepare=False)
resp = cur.fetchone()
@@ -107,7 +107,7 @@ def get_query_map(layernames : set[str]) -> dict[str,bytes]:
'FROM pg_index i '
'JOIN pg_attribute a '
' ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) '
- 'WHERE i.indrelid = %s::regclass AND i.indisprimary',
+ 'WHERE i.indrelid = $1::regclass AND i.indisprimary',
params=(common.escape_identifier(SCHEMA_NAME) + '.' +
common.escape_identifier(layername),),
prepare=False)
@@ -128,7 +128,7 @@ def get_query_map(layernames : set[str]) -> dict[str,bytes]:
column_names = []
cur.execute('SELECT column_name FROM information_schema.columns '
- 'WHERE table_schema = %s AND table_name = %s',
+ 'WHERE table_schema = $1 AND table_name = $2',
params=(SCHEMA_NAME, layername),
prepare=False)
# never empty since the we know the table exists and has a primary key
@@ -158,15 +158,12 @@ def get_query_map(layernames : set[str]) -> dict[str,bytes]:
query += 'ST_Perimeter(' + geom_col2d_esc +') AS geom_perimeter,'
elif d == 1:
query += 'ST_Length(' + geom_col2d_esc +') AS geom_length,'
- query += '%s AS layer_group,%s AS layer '
+ query += '$1 AS layer_group,$2 AS layer '
query += 'FROM ' + common.escape_identifier(SCHEMA_NAME) + '.'
query += common.escape_identifier(layername) + ' m '
- query += 'WHERE m.' + common.escape_identifier(pkey_col) + ' = %s'
+ query += 'WHERE m.' + common.escape_identifier(pkey_col) + ' = $3'
query += ') '
- # TODO[trixie] use json_serialize() from PostgreSQL 17 to avoid serializing on
- # the Python side. (There is also row_to_json() which might be of interest if
- # json not jsonb is needed.)
- query += 'SELECT to_jsonb(feature) FROM feature'
+ query += 'SELECT json_serialize(to_json(feature) RETURNING bytea) FROM feature'
# The query never returns more than one row since we filter on a single FID.
# TODO: batch queries using ANY[] or an IN set (the # consummer will then need
# to re-order the response)
@@ -237,7 +234,7 @@ def application(env, start_response) -> Iterator[bytes]:
first = False
else:
yield b','
- yield json_dumps(resp[0], ensure_ascii=False, separators=(',', ':')).encode('utf-8')
+ yield resp[0]
# the query never returns more than one row since we filter on a single FID
if first:
yield b'[]' # no match, empty response
@@ -272,16 +269,13 @@ def application(env, start_response) -> Iterator[bytes]:
PG_CONN = connect('postgresql://webmap_guest@/webmap',
autocommit=True,
prepare_threshold=0,
- # TODO[trixie] use cursor_factory=RawCursor
- # https://www.psycopg.org/psycopg3/docs/advanced/cursors.html#cursor-types
- cursor_factory=Cursor)
+ cursor_factory=RawCursor)
@atexit.register
def handler():
"""Gracefully close the connection before terminating the worker"""
# avoid "AttributeError: 'NoneType' object has no attribute 'connection_summary'"
# when destructing the object
- # TODO[trixie] reevaluate, possibly related to https://github.com/psycopg/psycopg/issues/591
PG_CONN.close() # pylint: disable=no-member
common.init_logger(app=os_path.basename(__file__), level=logging.INFO)