From 026ef9d5dd91d596260ae8830f4d76877eae6702 Mon Sep 17 00:00:00 2001 From: Emilio Mariscal Date: Thu, 30 May 2024 17:14:25 -0300 Subject: [PATCH] Fixes for Python DB API --- python/dbapi/api/db.py | 18 +++++++++--------- python/dbapi/api/raw.py | 6 +++--- python/dbapi/api/rawValidation.py | 9 +++++---- python/dbapi/api/stats.py | 2 ++ python/restapi/models.py | 4 ++-- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/python/dbapi/api/db.py b/python/dbapi/api/db.py index 1738c41e..6c1687ae 100644 --- a/python/dbapi/api/db.py +++ b/python/dbapi/api/db.py @@ -58,21 +58,21 @@ async def run(self, query, singleObject = False, asJson=False): if not self.pool: await self.connect() if self.pool: + result = None try: conn = await self.pool.acquire() result = await conn.fetch(query) + data = None if asJson: - if singleObject: - return result[0]['result'] - return result[0]['result'] + data = result[0]['result'] + elif singleObject: + data = result[0] else: - if singleObject: - return result[0] - return result + data = result + await self.pool.release(conn) + return data except Exception as e: print("\n******* \n" + query + "\n******* \n") print(e) return None - finally: - await self.pool.release(conn) - return None + diff --git a/python/dbapi/api/raw.py b/python/dbapi/api/raw.py index e8500434..ca4c2f9c 100644 --- a/python/dbapi/api/raw.py +++ b/python/dbapi/api/raw.py @@ -238,7 +238,7 @@ async def getPolygons( params.table = Table.polygons result = await self.db.run(geoFeaturesQuery(params, asJson), asJson=asJson) if asJson: - return result + return result or {} return deserializeTags(result) # Get line features @@ -250,7 +250,7 @@ async def getLines( params.table = Table.lines result = await self.db.run(geoFeaturesQuery(params, asJson), asJson=asJson) if asJson: - return result + return result or {} return deserializeTags(result) @@ -263,7 +263,7 @@ async def getNodes( params.table = Table.nodes result = await self.db.run(geoFeaturesQuery(params, asJson), asJson=asJson) if asJson: - return result + return result or {} return deserializeTags(result) # Get all (polygon, line, node) features diff --git a/python/dbapi/api/rawValidation.py b/python/dbapi/api/rawValidation.py index eec66a6e..924f5a95 100644 --- a/python/dbapi/api/rawValidation.py +++ b/python/dbapi/api/rawValidation.py @@ -30,7 +30,7 @@ from .filters import tagsQueryFilter, hashtagQueryFilter from .serialization import queryToJSON from .config import RESULTS_PER_PAGE, RESULTS_PER_PAGE_LIST, DEBUG -from .raw import RawFeaturesParamsDTO, ListFeaturesParamsDTO, rawQueryToJSON, listQueryToJSON +from .raw import RawFeaturesParamsDTO, ListFeaturesParamsDTO, rawQueryToJSON, listQueryToJSON, OrderBy from .serialization import deserializeTags import json @@ -148,6 +148,7 @@ def listFeaturesQuery( geoType:GeoType = GeoType[params.table] osmType:OsmType = OsmType[params.table] table:Table = Table[params.table] + orderBy:OrderBy = OrderBy[params.orderBy] query = "( \ SELECT '{type}' as type, \n \ @@ -178,12 +179,12 @@ def listFeaturesQuery( ) if params.area else "", tags=" AND (" + tagsQueryFilter(params.tags, table.value) + ")" if params.tags else "", status=" AND status = '{status}'".format(status=params.status.value) if (params.status) else "", - order=" AND {order} IS NOT NULL ORDER BY {order} DESC LIMIT {limit} OFFSET {offset}" + order=" ORDER BY {order} DESC LIMIT {limit} OFFSET {offset}" .format( - order=params.orderBy.value, + order=orderBy.value, limit=RESULTS_PER_PAGE_LIST, offset=params.page * RESULTS_PER_PAGE_LIST - ) if params.page else "" + ) ).replace("WHERE AND", "WHERE") if asJson: return listQueryToJSON(query, params) diff --git a/python/dbapi/api/stats.py b/python/dbapi/api/stats.py index 9495a091..864fb759 100644 --- a/python/dbapi/api/stats.py +++ b/python/dbapi/api/stats.py @@ -65,6 +65,7 @@ async def getNodesCount( result = await self.db.run(featureCountQuery(params), singleObject=True) if asJson: return json.dumps(dict(result)) + return result async def getLinesCount( self, @@ -75,6 +76,7 @@ async def getLinesCount( result = await self.db.run(featureCountQuery(params), singleObject=True) if asJson: return json.dumps(dict(result)) + return result async def getPolygonsCount( self, diff --git a/python/restapi/models.py b/python/restapi/models.py index 32bc6aeb..e79def91 100644 --- a/python/restapi/models.py +++ b/python/restapi/models.py @@ -32,14 +32,14 @@ class BaseRequest(BaseModel): featureType: str = None class BaseListRequest(BaseRequest): - orderBy: str = None + orderBy: str = "id" page: int = None class BaseRawValidationRequest(BaseRequest): status: str = None class RawValidationListRequest(BaseRawValidationRequest): - orderBy: str = None + orderBy: str = "id" page: int = None class RawRequest(BaseRequest):