简体   繁体   English

从HTTP GET请求参数生成搜索SQL

[英]Generate Search SQL from HTTP GET request parameters

We have a Java web app with a hibernate backend that provides REST resources. 我们有一个带有hibernate后端的Java Web应用程序,它提供REST资源。 Now we're facing the task to implement a generic search that is controlled by the query parameters in our get request: 现在,我们面临的任务是实现由get请求中的查询参数控制的通用搜索:

some/rest/resource?name_like=foo&created_on>=2012-09-12&sort_by_asc=something

or similar. 或类似的。

  • We don't want to predefine all possible parameters(name, created_on, something) 我们不想预定义所有可能的参数(name,created_on,something)
  • We don't want to have to analyze the request String to pick up control characters (like >=) 我们不想分析请求String来获取控制字符(比如> =)
  • nor do we don't want to implement our own grammar to reflect things like _eq _like _goe and so on (as an alternative or addition to control characters) 我们也不想实现自己的语法来反映_eq _like _goe等等(作为控制字符的替代或补充)

Is there some kind of framework that provides help with this mapping from GET request parameters to database query ? 是否有某种框架可以提供从GET请求参数到数据库查询的映射的帮助?

Since we know which REST resource we're GETing we have the entity / table (select). 由于我们知道我们正在获取哪个REST资源,因此我们有实体/表(select)。 It probably will also be necessary to predefine the JOINs that will be executed in order to limit the depths of a search. 可能还需要预先定义将要执行的JOIN以限制搜索的深度。

But other than that we want the REST consuming client to be able to execute any search without us having to predefine how a certain parameter and a certain control sequence will get translated into a search. 但除此之外,我们希望REST消费客户端能够执行任何搜索,而无需预定义某个参数和某个控制序列将如何转换为搜索。


Right now I'm trying some semi automatic solution building on Mysemas QueryDSL. 现在我正在尝试在Mysemas QueryDSL上构建一些半自动解决方案。 It allows me to predefine the where columns and sort columns and I'm working on a simple string comparison to detect things like '_like', '_loe', ... in a parameter and then activate the corresponding predefined part of the search. 它允许我预定义列和排序列的位置,我正在进行简单的字符串比较,以检测参数中的'_like','_ loe',...等内容,然后激活相应的预定义搜索部分。 Not much different from an SQL String except that it's SQL injection proof an type save. 与SQL字符串没什么不同,只是它的SQL注入证明类型保存。

However I still have to tell my search object that it should be able to potentially handle a query "look for a person with name like '???'". 但是我仍然必须告诉我的搜索对象它应该能够处理查询“寻找名字像'???'的人”。 Right now this is okay as we only consume the REST resource internally and isolate the actual search creation quite well. 现在这没关系,因为我们只在内部使用REST资源并且很好地隔离了实际的搜索创建。 If we need to make a search do more we can just add more predefinitions for now. 如果我们需要进行更多搜索,我们现在可以添加更多预定义。 But should we make our REST resources public at some time in the future that won't be so great. 但是,我们是否应该在未来的某个时间公开我们的REST资源,这将不会那么好。


So we're wondering, there has to be some framework or best practice or recommended solution to approaching this. 所以我们想知道,必须有一些框架或最佳实践或推荐的解决方案来解决这个问题。 We're not the first who want this. 我们不是第一个想要这个的人。 Redmine for example offers all of its resource via a REST interface and I can query at will. 例如Redmine通过REST接口提供所有资源,我可以随意查询。 Or facebook with its Graph API. 或Facebook及其Graph API。 I'm sure those guys didn't just predefine all possibilities but rather created some generic grammar. 我敢肯定那些人不只是预定义所有可能性,而是创建了一些通用语法。 We'd like to save as much as possible on that effort and use available solutions instead. 我们希望尽可能多地节省这些努力并使用可用的解决方案。

Like I said, we're using Hibernate so an SQL or HQL solution would be fine or anything that builds on entities like QueryDsl. 就像我说的那样,我们正在使用Hibernate,因此SQL或HQL解决方案可以很好或者像QueryDsl这样的实体构建。 (Also there's the security issue concerning SQL injection) (还有关于SQL注入的安全问题)

Any suggestions? 有什么建议? Ideas? 想法? Will we just have to do it all ourselves? 我们自己必须自己做吗?

From a .NET perspective the closest thing I can think of would be a WCF data service. 从.NET的角度来看,我能想到的最接近的是WCF数据服务。

Take a look at the uri-conventions specified on the OData website. 看一下OData网站上指定的uri约定 There is some good information on the section on 4.5 Filter System Query Option . 有关4.5过滤系统查询选项的部分有一些很好的信息。 You'll notice that a lot of the examples on this site are .NET related, but there are other suggestions for getting this to work with Java. 您会注意到此站点上的许多示例都与.NET相关,但还有其他 建议可以使其与Java一起使用。

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

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