private boolean isUcMatching(UniqueConstraint uniqueConstraint, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return uniqueConstraint.getAttributes().stream() .allMatch(a -> leftArguments.get(a.getIndex() -1) .equals(rightArguments.get(a.getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && !a.canNull()); }
/** * builds a UNIQUE constraint (this includes PRIMARY KEY) * * @param name * @return null if the list of attributes is empty */ public UniqueConstraint build(String name, boolean isPK) { ImmutableList<Attribute> attributes = builder.build(); if (attributes.isEmpty()) return null; return new UniqueConstraint(name, isPK, builder.build()); } }
@Override public ImmutableSet<Attribute> getDependents() { return getRelation().getAttributes().stream() .filter(a -> !attributes.contains(a)) .collect(ImmutableCollectors.toSet()); }
/** * adds a unique constraint (a primary key or a unique constraint proper) * * @param uc */ public void addUniqueConstraint(UniqueConstraint uc) { if (uc.isPrimaryKey()) { if (pk != null) throw new IllegalArgumentException("Duplicate PK " + pk + " " + uc); pk = uc; } else { if (pk != null) if (uc.getAttributes().equals(pk.getAttributes())) // ignore the unique index created for the primary key return; } ucs.add(uc); }
private static void extractPrimaryKey(DatabaseRelationDefinition relation, QuotedIDFactory idfac, RelationID id, ResultSet rs) throws SQLException { Map<Integer, String> primaryKeyAttributes = new HashMap<>(); String currentName = null; while (rs.next()) { // TABLE_CAT is ignored for now; assume here that relation has a fully specified name RelationID id2 = RelationID.createRelationIdFromDatabaseRecord(idfac, rs.getString("TABLE_SCHEM"), rs.getString("TABLE_NAME")); if (id2.equals(id)) { currentName = rs.getString("PK_NAME"); // may be null String attr = rs.getString("COLUMN_NAME"); int seq = rs.getShort("KEY_SEQ"); primaryKeyAttributes.put(seq, attr); } } if (!primaryKeyAttributes.isEmpty()) { // use the KEY_SEQ values to restore the correct order of attributes in the PK UniqueConstraint.Builder builder = UniqueConstraint.builder(relation); for (int i = 1; i <= primaryKeyAttributes.size(); i++) { QuotedID attrId = QuotedID.createIdFromDatabaseRecord(idfac, primaryKeyAttributes.get(i)); builder.add(relation.getAttribute(attrId)); } relation.addUniqueConstraint(builder.build(currentName, true)); } }
/** * adds a unique constraint (a primary key or a unique constraint proper) * * @param uc */ public void addUniqueConstraint(UniqueConstraint uc) { if (uc.isPrimaryKey()) { if (pk != null) throw new IllegalArgumentException("Duplicate PK " + pk + " " + uc); pk = uc; } else { if (pk != null) if (uc.getAttributes().equals(pk.getAttributes())) // ignore the unique index created for the primary key return; } ucs.add(uc); }
UniqueConstraint.Builder builder = UniqueConstraint.builder(relation); for (int i = 1; i <= primaryKeyAttributes.size(); i++) { QuotedID attrId = QuotedID.createIdFromDatabaseRecord(idfac, primaryKeyAttributes.get(i));
private Stream<Map.Entry<RelationPredicate, ImmutableList<Integer>>> extractUniqueConstraintsFromRelation( DatabaseRelationDefinition relation) { return relation.getUniqueConstraints().stream() .map(uc -> uc.getAttributes().stream() .map(Attribute::getIndex) .collect(ImmutableCollectors.toList())) .map(positions -> new AbstractMap.SimpleEntry<>(relation.getAtomPredicate(), positions)); }
builder = UniqueConstraint.builder(relation); currentName = rs.getString("INDEX_NAME");
@Override public ImmutableSet<Attribute> getDependents() { return getRelation().getAttributes().stream() .filter(a -> !attributes.contains(a)) .collect(ImmutableCollectors.toSet()); }
/** * builds a UNIQUE constraint (this includes PRIMARY KEY) * * @param name * @return null if the list of attributes is empty */ public UniqueConstraint build(String name, boolean isPK) { ImmutableList<Attribute> attributes = builder.build(); if (attributes.isEmpty()) return null; return new UniqueConstraint(name, isPK, builder.build()); } }
private boolean isUcMatching(UniqueConstraint uniqueConstraint, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return uniqueConstraint.getAttributes().stream() .allMatch(a -> leftArguments.get(a.getIndex() -1) .equals(rightArguments.get(a.getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && !a.canNull()); }
builder = UniqueConstraint.builder(relation); currentName = rs.getString("INDEX_NAME");
private Stream<Map.Entry<AtomPredicate, ImmutableList<Integer>>> extractUniqueConstraintsFromRelation( DatabaseRelationDefinition relation, Map<Predicate, AtomPredicate> predicateCache) { Predicate originalPredicate = Relation2Predicate.createPredicateFromRelation(relation); AtomPredicate atomPredicate = convertToAtomPredicate(originalPredicate, predicateCache); return relation.getUniqueConstraints().stream() .map(uc -> uc.getAttributes().stream() .map(Attribute::getIndex) .collect(ImmutableCollectors.toList())) .map(positions -> new AbstractMap.SimpleEntry<>(atomPredicate, positions)); }
continue; UniqueConstraint.Builder builder = UniqueConstraint.builder(td); String[] attrs = uc[1].split(","); for (String attr : attrs) {
private static List<Attribute> getIdentifyingAttributes(DatabaseRelationDefinition table) { UniqueConstraint pk = table.getPrimaryKey(); if (pk != null) return pk.getAttributes(); else return table.getAttributes(); }
continue; UniqueConstraint.Builder builder = UniqueConstraint.builder(td); String[] attrs = uc[1].split(","); for (String attr : attrs) {
private static List<Attribute> getIdentifyingAttributes(DatabaseRelationDefinition table) { UniqueConstraint pk = table.getPrimaryKey(); if (pk != null) return pk.getAttributes(); else return table.getAttributes(); }
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()); }