Skip to content

Commit

Permalink
TEIID-3671
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Sep 2, 2015
1 parent 4347e81 commit c379a5d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
80 changes: 38 additions & 42 deletions odata/src/main/java/org/teiid/odata/ODataSQLBuilder.java
Expand Up @@ -49,6 +49,7 @@
import org.odata4j.producer.QueryInfo;
import org.teiid.core.types.DataTypeManager;
import org.teiid.core.types.JDBCSQLTypeInfo;
import org.teiid.core.util.Assertion;
import org.teiid.metadata.Column;
import org.teiid.metadata.ForeignKey;
import org.teiid.metadata.KeyRecord;
Expand All @@ -69,7 +70,7 @@
public class ODataSQLBuilder extends ODataHierarchyVisitor {
private Query query = new Query();
private OrderBy orderBy = new OrderBy();
private Criteria criteria;
private Criteria where;
private MetadataStore metadata;
private ArrayList<SQLParam> params = new ArrayList<SQLParam>();
private boolean prepared = true;
Expand All @@ -82,17 +83,12 @@ public class ODataSQLBuilder extends ODataHierarchyVisitor {
private HashMap<String, String> aliasTableNames = new HashMap<String, String>();
private AtomicInteger groupCount = new AtomicInteger(1);
private boolean distinct = false;
private boolean useLimit = true;

public ODataSQLBuilder(MetadataStore metadata, boolean prepared) {
this.metadata = metadata;
this.prepared = prepared;
}

public void setUseLimit(boolean useLimit) {
this.useLimit = useLimit;
}

public Query selectString(String entityName, QueryInfo info, OEntityKey key, String navProperty, boolean countStar) {
Select select = new Select();

Expand All @@ -109,7 +105,7 @@ public Query selectString(String entityName, QueryInfo info, OEntityKey key, Str
this.aliasTableNames.put("g0", entityTable.getFullName());

if (key != null) {
this.criteria = buildEntityKeyCriteria(entityTable, this.resultEntityGroup, key);
this.where = buildEntityKeyCriteria(entityTable, this.resultEntityGroup, key);
}

this.fromCluse = new UnaryFromClause(this.resultEntityGroup);
Expand Down Expand Up @@ -183,12 +179,7 @@ public Query selectString(String entityName, QueryInfo info, OEntityKey key, Str

if (propSplit.length > 1) {
key = OEntityKey.parse("("+ propSplit[1]);
if (this.criteria != null) {
this.criteria = new CompoundCriteria(CompoundCriteria.AND, this.criteria, buildEntityKeyCriteria(entityTable, this.resultEntityGroup, key));
}
else {
this.criteria = buildEntityKeyCriteria(entityTable, this.resultEntityGroup, key);
}
this.where = Criteria.combineCriteria(this.where, buildEntityKeyCriteria(entityTable, this.resultEntityGroup, key));
}
}
}
Expand All @@ -202,7 +193,13 @@ public Query selectString(String entityName, QueryInfo info, OEntityKey key, Str
}

if (info.filter != null) {
Assertion.assertTrue(this.stack.isEmpty());
visitNode(info.filter);
Expression ex = this.getExpression();
if (!(ex instanceof Criteria)) {
ex = new ExpressionCriteria(ex);
}
this.where = Criteria.combineCriteria(this.where, (Criteria) ex);
}

if (!countStar) {
Expand Down Expand Up @@ -234,7 +231,7 @@ record = resultEntityTable.getUniqueKeys().get(0);
from.addClause(this.fromCluse);
query.setSelect(select);
query.setFrom(from);
query.setCriteria(this.criteria);
query.setCriteria(this.where);
return query;
}

Expand Down Expand Up @@ -384,9 +381,6 @@ public void visit(OrderByExpression expr) {
@Override
public void visit(Direction direction) {
Expression expr = stack.pop();
if (expr instanceof CompareCriteria) {
expr = ((CompareCriteria)expr).getLeftExpression();
}
this.orderBy.addVariable(expr, direction == Direction.ASCENDING);
}

Expand All @@ -405,8 +399,8 @@ public void visit(AndExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompoundCriteria(CompoundCriteria.AND, (Criteria)lhs, (Criteria)rhs);
stack.push(this.criteria);
Criteria criteria = new CompoundCriteria(CompoundCriteria.AND, (Criteria)lhs, (Criteria)rhs);
stack.push(criteria);
}

@Override
Expand Down Expand Up @@ -481,8 +475,8 @@ public void visit(EndsWithMethodCallExpression expr) {
Expression target = stack.pop();
visitNode(expr.getValue());
Expression value = stack.pop();
this.criteria = new CompareCriteria(new Function("ENDSWITH", new Expression[] {target, value}), CompareCriteria.EQ, new Constant(Boolean.TRUE));
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(new Function("ENDSWITH", new Expression[] {target, value}), CompareCriteria.EQ, new Constant(Boolean.TRUE));
stack.push(criteria);
}

@Override
Expand Down Expand Up @@ -514,13 +508,14 @@ public void visit(EqExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
Criteria criteria = null;
if (rhs instanceof Constant && ((Constant)rhs).getType() == DataTypeManager.DefaultDataClasses.NULL) {
this.criteria = new IsNullCriteria(lhs);
criteria = new IsNullCriteria(lhs);
}
else {
this.criteria = new CompareCriteria(lhs, CompareCriteria.EQ, rhs);
criteria = new CompareCriteria(lhs, CompareCriteria.EQ, rhs);
}
stack.push(this.criteria);
stack.push(criteria);
}

@Override
Expand All @@ -529,8 +524,8 @@ public void visit(GeExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompareCriteria(lhs, CompareCriteria.GE, rhs);
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(lhs, CompareCriteria.GE, rhs);
stack.push(criteria);
}

@Override
Expand All @@ -539,8 +534,8 @@ public void visit(GtExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompareCriteria(lhs, CompareCriteria.GT, rhs);
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(lhs, CompareCriteria.GT, rhs);
stack.push(criteria);
}

@Override
Expand Down Expand Up @@ -651,8 +646,8 @@ public void visit(LeExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompareCriteria(lhs, CompareCriteria.LE, rhs);
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(lhs, CompareCriteria.LE, rhs);
stack.push(criteria);
}

@Override
Expand All @@ -667,8 +662,8 @@ public void visit(LtExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompareCriteria(lhs, CompareCriteria.LT, rhs);
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(lhs, CompareCriteria.LT, rhs);
stack.push(criteria);
}

@Override
Expand All @@ -695,15 +690,16 @@ public void visit(NeExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
Criteria criteria = null;
if (rhs instanceof Constant && ((Constant)rhs).getType() == DataTypeManager.DefaultDataClasses.NULL) {
IsNullCriteria crit = new IsNullCriteria(lhs);
crit.setNegated(true);
this.criteria = crit;
criteria = crit;
}
else {
this.criteria = new CompareCriteria(lhs, CompareCriteria.NE, rhs);
criteria = new CompareCriteria(lhs, CompareCriteria.NE, rhs);
}
stack.push(this.criteria);
stack.push(criteria);
}

@Override
Expand All @@ -716,8 +712,8 @@ public void visit(NegateExpression expr) {
@Override
public void visit(NotExpression expr) {
visitNode(expr.getExpression());
this.criteria = new NotCriteria(new ExpressionCriteria(stack.pop()));
stack.push(this.criteria);
Criteria criteria = new NotCriteria(new ExpressionCriteria(stack.pop()));
stack.push(criteria);
}

@Override
Expand All @@ -731,8 +727,8 @@ public void visit(OrExpression expr) {
visitNode(expr.getRHS());
Expression rhs = stack.pop();
Expression lhs = stack.pop();
this.criteria = new CompoundCriteria(CompoundCriteria.OR, (Criteria)lhs, (Criteria)rhs);
stack.push(this.criteria);
Criteria criteria = new CompoundCriteria(CompoundCriteria.OR, (Criteria)lhs, (Criteria)rhs);
stack.push(criteria);
}

@Override
Expand Down Expand Up @@ -767,8 +763,8 @@ private void locate(BoolMethodExpression expr, int compare) {
Expression target = stack.pop();
visitNode(expr.getValue());
Expression value = stack.pop();
this.criteria = new CompareCriteria(new Function("LOCATE", new Expression[] {value, target, new Constant(1)}), compare, new Constant(1));
stack.push(this.criteria);
Criteria criteria = new CompareCriteria(new Function("LOCATE", new Expression[] {value, target, new Constant(1)}), compare, new Constant(1));
stack.push(criteria);
}

@Override
Expand Down Expand Up @@ -921,7 +917,7 @@ public void visit(AggregateAllFunction expr) {

ODataAggregateAnyBuilder builder = new ODataAggregateAnyBuilder(expr,
this.resultEntityTable, this.resultEntityGroup, joinTable, joinGroup);
this.criteria = builder.getCriteria();
Criteria criteria = builder.getCriteria();
stack.push(criteria);
}

Expand Down
8 changes: 8 additions & 0 deletions odata/src/test/java/org/teiid/odata/TestODataIntegration.java
Expand Up @@ -453,16 +453,24 @@ public void testGetEntity() throws Exception {

ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/x('a')"));
ClientResponse<String> response = request.get(String.class);
assertTrue(response.getEntity().contains("('a')"));
Assert.assertEquals(200, response.getStatus());

//filter is not applicable to getEntity
request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/x('a')?$filter=b eq 'd'"));
response = request.get(String.class);
assertTrue(response.getEntity().contains("('a')"));
Assert.assertEquals(200, response.getStatus());

//ensure that a child is nav property works
request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/x('a')/y"));
response = request.get(String.class);
assertTrue(response.getEntity().contains("('a')"));
Assert.assertEquals(200, response.getStatus());

request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/x('a')/y?$filter=a1 eq 'c'"));
response = request.get(String.class);
assertFalse(response.getEntity().contains("('c')"));
Assert.assertEquals(200, response.getStatus());
} finally {
es.stop();
Expand Down
Expand Up @@ -307,10 +307,10 @@ public void testFilterBasedAssosiation() throws Exception {
@Test
public void testOrderByWithCriteria() throws Exception {
testSelect(
"SELECT g0.ShipperID, g0.CompanyName, g0.Phone FROM nw.Shippers AS g0 WHERE g0.ShipperID = 12 ORDER BY g0.ShipperID DESC",
"SELECT g0.ShipperID, g0.CompanyName, g0.Phone FROM nw.Shippers AS g0 ORDER BY g0.ShipperID = 12 DESC",
"nw.Shippers", null, null, "ShipperID eq 12 desc", -1, null,
null);
}
}

@Test
public void testAny() throws Exception {
Expand All @@ -331,7 +331,7 @@ public void testAll() throws Exception {
@Test
public void testMultiEntitykey() throws Exception {
OEntityKey key = OEntityKey.parse("(11044)");
testSelect("SELECT g1.OrderID, g1.ProductID FROM nw.Orders AS g0 INNER JOIN nw.OrderDetails AS g1 ON g0.OrderID = g1.OrderID WHERE (g0.OrderID = 11044) AND ((g1.OrderID = 11044) AND (g1.ProductID = 62)) ORDER BY g1.OrderID, g1.ProductID",
testSelect("SELECT g1.OrderID, g1.ProductID FROM nw.Orders AS g0 INNER JOIN nw.OrderDetails AS g1 ON g0.OrderID = g1.OrderID WHERE (g0.OrderID = 11044) AND (g1.OrderID = 11044) AND (g1.ProductID = 62) ORDER BY g1.OrderID, g1.ProductID",
"nw.Orders", null,
"OrderID", null, -1, "nw.OrderDetails(OrderID=11044L,ProductID=62L)", key);
}
Expand All @@ -352,7 +352,7 @@ public void testMultiEntitykey() throws Exception {
Insert insert = visitor.insert(entitySet, entity);
assertEquals("INSERT INTO nw.Categories (Description) VALUES (?)", insert.toString());
}

}


0 comments on commit c379a5d

Please sign in to comment.