Search in sources :

Example 1 with TemporalType

use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.

the class OffsetDateTimeJavaType method getRecommendedJdbcType.

@Override
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators stdIndicators) {
    final TemporalType temporalPrecision = stdIndicators.getTemporalPrecision();
    final JdbcTypeRegistry jdbcTypeRegistry = stdIndicators.getTypeConfiguration().getJdbcTypeRegistry();
    if (temporalPrecision == null || temporalPrecision == TemporalType.TIMESTAMP) {
        return stdIndicators.getDefaultTimeZoneStorageStrategy() == TimeZoneStorageStrategy.NORMALIZE ? jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP) : jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP_WITH_TIMEZONE);
    }
    switch(temporalPrecision) {
        case TIME:
            {
                return jdbcTypeRegistry.getDescriptor(Types.TIME);
            }
        case DATE:
            {
                return jdbcTypeRegistry.getDescriptor(Types.DATE);
            }
        default:
            {
                throw new IllegalArgumentException("Unexpected jakarta.persistence.TemporalType : " + temporalPrecision);
            }
    }
}
Also used : JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) TemporalType(jakarta.persistence.TemporalType)

Example 2 with TemporalType

use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.

the class TimestampaddFunction method patternRenderer.

PatternRenderer patternRenderer(TemporalUnit unit, Expression interval, Expression to) {
    TemporalType temporalType = getSqlTemporalType(to.getExpressionType());
    IntervalType intervalType = getSqlIntervalType(interval.getExpressionType().getJdbcMappings().get(0));
    return new PatternRenderer(dialect.timestampaddPattern(unit, temporalType, intervalType));
}
Also used : IntervalType(org.hibernate.query.sqm.IntervalType) TypeConfiguration.getSqlIntervalType(org.hibernate.type.spi.TypeConfiguration.getSqlIntervalType) PatternRenderer(org.hibernate.query.sqm.produce.function.internal.PatternRenderer) TemporalType(jakarta.persistence.TemporalType) TypeConfiguration.getSqlTemporalType(org.hibernate.type.spi.TypeConfiguration.getSqlTemporalType)

Example 3 with TemporalType

use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.

the class TimestampdiffFunction method patternRenderer.

private PatternRenderer patternRenderer(TemporalUnit unit, Expression from, Expression to) {
    TemporalType lhsTemporalType = getSqlTemporalType(from.getExpressionType());
    TemporalType rhsTemporalType = getSqlTemporalType(to.getExpressionType());
    return new PatternRenderer(dialect.timestampdiffPattern(unit, lhsTemporalType, rhsTemporalType));
}
Also used : PatternRenderer(org.hibernate.query.sqm.produce.function.internal.PatternRenderer) TypeConfiguration.getSqlTemporalType(org.hibernate.type.spi.TypeConfiguration.getSqlTemporalType) TemporalType(jakarta.persistence.TemporalType)

Example 4 with TemporalType

use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitBinaryArithmeticExpression.

@Override
public Object visitBinaryArithmeticExpression(SqmBinaryArithmetic<?> expression) {
    SqmExpression<?> leftOperand = expression.getLeftHandOperand();
    SqmExpression<?> rightOperand = expression.getRightHandOperand();
    boolean durationToRight = isDuration(rightOperand.getNodeType());
    TypeConfiguration typeConfiguration = getCreationContext().getMappingMetamodel().getTypeConfiguration();
    TemporalType temporalTypeToLeft = typeConfiguration.getSqlTemporalType(leftOperand.getNodeType());
    TemporalType temporalTypeToRight = typeConfiguration.getSqlTemporalType(rightOperand.getNodeType());
    boolean temporalTypeSomewhereToLeft = adjustedTimestamp != null || temporalTypeToLeft != null;
    if (temporalTypeToLeft != null && durationToRight) {
        if (adjustmentScale != null || negativeAdjustment) {
            // we can't distribute a scale over a date/timestamp
            throw new SemanticException("scalar multiplication of temporal value");
        }
    }
    if (durationToRight && temporalTypeSomewhereToLeft) {
        return transformDurationArithmetic(expression);
    } else if (temporalTypeToLeft != null && temporalTypeToRight != null) {
        return transformDatetimeArithmetic(expression);
    } else {
        // Infer one operand type through the other
        final FromClauseIndex fromClauseIndex = fromClauseIndexStack.getCurrent();
        inferrableTypeAccessStack.push(() -> determineValueMapping(rightOperand, fromClauseIndex));
        final Expression lhs = toSqlExpression(leftOperand.accept(this));
        inferrableTypeAccessStack.pop();
        inferrableTypeAccessStack.push(() -> determineValueMapping(leftOperand, fromClauseIndex));
        final Expression rhs = toSqlExpression(rightOperand.accept(this));
        inferrableTypeAccessStack.pop();
        if (durationToRight && appliedByUnit != null) {
            return new BinaryArithmeticExpression(lhs, expression.getOperator(), rhs, // we always get a Long value back
            (BasicValuedMapping) appliedByUnit.getNodeType());
        } else {
            return new BinaryArithmeticExpression(lhs, expression.getOperator(), rhs, getExpressionType(expression));
        }
    }
}
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) BinaryArithmeticExpression(org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) TemporalType(jakarta.persistence.TemporalType) SemanticException(org.hibernate.query.SemanticException)

Example 5 with TemporalType

use of jakarta.persistence.TemporalType in project hibernate-orm by hibernate.

the class InferredBasicValueResolver method fromTemporal.

public static <T> InferredBasicValueResolution<T, T> fromTemporal(TemporalJavaType<T> reflectedJtd, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, Type resolvedJavaType, JdbcTypeIndicators stdIndicators, TypeConfiguration typeConfiguration) {
    final TemporalType requestedTemporalPrecision = stdIndicators.getTemporalPrecision();
    if (explicitJavaType != null) {
        if (!(explicitJavaType instanceof TemporalJavaType)) {
            throw new MappingException("Explicit JavaType [" + explicitJavaType + "] defined for temporal value must implement TemporalJavaType");
        }
        @SuppressWarnings("unchecked") final TemporalJavaType<T> explicitTemporalJtd = (TemporalJavaType<T>) explicitJavaType;
        if (requestedTemporalPrecision != null && explicitTemporalJtd.getPrecision() != requestedTemporalPrecision) {
            throw new MappingException("Temporal precision (`jakarta.persistence.TemporalType`) mismatch... requested precision = " + requestedTemporalPrecision + "; explicit JavaType (`" + explicitTemporalJtd + "`) precision = " + explicitTemporalJtd.getPrecision());
        }
        final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : explicitTemporalJtd.getRecommendedJdbcType(stdIndicators);
        final BasicType<T> jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitTemporalJtd, jdbcType);
        return new InferredBasicValueResolution<>(jdbcMapping, explicitTemporalJtd, explicitTemporalJtd, jdbcType, null, jdbcMapping, explicitTemporalJtd.getMutabilityPlan());
    }
    if (explicitJdbcType != null) {
        final TemporalJavaType<T> jtd;
        if (requestedTemporalPrecision != null) {
            jtd = reflectedJtd.resolveTypeForPrecision(requestedTemporalPrecision, typeConfiguration);
        } else {
            jtd = reflectedJtd;
        }
        final BasicType<T> jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(jtd, explicitJdbcType);
        return new InferredBasicValueResolution<>(jdbcMapping, jtd, jtd, explicitJdbcType, null, jdbcMapping, jtd.getMutabilityPlan());
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Case #3 - no explicit JavaType or JdbcType
    // 
    // - for the moment continue to use the legacy resolution to registered
    // BasicType
    final BasicType<T> basicType;
    if (requestedTemporalPrecision != null && requestedTemporalPrecision != reflectedJtd.getPrecision()) {
        basicType = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd.resolveTypeForPrecision(requestedTemporalPrecision, typeConfiguration), TemporalJavaType.resolveJdbcTypeCode(requestedTemporalPrecision));
    } else {
        basicType = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, reflectedJtd.getRecommendedJdbcType(stdIndicators));
    }
    return new InferredBasicValueResolution<>(basicType, basicType.getJavaTypeDescriptor(), basicType.getJavaTypeDescriptor(), basicType.getJdbcType(), null, basicType, reflectedJtd.getMutabilityPlan());
}
Also used : TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) TemporalType(jakarta.persistence.TemporalType) MappingException(org.hibernate.MappingException)

Aggregations

TemporalType (jakarta.persistence.TemporalType)9 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)3 BasicValuedMapping (org.hibernate.metamodel.mapping.BasicValuedMapping)2 SemanticException (org.hibernate.query.SemanticException)2 SelfRenderingAggregateFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingAggregateFunctionSqlAstExpression)2 SelfRenderingFunctionSqlAstExpression (org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression)2 PatternRenderer (org.hibernate.query.sqm.produce.function.internal.PatternRenderer)2 SqmExpression (org.hibernate.query.sqm.tree.expression.SqmExpression)2 SqmModifiedSubQueryExpression (org.hibernate.query.sqm.tree.expression.SqmModifiedSubQueryExpression)2 BinaryArithmeticExpression (org.hibernate.sql.ast.tree.expression.BinaryArithmeticExpression)2 CaseSearchedExpression (org.hibernate.sql.ast.tree.expression.CaseSearchedExpression)2 CaseSimpleExpression (org.hibernate.sql.ast.tree.expression.CaseSimpleExpression)2 Expression (org.hibernate.sql.ast.tree.expression.Expression)2 ModifiedSubQueryExpression (org.hibernate.sql.ast.tree.expression.ModifiedSubQueryExpression)2 SelfRenderingExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingExpression)2 SelfRenderingSqlFragmentExpression (org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression)2 SqlSelectionExpression (org.hibernate.sql.ast.tree.expression.SqlSelectionExpression)2 TemporalJavaType (org.hibernate.type.descriptor.java.TemporalJavaType)2 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)2 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)2