- Notifications
You must be signed in to change notification settings - Fork9
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 …
License
vineey/archelix-rsql
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Author : John Michael Vincent S. Rustia
This library brings the convenience of SQL declarative nature to restful APIs in the form of RSQLbut without the danger of sql injection by using a typesafe mapping of allowed field paths definedvia integration with querydsl library. Like sql, it supports clauses such as select, filter, paginationand 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 heartis dedicated to maintain highly cohesive and modular components. Contributions and suggestions are verymuch welcome and appreciated!
Rql specification used by this library is defined by thisreference andrsql-parser.
Please see for more information about rql expressions.
select - converts rql select expression into its querydsl projection equivalent
filter - usesrsql-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
#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
ThanksWallace Wadge for your effort in delivering this task, you are awesome!
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.
Add support for embeddable or nested Querydsl Paths
Add support for JPA Association Paths (OneToMany, ManyToOne)
Support for rsql conversion toJOOQ Library
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' }
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' }
Java 8 (will try to support Java 7 in the next release by externalizing Java 8 DateTime converters)
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
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojoand 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*'
DefaultFilterParserfilterParser =newDefaultFilterParser();StringrqlFilter ="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();Predicatepredicate =filterParser.parse(rsqlFilter,withBuilderAndParam(newQuerydslFilterBuilder(),newQuerydslFilterParam() .setMapping(pathHashMap)));//or a shorter versionPredicatepredicate =filterParser.parse(rsqlFilter,withMapping(pathHashMap));
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojoand other kinds of nested querydsl path.
//e.g. select(field1, field2,...)StringrqlSelectExpression ="select(contact.company, contact.name, contact.age)";DefaultSelectParserselectParser =newDefaultSelectParser();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();Expressionprojection =selectParser.parse(rqlSelectExpression,QuerydslSelectContext.withMapping(QContactDocument.contactDocument,mappings));
Note: AOTM, this only supports first level fields of the entity or document. But will support embeddable pojoand other kinds of nested querydsl path.
//ascending is +, descending is -//e.g. sort(+field1, -field2,...)StringsortExpression ="sort(+employeeNumber)";DefaultSortParsersortParser =newDefaultSortParser();Map<String,Path>mappings =ImmutableMap.<String,Path>builder() .put("employeeNumber",QEmployee.employee.employeeNumber) .build();OrderSpecifierListorderSpecifierList =sortParser.parse(sortExpression,QuerydslSortContext.withMapping(mappings));List<OrderSpecifier>orderSpecifiers =orderSpecifierList.getOrders();
//limit(<offset>, <size>)StringrqlPage ="limit(10, 5)";DefaultPageParserdefaultPageParser =newDefaultPageParser();QueryModifiersquerydslPage =defaultPageParser.parse(rqlPage,withDefault());orasimplifiedversionQuerydslPageParserquerydslPageParser =newQuerydslPageParser();QueryModifiersquerydslPage =querydslPageParser.parse(rqlPage);
StringrqlSelect ="select(contact.name, contact.age)";StringrqlFilter ="(contact.age =='1' and contact.name == 'A*') or (contact.age > '1' and contact.bday == '2015-05-05')";Stringlimit ="limit(0, 10)";Stringsort ="sort(+contact.name)";RqlInputrqlInput =newRqlInput() .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();QuerydslMappingResultquerydslMappingResult =querydslRqlParser.parse(rqlInput,newQuerydslMappingParam().setRootPath(QContactDocument.contactDocument).setPathMapping(pathMapping));ExpressionselectExpression =querydslMappingResult.getProjection();Predicatepredicate =querydslMappingResult.getPredicate();QueryModifiersquerydslPage =querydslMappingResult.getPage();List<OrderSpecifier>orderSpecifiers =querydslMappingResult.getOrderSpecifiers();
Pageablepageable =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.
About
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 …