use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class EmbeddedCollectionPart method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final List<Expression> expressions = new ArrayList<>();
getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
assert containingTableExpression.equals(selection.getContainingTableExpression());
final TableReference tableReference = tableGroup.resolveTableReference(tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName()), selection.getContainingTableExpression());
expressions.add(sqlExpressionResolver.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, sqlAstCreationState.getCreationContext().getSessionFactory())));
});
return new SqlTuple(expressions, this);
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class EmbeddedForeignKeyDescriptor method isSimpleJoinPredicate.
@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
if (!(predicate instanceof Junction)) {
return false;
}
final Junction junction = (Junction) predicate;
if (junction.getNature() != Junction.Nature.CONJUNCTION) {
return false;
}
final List<Predicate> predicates = junction.getPredicates();
if (predicates.size() != keySelectableMappings.getJdbcTypeCount()) {
return false;
}
Boolean lhsIsKey = null;
for (int i = 0; i < predicates.size(); i++) {
final Predicate p = predicates.get(i);
if (!(p instanceof ComparisonPredicate)) {
return false;
}
final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) p;
if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
return false;
}
final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
return false;
}
final ColumnReference lhs = (ColumnReference) lhsExpr;
final ColumnReference rhs = (ColumnReference) rhsExpr;
if (lhsIsKey == null) {
final String keyExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
final String targetExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
if (keyExpression.equals(targetExpression)) {
if (!lhs.getColumnExpression().equals(keyExpression) || !rhs.getColumnExpression().equals(keyExpression)) {
return false;
}
} else {
if (keyExpression.equals(lhs.getColumnExpression())) {
if (!targetExpression.equals(rhs.getColumnExpression())) {
return false;
}
lhsIsKey = true;
} else if (keyExpression.equals(rhs.getColumnExpression())) {
if (!targetExpression.equals(lhs.getColumnExpression())) {
return false;
}
lhsIsKey = false;
} else {
return false;
}
}
} else {
final String lhsSelectionExpression;
final String rhsSelectionExpression;
if (lhsIsKey) {
lhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
rhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
} else {
lhsSelectionExpression = targetSelectableMappings.getSelectable(i).getSelectionExpression();
rhsSelectionExpression = keySelectableMappings.getSelectable(i).getSelectionExpression();
}
if (!lhs.getColumnExpression().equals(lhsSelectionExpression) || !rhs.getColumnExpression().equals(rhsSelectionExpression)) {
return false;
}
}
}
return true;
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class SimpleForeignKeyDescriptor method isSimpleJoinPredicate.
@Override
public boolean isSimpleJoinPredicate(Predicate predicate) {
if (!(predicate instanceof ComparisonPredicate)) {
return false;
}
final ComparisonPredicate comparisonPredicate = (ComparisonPredicate) predicate;
if (comparisonPredicate.getOperator() != ComparisonOperator.EQUAL) {
return false;
}
final Expression lhsExpr = comparisonPredicate.getLeftHandExpression();
final Expression rhsExpr = comparisonPredicate.getRightHandExpression();
if (!(lhsExpr instanceof ColumnReference) || !(rhsExpr instanceof ColumnReference)) {
return false;
}
final String lhs = ((ColumnReference) lhsExpr).getColumnExpression();
final String rhs = ((ColumnReference) rhsExpr).getColumnExpression();
final String keyExpression = keySide.getModelPart().getSelectionExpression();
final String targetExpression = targetSide.getModelPart().getSelectionExpression();
return (lhs.equals(keyExpression) && rhs.equals(targetExpression)) || (lhs.equals(targetExpression) && rhs.equals(keyExpression));
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class OrderingExpression method applyCollation.
static Expression applyCollation(Expression expression, String collation, SqlAstCreationState creationState) {
final Expression sortExpression;
if (collation == null) {
sortExpression = expression;
} else {
final QueryEngine queryEngine = creationState.getCreationContext().getSessionFactory().getQueryEngine();
final SqmToSqlAstConverter converter;
if (creationState instanceof SqmToSqlAstConverter) {
converter = (SqmToSqlAstConverter) creationState;
} else {
converter = new FakeSqmToSqlAstConverter(creationState);
}
sortExpression = queryEngine.getSqmFunctionRegistry().findFunctionDescriptor("collate").generateSqmExpression(new SqmSelfRenderingExpression<>(walker -> expression, null, null), null, queryEngine, queryEngine.getTypeConfiguration()).convertToSqlAst(converter);
}
return sortExpression;
}
use of org.hibernate.sql.ast.tree.expression.Expression in project hibernate-orm by hibernate.
the class OracleSqlAstTranslator method visitOver.
@Override
public void visitOver(Over<?> over) {
final Expression expression = over.getExpression();
if (expression instanceof FunctionExpression && "row_number".equals(((FunctionExpression) expression).getFunctionName())) {
if (over.getPartitions().isEmpty() && over.getOrderList().isEmpty() && over.getStartKind() == FrameKind.UNBOUNDED_PRECEDING && over.getEndKind() == FrameKind.CURRENT_ROW && over.getExclusion() == FrameExclusion.NO_OTHERS) {
// Oracle doesn't allow an empty over clause for the row_number() function
append("rownum");
return;
}
}
super.visitOver(over);
}
Aggregations