@Override public String toString() { List<String> columns = new ArrayList<>(components.size()); List<String> refColumns = new ArrayList<>(components.size()); for (Component c : components) { columns.add(c.getAttribute().getID().toString()); refColumns.add(c.getReference().getID().toString()); } StringBuilder bf = new StringBuilder(); bf.append("ALTER TABLE ").append(relation.getID().getSQLRendering()) .append(" ADD CONSTRAINT ").append(name).append(" FOREIGN KEY ("); Joiner.on(", ").appendTo(bf, columns); bf.append(") REFERENCES ").append(referencedRelation.getID().getSQLRendering()) .append(" ("); Joiner.on(", ").appendTo(bf, refColumns); bf.append(")"); return bf.toString(); } }
for (ForeignKeyConstraint.Component comp : fks.getComponents()) { Attribute att1 = comp.getAttribute(); Attribute att2 = comp.getReference();
.map(c -> c.getReference().getIndex() - 1) .collect(ImmutableCollectors.toSet());
.map(c -> c.getReference().getIndex() - 1) .collect(ImmutableCollectors.toSet());
/*** * Definition reference triple: an RDF triple with: * <p/> * subject: the row node for the row. * predicate: the reference property IRI for the columns. * object: the row node for the referenced row. * * @param fk * @return */ private String getRefSQL(ForeignKeyConstraint fk) { Set<Object> columns = new LinkedHashSet<>(); // Set avoids duplicated and LinkedHashSet keeps the insertion order for (Attribute attr : getIdentifyingAttributes(fk.getRelation())) columns.add(getColumnNameWithAlias(attr)); List<String> conditions = new ArrayList<>(fk.getComponents().size()); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { columns.add(getColumnNameWithAlias(comp.getReference())); conditions.add(getColumnName(comp.getAttribute()) + " = " + getColumnName(comp.getReference())); } for (Attribute attr : getIdentifyingAttributes(fk.getReferencedRelation())) columns.add(getColumnNameWithAlias(attr)); final String tables = fk.getRelation().getID().getSQLRendering() + ", " + fk.getReferencedRelation().getID().getSQLRendering(); return String.format("SELECT %s FROM %s WHERE %s", Joiner.on(", ").join(columns), tables, Joiner.on(" AND ").join(conditions)); }
/*** * Definition reference triple: an RDF triple with: * <p/> * subject: the row node for the row. * predicate: the reference property IRI for the columns. * object: the row node for the referenced row. * * @param fk * @return */ private String getRefSQL(ForeignKeyConstraint fk) { Set<Object> columns = new LinkedHashSet<>(); // Set avoids duplicated and LinkedHashSet keeps the insertion order for (Attribute attr : getIdentifyingAttributes(fk.getRelation())) columns.add(getColumnNameWithAlias(attr)); List<String> conditions = new ArrayList<>(fk.getComponents().size()); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { columns.add(getColumnNameWithAlias(comp.getReference())); conditions.add(getColumnName(comp.getAttribute()) + " = " + getColumnName(comp.getReference())); } for (Attribute attr : getIdentifyingAttributes(fk.getReferencedRelation())) columns.add(getColumnNameWithAlias(attr)); final String tables = fk.getRelation().getID().getSQLRendering() + ", " + fk.getReferencedRelation().getID().getSQLRendering(); return String.format("SELECT %s FROM %s WHERE %s", Joiner.on(", ").join(columns), tables, Joiner.on(" AND ").join(conditions)); }
private LinearInclusionDependency getLinearInclusionDependency(ForeignKeyConstraint fk) { DatabaseRelationDefinition def = fk.getRelation(); DatabaseRelationDefinition def2 = fk.getReferencedRelation(); // create variables for the current table int len1 = def.getAttributes().size(); List<Term> terms1 = new ArrayList<>(len1); for (int i = 1; i <= len1; i++) terms1.add(termFactory.getVariable("t" + i)); // create variables for the referenced table int len2 = def2.getAttributes().size(); List<Term> terms2 = new ArrayList<>(len2); for (int i = 1; i <= len2; i++) terms2.add(termFactory.getVariable("p" + i)); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { // indexes start at 1 int pos1 = comp.getAttribute().getIndex() - 1; // current column (1) int pos2 = comp.getReference().getIndex() - 1; // referenced column (2) terms1.set(pos1, terms2.get(pos2)); } Function head = termFactory.getFunction(def2.getAtomPredicate(), terms2); Function body = termFactory.getFunction(def.getAtomPredicate(), terms1); return new LinearInclusionDependency(head, body); }
/** * adds a pair (attribute, referenced attribute) to the FK constraint * * @param attribute * @param referencedAttribute * @return */ public Builder add(Attribute attribute, Attribute referencedAttribute) { if (attribute == null) { throw new IllegalArgumentException("Missing Foreign Key column for table " + relation.getID() +" referring to primary key "+attribute+" in table " + referencedRelation.getID()); } if (referencedAttribute == null){ throw new IllegalArgumentException("Missing Primary Key column for table " + referencedRelation.getID() +" referring to foreign key "+attribute+" in table "+ relation.getID() ); } if (relation != attribute.getRelation()) throw new IllegalArgumentException("Foreign Key requires the same table in all attributes: " + relation + " -> " + referencedRelation + " (attribute " + attribute.getRelation().getID() + "." + attribute + ")"); if (referencedRelation != referencedAttribute.getRelation()) throw new IllegalArgumentException("Foreign Key requires the same table in all referenced attributes: " + relation + " -> " + referencedRelation + " (attribute " + referencedAttribute.getRelation().getID() + "." + referencedAttribute + ")"); builder.add(new Component(attribute, referencedAttribute)); return this; }
/** * adds a pair (attribute, referenced attribute) to the FK constraint * * @param attribute * @param referencedAttribute * @return */ public Builder add(Attribute attribute, Attribute referencedAttribute) { if (attribute == null) { throw new IllegalArgumentException("Missing Foreign Key column for table " + relation.getID() +" referring to primary key "+attribute+" in table " + referencedRelation.getID()); } if (referencedAttribute == null){ throw new IllegalArgumentException("Missing Primary Key column for table " + referencedRelation.getID() +" referring to foreign key "+attribute+" in table "+ relation.getID() ); } if (relation != attribute.getRelation()) throw new IllegalArgumentException("Foreign Key requires the same table in all attributes: " + relation + " -> " + referencedRelation + " (attribute " + attribute.getRelation().getID() + "." + attribute + ")"); if (referencedRelation != referencedAttribute.getRelation()) throw new IllegalArgumentException("Foreign Key requires the same table in all referenced attributes: " + relation + " -> " + referencedRelation + " (attribute " + referencedAttribute.getRelation().getID() + "." + referencedAttribute + ")"); builder.add(new Component(attribute, referencedAttribute)); return this; }
@Override public String toString() { List<String> columns = new ArrayList<>(components.size()); List<String> refColumns = new ArrayList<>(components.size()); for (Component c : components) { columns.add(c.getAttribute().getID().toString()); refColumns.add(c.getReference().getID().toString()); } StringBuilder bf = new StringBuilder(); bf.append("ALTER TABLE ").append(relation.getID().getSQLRendering()) .append(" ADD CONSTRAINT ").append(name).append(" FOREIGN KEY ("); Joiner.on(", ").appendTo(bf, columns); bf.append(") REFERENCES ").append(referencedRelation.getID().getSQLRendering()) .append(" ("); Joiner.on(", ").appendTo(bf, refColumns); bf.append(")"); return bf.toString(); } }
private ImmutableSet<Integer> extractNonMatchedRightAttributeIndexes(ImmutableCollection<UniqueConstraint> matchedUCs, ImmutableCollection<ForeignKeyConstraint> matchedFKs, int arity) { return IntStream.range(0, arity) .filter(i -> (matchedUCs.stream() .noneMatch(uc -> uc.getAttributes().stream() .anyMatch(a -> a.getIndex() == (i + 1))))) .filter(i -> (matchedFKs.stream() .noneMatch(fk -> fk.getComponents().stream() .anyMatch(c -> c.getReference().getIndex() == (i + 1))))) .boxed() .collect(ImmutableCollectors.toSet()); }
private ImmutableSet<Integer> extractNonMatchedRightAttributeIndexes(ImmutableCollection<UniqueConstraint> matchedUCs, ImmutableCollection<ForeignKeyConstraint> matchedFKs, int arity) { return IntStream.range(0, arity) .filter(i -> (matchedUCs.stream() .noneMatch(uc -> uc.getAttributes().stream() .anyMatch(a -> a.getIndex() == (i + 1))))) .filter(i -> (matchedFKs.stream() .noneMatch(fk -> fk.getComponents().stream() .anyMatch(c -> c.getReference().getIndex() == (i + 1))))) .boxed() .collect(ImmutableCollectors.toSet()); }
private boolean isFkMatching(ForeignKeyConstraint foreignKey, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return foreignKey.getComponents().stream() .allMatch(c -> leftArguments.get(c.getAttribute().getIndex() - 1) .equals(rightArguments.get(c.getReference().getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && (!c.getAttribute().canNull())); }
private String getReferencePropertyIRI(ForeignKeyConstraint fk) { List<String> attributes = new ArrayList<>(fk.getComponents().size()); for (Component component : fk.getComponents()) attributes.add(R2RMLIRISafeEncoder.encode(component.getAttribute().getID().getName())); return baseIRI + R2RMLIRISafeEncoder.encode(fk.getRelation().getID().getTableName()) + "#ref-" + Joiner.on(";").join(attributes); }
/** * private constructor (use Builder instead) * * @param name * @param components */ private ForeignKeyConstraint(String name, ImmutableList<Component> components) { this.name = name; this.components = components; this.relation = (DatabaseRelationDefinition)components.get(0).getAttribute().getRelation(); this.referencedRelation = (DatabaseRelationDefinition)components.get(0).getReference().getRelation(); }
private boolean isFkMatching(ForeignKeyConstraint foreignKey, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return foreignKey.getComponents().stream() .allMatch(c -> leftArguments.get(c.getAttribute().getIndex() - 1) .equals(rightArguments.get(c.getReference().getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && (!c.getAttribute().canNull())); }
/** * * TODO: explain */ private boolean areMatching(ExtensionalDataNode sourceDataNode, ExtensionalDataNode targetDataNode, ForeignKeyConstraint constraint) { ImmutableList<? extends VariableOrGroundTerm> sourceArguments = sourceDataNode.getProjectionAtom().getArguments(); ImmutableList<? extends VariableOrGroundTerm> targetArguments = targetDataNode.getProjectionAtom().getArguments(); return constraint.getComponents().stream() .allMatch(c -> sourceArguments.get(c.getAttribute().getIndex() - 1) .equals(targetArguments.get(c.getReference().getIndex() - 1))); }
private IRI getReferencePropertyIRI(ForeignKeyConstraint fk) { List<String> attributes = new ArrayList<>(fk.getComponents().size()); for (Component component : fk.getComponents()) attributes.add(R2RMLIRISafeEncoder.encode(component.getAttribute().getID().getName())); return rdfFactory.createIRI(baseIRI + R2RMLIRISafeEncoder.encode(fk.getRelation().getID().getTableName()) + "#ref-" + Joiner.on(";").join(attributes)); }
/** * * TODO: explain */ private boolean areMatching(DataNode sourceDataNode, DataNode targetDataNode, ForeignKeyConstraint constraint) { ImmutableList<? extends VariableOrGroundTerm> sourceArguments = sourceDataNode.getProjectionAtom().getArguments(); ImmutableList<? extends VariableOrGroundTerm> targetArguments = targetDataNode.getProjectionAtom().getArguments(); return constraint.getComponents().stream() .allMatch(c -> sourceArguments.get(c.getAttribute().getIndex() - 1) .equals(targetArguments.get(c.getReference().getIndex() - 1))); }
/** * private constructor (use Builder instead) * * @param name * @param components */ private ForeignKeyConstraint(String name, ImmutableList<Component> components) { this.name = name; this.components = components; this.relation = (DatabaseRelationDefinition)components.get(0).getAttribute().getRelation(); this.referencedRelation = (DatabaseRelationDefinition)components.get(0).getReference().getRelation(); }