Search in sources :

Example 6 with TemporalType

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

the class VersionResolution method from.

// todo (6.0) : support explicit JTD?
// todo (6.0) : support explicit STD?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <E> VersionResolution<E> from(Function<TypeConfiguration, java.lang.reflect.Type> implicitJavaTypeAccess, Function<TypeConfiguration, BasicJavaType> explicitJtdAccess, Function<TypeConfiguration, JdbcType> explicitStdAccess, TimeZoneStorageType timeZoneStorageType, TypeConfiguration typeConfiguration, @SuppressWarnings("unused") MetadataBuildingContext context) {
    // todo (6.0) : add support for Dialect-specific interpretation?
    final java.lang.reflect.Type implicitJavaType = implicitJavaTypeAccess.apply(typeConfiguration);
    final JavaType registered = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(implicitJavaType);
    final BasicJavaType jtd = (BasicJavaType) registered;
    final JdbcType recommendedJdbcType = jtd.getRecommendedJdbcType(new JdbcTypeIndicators() {

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return typeConfiguration;
        }

        @Override
        public TemporalType getTemporalPrecision() {
            // if it is a temporal version, it needs to be a TIMESTAMP
            return TemporalType.TIMESTAMP;
        }

        @Override
        public TimeZoneStorageStrategy getDefaultTimeZoneStorageStrategy() {
            if (timeZoneStorageType != null) {
                switch(timeZoneStorageType) {
                    case NATIVE:
                        return TimeZoneStorageStrategy.NATIVE;
                    case NORMALIZE:
                        return TimeZoneStorageStrategy.NORMALIZE;
                }
            }
            return context.getBuildingOptions().getDefaultTimeZoneStorage();
        }
    });
    final BasicType<?> basicType = typeConfiguration.getBasicTypeRegistry().resolve(jtd, recommendedJdbcType);
    final BasicType legacyType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(jtd.getJavaType());
    assert legacyType.getJdbcType().equals(recommendedJdbcType);
    return new VersionResolution<>(jtd, recommendedJdbcType, basicType, legacyType);
}
Also used : JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) TimeZoneStorageStrategy(org.hibernate.TimeZoneStorageStrategy) BasicType(org.hibernate.type.BasicType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JavaType(org.hibernate.type.descriptor.java.JavaType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) TemporalType(jakarta.persistence.TemporalType)

Example 7 with TemporalType

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

the class BindingTypeHelper method resolveBindType.

public JdbcMapping resolveBindType(Object value, JdbcMapping baseType, TypeConfiguration typeConfiguration) {
    if (value == null || !(baseType.getJavaTypeDescriptor() instanceof TemporalJavaType<?>)) {
        return baseType;
    }
    final Class<?> javaType = value.getClass();
    final TemporalType temporalType = ((TemporalJavaType<?>) baseType.getJavaTypeDescriptor()).getPrecision();
    switch(temporalType) {
        case TIMESTAMP:
            {
                return (JdbcMapping) resolveTimestampTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
            }
        case DATE:
            {
                return (JdbcMapping) resolveDateTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
            }
        case TIME:
            {
                return (JdbcMapping) resolveTimeTemporalTypeVariant(javaType, (BindableType<?>) baseType, typeConfiguration);
            }
        default:
            {
                throw new IllegalArgumentException("Unexpected TemporalType [" + temporalType + "]; expecting TIMESTAMP, DATE or TIME");
            }
    }
}
Also used : TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) TemporalType(jakarta.persistence.TemporalType)

Example 8 with TemporalType

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

the class BaseSqmToSqlAstConverter method transformDatetimeArithmetic.

private Object transformDatetimeArithmetic(SqmBinaryArithmetic<?> expression) {
    BinaryArithmeticOperator operator = expression.getOperator();
    // timestamps are ill-formed
    if (operator != SUBTRACT) {
        throw new SemanticException("illegal operator for temporal type: " + operator);
    }
    // a difference between two dates or two
    // timestamps is a leaf duration, so we
    // must apply the scale, and the 'by unit'
    // ts1 - ts2
    Expression left = cleanly(() -> toSqlExpression(expression.getLeftHandOperand().accept(this)));
    Expression right = cleanly(() -> toSqlExpression(expression.getRightHandOperand().accept(this)));
    TypeConfiguration typeConfiguration = getCreationContext().getMappingMetamodel().getTypeConfiguration();
    TemporalType leftTimestamp = typeConfiguration.getSqlTemporalType(expression.getLeftHandOperand().getNodeType());
    TemporalType rightTimestamp = typeConfiguration.getSqlTemporalType(expression.getRightHandOperand().getNodeType());
    // when we're dealing with Dates, we use
    // DAY as the smallest unit, otherwise we
    // use a platform-specific granularity
    TemporalUnit baseUnit = (rightTimestamp == TemporalType.TIMESTAMP || leftTimestamp == TemporalType.TIMESTAMP) ? NATIVE : DAY;
    if (adjustedTimestamp != null) {
        if (appliedByUnit != null) {
            throw new IllegalStateException();
        }
        // we're using the resulting duration to
        // adjust a date or timestamp on the left
        // baseUnit is the finest resolution for the
        // temporal type, so we must use it for both
        // the diff, and then the subsequent add
        DurationUnit unit = new DurationUnit(baseUnit, basicType(Integer.class));
        Expression magnitude = applyScale(timestampdiff().expression(null, unit, right, left));
        return timestampadd().expression(// TODO should be adjustedTimestamp.getType()
        (ReturnableType<?>) adjustedTimestampType, unit, magnitude, adjustedTimestamp);
    } else if (appliedByUnit != null) {
        // we're immediately converting the resulting
        // duration to a scalar in the given unit
        DurationUnit unit = (DurationUnit) appliedByUnit.getUnit().accept(this);
        return applyScale(timestampdiff().expression(null, unit, right, left));
    } else {
        // a plain "bare" Duration
        DurationUnit unit = new DurationUnit(baseUnit, basicType(Integer.class));
        BasicValuedMapping durationType = (BasicValuedMapping) expression.getNodeType();
        Expression scaledMagnitude = applyScale(timestampdiff().expression((ReturnableType<?>) expression.getNodeType(), unit, right, left));
        return new Duration(scaledMagnitude, baseUnit, durationType);
    }
}
Also used : BigInteger(java.math.BigInteger) 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) BinaryArithmeticOperator(org.hibernate.query.sqm.BinaryArithmeticOperator) 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) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) SemanticException(org.hibernate.query.SemanticException) TemporalType(jakarta.persistence.TemporalType)

Example 9 with TemporalType

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

the class ZonedDateTimeJavaType 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)

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