@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 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(); }
public String getSQL(DatabaseRelationDefinition table) { return String.format("SELECT * FROM %s", table.getID().getSQLRendering()); }
public String getSQL(DatabaseRelationDefinition table) { return String.format("SELECT * FROM %s", table.getID().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(); } }
@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(); } }
/** * 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; }
/** * creates a new attribute * * @param id * @param type * @param typeName * @param canNull */ public Attribute addAttribute(QuotedID id, int type, String typeName, boolean canNull) { Attribute att = new Attribute(this, new QualifiedAttributeID(getID(), id), attributes.size() + 1, type, typeName, canNull); //check for duplicate names (put returns the previous value) Attribute prev = attributeMap.put(id, att); if (prev != null) throw new IllegalArgumentException("Duplicate attribute names"); attributes.add(att); return att; }
/** * creates a new attribute * * @param id * @param type * @param typeName * @param canNull */ public Attribute addAttribute(QuotedID id, int type, String typeName, boolean canNull) { Attribute att = new Attribute(this, new QualifiedAttributeID(getID(), id), attributes.size() + 1, type, typeName, canNull, typeMapper.getTermType(type, typeName)); //check for duplicate names (put returns the previous value) Attribute prev = attributeMap.put(id, att); if (prev != null) throw new IllegalArgumentException("Duplicate attribute names"); attributes.add(att); return att; }
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 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)); }
/** * Retrieves the primary key for the table * */ private static void getPrimaryKey(DatabaseMetaData md, DatabaseRelationDefinition relation, QuotedIDFactory idfac) throws SQLException { RelationID id = relation.getID(); // Retrieves a description of the given table's primary key columns. They are ordered by COLUMN_NAME (sic!) try (ResultSet rs = md.getPrimaryKeys(null, id.getSchemaName(), id.getTableName())) { extractPrimaryKey(relation, idfac, id, rs); } catch (SQLSyntaxErrorException e) { // WORKAROUND for MySQL connector >= 8.0: // <https://github.com/ontop/ontop/issues/270> try (ResultSet rs = md.getPrimaryKeys(id.getSchemaName(), null, id.getTableName())) { extractPrimaryKey(relation, idfac, id, rs); } } }
/*** * 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)); }
/** * Retrieves the unique attributes(s) * @param md * @return * @throws SQLException */ private static void getUniqueAttributes(DatabaseMetaData md, DatabaseRelationDefinition relation, QuotedIDFactory idfac) throws SQLException { RelationID id = relation.getID(); // extracting unique try (ResultSet rs = md.getIndexInfo(null, id.getSchemaName(), id.getTableName(), true, true)) { extractUniqueAttributes(relation, idfac, rs); } catch (Exception e){ // Workaround for MySQL-connector >= 8.0 try (ResultSet rs = md.getIndexInfo(id.getSchemaName(),null, id.getTableName(), true, true)) { extractUniqueAttributes(relation, idfac, rs); } } }
@Override public void visit(Table tableName) { RelationID id = idfac.createRelationID(tableName.getSchemaName(), tableName.getName()); // construct the predicate using the table name DatabaseRelationDefinition relation = metadata.getDatabaseRelation(id); if (relation == null) throw new InvalidSelectQueryRuntimeException("Table " + id + " not found in metadata", tableName); relationIndex++; RelationID alias = (tableName.getAlias() != null) ? idfac.createRelationID(null, tableName.getAlias().getName()) : relation.getID(); ImmutableMap<QuotedID, Term> attributes = relation.getAttributes().stream() .collect(ImmutableCollectors.toMap(Attribute::getID, attribute -> createVariable(attribute.getID()))); // DEFAULT SCHEMA // TODO: to be improved if ((tableName.getAlias() == null) && relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) result = RAExpressionAttributes.create(attributes, alias, relation.getID().getSchemalessID()); else result = RAExpressionAttributes.create(attributes, alias); }
@Override public void visit(Table tableName) { RelationID id = idfac.createRelationID(tableName.getSchemaName(), tableName.getName()); // construct the predicate using the table name DatabaseRelationDefinition relation = metadata.getDatabaseRelation(id); if (relation == null) throw new InvalidSelectQueryRuntimeException("Table " + id + " not found in metadata", tableName); relationIndex++; RelationID alias = (tableName.getAlias() != null) ? idfac.createRelationID(null, tableName.getAlias().getName()) : relation.getID(); ImmutableMap<QuotedID, Term> attributes = relation.getAttributes().stream() .collect(ImmutableCollectors.toMap(Attribute::getID, attribute -> createVariable(attribute.getID()))); // DEFAULT SCHEMA // TODO: to be improved if ((tableName.getAlias() == null) && relation.getID().getSchemaName() != null && metadata.getDatabaseRelation(relation.getID().getSchemalessID()).equals(relation)) result = RAExpressionAttributes.create(attributes, alias, relation.getID().getSchemalessID()); else result = RAExpressionAttributes.create(attributes, alias); }
/** * 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(); }
/** * 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(); }
/** * Retrieves the foreign keys for the table * */ private static void getForeignKeys(DatabaseMetaData md, DatabaseRelationDefinition relation, DBMetadata metadata) throws SQLException { QuotedIDFactory idfac = metadata.getQuotedIDFactory(); RelationID relationId = relation.getID(); try (ResultSet rs = md.getImportedKeys(null, relationId.getSchemaName(), relationId.getTableName())) { extractForeignKeys(relation, metadata, idfac, rs); } catch (Exception ex) { try (ResultSet rs = md.getImportedKeys(relationId.getSchemaName(),null, relationId.getTableName())) { extractForeignKeys(relation, metadata, idfac, rs); } } }