Skip to content

Commit

Permalink
TEIID-3112 using the relationship name and not adding fks for null
Browse files Browse the repository at this point in the history
relationship names
  • Loading branch information
shawkins committed Mar 25, 2015
1 parent 66ad2e2 commit dc46e09
Show file tree
Hide file tree
Showing 4 changed files with 529 additions and 789 deletions.
Expand Up @@ -36,7 +36,6 @@ public class SalesForceMetadataProcessor implements MetadataProcessor<Salesforce

private Map<String, Table> tableMap = new HashMap<String, Table>();
private Map<String, List<ChildRelationship>> relationships = new LinkedHashMap<String, List<ChildRelationship>>();
private boolean hasUpdateableColumn = false;
private List<Column> columns;
private boolean auditModelFields = false;

Expand Down Expand Up @@ -66,8 +65,6 @@ public class SalesForceMetadataProcessor implements MetadataProcessor<Salesforce
static final String TABLE_SUPPORTS_RETRIEVE = MetadataFactory.SF_URI+"Supports Retrieve"; //$NON-NLS-1$
@ExtensionMetadataProperty(applicable={Table.class}, datatype=Boolean.class, display="Supports Search")
static final String TABLE_SUPPORTS_SEARCH = MetadataFactory.SF_URI+"Supports Search"; //$NON-NLS-1$
@ExtensionMetadataProperty(applicable={Table.class}, datatype=String.class, display="The plural name")
public static final String TABLE_LABEL_PLURAL = MetadataFactory.SF_URI+"label_plural"; //$NON-NLS-1$

