Search in sources :

Example 31 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitEnumLiteral.

@SuppressWarnings({ "raw", "unchecked" })
@Override
public Object visitEnumLiteral(SqmEnumLiteral<?> sqmEnumLiteral) {
    final BasicValuedMapping inferrableType = (BasicValuedMapping) resolveInferredType();
    if (inferrableType instanceof ConvertibleModelPart) {
        final ConvertibleModelPart inferredPart = (ConvertibleModelPart) inferrableType;
        final BasicValueConverter<Enum<?>, ?> valueConverter = inferredPart.getValueConverter();
        final Object jdbcValue = valueConverter.toRelationalValue(sqmEnumLiteral.getEnumValue());
        return new QueryLiteral<>(jdbcValue, inferredPart);
    }
    final EnumJavaType<?> enumJtd = sqmEnumLiteral.getExpressibleJavaType();
    final JdbcType jdbcType = getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(SqlTypes.TINYINT);
    final BasicJavaType<Integer> relationalJtd = (BasicJavaType) getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Integer.class);
    final BasicType<?> jdbcMappingType = getTypeConfiguration().getBasicTypeRegistry().resolve(relationalJtd, jdbcType);
    return new ConvertedQueryLiteral(sqmEnumLiteral.getEnumValue(), new OrdinalEnumValueConverter<>(enumJtd, jdbcType, relationalJtd), jdbcMappingType);
}
Also used : JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) BasicValuedMapping(org.hibernate.metamodel.mapping.BasicValuedMapping) BigInteger(java.math.BigInteger) QueryLiteral(org.hibernate.sql.ast.tree.expression.QueryLiteral) ConvertedQueryLiteral(org.hibernate.sql.ast.tree.expression.ConvertedQueryLiteral) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) ConvertibleModelPart(org.hibernate.metamodel.mapping.ConvertibleModelPart)

Example 32 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class FunctionReturnImpl method toJdbcFunctionReturn.

@Override
public JdbcCallFunctionReturn toJdbcFunctionReturn(SharedSessionContractImplementor persistenceContext) {
    final BindableType<T> ormType;
    final JdbcCallRefCursorExtractorImpl refCursorExtractor;
    final JdbcCallParameterExtractorImpl<T> parameterExtractor;
    if (getJdbcTypeCode() == Types.REF_CURSOR) {
        refCursorExtractor = new JdbcCallRefCursorExtractorImpl(null, 1);
        ormType = null;
        parameterExtractor = null;
    } else {
        final TypeConfiguration typeConfiguration = persistenceContext.getFactory().getTypeConfiguration();
        final JdbcType sqlTypeDescriptor = typeConfiguration.getJdbcTypeRegistry().getDescriptor(getJdbcTypeCode());
        final BasicJavaType<?> javaTypeMapping = sqlTypeDescriptor.getJdbcRecommendedJavaTypeMapping(null, null, typeConfiguration);
        // noinspection unchecked
        ormType = (BindableType<T>) typeConfiguration.standardBasicTypeForJavaType(javaTypeMapping.getJavaTypeClass());
        parameterExtractor = new JdbcCallParameterExtractorImpl<>(procedureCall.getProcedureName(), null, 1, ormType);
        refCursorExtractor = null;
    }
    return new JdbcCallFunctionReturnImpl(ormType, parameterExtractor, refCursorExtractor);
}
Also used : JdbcCallRefCursorExtractorImpl(org.hibernate.sql.exec.internal.JdbcCallRefCursorExtractorImpl) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) JdbcCallFunctionReturnImpl(org.hibernate.sql.exec.internal.JdbcCallFunctionReturnImpl) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 33 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class ResultSetAccess method resolveType.

@Override
default <J> BasicType<J> resolveType(int position, JavaType<J> explicitJavaType, SessionFactoryImplementor sessionFactory) {
    final TypeConfiguration typeConfiguration = getFactory().getTypeConfiguration();
    final JdbcServices jdbcServices = getFactory().getJdbcServices();
    try {
        final ResultSetMetaData metaData = getResultSet().getMetaData();
        final String columnTypeName = metaData.getColumnTypeName(position);
        final int columnType = metaData.getColumnType(position);
        final int scale = metaData.getScale(position);
        final int precision = metaData.getPrecision(position);
        final int displaySize = metaData.getColumnDisplaySize(position);
        final Dialect dialect = jdbcServices.getDialect();
        final int length = dialect.resolveSqlTypeLength(columnTypeName, columnType, precision, scale, displaySize);
        final JdbcType resolvedJdbcType = dialect.resolveSqlTypeDescriptor(columnTypeName, columnType, length, scale, typeConfiguration.getJdbcTypeRegistry());
        final JavaType<J> javaType;
        final JdbcType jdbcType;
        // If there is an explicit JavaType, then prefer its recommended JDBC type
        if (explicitJavaType != null) {
            javaType = explicitJavaType;
            jdbcType = explicitJavaType.getRecommendedJdbcType(new JdbcTypeIndicators() {

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

                @Override
                public long getColumnLength() {
                    return length;
                }

                @Override
                public int getColumnPrecision() {
                    return precision;
                }

                @Override
                public int getColumnScale() {
                    return scale;
                }

                @Override
                public EnumType getEnumeratedType() {
                    return resolvedJdbcType.isNumber() ? EnumType.ORDINAL : EnumType.STRING;
                }
            });
        } else {
            jdbcType = resolvedJdbcType;
            javaType = jdbcType.getJdbcRecommendedJavaTypeMapping(length, scale, typeConfiguration);
        }
        return typeConfiguration.getBasicTypeRegistry().resolve(javaType, jdbcType);
    } catch (SQLException e) {
        throw jdbcServices.getSqlExceptionHelper().convert(e, "Unable to determine JDBC type code for ResultSet position " + position);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) JdbcTypeIndicators(org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators) SQLException(java.sql.SQLException) Dialect(org.hibernate.dialect.Dialect) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration)

Example 34 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class JdbcTypeRegistry method getDescriptor.

public JdbcType getDescriptor(int jdbcTypeCode) {
    JdbcType descriptor = descriptorMap.get(jdbcTypeCode);
    if (descriptor != null) {
        return descriptor;
    }
    if (JdbcTypeNameMapper.isStandardTypeCode(jdbcTypeCode)) {
        log.debugf("A standard JDBC type code [%s] was not defined in SqlTypeDescriptorRegistry", jdbcTypeCode);
    }
    // see if the typecode is part of a known type family...
    JdbcTypeFamilyInformation.Family family = JdbcTypeFamilyInformation.INSTANCE.locateJdbcTypeFamilyByTypeCode(jdbcTypeCode);
    if (family != null) {
        for (int potentialAlternateTypeCode : family.getTypeCodes()) {
            if (potentialAlternateTypeCode != jdbcTypeCode) {
                final JdbcType potentialAlternateDescriptor = descriptorMap.get(potentialAlternateTypeCode);
                if (potentialAlternateDescriptor != null) {
                    // todo (6.0) : add a SqlTypeDescriptor#canBeAssignedFrom method ?
                    return potentialAlternateDescriptor;
                }
                if (JdbcTypeNameMapper.isStandardTypeCode(potentialAlternateTypeCode)) {
                    log.debugf("A standard JDBC type code [%s] was not defined in SqlTypeDescriptorRegistry", potentialAlternateTypeCode);
                }
            }
        }
    }
    // finally, create a new descriptor mapping to getObject/setObject for this type code...
    final ObjectJdbcType fallBackDescriptor = new ObjectJdbcType(jdbcTypeCode);
    addDescriptor(fallBackDescriptor);
    return fallBackDescriptor;
}
Also used : ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) JdbcTypeFamilyInformation(org.hibernate.type.descriptor.jdbc.JdbcTypeFamilyInformation) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType)

Example 35 with JdbcType

use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.

the class YesNoBinder method bind.

@Override
public void bind(YesNo annotation, MetadataBuildingContext buildingContext, PersistentClass persistentClass, Property property) {
    final BasicValue booleanValueMapping = (BasicValue) property.getValue();
    final BasicJavaType<?> javaType = (BasicJavaType<?>) buildingContext.getBootstrapContext().getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Boolean.class);
    final JdbcType jdbcType = buildingContext.getBootstrapContext().getTypeConfiguration().getJdbcTypeRegistry().getDescriptor(Types.CHAR);
    final InstanceBasedConverterDescriptor converter = new InstanceBasedConverterDescriptor(YesNoConverter.INSTANCE, buildingContext.getBootstrapContext().getClassmateContext());
    booleanValueMapping.setExplicitJavaTypeAccess((typeConfiguration) -> javaType);
    booleanValueMapping.setExplicitJdbcTypeAccess((typeConfiguration) -> jdbcType);
    booleanValueMapping.setJpaAttributeConverterDescriptor(converter);
}
Also used : BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) InstanceBasedConverterDescriptor(org.hibernate.boot.model.convert.internal.InstanceBasedConverterDescriptor) BasicValue(org.hibernate.mapping.BasicValue)

Aggregations

JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)36 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)11 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)7 BasicValue (org.hibernate.mapping.BasicValue)6 ObjectJdbcType (org.hibernate.type.descriptor.jdbc.ObjectJdbcType)6 MappingException (org.hibernate.MappingException)5 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 AttributeConverterTypeAdapter (org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter)5 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)5 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)5 MapKeyJdbcType (org.hibernate.annotations.MapKeyJdbcType)4 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)4 AbstractStandardBasicType (org.hibernate.type.AbstractStandardBasicType)4 Type (org.hibernate.type.Type)4 JavaType (org.hibernate.type.descriptor.java.JavaType)4 StringJavaType (org.hibernate.type.descriptor.java.StringJavaType)4 Test (org.junit.Test)4 MetadataSources (org.hibernate.boot.MetadataSources)3 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)3 PersistentClass (org.hibernate.mapping.PersistentClass)3