Skip to content

Commit

Permalink
implemented querydsl module for rql select api
Browse files Browse the repository at this point in the history
  • Loading branch information
vineey committed May 9, 2016
1 parent 3e71eb2 commit e774ff9
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def deployableSubproject =
'rsql-api-sort',
'rsql-api-select',
'rsql-api-all',
'rsql-querydsl-select',
'rsql-querydsl-filter',
'rsql-querydsl-page',
'rsql-querydsl-sort',
Expand Down
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);

}
}
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);
}
1 change: 1 addition & 0 deletions rsql-querydsl-parent/rsql-querydsl-all/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ description = 'Bundles all rql API modules and contain classes needed for aggreg

dependencies {
compile project(':rsql-api-parent:rsql-api-all')
compile project(':rsql-querydsl-parent:rsql-querydsl-select')
compile project(':rsql-querydsl-parent:rsql-querydsl-filter')
compile project(':rsql-querydsl-parent:rsql-querydsl-page')
compile project(':rsql-querydsl-parent:rsql-querydsl-sort')
Expand Down
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[]{}));

}
}
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());
}
}
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;
}
}
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);
}
}
2 changes: 1 addition & 1 deletion rsql-querydsl-parent/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include 'rsql-querydsl-all'
include 'rsql-querydsl-filter'
include 'rsql-querydsl-page'
include 'rsql-querydsl-sort'
include 'rsql-querydsl-select'
include 'rsql-querydsl-test'
include 'rsql-querydsl-spring'
include 'rsql-querydsl-select'

1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include 'rsql-api-parent:rsql-api-sort'
include 'rsql-api-parent:rsql-api-all'

include 'rsql-querydsl-parent:rsql-querydsl-test'
include 'rsql-querydsl-parent:rsql-querydsl-select'
include 'rsql-querydsl-parent:rsql-querydsl-filter'
include 'rsql-querydsl-parent:rsql-querydsl-page'
include 'rsql-querydsl-parent:rsql-querydsl-sort'
Expand Down

0 comments on commit e774ff9

Please sign in to comment.