@ExtensionMetadataProperty(applicable={Column.class}, datatype=Boolean.class, display="Defaulted on Create")
static final String COLUMN_DEFAULTED = MetadataFactory.SF_URI+"Defaulted on Create"; //$NON-NLS-1$
Expand Down Expand Up @@ -141,7 +138,9 @@ public void processMetadata() throws TranslatorException {
private void addRelationships() {
for (Map.Entry<String, List<ChildRelationship>> entry : this.relationships.entrySet()) {
for (ChildRelationship relationship : entry.getValue()) {

if (relationship.getRelationshipName() == null) {
continue; //not queryable
}
if (!isModelAuditFields() && isAuditField(relationship.getField())) {
continue;
}
Expand Down Expand Up @@ -170,7 +169,7 @@ private void addRelationships() {
ArrayList<String> columnNames = new ArrayList<String>();
columnNames.add(col.getName());
ForeignKey fk = metadataFactory.addForiegnKey(name, columnNames, parent.getName(), child);
//fk.setNameInSource(relationship.getRelationshipName()); //TODO: only needed for custom relationships
fk.setNameInSource(relationship.getRelationshipName()); //TODO: only needed for custom relationships
}
}
}
Expand Down Expand Up @@ -210,11 +209,8 @@ private void addTable(DescribeGlobalSObjectResult object) throws TranslatorExcep
table.setProperty(TABLE_SUPPORTS_REPLICATE, String.valueOf(objectMetadata.isReplicateable()));
table.setProperty(TABLE_SUPPORTS_RETRIEVE, String.valueOf(objectMetadata.isRetrieveable()));
table.setProperty(TABLE_SUPPORTS_SEARCH, String.valueOf(objectMetadata.isSearchable()));
table.setProperty(TABLE_LABEL_PLURAL, objectMetadata.getLabelPlural());


hasUpdateableColumn = false;
addColumns(objectMetadata, table);
boolean hasUpdateableColumn = addColumns(objectMetadata, table);

// Some SF objects return true for isUpdateable() but have no updateable columns.
if(hasUpdateableColumn && objectMetadata.isUpdateable()) {
Expand All @@ -229,7 +225,8 @@ private void getRelationships(DescribeSObjectResult objectMetadata) {
}
}

private void addColumns(DescribeSObjectResult objectMetadata, Table table) throws TranslatorException {
private boolean addColumns(DescribeSObjectResult objectMetadata, Table table) throws TranslatorException {
boolean hasUpdateableColumn = false;
List<Field> fields = objectMetadata.getFields();
for (Field field : fields) {
String normalizedName = NameUtil.normalizeName(field.getName());
Expand Down Expand Up @@ -336,6 +333,7 @@ else if(sfTypeName.equals(FieldType.TIME.value())) { //"time"
column.setProperty(COLUMN_CUSTOM, String.valueOf(field.isCustom()));
column.setProperty(COLUMN_DEFAULTED, String.valueOf(field.isDefaultedOnCreate()));
}
return hasUpdateableColumn;
}

private String getPicklistValues(Field field) {
Expand Down
Expand Up @@ -10,10 +10,10 @@
import org.teiid.language.NamedTable;
import org.teiid.language.TableReference;
import org.teiid.metadata.Column;
import org.teiid.metadata.ForeignKey;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.translator.TranslatorException;
import org.teiid.translator.salesforce.SalesForceMetadataProcessor;


/**
Expand All @@ -34,6 +34,7 @@ public class JoinQueryVisitor extends SelectVisitor {
private Table rightTableInJoin;
private Table childTable;
private String parentName;
private ForeignKey foreignKey;

public JoinQueryVisitor(RuntimeMetadata metadata) {
super(metadata);
Expand Down Expand Up @@ -75,6 +76,16 @@ public void visit(Join join) {
if (StringUtil.endsWithIgnoreCase(name, "id")) {
this.parentName = name.substring(0, name.length() - 2);
}
Table parent = leftTableInJoin;
if (isChildToParentJoin()) {
parent = rightTableInJoin;
}
for (ForeignKey fk : childTable.getForeignKeys()) {
if (fk.getReferenceKey().equals(parent.getPrimaryKey())) {
foreignKey = fk;
break;
}
}
} else {
// Only add the criteria to the query if it is not the join criteria.
// The join criteria is implicit in the salesforce syntax.
Expand Down Expand Up @@ -108,8 +119,11 @@ public String getQuery() throws TranslatorException {
subselect.append(SPACE);

subselect.append(FROM).append(SPACE);
String pluralName = rightTableInJoin.getProperty(SalesForceMetadataProcessor.TABLE_LABEL_PLURAL, false);
if (pluralName == null) {

String pluralName = null;
if (this.foreignKey != null && this.foreignKey.getNameInSource() != null) {
pluralName = this.foreignKey.getNameInSource();
} else {
pluralName = rightTableInJoin.getNameInSource() + "s"; //$NON-NLS-1$
}
subselect.append(pluralName);
Expand Down
Expand Up @@ -47,13 +47,14 @@
import org.teiid.metadata.ProcedureParameter;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.metadata.Table;
import org.teiid.query.metadata.CompositeMetadataStore;
import org.teiid.query.metadata.MetadataValidator;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.metadata.SystemMetadata;
import org.teiid.query.metadata.TransformationMetadata;
import org.teiid.query.parser.QueryParser;
import org.teiid.query.sql.lang.SPParameter;
import org.teiid.query.unittest.RealMetadataFactory;
import org.teiid.query.validator.ValidatorReport;
import org.teiid.translator.Execution;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.TypeFacility;
Expand Down Expand Up @@ -109,7 +110,12 @@ public static QueryMetadataInterface exampleSalesforce() {
nativeProc.setProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, "search;select accountname from account where accountid = $1");
nativeProc.setResultSet(RealMetadataFactory.createResultSet("rs", new String[] {"accountname"}, new String[] {TypeFacility.RUNTIME_NAMES.STRING}));

return new TransformationMetadata(null, new CompositeMetadataStore(mf.asMetadataStore()), null, RealMetadataFactory.SFM.getSystemFunctions(), null);
TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "x");
ValidatorReport report = new MetadataValidator().validate(tm.getVdbMetaData(), tm.getMetadataStore());
if (report.hasItems()) {
throw new RuntimeException(report.getFailureMessage());
}
return tm;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -282,4 +288,11 @@ private void helpTest(String sql, String expected) throws Exception {
helpTest(sql, source);
}

@Test public void testPluralNameFromKey() throws Exception {
String sql = "SELECT CaseSolution.SolutionId, Case_.Origin FROM Case_ LEFT OUTER JOIN CaseSolution ON Case_.Id = CaseSolution.CaseId";
String source = "SELECT Case.Origin, (SELECT CaseSolution.SolutionId FROM CaseSolutions) FROM Case";
helpTest(sql, source);

}

}

0 comments on commit dc46e09

Please sign in to comment.