private String generateSQLString(RelationDefinition table) { StringBuilder sb = new StringBuilder("select"); boolean needComma = false; for (Attribute attr : table.getAttributes()) { if (needComma) { sb.append(","); } sb.append(" "); sb.append(attr.getID()); needComma = true; } sb.append(" "); sb.append("from"); sb.append(" "); sb.append(table.getID()); return sb.toString(); }
@Override public Optional<Predicate.COL_TYPE> getColType(Attribute attribute) { return Optional.of(jdbcTypeMapper.getPredicate(attribute.getType())); }
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 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()); }
@Override public String toString() { List<String> columns = new ArrayList<>(attributes.size()); for (Attribute c : attributes) columns.add(c.getID().toString()); StringBuilder bf = new StringBuilder(); bf.append("ALTER TABLE ").append(attributes.get(0).getRelation().getID()) .append(" ADD CONSTRAINT ").append(name).append(isPK ? " PRIMARY KEY " : " UNIQUE ") .append("("); Joiner.on(", ").appendTo(bf, columns); bf.append(")"); return bf.toString(); } }
/** * 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<ImmutableFunctionalTerm> getCQ(DatabaseRelationDefinition table) { ImmutableList.Builder<ImmutableFunctionalTerm> atoms = ImmutableList.builder(); //Class Atom ImmutableTerm sub = generateSubject(table, false); atoms.add(df.getImmutableFunctionalTerm(df.getClassPredicate(getTableIRI(table.getID())), sub)); //DataType Atoms for (Attribute att : table.getAttributes()) { Predicate.COL_TYPE type = typeMapper.getPredicate(att.getType()); Variable objV = df.getVariable(att.getID().getName()); ImmutableTerm obj = df.getImmutableTypedTerm(objV, type); atoms.add(df.getImmutableFunctionalTerm(df.getDataPropertyPredicate(getLiteralPropertyIRI(att)), sub, obj)); } return atoms.build(); }
/** * 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(); }
Optional<RDFDatatype> type; if (attribute.getType() == 0){ type = Optional.of((RDFDatatype) attribute.getTermType());
/** * return the database relation for the unique constraint * * @return */ public DatabaseRelationDefinition getRelation() { return (DatabaseRelationDefinition)attributes.get(0).getRelation(); }
defaultTypeBuilder.add(attribute.getTermType());
/** * * @param name * @param statement */ public ParserViewDefinition(RelationID name, ImmutableList<QuotedID> attrs, String statement) { super(name); this.statement = statement; ImmutableList.Builder<Attribute> attributeBuilder = ImmutableList.builder(); ImmutableMap.Builder<QuotedID, Attribute> attributeMapBuilder = ImmutableMap.builder(); int c = 1; for (QuotedID id : attrs) { Attribute att = new Attribute(this, new QualifiedAttributeID(name, id), c, 0, null, true); c++; attributeMapBuilder.put(id, att); attributeBuilder.add(att); } this.attributes = attributeBuilder.build(); this.attributeMap = attributeMapBuilder.build(); }
private static String getColumnName(Attribute attr) { return attr.getQualifiedID().getSQLRendering(); }
@Override public VariableNullability getVariableNullability() { if (variableNullability == null) { DataAtom<RelationPredicate> atom = getProjectionAtom(); RelationDefinition relation = atom.getPredicate().getRelationDefinition(); ImmutableList<? extends VariableOrGroundTerm> arguments = atom.getArguments(); ImmutableMultiset<? extends VariableOrGroundTerm> argMultiset = ImmutableMultiset.copyOf(arguments); // NB: DB column indexes start at 1. ImmutableSet<ImmutableSet<Variable>> nullableGroups = IntStream.range(0, arguments.size()) .filter(i -> arguments.get(i) instanceof Variable) .filter(i -> relation.getAttribute(i + 1).canNull()) .mapToObj(arguments::get) .map(a -> (Variable) a) // An implicit filter condition makes them non-nullable .filter(a -> argMultiset.count(a) < 2) .map(ImmutableSet::of) .collect(ImmutableCollectors.toSet()); variableNullability = new VariableNullabilityImpl(nullableGroups); } return variableNullability; }
@Override public String toString() { List<String> columns = new ArrayList<>(attributes.size()); for (Attribute c : attributes) columns.add(c.getID().toString()); StringBuilder bf = new StringBuilder(); bf.append("ALTER TABLE ").append(attributes.get(0).getRelation().getID()) .append(" ADD CONSTRAINT ").append(name).append(isPK ? " PRIMARY KEY " : " UNIQUE ") .append("("); Joiner.on(", ").appendTo(bf, columns); bf.append(")"); return bf.toString(); } }
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()); }
/** * return the database relation for the unique constraint * * @return */ public DatabaseRelationDefinition getRelation() { return (DatabaseRelationDefinition)attributes.get(0).getRelation(); }
/** * @param name * @param statement * @param defaultType */ public ParserViewDefinition(RelationID name, ImmutableList<QuotedID> attrs, String statement, TermType defaultType) { super(name); this.statement = statement; ImmutableList.Builder<Attribute> attributeBuilder = ImmutableList.builder(); ImmutableMap.Builder<QuotedID, Attribute> attributeMapBuilder = ImmutableMap.builder(); int c = 1; for (QuotedID id : attrs) { Attribute att = new Attribute(this, new QualifiedAttributeID(name, id), c, 0, null, true, // TODO: make sure a type is assigned defaultType); c++; attributeMapBuilder.put(id, att); attributeBuilder.add(att); } this.attributes = attributeBuilder.build(); this.attributeMap = attributeMapBuilder.build(); }
private static String getColumnName(Attribute attr) { return attr.getQualifiedID().getSQLRendering(); }
@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(); } }