use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method visitPartitionExpressions.
protected final void visitPartitionExpressions(List<Expression> partitionExpressions, Function<Expression, Expression> resolveAliasExpression, boolean inlineParametersOfAliasedExpressions) {
String separator = "";
for (Expression partitionExpression : partitionExpressions) {
final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(partitionExpression);
if (sqlTuple != null) {
for (Expression e : sqlTuple.getExpressions()) {
appendSql(separator);
final Expression resolved = resolveAliasExpression.apply(e);
if (inlineParametersOfAliasedExpressions && resolved != e) {
final SqlAstNodeRenderingMode original = parameterRenderingMode;
parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
renderPartitionItem(resolved);
parameterRenderingMode = original;
} else {
renderPartitionItem(resolved);
}
separator = COMA_SEPARATOR;
}
} else {
appendSql(separator);
final Expression resolved = resolveAliasExpression.apply(partitionExpression);
if (inlineParametersOfAliasedExpressions && resolved != partitionExpression) {
final SqlAstNodeRenderingMode original = parameterRenderingMode;
parameterRenderingMode = SqlAstNodeRenderingMode.INLINE_ALL_PARAMETERS;
renderPartitionItem(resolved);
parameterRenderingMode = original;
} else {
renderPartitionItem(resolved);
}
}
separator = COMA_SEPARATOR;
}
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method visitSortSpecification.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ORDER BY clause
@Override
public void visitSortSpecification(SortSpecification sortSpecification) {
final Expression sortExpression = sortSpecification.getSortExpression();
final NullPrecedence nullPrecedence = sortSpecification.getNullPrecedence();
final SortOrder sortOrder = sortSpecification.getSortOrder();
final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(sortExpression);
if (sqlTuple != null) {
String separator = NO_SEPARATOR;
for (Expression expression : sqlTuple.getExpressions()) {
appendSql(separator);
visitSortSpecification(expression, sortOrder, nullPrecedence);
separator = COMA_SEPARATOR;
}
} else {
visitSortSpecification(sortExpression, sortOrder, nullPrecedence);
}
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method visitInListPredicate.
@Override
public void visitInListPredicate(InListPredicate inListPredicate) {
final List<Expression> listExpressions = inListPredicate.getListExpressions();
if (listExpressions.isEmpty()) {
appendSql("1=0");
return;
}
Function<Expression, Expression> itemAccessor = Function.identity();
final SqlTuple lhsTuple;
if ((lhsTuple = SqlTupleContainer.getSqlTuple(inListPredicate.getTestExpression())) != null) {
if (lhsTuple.getExpressions().size() == 1) {
// Special case for tuples with arity 1 as any DBMS supports scalar IN predicates
itemAccessor = listExpression -> SqlTupleContainer.getSqlTuple(listExpression).getExpressions().get(0);
} else if (!supportsRowValueConstructorSyntaxInInList()) {
final ComparisonOperator comparisonOperator = inListPredicate.isNegated() ? ComparisonOperator.NOT_EQUAL : ComparisonOperator.EQUAL;
// Some DBs like Oracle support tuples only for the IN subquery predicate
if (supportsRowValueConstructorSyntaxInInSubQuery() && getDialect().supportsUnionAll()) {
inListPredicate.getTestExpression().accept(this);
if (inListPredicate.isNegated()) {
appendSql(" not");
}
appendSql(" in(");
String separator = NO_SEPARATOR;
for (Expression expression : listExpressions) {
appendSql(separator);
renderExpressionsAsSubquery(SqlTupleContainer.getSqlTuple(expression).getExpressions());
separator = " union all ";
}
appendSql(CLOSE_PARENTHESIS);
} else {
String separator = NO_SEPARATOR;
for (Expression expression : listExpressions) {
appendSql(separator);
emulateTupleComparison(lhsTuple.getExpressions(), SqlTupleContainer.getSqlTuple(expression).getExpressions(), comparisonOperator, true);
separator = " or ";
}
}
return;
}
}
inListPredicate.getTestExpression().accept(this);
if (inListPredicate.isNegated()) {
appendSql(" not");
}
appendSql(" in(");
String separator = NO_SEPARATOR;
int bindValueCount = listExpressions.size();
int bindValueMaxCount = bindValueCount;
final Dialect dialect = getSessionFactory().getJdbcServices().getDialect();
int inExprLimit = dialect.getInExpressionCountLimit();
final boolean inClauseParameterPaddingEnabled = getSessionFactory().getSessionFactoryOptions().inClauseParameterPaddingEnabled() && bindValueCount > 2;
if (inClauseParameterPaddingEnabled) {
// bindValueCount: 1005
// bindValuePaddingCount: 1024
int bindValuePaddingCount = MathHelper.ceilingPowerOfTwo(bindValueCount);
// inExprLimit: 1000
if (inExprLimit > 0) {
if (bindValuePaddingCount > inExprLimit) {
// bindValuePaddingCount: 8
if (bindValueCount < inExprLimit) {
bindValueMaxCount = inExprLimit;
} else {
bindValueMaxCount = MathHelper.ceilingPowerOfTwo(bindValueCount % inExprLimit);
}
} else if (bindValueCount < bindValuePaddingCount) {
bindValueMaxCount = bindValuePaddingCount;
}
} else if (bindValueCount < bindValuePaddingCount) {
bindValueMaxCount = bindValuePaddingCount;
}
}
final Iterator<Expression> iterator = listExpressions.iterator();
int itemNumber = 0;
while (iterator.hasNext() && (inExprLimit == 0 || itemNumber < inExprLimit)) {
final Expression listExpression = itemAccessor.apply(iterator.next());
appendSql(separator);
listExpression.accept(this);
separator = COMA_SEPARATOR;
itemNumber++;
// and just render through the in list expressions as they are without padding/splitting
if (!(listExpression instanceof JdbcParameter || listExpression instanceof SqmParameterInterpretation)) {
inExprLimit = 0;
bindValueMaxCount = bindValueCount;
}
}
if (itemNumber != inExprLimit && bindValueCount == bindValueMaxCount) {
appendSql(CLOSE_PARENTHESIS);
return;
}
if (inExprLimit > 0 && bindValueCount > inExprLimit) {
do {
append(") and ");
inListPredicate.getTestExpression().accept(this);
if (inListPredicate.isNegated()) {
appendSql(" not");
}
appendSql(" in(");
separator = NO_SEPARATOR;
itemNumber = 0;
while (iterator.hasNext() && itemNumber < inExprLimit) {
final Expression listExpression = iterator.next();
appendSql(separator);
itemAccessor.apply(listExpression).accept(this);
separator = COMA_SEPARATOR;
itemNumber++;
}
} while (iterator.hasNext());
}
int i;
if (inExprLimit > 0 && bindValueCount > inExprLimit) {
i = bindValueCount % inExprLimit;
} else {
i = bindValueCount;
}
final Expression lastExpression = itemAccessor.apply(listExpressions.get(listExpressions.size() - 1));
for (; i < bindValueMaxCount; i++) {
appendSql(separator);
lastExpression.accept(this);
separator = COMA_SEPARATOR;
}
appendSql(CLOSE_PARENTHESIS);
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method visitUpdateStatementOnly.
protected void visitUpdateStatementOnly(UpdateStatement statement) {
// todo (6.0) : to support joins we need dialect support
appendSql("update ");
final Stack<Clause> clauseStack = getClauseStack();
try {
clauseStack.push(Clause.UPDATE);
renderNamedTableReference(statement.getTargetTable(), LockMode.NONE);
} finally {
clauseStack.pop();
}
appendSql(" set ");
boolean firstPass = true;
try {
clauseStack.push(Clause.SET);
for (Assignment assignment : statement.getAssignments()) {
if (firstPass) {
firstPass = false;
} else {
appendSql(COMA_SEPARATOR_CHAR);
}
final List<ColumnReference> columnReferences = assignment.getAssignable().getColumnReferences();
if (columnReferences.size() == 1) {
columnReferences.get(0).accept(this);
appendSql('=');
final Expression assignedValue = assignment.getAssignedValue();
final SqlTuple sqlTuple = SqlTupleContainer.getSqlTuple(assignedValue);
if (sqlTuple != null) {
final Expression expression = sqlTuple.getExpressions().get(0);
expression.accept(this);
} else {
assignedValue.accept(this);
}
} else {
appendSql(OPEN_PARENTHESIS);
for (ColumnReference columnReference : columnReferences) {
columnReference.accept(this);
}
appendSql(")=");
assignment.getAssignedValue().accept(this);
}
}
} finally {
clauseStack.pop();
}
visitWhereClause(statement.getRestriction());
visitReturningColumns(statement);
}
Aggregations