Search in sources :

Example 1 with JoinedList

use of org.hibernate.internal.util.collections.JoinedList in project hibernate-orm by hibernate.

the class UnionSubclassEntityPersister method generateSubquery.

protected String generateSubquery(PersistentClass model, Metadata mapping) {
    Dialect dialect = getFactory().getJdbcServices().getDialect();
    SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
    if (!model.hasSubclasses()) {
        return model.getTable().getQualifiedName(sqlStringGenerationContext);
    }
    Set<Column> columns = new LinkedHashSet<>();
    for (Table table : model.getSubclassTableClosure()) {
        if (!table.isAbstractUnionTable()) {
            columns.addAll(table.getColumns());
        }
    }
    StringBuilder buf = new StringBuilder().append("( ");
    List<PersistentClass> classes = new JoinedList<>(List.of(model), Collections.unmodifiableList(model.getSubclasses()));
    for (PersistentClass clazz : classes) {
        Table table = clazz.getTable();
        if (!table.isAbstractUnionTable()) {
            // TODO: move to .sql package!!
            buf.append("select ");
            for (Column col : columns) {
                if (!table.containsColumn(col)) {
                    int sqlType = col.getSqlTypeCode(mapping);
                    buf.append(dialect.getSelectClauseNullString(sqlType)).append(" as ");
                }
                buf.append(col.getQuotedName(dialect));
                buf.append(", ");
            }
            buf.append(clazz.getSubclassId()).append(" as clazz_");
            buf.append(" from ").append(table.getQualifiedName(sqlStringGenerationContext));
            buf.append(" union ");
            if (dialect.supportsUnionAll()) {
                buf.append("all ");
            }
        }
    }
    if (buf.length() > 2) {
        // chop the last union (all)
        buf.setLength(buf.length() - (dialect.supportsUnionAll() ? 11 : 7));
    }
    return buf.append(" )").toString();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) Table(org.hibernate.mapping.Table) Column(org.hibernate.mapping.Column) Dialect(org.hibernate.dialect.Dialect) JoinedList(org.hibernate.internal.util.collections.JoinedList) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)1 SqlStringGenerationContext (org.hibernate.boot.model.relational.SqlStringGenerationContext)1 Dialect (org.hibernate.dialect.Dialect)1 JoinedList (org.hibernate.internal.util.collections.JoinedList)1 Column (org.hibernate.mapping.Column)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 Table (org.hibernate.mapping.Table)1