RSQL-QueryDSL
Author : John Michael Vincent S. Rustia
This library brings the convenience of SQL declarative nature to restful APIs in the form of RSQL but without the danger of sql injection by using a typesafe mapping of allowed field paths defined via integration with querydsl library. Like sql, it supports clauses such as select, filter, pagination and sorting that can easily be represented in http request parameters.
It primarily supports JPA model and MongoDB query via Querydsl. This is a small project but at its heart is dedicated to maintain highly cohesive and modular components. Contributions and suggestions are very much welcome and appreciated!
QueryDSL API Modules
Rql specification used by this library is defined by this reference and rsql-parser.
Please see for more information about rql expressions.
-
select - converts rql select expression into its querydsl projection equivalent
-
filter - uses rsql-parser library ast to convert filter string into its querydsl predicate equivalent
-
page - converts rql limit expression into its querydsl pagination equivalent
-
sort - converts rql sort expression into its querydsl sorting equivalent
-
spring - provides integration with spring data such as pageable
2.0.0.RELEASE Notes
#13 Ported rsql-querydsl v1.0.0.RELEASE code to v2.0.0 branch to be compatible with querydsl latest version v4.x.x.
1.0.0.RELEASE and 2.0.0 will be simultaneous releases.
Contributions:
-
Thanks @natros for pointing me on this direction, hence a simultaneous 2.0.0.RELEASE
-
Thanks Wallace Wadge for your effort in delivering this task, you are awesome!
1.0.0.RELEASE Notes
Completes filter conversion to querydsl predicate. Completes sort and page conversion for rsql querydsl
Completes select conversion to querydsl projections.
Full support on Mongodb projection, filter, sort and page conversion.
NEXT MILESTONES
2.1.0 and 1.1.0 Milestones
-
Add support for embeddable or nested Querydsl Paths
-
Add support for JPA Association Paths (OneToMany, ManyToOne)
Rsql-JOOQ Milestones
-
Support for rsql conversion to JOOQ Library
MAVEN
Bundled Rsql Querydsl Modules
You can get all rsql-querydsl modules via this dependency,
For Querydsl 3.x.x, use 1.0.0.RELEASE
For Querydsl 4.x.x, use 2.0.0.RELEASE
<dependency>
<groupId>com.github.vineey</groupId>
<artifactId>rsql-querydsl-all</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
dependencies {
compile 'com.github.vineey:rsql-querydsl-all:2.0.0.RELEASE'
}
Selective Rsql Querydsl Modules
or you can specify which module you only need by changing the artifactId by any of the ff:
-
rsql-querydsl-select
-
rsql-querydsl-filter
-
rsql-querydsl-sort
-
rsql-querydsl-page
such as
dependencies {
compile 'com.github.vineey:rsql-querydsl-filter:2.0.0.RELEASE'
}
To integrate with Spring Data Pageable, include this dependency,
dependencies {
compile 'com.github.vineey:rsql-querydsl-spring:2.0.0.RELEASE'
}
Minimum JDK Required
-
Java 8 (will try to support Java 7 in the next release by externalizing Java 8 DateTime converters)
Querydsl Integration Dependencies Required
-
com.mysema.querydsl:querydsl-core:3.x.x or prior for rsql-querydsl 1.x.x.RELEASE
-
com.querydsl:querydsl-core:4.x.x or latest for rsql-querydsl 2.x.x.RELEASE
Spring Integration Dependencies Required
-
org.springframework.data:spring-data-commons:1.x.x or latest
API DOCS
Querydsl Filter (rsql-querydsl-filter)
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojo and other kinds of nested querydsl path.
Sample operators
equals is == e.g. field == 'value' not equals is != e.g. field != 'value' like is =* startsWith e.g. field == 'value*' endsWith e.g. field == '*value' contains e.g. field == '*value*'
DefaultFilterParser filterParser = new DefaultFilterParser();
String rqlFilter = "employee.name == 'John'";
Map<String, Path> pathHashMap = ImmutableMap.<String, Path>builder()
.put("employee.name", QEmployee.employee.name)
.put("employee.age", QEmployee.employee.age)
.put("employee.bday", QEmployee.employee.birthDate)
.build();
Predicate predicate = filterParser.parse(rsqlFilter, withBuilderAndParam(new QuerydslFilterBuilder(), new QuerydslFilterParam()
.setMapping(pathHashMap)));
//or a shorter version
Predicate predicate = filterParser.parse(rsqlFilter, withMapping(pathHashMap));
Querydsl Select Conversion (rsql-querydsl-select)
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojo and other kinds of nested querydsl path.
//e.g. select(field1, field2,...)
String rqlSelectExpression = "select(contact.company, contact.name, contact.age)";
DefaultSelectParser selectParser = new DefaultSelectParser();
Map<String, Path> mappings = ImmutableMap.<String, Path>builder()
.put("contact.age", QContactDocument.contactDocument.age)
.put("contact.name", QContactDocument.contactDocument.name)
.put("contact.bday", QContactDocument.contactDocument.bday)
.put("contact.company", QContactDocument.contactDocument.company)
.build();
Expression projection = selectParser.parse(rqlSelectExpression, QuerydslSelectContext.withMapping(QContactDocument.contactDocument, mappings));
Querydsl Sort Conversion (rsql-querydsl-sort)
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojo and other kinds of nested querydsl path.
//ascending is +, descending is -
//e.g. sort(+field1, -field2,...)
String sortExpression = "sort(+employeeNumber)";
DefaultSortParser sortParser = new DefaultSortParser();
Map<String, Path> mappings = ImmutableMap.<String, Path>builder()
.put("employeeNumber", QEmployee.employee.employeeNumber)
.build();
OrderSpecifierList orderSpecifierList = sortParser.parse(sortExpression, QuerydslSortContext.withMapping(mappings));
List<OrderSpecifier> orderSpecifiers = orderSpecifierList.getOrders();
Querydsl Page Conversion (rsql-querydsl-page)
//limit(<offset>, <size>)
String rqlPage = "limit(10, 5)";
DefaultPageParser defaultPageParser = new DefaultPageParser();
QueryModifiers querydslPage = defaultPageParser.parse(rqlPage, withDefault());
or a simplified version
QuerydslPageParser querydslPageParser = new QuerydslPageParser();
QueryModifiers querydslPage = querydslPageParser.parse(rqlPage);
Bundled All Querydsl Modules (rsql-querydsl-all)
String rqlSelect = "select(contact.name, contact.age)";
String rqlFilter = "(contact.age =='1' and contact.name == 'A*') or (contact.age > '1' and contact.bday == '2015-05-05')";
String limit = "limit(0, 10)";
String sort = "sort(+contact.name)";
RqlInput rqlInput = new RqlInput()
.setSelect(rqlSelect)
.setFilter(rqlFilter)
.setLimit(limit)
.setSort(sort);
Map<String , Path> pathMapping = ImmutableMap.<String, Path>builder()
.put("contact.name", QContactDocument.contactDocument.name)
.put("contact.age", QContactDocument.contactDocument.age)
.put("contact.bday", QContactDocument.contactDocument.bday)
.build();
QuerydslMappingResult querydslMappingResult = querydslRqlParser.parse(rqlInput, new QuerydslMappingParam().setRootPath(QContactDocument.contactDocument).setPathMapping(pathMapping));
Expression selectExpression = querydslMappingResult.getProjection();
Predicate predicate = querydslMappingResult.getPredicate();
QueryModifiers querydslPage = querydslMappingResult.getPage();
List<OrderSpecifier> orderSpecifiers = querydslMappingResult.getOrderSpecifiers();
Integration of Querydsl to Spring Data Pageable
Pageable pageable = SpringUtil.toPageable(orderSpecifiers, querydslPage);
You can now use Expression, Predicate, QueryModifiers, OrderSpecifier or Pageable
in the Querydsl API, or in JPAQuery,
or in the Spring Data JPA/Mongo Repository.
A MORE APPROPRIATE WIKI
To be follow!!!