-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented querydsl module for rql select api
- Loading branch information
Showing
10 changed files
with
323 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...sql-api-select/src/main/java/com/github/vineey/rql/select/parser/DefaultSelectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.select.parser; | ||
|
||
import com.github.vineey.rql.select.SelectContext; | ||
import com.github.vineey.rql.select.SelectParam; | ||
import com.github.vineey.rql.select.parser.ast.SelectTokenParserAdapter; | ||
|
||
/** | ||
* @author vrustia - 5/9/16. | ||
*/ | ||
public class DefaultSelectParser implements SelectParser { | ||
@Override | ||
public <T, E extends SelectParam> T parse(String selectExpression, SelectContext<T, E> selectContext) { | ||
E selectParam = selectContext.getSelectParam(); | ||
return selectContext.getSelectBuilder().visit(new SelectTokenParserAdapter().parse(selectExpression), selectParam); | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...arent/rsql-api-select/src/main/java/com/github/vineey/rql/select/parser/SelectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.select.parser; | ||
|
||
import com.github.vineey.rql.select.SelectContext; | ||
import com.github.vineey.rql.select.SelectParam; | ||
|
||
/** | ||
* @author vrustia - 5/9/16. | ||
*/ | ||
public interface SelectParser { | ||
|
||
<T, E extends SelectParam> T parse(String selectExpression, SelectContext<T, E> sortContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...dsl-select/src/main/java/com/github/vineey/rql/querydsl/select/QuerydslSelectBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.querydsl.select; | ||
|
||
import com.github.vineey.rql.select.SelectBuilder; | ||
import com.github.vineey.rql.select.parser.ast.SelectNodeList; | ||
import com.mysema.query.types.Expression; | ||
import com.mysema.query.types.Path; | ||
import com.mysema.query.types.Projections; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author vrustia - 4/17/16. | ||
*/ | ||
public class QuerydslSelectBuilder implements SelectBuilder<Expression, QuerydslSelectParam> { | ||
@Override | ||
public Expression visit(SelectNodeList node, QuerydslSelectParam selectParam) { | ||
List<String> sortNodes = node.getFields(); | ||
|
||
List<Expression> selectPath = new ArrayList<>(); | ||
Map<String, Path> mapping = selectParam.getMapping(); | ||
for (String selectNode : sortNodes) { | ||
Path path = mapping.get(selectNode); | ||
selectPath.add(path); | ||
} | ||
|
||
return Projections.bean(selectParam.getRootPath(), selectPath.toArray(new Expression[]{})); | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...dsl-select/src/main/java/com/github/vineey/rql/querydsl/select/QuerydslSelectContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.querydsl.select; | ||
|
||
import com.github.vineey.rql.select.SelectContext; | ||
import com.mysema.query.types.EntityPath; | ||
import com.mysema.query.types.Expression; | ||
import com.mysema.query.types.Path; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author vrustia - 4/17/16. | ||
*/ | ||
public class QuerydslSelectContext extends SelectContext<Expression, QuerydslSelectParam> { | ||
|
||
public static QuerydslSelectContext withMapping(EntityPath rootPath, Map<String, Path> mappings) { | ||
return (QuerydslSelectContext) new QuerydslSelectContext() | ||
.setSelectParam(new QuerydslSelectParam() | ||
.setRootPath(rootPath) | ||
.setMapping(mappings)) | ||
.setSelectBuilder(new QuerydslSelectBuilder()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...rydsl-select/src/main/java/com/github/vineey/rql/querydsl/select/QuerydslSelectParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.querydsl.select; | ||
|
||
import com.github.vineey.rql.select.SelectParam; | ||
import com.mysema.query.types.EntityPath; | ||
import com.mysema.query.types.Path; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author vrustia - 4/17/16. | ||
*/ | ||
public class QuerydslSelectParam extends SelectParam { | ||
|
||
private EntityPath rootPath; | ||
|
||
private Map<String, Path> mapping; | ||
|
||
public Map<String, Path> getMapping() { | ||
return mapping; | ||
} | ||
|
||
public QuerydslSelectParam setMapping(Map<String, Path> mapping) { | ||
this.mapping = mapping; | ||
return this; | ||
} | ||
|
||
public EntityPath getRootPath() { | ||
return rootPath; | ||
} | ||
|
||
public QuerydslSelectParam setRootPath(EntityPath rootPath) { | ||
this.rootPath = rootPath; | ||
return this; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...select/src/test/java/com/github/vineey/rql/querydsl/select/QuerydslSelectContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2016 John Michael Vincent S. Rustia | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
package com.github.vineey.rql.querydsl.select; | ||
|
||
import com.github.vineey.rql.querydsl.test.jpa.QEmployee; | ||
import com.github.vineey.rql.querydsl.test.mongo.QContactDocument; | ||
import com.github.vineey.rql.select.parser.DefaultSelectParser; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.mysema.query.types.Expression; | ||
import com.mysema.query.types.Path; | ||
import com.mysema.query.types.Projections; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* @author vrustia - 5/9/16. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class QuerydslSelectContextTest { | ||
|
||
@Test | ||
public void singleSelect() { | ||
String sortExpression = "select(employee.number)"; | ||
DefaultSelectParser selectParser = new DefaultSelectParser(); | ||
Map<String, Path> mappings = ImmutableMap.<String, Path>builder() | ||
.put("employee.number", QEmployee.employee.employeeNumber) | ||
.build(); | ||
|
||
Expression selectExpression = selectParser.parse(sortExpression, QuerydslSelectContext.withMapping(QEmployee.employee, mappings)); | ||
assertNotNull(selectExpression); | ||
assertEquals(Projections.bean(QEmployee.employee, QEmployee.employee.employeeNumber), selectExpression); | ||
} | ||
|
||
@Test | ||
public void multiSelect() { | ||
String sortExpression = "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 selectExpression = selectParser.parse(sortExpression, QuerydslSelectContext.withMapping(QContactDocument.contactDocument, mappings)); | ||
|
||
assertNotNull(selectExpression); | ||
|
||
assertEquals(Projections.bean(QContactDocument.contactDocument, | ||
QContactDocument.contactDocument.company, | ||
QContactDocument.contactDocument.name, | ||
QContactDocument.contactDocument.age), selectExpression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters