Search in sources :

Example 6 with QueryLiteral

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);
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)

Example 7 with QueryLiteral

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);
    }
}
Also used : QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) SqlAstNode(org.hibernate.sql.ast.tree.SqlAstNode)

Example 8 with QueryLiteral

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);
    }
}
Also used : QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate)

Example 9 with QueryLiteral

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));
}
Also used : BasicType(org.hibernate.type.BasicType) ArrayList(java.util.ArrayList) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) InListPredicate(org.hibernate.sql.ast.tree.predicate.InListPredicate) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) NegatedPredicate(org.hibernate.sql.ast.tree.predicate.NegatedPredicate) Expression(org.hibernate.sql.ast.tree.expression.Expression) Junction(org.hibernate.sql.ast.tree.predicate.Junction) NullnessPredicate(org.hibernate.sql.ast.tree.predicate.NullnessPredicate) ColumnReference(org.hibernate.sql.ast.tree.expression.ColumnReference)

Example 10 with QueryLiteral

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);
    }
}
Also used : QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate) Predicate(org.hibernate.sql.ast.tree.predicate.Predicate) BooleanExpressionPredicate(org.hibernate.sql.ast.tree.predicate.BooleanExpressionPredicate)

Aggregations

QueryLiteral (org.hibernate.sql.ast.tree.expression.QueryLiteral)19 ArrayList (java.util.ArrayList)10 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)10 Expression (org.hibernate.sql.ast.tree.expression.Expression)10 SqlSelectionImpl (org.hibernate.sql.results.internal.SqlSelectionImpl)10 ConvertedQueryLiteral (org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral)8 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)8 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)8 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)7 TableReference (org.hibernate.sql.ast.tree.from.TableReference)7 InsertStatement (org.hibernate.sql.ast.tree.insert.InsertStatement)7 ComparisonPredicate (org.hibernate.sql.ast.tree.predicate.ComparisonPredicate)7 Assignment (org.hibernate.sql.ast.tree.update.Assignment)7 List (java.util.List)6 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Predicate (org.hibernate.sql.ast.tree.predicate.Predicate)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)5 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)5