Skip to content

Commit

Permalink
Merge pull request #1 from benoitblanc/use-context-manager
Browse files Browse the repository at this point in the history
Use context manager
  • Loading branch information
benoitblanc authored Apr 18, 2024
2 parents 191c8b1 + accfc55 commit df3fadc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
12 changes: 6 additions & 6 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Flask==2.3.2
flask-restx==1.1.0
Flask-JWT-Extended==4.4.4
SQLAlchemy==1.4.48
psycopg2==2.9.6
Flask==3.0.0
flask-restx==1.3.0
Flask-JWT-Extended==4.6.0
SQLAlchemy==2.0.29
psycopg2==2.9.9
requests==2.31.0
qwc-services-core==1.3.19
qwc-services-core==1.3.26
20 changes: 11 additions & 9 deletions search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ def search(self, searchtext):
)
)
self.logger.debug(f"SQL Query : {sql}")
conn = self.db_engine.db_engine(document["db_url"]).connect()
results[key] = []
try:
result = conn.execute(sql)
for row in result:
row_result = self._feature_from_query(row, document["primary_key"])
results[key].append(row_result)
self.logger.debug(f"SQL Result for document {key} : {results[key]}")
except Exception as e:
self.logger.error(f"Error for document {key} on query {sql}: {e}")
with self.db_engine.db_engine(document["db_url"]).connect() as conn:
try:
result = conn.execute(sql)
for row in result:
row_result = self._feature_from_query(
row, document["primary_key"]
)
results[key].append(row_result)
self.logger.debug(f"SQL Result for document {key} : {results[key]}")
except Exception as e:
self.logger.error(f"Error for document {key} on query {sql}: {e}")
return results

def _feature_from_query(self, row, primary_key):
Expand Down

0 comments on commit df3fadc

Please sign in to comment.