简体   繁体   English

自定义SQL查询以检查GET-Request中的列

[英]Customize a SQL-query to check column from GET-Request

I have a table with 5 columns: 我有一个包含5列的表格:

Table

id----A-----B------C-----D-----E------F
1-----32----789----65----21----56-----25
2-----34----54-----45----90----34-----96
3-----10----56-----78----56----90-----45

I got a query with the 5 columns and I have to search in the database. 我有5列的查询,我必须在数据库中搜索。 But I don't know which column the user could want 但是我不知道用户可能想要哪一列

public Response getColum(
            @QueryParam("a") short a, @QueryParam("b") short b,
            @QueryParam("c") short c,
            @QueryParam("d") short d,
            @QueryParam("e") short e) {
...

in my SQL-Query: 在我的SQL查询中:

SELECT * FROM Table where a = ? AND b = ?

One alternativ is to check the different case, but too expensive. 一种替代方法是检查不同的情况,但是太昂贵了。

How can I customize my query when I can not know which column the user could request? 当我不知道用户可以请求哪一列时,如何自定义查询? I don't want to build a IF-Statements, there are too many different cases. 我不想建立IF语句,有太多不同的情况。

Any ideas? 有任何想法吗?

One approach would be instead of putting all QueryParams in the method parameters and not know which one has received values, you could just inject UriInfo object using @Context annotation and then you can have: 一种方法是将所有QueryParams放在方法参数中,而不知道哪一个接收了值,您可以使用@Context注释注入UriInfo对象,然后可以:

MultiValuedMap<String,String> params = uriInfo.getQueryParameters()

which will contain only the parameters send. 其中仅包含send参数。 then you can iterate through this map, and just dynamically create WHERE criteria which is added using a StringBuilder to the basic statement "SELECT * FROM TABLE", due to the fact that the key is the filter name (aka column name) and the value is the filter value. 那么您可以遍历此映射,并动态创建WHERE条件,该条件使用StringBuilder添加到基本语句“ SELECT * FROM TABLE”,这是因为键是过滤器名称(又名列名称)和值是过滤器值。

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

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