/** * creates relation id from the database record (as though it is quoted) * * @param schema as is in DB (possibly null) * @param table as is in DB * @return */ public static RelationID createRelationIdFromDatabaseRecord(QuotedIDFactory idfac, String schema, String table) { // both IDs are as though they are quoted -- DB stores names as is return new RelationID(QuotedID.createIdFromDatabaseRecord(idfac, schema), QuotedID.createIdFromDatabaseRecord(idfac, table)); }
/** * * @return null if the schema name is empty or the schema name (as is, without quotation marks) */ public String getSchemaName() { return schema.getName(); }
/** * * @return null if the schema name is empty or SQL rendering of the schema name (possibly in quotation marks) */ public String getSchemaSQLRendering() { return schema.getSQLRendering(); }
public QuotedID createFromString(String s) { if (s == null) return new QuotedID(s, QuotedID.NO_QUOTATION); if (s.startsWith("\"") && s.endsWith("\"")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("`") && s.endsWith("`")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("[") && s.endsWith("]")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("'") && s.endsWith("'")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); return new QuotedID(s.toLowerCase(), QuotedID.NO_QUOTATION); }
@Override public int hashCode() { return attribute.hashCode(); }
@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(); } }
/** * * @return SQL rendering of the table name (possibly in quotation marks) */ public String getTableNameSQLRendering() { return table.getSQLRendering(); }
private QuotedID createFromString(String s) { if (s == null) return new QuotedID(s, QuotedID.NO_QUOTATION); if (s.startsWith("\"") && s.endsWith("\"")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("`") && s.endsWith("`")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("[") && s.endsWith("]")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("'") && s.endsWith("'")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); return new QuotedID(s, QuotedID.NO_QUOTATION); }
@Override public int hashCode() { return table.hashCode(); }
@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(); } }
/** * * @return table name (as is, without quotation marks) */ public String getTableName() { return table.getName(); }
/** * creates relation id from the database record (as though it is quoted) * * @param schema as is in DB (possibly null) * @param table as is in DB * @return */ public static RelationID createRelationIdFromDatabaseRecord(QuotedIDFactory idfac, String schema, String table) { // both IDs are as though they are quoted -- DB stores names as is return new RelationID(QuotedID.createIdFromDatabaseRecord(idfac, schema), QuotedID.createIdFromDatabaseRecord(idfac, table)); }
/** * * @return SQL rendering of the name (possibly with quotation marks) */ public String getSQLRendering() { String s = schema.getSQLRendering(); if (s == null) return table.getSQLRendering(); return s + "." + table.getSQLRendering(); }
private QuotedID createFromString(String s) { if (s == null) return new QuotedID(s, QuotedID.NO_QUOTATION); if (s.startsWith("\"") && s.endsWith("\"")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("`") && s.endsWith("`")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("[") && s.endsWith("]")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); if (s.startsWith("'") && s.endsWith("'")) return new QuotedID(s.substring(1, s.length() - 1), quotationString); return new QuotedID(s, QuotedID.NO_QUOTATION); }
@Override public int hashCode() { return table.hashCode(); }
@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(); } }
/** * * @return true if the relation ID contains schema */ public boolean hasSchema() { return schema.getName() != null; }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj instanceof QualifiedAttributeID) { QualifiedAttributeID other = (QualifiedAttributeID)obj; return (this.attribute.equals(other.attribute) && ((this.relation == other.relation) || ((this.relation != null) && this.relation.equals(other.relation)))); } return false; }