@@ -72,8 +72,11 @@ class DatabaseConnection(object):
|
||||
(defaults to UNIX socket if not provided)
|
||||
port – connection port number
|
||||
(defaults to 5432 if not provided)
|
||||
schema – schema to use as search path, normally
|
||||
data is in the public schema
|
||||
search_path – search path to be used (by order) , normally
|
||||
data is in the public schema, [public],
|
||||
or in a specific schema ["osm", "public"].
|
||||
Note: First we should have the schema
|
||||
being used and then public
|
||||
|
||||
:param table: table name containing the data. This variable is used to
|
||||
assemble column information
|
||||
@@ -87,18 +90,14 @@ class DatabaseConnection(object):
|
||||
self.context = context
|
||||
self.columns = None
|
||||
self.conn = None
|
||||
self.schema = None
|
||||
|
||||
def __enter__(self):
|
||||
try:
|
||||
self.schema = self.conn_dic.pop('schema', None)
|
||||
if self.schema == 'public' or self.schema is None:
|
||||
pass
|
||||
else:
|
||||
self.conn_dic["options"] = '-c search_path={}'.format(
|
||||
self.schema)
|
||||
LOGGER.debug('Using schema {} as search path'.format(
|
||||
self.schema))
|
||||
search_path = self.conn_dic.pop('search_path', ['public'])
|
||||
if search_path != ['public']:
|
||||
self.conn_dic["options"] = f'-c \
|
||||
search_path={",".join(search_path)}'
|
||||
LOGGER.debug(f'Using search path: {search_path} ')
|
||||
self.conn = psycopg2.connect(**self.conn_dic)
|
||||
|
||||
except psycopg2.OperationalError:
|
||||
@@ -149,6 +148,7 @@ class PostgreSQLProvider(BaseProvider):
|
||||
self.table = provider_def['table']
|
||||
self.id_field = provider_def['id_field']
|
||||
self.conn_dic = provider_def['data']
|
||||
self.geom = provider_def.get('geom_field', 'geom')
|
||||
|
||||
LOGGER.debug('Setting Postgresql properties:')
|
||||
LOGGER.debug('Connection String:{}'.format(
|
||||
@@ -188,7 +188,6 @@ class PostgreSQLProvider(BaseProvider):
|
||||
except Exception as err:
|
||||
LOGGER.error('Error executing sql_query: {}: {}'.format(
|
||||
sql_query.as_string(cursor)), err)
|
||||
LOGGER.error('Using public schema: {}'.format(db.schema))
|
||||
raise ProviderQueryError()
|
||||
|
||||
hits = cursor.fetchone()["hits"]
|
||||
@@ -202,7 +201,7 @@ class PostgreSQLProvider(BaseProvider):
|
||||
sql_query = SQL("DECLARE \"geo_cursor\" CURSOR FOR \
|
||||
SELECT {0},ST_AsGeoJSON({1}) FROM {2}").\
|
||||
format(db.columns,
|
||||
Identifier('geom'),
|
||||
Identifier(self.geom),
|
||||
Identifier(self.table))
|
||||
|
||||
LOGGER.debug('SQL Query: {}'.format(sql_query))
|
||||
@@ -216,7 +215,6 @@ class PostgreSQLProvider(BaseProvider):
|
||||
except Exception as err:
|
||||
LOGGER.error('Error executing sql_query: {}'.format(
|
||||
sql_query.as_string(cursor)))
|
||||
LOGGER.error('Using public schema: {}'.format(db.schema))
|
||||
LOGGER.error(err)
|
||||
raise ProviderQueryError()
|
||||
|
||||
@@ -249,7 +247,7 @@ class PostgreSQLProvider(BaseProvider):
|
||||
|
||||
sql_query = SQL("select {0},ST_AsGeoJSON({1}) \
|
||||
from {2} WHERE {3}=%s").format(db.columns,
|
||||
Identifier('geom'),
|
||||
Identifier(self.geom),
|
||||
Identifier(self.table),
|
||||
Identifier(self.id_field))
|
||||
|
||||
@@ -260,7 +258,6 @@ class PostgreSQLProvider(BaseProvider):
|
||||
except Exception as err:
|
||||
LOGGER.error('Error executing sql_query: {}'.format(
|
||||
sql_query.as_string(cursor)))
|
||||
LOGGER.error('Using public schema: {}'.format(db.schema))
|
||||
LOGGER.error(err)
|
||||
raise ProviderQueryError()
|
||||
|
||||
|
||||
Binary file not shown.
@@ -42,10 +42,12 @@ def config():
|
||||
'data': {'host': '127.0.0.1',
|
||||
'dbname': 'test',
|
||||
'user': 'postgres',
|
||||
'password': 'postgres'
|
||||
'password': 'postgres',
|
||||
'search_path': ['osm', 'public']
|
||||
},
|
||||
'id_field': 'osm_id',
|
||||
'table': 'hotosm_bdi_waterways'
|
||||
'table': 'hotosm_bdi_waterways',
|
||||
'geom_field': 'foo_geom'
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user