Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Fixes for Python DB API
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed May 30, 2024
1 parent b89ccee commit 026ef9d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
18 changes: 9 additions & 9 deletions python/dbapi/api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 3 additions & 3 deletions python/dbapi/api/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)


Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions python/dbapi/api/rawValidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions python/dbapi/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions python/restapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 026ef9d

Please sign in to comment.