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

Order By named parameter #805

Closed
engilyin opened this issue Dec 11, 2022 · 4 comments
Closed

Order By named parameter #805

engilyin opened this issue Dec 11, 2022 · 4 comments
Labels
status: invalid An issue that we don't feel is valid status: waiting-for-triage An issue we've not yet triaged

Comments

@engilyin
Copy link

I can't set the query order using the named parameter like

@Query("""
    SELECT * 
    FROM mytable
    ORDER BY :orderParam
    OFFSET :offset LIMIT :limit
""")
public Flux<MyObject> findObjects(@Param("orderParam") String orderParam, 
            @Param("offset") long offset,
            @Param("limit") long limit);

If I call:

var objects = repo.findObjects("fieldname DESC", 0, 100);

It works but using default order.

That happens because this parameter is wrapped with single quotas as a SQL string literal:

    SELECT * 
    FROM mytable
    ORDER BY 'fieldname DESC'
    OFFSET 0 LIMIT 100

Also you can find similar request at:
https://stackoverflow.com/questions/74111689/r2dbc-querying-from-multiple-tables-with-sorting-and-pagination/74756531#74756531

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 11, 2022
@mp911de
Copy link
Member

mp911de commented Dec 12, 2022

Binding parameters only work well within SELECT, WHERE and HAVING. ORDER BY cannot be parametrized.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2022
@mp911de mp911de added the status: invalid An issue that we don't feel is valid label Dec 12, 2022
@tiagodevss
Copy link

Hi! Would you have any alternative?

@mp911de
Copy link
Member

mp911de commented Feb 22, 2024

The alternative is to create a custom repository fragment and running your query via DatabaseClient and mapping rows through R2dbcConverter. Alternatively, you can wrap your query into PreparedOperation and run it via R2dbcEntityTemplate.

@NursultanMuss
Copy link

NursultanMuss commented Aug 18, 2024

Hi. Can you expose some examples of usage this advice?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

5 participants