Skip to content
Snippets Groups Projects

Resolve "Implement server-side cursors"

Merged Laurens Oostwegel requested to merge 9-implement-server-side-cursors into main
+ 20
12
@@ -21,6 +21,7 @@ import logging
import sqlite3
import psycopg2
from packaging import version
from uuid import uuid4
logger = logging.getLogger()
@@ -213,8 +214,15 @@ class PostGISDatabase(AbstractDatabase):
self.username = username
self.password = password
self.timeout = timeout
self.connection_string = (
f"host={self.host} "
f"dbname={self.dbname} "
f"user={self.username} "
f"password={self.password} "
f"port={self.port}"
)
def connect(self, timeout: int = None):
def connect(self, timeout: int = None, server_side_cursor: bool = False):
"""
Creates a database connection and a cursor with psycopg2 with the
given credentials and include the connection and cursor as new attributes.
@@ -222,26 +230,26 @@ class PostGISDatabase(AbstractDatabase):
Args:
timeout (int, optional):
Timeout value for SQL queries in milliseconds.
server_side_cursor (bool, optional):
If `True`, the queries are run on the server side. This is needed if big queries
lead to memory issues on the client side.
"""
connection_string = (
f"host={self.host} "
f"dbname={self.dbname} "
f"user={self.username} "
f"password={self.password} "
f"port={self.port}"
)
try:
if timeout:
self.connection = psycopg2.connect(
connection_string, options=f"-c statement_timeout={timeout * 1000}"
self.connection_string, options=f"-c statement_timeout={timeout * 1000}"
)
elif self.timeout:
self.connection = psycopg2.connect(
connection_string, options=f"-c statement_timeout={self.timeout * 1000}"
self.connection_string, options=f"-c statement_timeout={self.timeout * 1000}"
)
else:
self.connection = psycopg2.connect(connection_string)
self.cursor = self.connection.cursor()
self.connection = psycopg2.connect(self.connection_string)
if server_side_cursor:
self.cursor = self.connection.cursor(name=f"{uuid4()}")
else:
self.cursor = self.connection.cursor()
except Exception as e:
logger.exception(e)
Loading