fastapi-filters icon indicating copy to clipboard operation
fastapi-filters copied to clipboard

Support for Postgres prepare statements raw SQL

Open espdev opened this issue 7 months ago • 1 comments

Hello!

I see that you have support sqlalchemy and tortoise. How hard is it to do support raw SQL query for Postgres/asyncpg prepare statements? I mean, we are using plain SQL queries with asyncpg and it would be nice to get the WHERE part of the query from the filters object, perhaps with query arguments. Which could then be merged with the rest of the SQL query and pass the arguments.

I'm just fantasizing, but maybe something like this:

where, args = apply_filters(filters, arg_start=2)

In this case where may be, for example:

'WHERE a > $2 AND b = $3'

and args is a tuple of arguments: (3, 'hello').

Eventually:

where, args = apply_filters(filters)
n = len(args) + 1
query = f'SELECT * FROM foo {where} LIMIT ${n}'
res = await conn.fetch(query, *args, 5)

espdev avatar May 27 '25 17:05 espdev

Hmmm, it's an interesting idea, I guess it's possible even to use sqlalchemy to generate where clause. I will take a look.

uriyyo avatar May 27 '25 20:05 uriyyo