简体   繁体   English

R2DBC 通过排序和分页从多个表中查询

[英]R2DBC Querying From Multiple Tables With Sorting and Pagination

I am trying to create endpoint using spring webflux.我正在尝试使用 spring webflux 创建端点。 The result of endpoint to query from several tables, and most of values in where clause are optional.从多个表中查询的端点结果,where 子句中的大多数值是可选的。 (example query is provided in the bottom). (示例查询在底部提供)。 Sorting column and direction is provided by user, the same with pagination.排序列和方向由用户提供,与分页相同。
I tried to use native query along with Sort,Pageable.我尝试将本机查询与 Sort、Pageable 一起使用。 However, Sort and Pageable (org.springframework.data.domain) got ignored.但是,Sort 和 Pageable (org.springframework.data.domain) 被忽略了。 Later used fully native query without Sort, Pageable, however couldn't inject ASC and DESC by parameter to query.后来使用了没有Sort,Pageable的完全原生查询,但是无法通过参数注入ASC和DESC来查询。 Is there any other options I can do?我还有其他选择吗?

SELECT u.id, u.catalog_id, u.amount , u.currency , u.created_date , cc.customer_id , c.country
FROM user u join catalog c ON u.catalog_id = c.id 
JOIN catalog_contract cc ON cc.id  = c.catalog_contract_id 
where 
(:user_country_id is null OR u.user_country_id = :user_country_id)
AND
(:customer_id is null or cc.customer_id = :customer_id)
ORDER BY :column :direction
OFFSET :offset LIMIT :limit
  • spring-data-r2dbc: 1.3 (Pageable is supposed to work from 1.2) spring-data-r2dbc:1.3(Pageable 应该从 1.2 开始工作)

  • spring-data-webflux: 2.5.6弹簧数据-webflux:2.5.6

Since you are using parameter binding and your parameters are obviously strings it would be treated as strings and wrapped with single quotas.由于您正在使用参数绑定并且您的参数显然是字符串,因此它将被视为字符串并用单个配额包装。 So you get所以你得到

ORDER BY 'your-column' 'ASC'

instead of代替

ORDER BY your-column ASC

As a work around you can build SQL manually concatenating the Order parameters instead of binding.作为解决方法,您可以构建 SQL 手动连接订单参数而不是绑定。

Also you can check the appropriated issue:https://github.com/spring-projects/spring-data-r2dbc/issues/805你也可以检查适当的问题:https://github.com/spring-projects/spring-data-r2dbc/issues/805

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM