use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.
the class SqlGenerator method fromFragmentSeparator.
@Override
protected void fromFragmentSeparator(AST a) {
// check two "adjecent" nodes at the top of the from-clause tree
AST next = a.getNextSibling();
if (next == null || !hasText(a)) {
return;
}
FromElement left = (FromElement) a;
FromElement right = (FromElement) next;
// writes something to the SQL
while (right != null && !hasText(right)) {
right = (FromElement) right.getNextSibling();
}
if (right == null) {
return;
}
if (!hasText(right)) {
return;
}
if (right.getType() == ENTITY_JOIN) {
out(" ");
} else if (right.getRealOrigin() == left || (right.getRealOrigin() != null && right.getRealOrigin() == left.getRealOrigin())) {
// both right and left reprersent joins originating from the same FromElement
if (right.getJoinSequence() != null && right.getJoinSequence().isThetaStyle()) {
writeCrossJoinSeparator();
} else {
out(" ");
}
} else {
// these are just two unrelated table references
writeCrossJoinSeparator();
}
}
use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.
the class HqlSqlWalker method isNonQualifiedPropertyRef.
@Override
protected boolean isNonQualifiedPropertyRef(AST ident) {
final String identText = ident.getText();
if (currentFromClause.isFromElementAlias(identText)) {
return false;
}
List fromElements = currentFromClause.getExplicitFromElements();
if (fromElements.size() == 1) {
final FromElement fromElement = (FromElement) fromElements.get(0);
try {
LOG.tracev("Attempting to resolve property [{0}] as a non-qualified ref", identText);
return fromElement.getPropertyMapping(identText).toType(identText) != null;
} catch (QueryException e) {
// Should mean that no such property was found
}
}
return false;
}
use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.
the class HqlSqlWalker method lookupNonQualifiedProperty.
@Override
protected AST lookupNonQualifiedProperty(AST property) throws SemanticException {
final FromElement fromElement = (FromElement) currentFromClause.getExplicitFromElements().get(0);
AST syntheticDotNode = generateSyntheticDotNodeForNonQualifiedPropertyRef(property, fromElement);
return lookupProperty(syntheticDotNode, false, getCurrentClauseType() == HqlSqlTokenTypes.SELECT);
}
use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.
the class HqlSqlWalker method createFromFilterElement.
@Override
protected AST createFromFilterElement(AST filterEntity, AST alias) throws SemanticException {
FromElement fromElement = currentFromClause.addFromElement(filterEntity.getText(), alias);
FromClause fromClause = fromElement.getFromClause();
QueryableCollection persister = sessionFactoryHelper.getCollectionPersister(collectionFilterRole);
// Get the names of the columns used to link between the collection
// owner and the collection elements.
String[] keyColumnNames = persister.getKeyColumnNames();
String fkTableAlias = persister.isOneToMany() ? fromElement.getTableAlias() : fromClause.getAliasGenerator().createName(collectionFilterRole);
JoinSequence join = sessionFactoryHelper.createJoinSequence();
join.setRoot(persister, fkTableAlias);
if (!persister.isOneToMany()) {
join.addJoin((AssociationType) persister.getElementType(), fromElement.getTableAlias(), JoinType.INNER_JOIN, persister.getElementColumnNames(fkTableAlias));
}
join.addCondition(fkTableAlias, keyColumnNames, " = ?");
fromElement.setJoinSequence(join);
fromElement.setFilter(true);
LOG.debug("createFromFilterElement() : processed filter FROM element.");
return fromElement;
}
use of org.hibernate.hql.internal.ast.tree.FromElement in project hibernate-orm by hibernate.
the class EntityGraphQueryHint method getFromElements.
private List<FromElement> getFromElements(List attributeNodes, FromElement origin, FromClause fromClause, HqlSqlWalker walker, Map<String, FromElement> explicitFetches) {
final List<FromElement> fromElements = new ArrayList<FromElement>();
for (Object obj : attributeNodes) {
final AttributeNode<?> attributeNode = (AttributeNode<?>) obj;
final String attributeName = attributeNode.getAttributeName();
final String className = origin.getClassName();
// TODO: This is ignored by collection types and probably wrong for entity types. Presumably it screws
// with inheritance.
final String role = className + "." + attributeName;
final String classAlias = origin.getClassAlias();
final String originTableAlias = origin.getTableAlias();
final Type propertyType = origin.getPropertyType(attributeName, attributeName);
try {
FromElement fromElement = explicitFetches.get(role);
boolean explicitFromElement = false;
if (fromElement == null) {
if (propertyType.isEntityType()) {
final EntityType entityType = (EntityType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final String tableAlias = walker.getAliasGenerator().createName(entityType.getAssociatedEntityName());
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final JoinSequence joinSequence = walker.getSessionFactoryHelper().createJoinSequence(false, entityType, tableAlias, JoinType.LEFT_OUTER_JOIN, columns);
fromElement = fromElementFactory.createEntityJoin(entityType.getAssociatedEntityName(), tableAlias, joinSequence, true, walker.isInFrom(), entityType, role, null);
} else if (propertyType.isCollectionType()) {
CollectionType collectionType = (CollectionType) propertyType;
final String[] columns = origin.toColumns(originTableAlias, attributeName, false);
final FromElementFactory fromElementFactory = new FromElementFactory(fromClause, origin, attributeName, classAlias, columns, false);
final QueryableCollection queryableCollection = walker.getSessionFactoryHelper().requireQueryableCollection(collectionType.getRole());
fromElement = fromElementFactory.createCollection(queryableCollection, collectionType.getRole(), JoinType.LEFT_OUTER_JOIN, true, false);
}
} else {
explicitFromElement = true;
fromElement.setInProjectionList(true);
fromElement.setFetch(true);
}
if (fromElement != null) {
if (!explicitFromElement) {
fromElements.add(fromElement);
}
// recurse into subgraphs
for (Subgraph<?> subgraph : attributeNode.getSubgraphs().values()) {
fromElements.addAll(getFromElements(subgraph.getAttributeNodes(), fromElement, fromClause, walker, explicitFetches));
}
}
} catch (Exception e) {
throw new QueryException("Could not apply the EntityGraph to the Query!", e);
}
}
return fromElements;
}
Aggregations