use of org.hibernate.sql.ast.tree.expression.QueryLiteral in project hibernate-orm by hibernate.
the class IntegralTimestampaddFunction method convertedArgument.
private Expression convertedArgument(DurationUnit field, TemporalUnit unit, Expression magnitude) {
final BasicValuedMapping expressionType = (BasicValuedMapping) magnitude.getExpressionType();
final String conversionFactor = field.getUnit().conversionFactor(unit, dialect);
return conversionFactor.isEmpty() ? magnitude : new BinaryArithmeticExpression(magnitude, conversionFactor.charAt(0) == '*' ? BinaryArithmeticOperator.MULTIPLY : BinaryArithmeticOperator.DIVIDE, new QueryLiteral<>(expressionType.getExpressibleJavaType().fromString(conversionFactor.substring(1)), expressionType), expressionType);
}
use of org.hibernate.sql.ast.tree.expression.QueryLiteral in project hibernate-orm by hibernate.
the class SqlFunction method render.
@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> arguments, SqlAstTranslator<?> walker) {
final QueryLiteral<String> sqlFragmentLiteral = (QueryLiteral<String>) arguments.get(0);
final String sqlFragment = sqlFragmentLiteral.getLiteralValue();
if (arguments.size() != 1) {
int index = 0;
for (int i = 1; i < arguments.size(); i++) {
final SqlAstNode argument = arguments.get(i);
final int paramIndex = sqlFragment.indexOf('?', index);
if (paramIndex == -1) {
throw new IllegalArgumentException("The SQL function passes an argument at index " + i + " but the fragment contains no placeholder for the argument: " + sqlFragment);
}
sqlAppender.append(sqlFragment, index, paramIndex);
argument.accept(walker);
index = paramIndex + 1;
}
} else {
sqlAppender.appendSql(sqlFragment);
}
}
use of org.hibernate.sql.ast.tree.expression.QueryLiteral in project hibernate-orm by hibernate.
the class TimesTenSqlAstTranslator method renderTableGroupJoin.
@Override
protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGroupJoin> tableGroupJoinCollector) {
if (tableGroupJoin.getJoinType() == SqlAstJoinType.CROSS) {
appendSql(", ");
} else {
appendSql(WHITESPACE);
appendSql(tableGroupJoin.getJoinType().getText());
appendSql("join ");
}
final Predicate predicate;
if (tableGroupJoin.getPredicate() == null) {
if (tableGroupJoin.getJoinType() == SqlAstJoinType.CROSS) {
predicate = null;
} else {
predicate = new BooleanExpressionPredicate(new QueryLiteral<>(true, getBooleanType()));
}
} else {
predicate = tableGroupJoin.getPredicate();
}
if (predicate != null && !predicate.isEmpty()) {
renderTableGroup(tableGroupJoin.getJoinedGroup(), predicate, tableGroupJoinCollector);
} else {
renderTableGroup(tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector);
}
}
use of org.hibernate.sql.ast.tree.expression.QueryLiteral in project hibernate-orm by hibernate.
the class SingleTableEntityPersister method createDiscriminatorPredicate.
private Predicate createDiscriminatorPredicate(String alias, TableGroup tableGroup, SqlExpressionResolver sqlExpressionResolver) {
final String columnReferenceKey;
final String discriminatorExpression;
if (isDiscriminatorFormula()) {
discriminatorExpression = getDiscriminatorFormulaTemplate();
columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorFormulaTemplate());
} else {
discriminatorExpression = getDiscriminatorColumnName();
columnReferenceKey = SqlExpressionResolver.createColumnReferenceKey(tableGroup.getPrimaryTableReference(), getDiscriminatorColumnName());
}
final BasicType<?> discriminatorType = (BasicType<?>) getDiscriminatorType();
final Expression sqlExpression = sqlExpressionResolver.resolveSqlExpression(columnReferenceKey, sqlAstProcessingState -> new ColumnReference(alias, discriminatorExpression, isDiscriminatorFormula(), null, null, discriminatorType.getJdbcMapping(), getFactory()));
if (hasSubclasses()) {
final List<Expression> values = new ArrayList<>(fullDiscriminatorValues.length);
boolean hasNull = false, hasNonNull = false;
for (Object discriminatorValue : fullDiscriminatorValues) {
if (discriminatorValue == DiscriminatorHelper.NULL_DISCRIMINATOR) {
hasNull = true;
} else if (discriminatorValue == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR) {
hasNonNull = true;
} else {
values.add(new QueryLiteral<>(discriminatorValue, discriminatorType));
}
}
final Predicate p = new InListPredicate(sqlExpression, values);
if (hasNull || hasNonNull) {
final Junction junction = new Junction(Junction.Nature.DISJUNCTION);
// so we return an empty Junction
if (hasNull && hasNonNull) {
return junction;
}
junction.add(new NullnessPredicate(sqlExpression));
junction.add(p);
return junction;
}
return p;
}
final Object value = getDiscriminatorValue();
final boolean hasNotNullDiscriminator = value == DiscriminatorHelper.NOT_NULL_DISCRIMINATOR;
final boolean hasNullDiscriminator = value == DiscriminatorHelper.NULL_DISCRIMINATOR;
if (hasNotNullDiscriminator || hasNullDiscriminator) {
final NullnessPredicate nullnessPredicate = new NullnessPredicate(sqlExpression);
if (hasNotNullDiscriminator) {
return new NegatedPredicate(nullnessPredicate);
}
return nullnessPredicate;
}
return new ComparisonPredicate(sqlExpression, ComparisonOperator.EQUAL, new QueryLiteral<>(value, discriminatorType));
}
use of org.hibernate.sql.ast.tree.expression.QueryLiteral in project hibernate-orm by hibernate.
the class SybaseASESqlAstTranslator method renderTableGroupJoin.
@Override
protected void renderTableGroupJoin(TableGroupJoin tableGroupJoin, List<TableGroupJoin> tableGroupJoinCollector) {
if (tableGroupJoin.getJoinType() == SqlAstJoinType.CROSS) {
appendSql(", ");
} else {
appendSql(WHITESPACE);
appendSql(tableGroupJoin.getJoinType().getText());
appendSql("join ");
}
final Predicate predicate;
if (tableGroupJoin.getPredicate() == null) {
if (tableGroupJoin.getJoinType() == SqlAstJoinType.CROSS) {
predicate = null;
} else {
predicate = new BooleanExpressionPredicate(new QueryLiteral<>(true, getBooleanType()));
}
} else {
predicate = tableGroupJoin.getPredicate();
}
if (predicate != null && !predicate.isEmpty()) {
renderTableGroup(tableGroupJoin.getJoinedGroup(), predicate, tableGroupJoinCollector);
} else {
renderTableGroup(tableGroupJoin.getJoinedGroup(), null, tableGroupJoinCollector);
}
}
Aggregations