Skip to content

Commit

Permalink
TEIID-3378: Correctly generating the SF query in the case inner join …
Browse files Browse the repository at this point in the history
…where a implicit criteria is added
  • Loading branch information
rareddy authored and shawkins committed Mar 25, 2015
1 parent 91a916a commit fab01c5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Expand Up @@ -88,8 +88,8 @@ public void visit(Join join) {
this.leftTableInJoin = rightTableInJoin;
this.rightTableInJoin = t;
}
//add is null criteria
visitNode(new Comparison(fKey, new Literal(null, fKey.getType()), Comparison.Operator.NE));
//add is null criteria
addCriteria(new Comparison(fKey, new Literal(null, fKey.getType()), Comparison.Operator.NE));
}
} else {
// Only add the criteria to the query if it is not the join criteria.
Expand All @@ -104,7 +104,7 @@ public void visit(Join join) {
}
}

@Override
@Override
public String getQuery() throws TranslatorException {

if (isChildToParentJoin()) {
Expand Down
Expand Up @@ -22,13 +22,10 @@
package org.teiid.translator.salesforce.execution.visitors;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.teiid.language.*;
import org.teiid.language.AndOr.Operator;
import org.teiid.metadata.Column;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.TranslatorException;
Expand All @@ -47,14 +44,27 @@ public class SelectVisitor extends CriteriaVisitor implements IQueryProvidingVis
protected StringBuilder groupByClause = new StringBuilder();
protected StringBuilder havingClause = new StringBuilder();
private Boolean objectSupportsRetrieve;
private Condition implicitCondition;

public SelectVisitor(RuntimeMetadata metadata) {
super(metadata);
}

public void visit(Select query) {
@Override
public void visit(Select query) {
super.visitNodes(query.getFrom());
super.visitNode(query.getWhere());

Condition condition = query.getWhere();
if (this.implicitCondition != null) {
if (condition != null) {
condition = LanguageFactory.INSTANCE.createAndOr(Operator.AND, condition, this.implicitCondition);
}
else {
condition = implicitCondition;
}
}

super.visitNode(condition);
super.visitNode(query.getGroupBy());
if (query.getHaving() != null) {
//since the base is a criteria hierarchy visitor,
Expand Down Expand Up @@ -98,6 +108,10 @@ public void visit(Select query) {
}
}

protected void addCriteria(Condition condition) {
this.implicitCondition = condition;
}

@Override
public void visit(GroupBy obj) {
this.groupByClause.append("GROUP BY "); //$NON-NLS-1$
Expand Down
Expand Up @@ -185,6 +185,13 @@ public static QueryMetadataInterface exampleSalesforce() {
assertEquals("SELECT (SELECT Contact.Name FROM Contacts) FROM Account", visitor.getQuery().toString().trim()); //$NON-NLS-1$
}

@Test public void testJoin4() throws Exception {
Select command = (Select)translationUtility.parseCommand("SELECT Contact.Name FROM Account INNER JOIN Contact ON Account.Id = Contact.AccountId WHERE Contact.Name='foo' AND Account.Id=5"); //$NON-NLS-1$
SelectVisitor visitor = new JoinQueryVisitor(translationUtility.createRuntimeMetadata());
visitor.visit(command);
assertEquals("SELECT Contact.Name FROM Contact WHERE ((Contact.Name = 'foo') AND (Account.Id = '5')) AND (Contact.AccountId != NULL)", visitor.getQuery().toString().trim()); //$NON-NLS-1$
}

@Test public void testInnerJoin() throws Exception {
Select command = (Select)translationUtility.parseCommand("SELECT Account.Phone, Account.Name, Account.Type, Contact.LastName FROM Account inner join Contact on Account.Id = Contact.AccountId"); //$NON-NLS-1$
SelectVisitor visitor = new JoinQueryVisitor(translationUtility.createRuntimeMetadata());
Expand Down

0 comments on commit fab01c5

Please sign in to comment.