Skip to content

Commit

Permalink
fix: Simplify format_sql
Browse files Browse the repository at this point in the history
Update the non-cloud postgres provider also to provide the format_sql method with a dictionary.
  • Loading branch information
anubhav756 committed Sep 20, 2024
1 parent 82ef478 commit eeead36
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 130 deletions.
16 changes: 4 additions & 12 deletions retrieval_service/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Union

import sqlparse


def format_sql(sql: str, params: Union[tuple, dict]):
def format_sql(sql: str, params: dict):
"""
Format Postgres SQL to human readable text by replacing placeholders.
Handles tuple-based ($1, $2, ...) and dict-based (:key) formats.
Handles dict-based (:key) formats.
"""
if isinstance(params, tuple):
for i, value in enumerate(params):
sql = sql.replace(f"${i+1}", f"{value}")
elif isinstance(params, dict):
for key, value in params.items():
sql = sql.replace(f":{key}", f"{value}")
else:
raise ValueError("params must be a tuple or dict")
for key, value in params.items():
sql = sql.replace(f":{key}", f"{value}")
# format the SQL
formatted_sql = (
sqlparse.format(
Expand Down
Loading

0 comments on commit eeead36

Please sign in to comment.