use of org.sql.generation.api.grammar.query.TableReferenceByName 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<>();
Stack<PropertyFunction<?>> refStack = new Stack<>();
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;
}
Aggregations