Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid fstring in queries #554

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

* Default branch to **main** ([#544](https://github.com/stac-utils/stac-fastapi/pull/544))

### Fixed

* Use `V()` instead of f-strings for pgstac queries ([#554](https://github.com/stac-utils/stac-fastapi/pull/554))

## [2.4.4] - 2023-03-09

### Added
Expand Down
16 changes: 9 additions & 7 deletions stac_fastapi/pgstac/stac_fastapi/pgstac/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import attr
import orjson
from asyncpg import exceptions, pool
from buildpg import asyncpg, render
from buildpg import V, asyncpg, render
from fastapi import FastAPI

from stac_fastapi.types.errors import (
Expand Down Expand Up @@ -66,18 +66,20 @@ async def dbfunc(pool: pool, func: str, arg: Union[str, Dict]):
if isinstance(arg, str):
async with pool.acquire() as conn:
q, p = render(
f"""
SELECT * FROM {func}(:item::text);
""",
"""
SELECT * FROM :func(:item::text);
""",
func=V(func),
item=arg,
)
return await conn.fetchval(q, *p)
else:
async with pool.acquire() as conn:
q, p = render(
f"""
SELECT * FROM {func}(:item::text::jsonb);
""",
"""
SELECT * FROM :func(:item::text::jsonb);
""",
func=V(func),
item=json.dumps(arg),
)
return await conn.fetchval(q, *p)
Expand Down