@Override public String toString() { StringBuilder bf = new StringBuilder(); bf.append("CREATE TABLE ").append(getID()).append(" (\n "); Joiner.on(",\n ").appendTo(bf, attributes); bf.append("\n)"); return bf.toString(); }
@Override public ImmutableSet<Attribute> getDependents() { return getRelation().getAttributes().stream() .filter(a -> !attributes.contains(a)) .collect(ImmutableCollectors.toSet()); }
private Stream<ForeignKeyConstraint> extractMatchedFKsForARelation( DatabaseRelationDefinition leftRelation, Collection<ImmutableList<? extends VariableOrGroundTerm>> leftArgumentLists, ImmutableList<? extends VariableOrGroundTerm> rightArguments, DatabaseRelationDefinition rightRelation) { return leftRelation.getForeignKeys().stream() .filter(fk -> fk.getReferencedRelation().equals(rightRelation)) .filter(fk -> leftArgumentLists.stream() .anyMatch(leftArguments -> isFkMatching(fk, leftArguments, rightArguments))); }
private static List<Attribute> getIdentifyingAttributes(DatabaseRelationDefinition table) { UniqueConstraint pk = table.getPrimaryKey(); if (pk != null) return pk.getAttributes(); else return table.getAttributes(); }
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)); }
@Override public String printKeys() { StringBuilder builder = new StringBuilder(); Collection<DatabaseRelationDefinition> table_list = getDatabaseRelations(); // Prints all primary keys builder.append("\n====== Unique constraints ==========\n"); for (DatabaseRelationDefinition dd : table_list) { builder.append(dd + ";\n"); for (UniqueConstraint uc : dd.getUniqueConstraints()) builder.append(uc + ";\n"); builder.append("\n"); } // Prints all foreign keys builder.append("====== Foreign key constraints ==========\n"); for(DatabaseRelationDefinition dd : table_list) { for (ForeignKeyConstraint fk : dd.getForeignKeys()) builder.append(fk + ";\n"); } return builder.toString(); }
: relation.getID(); List<Term> terms = new ArrayList<>(relation.getAttributes().size()); ImmutableMap.Builder attributes = ImmutableMap.<QuotedID, Variable>builder(); relation.getAttributes().forEach(attribute -> { QuotedID attributeId = attribute.getID(); Variable var = TERM_FACTORY.getVariable(attributeId.getName() + relationIndex); relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) attrs = RAExpressionAttributes.create(attributes.build(), alias, relation.getID().getSchemalessID()); else attrs = RAExpressionAttributes.create(attributes.build(), alias);
RelationID id = relation.getID(); 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));
varNamePrefix = td.getID().getTableName() + "_"; UniqueConstraint pk = td.getPrimaryKey(); if (pk != null) { List<ImmutableTerm> terms = new ArrayList<>(pk.getAttributes().size() + 1); attributes.add(R2RMLIRISafeEncoder.encode(att.getID().getName()) + "={}"); String template = baseIRI + R2RMLIRISafeEncoder.encode(td.getID().getTableName()) + "/" + Joiner.on(";").join(attributes); terms.add(termFactory.getConstantLiteral(template)); List<ImmutableTerm> vars = new ArrayList<>(td.getAttributes().size()); for (Attribute att : td.getAttributes()) vars.add(termFactory.getVariable(varNamePrefix + att.getID().getName()));
: relation.getID(); List<Term> terms = new ArrayList<>(relation.getAttributes().size()); ImmutableMap.Builder attributes = ImmutableMap.<QuotedID, Variable>builder(); relation.getAttributes().forEach(attribute -> { QuotedID attributeId = attribute.getID(); Variable var = termFactory.getVariable(attributeId.getName() + relationIndex); }); Function atom = termFactory.getFunction(relation.getAtomPredicate(), terms); relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) attrs = RAExpressionAttributes.create(attributes.build(), alias, relation.getID().getSchemalessID()); else attrs = RAExpressionAttributes.create(attributes.build(), alias);
RelationID relationId = relation.getID(); try (ResultSet rs = md.getImportedKeys(null, relationId.getSchemaName(), relationId.getTableName())) { ForeignKeyConstraint.Builder builder = null; if (seq == 1) { if (builder != null) relation.addForeignKeyConstraint(builder.build(currentName)); QuotedID attrId = QuotedID.createIdFromDatabaseRecord(idfac, rs.getString("FKCOLUMN_NAME")); QuotedID refAttrId = QuotedID.createIdFromDatabaseRecord(idfac, rs.getString("PKCOLUMN_NAME")); builder.add(relation.getAttribute(attrId), ref.getAttribute(refAttrId)); relation.addForeignKeyConstraint(builder.build(currentName));
for (ForeignKeyConstraint fks : def.getForeignKeys()) { int len1 = def.getAttributes().size(); List<Term> terms1 = new ArrayList<>(len1); for (int i = 1; i <= len1; i++) int len2 = def2.getAttributes().size(); List<Term> terms2 = new ArrayList<>(len2); for (int i = 1; i <= len2; i++)
System.out.println(" " + relationId + "." + attributeId); if (currentRelation == null || !currentRelation.getID().equals(relationId)) { int dataType = dt.getCorrectedDatatype(rs.getInt("DATA_TYPE"), typeName); currentRelation.addAttribute(attributeId, dataType, typeName, isNullable);
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); }
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)); } }
if (seq == 1) { if (builder != null) relation.addForeignKeyConstraint(builder.build(currentName)); QuotedID attrId = QuotedID.createIdFromDatabaseRecord(idfac, rs.getString("FKCOLUMN_NAME")); QuotedID refAttrId = QuotedID.createIdFromDatabaseRecord(idfac, rs.getString("PKCOLUMN_NAME")); builder.add(relation.getAttribute(attrId), ref.getAttribute(refAttrId)); relation.addForeignKeyConstraint(builder.build(currentName));
public void addFunctionalDependency(FunctionalDependency constraint) { if (constraint instanceof UniqueConstraint) addUniqueConstraint((UniqueConstraint) constraint); else otherFunctionalDependencies.add(constraint); }
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)); }
/** * Definition row graph: an RDF graph consisting of the following triples: * <p/> * - the row type triple. * - a literal triple for each column in a table where the column value is non-NULL. * */ public ImmutableList<TargetAtom> getCQ(DatabaseRelationDefinition table) { ImmutableList.Builder<TargetAtom> atoms = ImmutableList.builder(); //Class Atom ImmutableTerm sub = generateSubject(table, false); atoms.add(getAtom(getTableIRI(table.getID()), sub)); //DataType Atoms for (Attribute att : table.getAttributes()) { // TODO: revisit this RDFDatatype type = (RDFDatatype) att.getTermType(); Variable objV = termFactory.getVariable(att.getID().getName()); ImmutableTerm obj = termFactory.getImmutableTypedTerm(objV, type); atoms.add(getAtom(getLiteralPropertyIRI(att), sub, obj)); } return atoms.build(); }
RelationID id = relation.getID(); relation.addUniqueConstraint(builder.build(currentName, false)); relation.addUniqueConstraint(builder.build(currentName, false)); Attribute attr = relation.getAttribute(attrId); if (attr == null) { // Compensate for the bug in PostgreSQL JBDC driver that attr = relation.getAttribute(attrId); relation.addUniqueConstraint(builder.build(currentName, false));