use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestODataMetadataProcessor method testAssosiationWithReferentialContriant.
@Test
public void testAssosiationWithReferentialContriant() throws Exception {
ODataMetadataProcessor processor = new ODataMetadataProcessor();
MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
CsdlEntityType g1Entity = entityType("g1");
g1Entity.getProperties().add(createProperty("g2e2", EdmPrimitiveTypeKind.String));
g1Entity.setKey(Arrays.asList(new CsdlPropertyRef().setName("g2e2")));
CsdlEntityType g2Entity = entityType("g2");
CsdlNavigationProperty navProperty = new CsdlNavigationProperty();
navProperty.setName("one_2_one");
navProperty.setType("namespace.g2");
navProperty.setNullable(false);
navProperty.setPartner("PartnerPath");
navProperty.setReferentialConstraints(Arrays.asList(new CsdlReferentialConstraint().setProperty("g2e2").setReferencedProperty("e2")));
g1Entity.setNavigationProperties(Arrays.asList(navProperty));
CsdlEntitySet g1Set = createES("G1", "namespace.g1");
CsdlEntitySet g2Set = createES("G2", "namespace.g2");
CsdlNavigationPropertyBinding navBinding = new CsdlNavigationPropertyBinding();
navBinding.setPath("one_2_one");
navBinding.setTarget("G2");
g1Set.setNavigationPropertyBindings(Arrays.asList(navBinding));
XMLMetadata metadata = buildXmlMetadata(g1Entity, g1Set, g2Entity, g2Set);
processor.getMetadata(mf, metadata);
Table g1 = mf.getSchema().getTable("G1");
Table g2 = mf.getSchema().getTable("G2");
assertNotNull(g1);
assertNotNull(g2);
ForeignKey fk = g1.getForeignKeys().get(0);
assertEquals("G1_one_2_one", fk.getName());
assertNotNull(fk.getColumnByName("g2e2"));
assertEquals("e2", fk.getReferenceColumns().get(0));
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestODataMetadataProcessor method testMultipleNavigationProperties.
@Test
public void testMultipleNavigationProperties() throws Exception {
MetadataFactory mf = multipleNavigationProperties();
String metadataDDL = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
Table g1 = mf.getSchema().getTable("G1");
Table g2 = mf.getSchema().getTable("G2");
ForeignKey fk = g2.getForeignKeys().get(0);
assertEquals("G1_one_2_many", fk.getName());
assertNotNull(fk.getColumnByName("e1"));
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestODataMetadataProcessor method testMultipleEnititySetWithSameComplexType.
@Test
public void testMultipleEnititySetWithSameComplexType() throws Exception {
ODataMetadataProcessor processor = new ODataMetadataProcessor();
MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
CsdlComplexType address = complexType("Address");
XMLMetadata metadata = buildXmlMetadata(createES("Persons", "namespace.Person"), buildPersonEntity(address), address, createES("Corporate", "namespace.Business"), buildBusinessEntity(address));
processor.getMetadata(mf, metadata);
assertEquals(5, mf.getSchema().getTables().size());
assertNotNull(mf.getSchema().getTable("Persons"));
assertNotNull(mf.getSchema().getTable("Corporate"));
assertNotNull(mf.getSchema().getTable("Persons_address"));
assertNotNull(mf.getSchema().getTable("Corporate_address"));
Table personTable = mf.getSchema().getTable("Persons");
assertEquals(2, personTable.getColumns().size());
Table personAddress = mf.getSchema().getTable("Persons_address");
assertEquals(4, personAddress.getColumns().size());
ForeignKey fk = personAddress.getForeignKeys().get(0);
assertNotNull(fk.getColumnByName("Persons_ssn"));
Table businessTable = mf.getSchema().getTable("Corporate");
assertEquals(1, businessTable.getColumns().size());
Table corporateAddress = mf.getSchema().getTable("Corporate_address");
assertEquals(4, corporateAddress.getColumns().size());
fk = corporateAddress.getForeignKeys().get(0);
assertNotNull(fk.getColumnByName("Corporate_name"));
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class JoinQueryVisitor method visit.
// Has to be a left outer join of only 2 tables. criteria must be a compare
@Override
public void visit(Join join) {
try {
TableReference left = join.getLeftItem();
NamedTable leftGroup = (NamedTable) left;
leftTableInJoin = leftGroup.getMetadataObject();
loadColumnMetadata(leftGroup);
TableReference right = join.getRightItem();
NamedTable rightGroup = (NamedTable) right;
rightTableInJoin = rightGroup.getMetadataObject();
loadColumnMetadata((NamedTable) right);
Comparison criteria = (Comparison) join.getCondition();
Expression lExp = criteria.getLeftExpression();
Expression rExp = criteria.getRightExpression();
if (isIdColumn(rExp) || isIdColumn(lExp)) {
Column rColumn = ((ColumnReference) rExp).getMetadataObject();
String rTableName = rColumn.getParent().getSourceName();
Column lColumn = ((ColumnReference) lExp).getMetadataObject();
String lTableName = lColumn.getParent().getSourceName();
if (leftTableInJoin.getSourceName().equals(rTableName) || leftTableInJoin.getSourceName().equals(lTableName) && rightTableInJoin.getSourceName().equals(rTableName) || rightTableInJoin.getSourceName().equals(lTableName) && !rTableName.equals(lTableName)) {
// This is the join criteria, the one that is the ID is the parent.
Expression fKey = !isIdColumn(lExp) ? lExp : rExp;
ColumnReference columnReference = (ColumnReference) fKey;
table = childTable = (Table) columnReference.getMetadataObject().getParent();
String name = columnReference.getMetadataObject().getSourceName();
if (StringUtil.endsWithIgnoreCase(name, "id")) {
this.parentName = name.substring(0, name.length() - 2);
} else if (name.endsWith("__c")) {
// $NON-NLS-1$
// $NON-NLS-1$
this.parentName = name.substring(0, name.length() - 1) + "r";
}
Table parent = leftTableInJoin;
if (isChildToParentJoin()) {
parent = rightTableInJoin;
}
for (ForeignKey fk : childTable.getForeignKeys()) {
if (fk.getColumns().get(0).equals(columnReference.getMetadataObject()) && fk.getReferenceKey().equals(parent.getPrimaryKey())) {
foreignKey = fk;
break;
}
}
// inner joins require special handling as relationship queries are outer by default
if (join.getJoinType() == JoinType.INNER_JOIN) {
if (!isChildToParentJoin()) {
// flip the relationship
Table t = leftTableInJoin;
this.leftTableInJoin = rightTableInJoin;
this.rightTableInJoin = t;
}
// 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.
// The join criteria is implicit in the salesforce syntax.
// TODO: not valid
super.visit(criteria);
}
} else {
// TODO: not valid
super.visit(criteria);
}
} catch (TranslatorException ce) {
exceptions.add(ce);
}
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class SchemaToProtobufProcessor method processFKTable.
// Find FK to this table, and add the column
private void processFKTable(Table table) {
int increment = 1;
for (Table t : schema.getTables().values()) {
if (table == t) {
continue;
}
if (!t.getForeignKeys().isEmpty()) {
List<ForeignKey> fks = t.getForeignKeys();
for (ForeignKey fk : fks) {
if (fk.getReferenceTableName().equals(table.getName())) {
addTab();
String messageName = ProtobufMetadataProcessor.getMessageName(t);
if (messageName == null) {
messageName = table.getName();
} else {
messageName = messageName.substring(messageName.lastIndexOf('.') + 1);
}
String columnName = ProtobufMetadataProcessor.getParentColumnName(t);
if (columnName == null) {
columnName = t.getName().toLowerCase();
}
int tag = ProtobufMetadataProcessor.getParentTag(t);
if (tag == -1) {
tag = table.getColumns().size() + increment;
increment++;
}
buffer.append("repeated ");
buffer.append(messageName).append(SPACE).append(columnName);
buffer.append(" = ").append(tag).append(SEMICOLON).append(NL);
}
}
}
}
}
Aggregations