Search in sources :

Example 1 with PropertyFunction

use of org.qi4j.api.query.grammar.PropertyFunction in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method traversePropertyPath.

protected Integer traversePropertyPath(PropertyFunction<?> reference, Integer lastTableIndex, Integer nextAvailableIndex, SQLVendor vendor, TableReferenceBuilder builder, JoinType joinStyle) {
    Stack<QualifiedName> qNameStack = new Stack<QualifiedName>();
    Stack<PropertyFunction<?>> refStack = new Stack<PropertyFunction<?>>();
    while (reference != null) {
        qNameStack.add(QualifiedName.fromAccessor(reference.accessor()));
        refStack.add(reference);
        if (reference.traversedProperty() == null && (reference.traversedAssociation() != null || reference.traversedManyAssociation() != null)) {
            Integer lastAssoTableIndex = this.traverseAssociationPath(new TraversedAssoOrManyAssoRef(reference), lastTableIndex, nextAvailableIndex, vendor, builder, joinStyle, true);
            if (lastAssoTableIndex > lastTableIndex) {
                lastTableIndex = lastAssoTableIndex;
                nextAvailableIndex = lastTableIndex + 1;
            }
        }
        reference = reference.traversedProperty();
    }
    PropertyFunction<?> prevRef = null;
    String schemaName = this._state.schemaName().get();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    BooleanFactory b = vendor.getBooleanFactory();
    ColumnsFactory c = vendor.getColumnsFactory();
    while (!qNameStack.isEmpty()) {
        QualifiedName qName = qNameStack.pop();
        PropertyFunction<?> ref = refStack.pop();
        if (!qName.type().equals(Identity.class.getName())) {
            QNameInfo info = this._state.qNameInfos().get().get(qName);
            if (info == null) {
                throw new InternalError("No qName info found for qName [" + qName + "].");
            }
            String prevTableAlias = TABLE_NAME_PREFIX + lastTableIndex;
            String nextTableAlias = TABLE_NAME_PREFIX + nextAvailableIndex;
            TableReferenceByName nextTable = t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(nextTableAlias));
            // @formatter:off
            if (prevRef == null) {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).and(b.isNull(c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).createExpression()));
            } else {
                builder.addQualifiedJoin(joinStyle, nextTable, t.jc(b.booleanBuilder(b.eq(c.colName(prevTableAlias, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).and(b.eq(c.colName(prevTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableAlias, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).createExpression()));
            }
            // @formatter:on
            lastTableIndex = nextAvailableIndex;
            ++nextAvailableIndex;
            prevRef = ref;
        }
    }
    return lastTableIndex;
}
Also used : TableReferenceByName(org.sql.generation.api.grammar.query.TableReferenceByName) QualifiedName(org.qi4j.api.common.QualifiedName) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) Stack(java.util.Stack) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) PropertyFunction(org.qi4j.api.query.grammar.PropertyFunction) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo)

Aggregations

Stack (java.util.Stack)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 PropertyFunction (org.qi4j.api.query.grammar.PropertyFunction)1 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)1 BooleanFactory (org.sql.generation.api.grammar.factories.BooleanFactory)1 ColumnsFactory (org.sql.generation.api.grammar.factories.ColumnsFactory)1 TableReferenceFactory (org.sql.generation.api.grammar.factories.TableReferenceFactory)1 TableReferenceByName (org.sql.generation.api.grammar.query.TableReferenceByName)1