use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class TestODataMetadataProcessor method testOneToManyAssosiation.
@Test
public void testOneToManyAssosiation() throws Exception {
MetadataFactory mf = oneToManyRelationMetadata();
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 testAssosiationWithReferentialContriant.
@Test
public void testAssosiationWithReferentialContriant() throws Exception {
ODataMetadataProcessor processor = new ODataMetadataProcessor();
MetadataFactory mf = new MetadataFactory("vdb", 1, "northwind", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
EdmEntityType.Builder g1Entity = entityType("g1");
g1Entity.addProperties(EdmProperty.newBuilder("g2e2").setType(EdmSimpleType.STRING).setNullable(false));
EdmEntityType.Builder g2Entity = entityType("g2");
EdmAssociationEnd.Builder aend1 = EdmAssociationEnd.newBuilder().setRole("source").setType(g1Entity).setMultiplicity(EdmMultiplicity.ONE);
EdmAssociationEnd.Builder aend2 = EdmAssociationEnd.newBuilder().setRole("target").setType(g2Entity).setMultiplicity(EdmMultiplicity.ONE);
EdmReferentialConstraint.Builder refContraint = EdmReferentialConstraint.newBuilder().addPrincipalReferences("g2e2").setPrincipalRole("source").addDependentReferences("e1").setDependentRole("target");
EdmAssociation.Builder assocition = EdmAssociation.newBuilder().setNamespace("namspace").setName("one_2_one").setEnds(aend2, aend1).setRefConstraint(refContraint);
EdmNavigationProperty.Builder navigation = EdmNavigationProperty.newBuilder("g1").setFromTo(aend2, aend1).setFromToName("source", "target").setRelationship(assocition);
g2Entity.addNavigationProperties(navigation);
EdmEntitySet g1Set = EdmEntitySet.newBuilder().setName("G1").setEntityType(g1Entity).build();
EdmEntitySet g2Set = EdmEntitySet.newBuilder().setName("G2").setEntityType(g2Entity).build();
Table t1 = processor.addEntitySetAsTable(mf, g1Set);
Table t2 = processor.addEntitySetAsTable(mf, g2Set);
KeyRecord record = new KeyRecord(Type.Unique);
record.addColumn(t1.getColumnByName("g2e2"));
t1.setUniqueKeys(Arrays.asList(record));
processor.addNavigationRelations(mf, "G2", g2Entity.build());
Table g1 = mf.getSchema().getTable("G1");
Table g2 = mf.getSchema().getTable("G2");
assertNotNull(g1);
assertNotNull(g2);
ForeignKey fk = g1.getForeignKeys().get(0);
assertEquals("one_2_one", fk.getName());
assertNotNull(fk.getColumnByName("g2e2"));
assertEquals("e1", fk.getReferenceColumns().get(0));
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class ODataQuery method isJoinOrPkColumn.
private boolean isJoinOrPkColumn(Column column) {
Table table = (Table) column.getParent();
boolean isKey = (table.getPrimaryKey().getColumnByName(column.getName()) != null);
if (!isKey) {
for (ForeignKey fk : table.getForeignKeys()) {
if (fk.getColumnByName(column.getName()) != null) {
isKey = true;
}
}
}
return isKey;
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class SalesForceMetadataProcessor method addRelationships.
private void addRelationships() {
for (Map.Entry<String, ChildRelationship[]> entry : this.relationships.entrySet()) {
for (ChildRelationship relationship : entry.getValue()) {
if (relationship.getRelationshipName() == null) {
// not queryable
continue;
}
if (!isModelAuditFields() && isAuditField(relationship.getField())) {
continue;
}
Table parent = tableMap.get(entry.getKey());
KeyRecord pk = parent.getPrimaryKey();
if (null == pk) {
// $NON-NLS-1$
throw new RuntimeException("ERROR !!primary key column not found!!");
}
Table child = tableMap.get(relationship.getChildSObject());
if (child == null) {
// child must have been excluded
continue;
}
Column col = null;
columns = child.getColumns();
for (Iterator<Column> colIter = columns.iterator(); colIter.hasNext(); ) {
Column column = colIter.next();
if (column.getNameInSource().equals(relationship.getField())) {
col = column;
}
}
if (null == col) {
throw new RuntimeException(// $NON-NLS-1$
"ERROR !!foreign key column not found!! " + child.getName() + relationship.getField());
}
// $NON-NLS-1$ //$NON-NLS-2$
String name = "FK_" + parent.getName() + "_" + col.getName();
ArrayList<String> columnNames = new ArrayList<String>();
columnNames.add(col.getName());
ForeignKey fk = metadataFactory.addForeignKey(name, columnNames, parent.getName(), child);
// TODO: only needed for custom relationships
fk.setNameInSource(relationship.getRelationshipName());
}
}
}
use of org.teiid.metadata.ForeignKey in project teiid by teiid.
the class CoherenceUpdateExecution method addChildObject.
private void addChildObject(Table t) throws TranslatorException {
List<ForeignKey> fks = t.getForeignKeys();
ForeignKey fk = fks.get(0);
Table parentTable = fk.getParent();
// the name of the method to obtain the collection is the nameInSource of the foreginKey
String parentToChildMethod = fk.getNameInSource();
if (parentToChildMethod == null) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noNameInSourceForForeingKey", new Object[] { fk.getName() });
throw new TranslatorException(msg);
}
// there must only be 1 column in the primary key
String parentColName = visitor.getNameFromElement(fk.getPrimaryKey().getColumns().get(0));
Insert icommand = (Insert) command;
List<ColumnReference> insertElementList = icommand.getColumns();
List<Expression> insertValueList = ((ExpressionValueSource) icommand.getValueSource()).getValues();
if (insertElementList.size() != insertValueList.size()) {
throw new TranslatorException("Error: columns.size and values.size are not the same.");
}
ColumnReference insertElement;
String[] nameOfElement = new String[insertElementList.size()];
int parentValueLoc = -1;
for (int i = 0; i < insertElementList.size(); i++) {
insertElement = insertElementList.get(i);
// // call utility class to get NameInSource/Name of element
nameOfElement[i] = visitor.getNameFromElement(insertElement.getMetadataObject());
// match the parent column to the colum in the insert statement
if (nameOfElement[i].equalsIgnoreCase(parentColName)) {
parentValueLoc = i;
}
}
if (parentColName != null && parentValueLoc == -1) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noColumnMatchedForeignColumn", new Object[] { t.getName(), parentColName });
throw new TranslatorException(msg);
}
// get the parent key and find the root object
Object parentValue = insertValueList.get(parentValueLoc);
Object val;
if (parentValue instanceof Literal) {
Literal literalValue = (Literal) parentValue;
val = literalValue.getValue();
} else {
val = parentValue;
}
Object parentObject = null;
// get the parent object from the cache
try {
List<Object> result = this.connection.get(CoherenceFilterUtil.createCompareFilter(parentColName, val, Operator.EQ, val.getClass()));
// visitor.createFilter(parentColName + " = " + val));
if (result == null || result.isEmpty()) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noobjectfound", new Object[] { parentTable.getName(), parentColName, val });
throw new TranslatorException(msg);
}
parentObject = result.get(0);
} catch (ResourceException e) {
throw new TranslatorException(e);
}
// create and load the child object data
Object newChildObject = cacheTranslator.createObject(insertElementList, insertValueList, this.visitor, t);
// /--- questions
// / --- how to not process - setvalue for parent column
// / --- need to get the key value off the object of the parent
// get the key value to use to for adding to the cache
Object parentContainer = ObjectSourceMethodManager.getValue("get" + parentToChildMethod, parentObject);
if (parentContainer == null) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noParentContainerObjectFound", new Object[] { parentTable.getName(), parentToChildMethod });
throw new TranslatorException(msg);
}
if (parentContainer.getClass().isArray()) {
} else if (parentContainer instanceof Collection) {
Collection c = (Collection) parentContainer;
c.add(newChildObject);
} else if (parentContainer instanceof Map) {
Map m = (Map) parentContainer;
m.put(1, newChildObject);
}
try {
this.connection.update(parentValue, parentObject);
} catch (ResourceException e) {
throw new TranslatorException(e);
}
}
Aggregations