Search in sources :

Example 1 with SqmToDuration

use of org.hibernate.query.sqm.tree.expression.SqmToDuration in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitToDuration.

@Override
public Object visitToDuration(SqmToDuration<?> toDuration) {
    // TODO: do we need to temporarily set appliedByUnit
    // to null before we recurse down the tree?
    // and what about scale?
    Expression magnitude = toSqlExpression(toDuration.getMagnitude().accept(this));
    DurationUnit unit = (DurationUnit) toDuration.getUnit().accept(this);
    // let's start by applying the propagated scale
    // so we don't forget to do it in what follows
    Expression scaledMagnitude = applyScale(magnitude);
    if (adjustedTimestamp != null) {
        // adjusted date or timestamp
        if (appliedByUnit != null) {
            throw new IllegalStateException();
        }
        return timestampadd().expression(// TODO should be adjustedTimestamp.getType()
        (ReturnableType<?>) adjustedTimestampType, unit, scaledMagnitude, adjustedTimestamp);
    } else {
        BasicValuedMapping durationType = (BasicValuedMapping) toDuration.getNodeType();
        Duration duration;
        if (scaledMagnitude.getExpressionType().getJdbcMappings().get(0).getJdbcType().isInterval()) {
            duration = new Duration(extractEpoch(scaledMagnitude), SECOND, durationType);
        } else {
            duration = new Duration(scaledMagnitude, unit.getUnit(), durationType);
        }
        if (appliedByUnit != null) {
            // we're applying the 'by unit' operator,
            // producing a literal scalar value in
            // the given unit
            TemporalUnit appliedUnit = appliedByUnit.getUnit().getUnit();
            BasicValuedMapping scalarType = (BasicValuedMapping) appliedByUnit.getNodeType();
            return new Conversion(duration, appliedUnit, scalarType);
        } else {
            // a "bare" Duration value (gets rendered as nanoseconds)
            return duration;
        }
    }
}
Also used : BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) SqmModifiedSubQueryExpression(org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression) SelfRenderingFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression) SelfRenderingAggregateFunctionSqlAstExpression(org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression) CaseSearchedExpression(org.hibernate.sql.ast.tree.expression.CaseSearchedExpression) SelfRenderingSqlFragmentExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression) Expression(org.hibernate.sql.ast.tree.expression.Expression) SelfRenderingExpression(org.hibernate.sql.ast.tree.expression.SelfRenderingExpression) SqmExpression(org.hibernate.query.sqm.tree.expression.SqmExpression) CaseSimpleExpression(org.hibernate.sql.ast.tree.expression.CaseSimpleExpression) SqlSelectionExpression(org.hibernate.sql.ast.tree.expression.SqlSelectionExpression) ModifiedSubQueryExpression(org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression) TemporalUnit(org.hibernate.query.sqm.TemporalUnit) SqmDurationUnit(org.hibernate.query.sqm.tree.expression.SqmDurationUnit) DurationUnit(org.hibernate.sql.ast.tree.expression.DurationUnit) SqmToDuration(org.hibernate.query.sqm.tree.expression.SqmToDuration) Duration(org.hibernate.sql.ast.tree.expression.Duration) TypeConfiguration.isDuration(org.hibernate.type.spi.TypeConfiguration.isDuration) Conversion(org.hibernate.query.sqm.tree.expression.Conversion)

Aggregations

BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)1 TemporalUnit (org.hibernate.query.sqm.TemporalUnit)1 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)1 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)1 Conversion (org.hibernate.query.sqm.tree.expression.Conversion)1 SqmDurationUnit (org.hibernate.query.sqm.tree.expression.SqmDurationUnit)1 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)1 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)1 SqmToDuration (org.hibernate.query.sqm.tree.expression.SqmToDuration)1 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)1 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)1 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)1 Duration (org.hibernate.sql.ast.tree.expression.Duration)1 DurationUnit (org.hibernate.sql.ast.tree.expression.DurationUnit)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)1 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)1 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)1 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)1 TypeConfiguration.isDuration (org.hibernate.type.spi.TypeConfiguration.isDuration)1