use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class MySQLDialect method getCastTypeName.
@Override
public String getCastTypeName(SqlExpressible type, Long length, Integer precision, Integer scale) {
final JdbcMapping jdbcMapping = type.getJdbcMapping();
final JdbcType jdbcType = jdbcMapping.getJdbcType();
final JavaType<?> javaType = jdbcMapping.getJavaTypeDescriptor();
switch(jdbcType.getDefaultSqlTypeCode()) {
case Types.INTEGER:
case Types.BIGINT:
case Types.SMALLINT:
case Types.TINYINT:
// MySQL doesn't let you cast to INTEGER/BIGINT/TINYINT
return "signed";
case Types.BIT:
// special case for casting to Boolean
return "unsigned";
case Types.FLOAT:
case Types.DOUBLE:
case Types.REAL:
// the default scale is 0 (no decimal places)
return String.format("decimal(%d, %d)", precision == null ? javaType.getDefaultSqlPrecision(this, jdbcType) : precision, scale == null ? javaType.getDefaultSqlScale(this, jdbcType) : scale);
case Types.VARBINARY:
case Types.LONGVARBINARY:
// MySQL doesn't let you cast to BLOB/TINYBLOB/LONGBLOB
if (length == null) {
return "binary";
}
return String.format("binary(%d)", length);
case Types.VARCHAR:
case Types.LONGVARCHAR:
// MySQL doesn't let you cast to TEXT/LONGTEXT
if (length == null) {
return "char";
}
return String.format("char(%d)", length);
default:
return super.getCastTypeName(type, length, precision, scale);
}
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class MetadataBuildingProcess method handleTypes.
// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
//
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
final TypeContributions typeContributions = new TypeContributions() {
@Override
public void contributeType(BasicType type) {
getBasicTypeRegistry().register(type);
conditionallyRegisterJtd(type.getJavaTypeDescriptor());
}
private void conditionallyRegisterJtd(JavaType jtd) {
final JavaTypeRegistry jtdRegistry = getTypeConfiguration().getJavaTypeRegistry();
jtdRegistry.resolveDescriptor(jtd.getJavaTypeClass(), () -> jtd);
}
@Override
public void contributeType(BasicType type, String... keys) {
getBasicTypeRegistry().register(type, keys);
conditionallyRegisterJtd(type.getJavaTypeDescriptor());
}
@Override
public void contributeType(UserType type, String[] keys) {
contributeType(new CustomType<Object>(type, keys, getTypeConfiguration()));
}
@Override
public void contributeJavaType(JavaType<?> descriptor) {
typeConfiguration.getJavaTypeRegistry().addDescriptor(descriptor);
}
@Override
public void contributeJdbcType(JdbcType descriptor) {
typeConfiguration.getJdbcTypeRegistry().addDescriptor(descriptor);
}
@Override
public <T> void contributeType(UserType<T> descriptor) {
typeConfiguration.getBasicTypeRegistry().register(descriptor, descriptor.returnedClass().getName());
}
@Override
public TypeConfiguration getTypeConfiguration() {
return typeConfiguration;
}
final BasicTypeRegistry getBasicTypeRegistry() {
return getTypeConfiguration().getBasicTypeRegistry();
}
};
// add Dialect contributed types
final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
dialect.contributeTypes(typeContributions, options.getServiceRegistry());
// add TypeContributor contributed types.
for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
contributor.contribute(typeContributions, options.getServiceRegistry());
}
// add fallback type descriptors
final JdbcTypeRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeRegistry();
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.UUID, SqlTypes.BINARY);
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.JSON, SqlTypes.VARBINARY);
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INET, SqlTypes.VARBINARY);
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.INTERVAL_SECOND, SqlTypes.NUMERIC);
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.GEOMETRY, SqlTypes.VARBINARY);
addFallbackIfNecessary(jdbcTypeRegistry, SqlTypes.POINT, SqlTypes.VARBINARY);
// add explicit application registered types
typeConfiguration.addBasicTypeRegistrationContributions(options.getBasicTypeRegistrations());
// For NORMALIZE, we replace the standard types that use TIMESTAMP_WITH_TIMEZONE to use TIMESTAMP
if (options.getDefaultTimeZoneStorage() == TimeZoneStorageStrategy.NORMALIZE) {
final JavaTypeRegistry javaTypeRegistry = typeConfiguration.getJavaTypeRegistry();
final JdbcType timestampDescriptor = jdbcTypeRegistry.getDescriptor(Types.TIMESTAMP);
final BasicTypeRegistry basicTypeRegistry = typeConfiguration.getBasicTypeRegistry();
final BasicType<?> offsetDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(OffsetDateTime.class), timestampDescriptor, "OffsetDateTime");
final BasicType<?> zonedDateTimeType = new NamedBasicTypeImpl<>(javaTypeRegistry.getDescriptor(ZonedDateTime.class), timestampDescriptor, "ZonedDateTime");
basicTypeRegistry.register(offsetDateTimeType, "org.hibernate.type.OffsetDateTimeType", OffsetDateTime.class.getSimpleName(), OffsetDateTime.class.getName());
basicTypeRegistry.register(zonedDateTimeType, "org.hibernate.type.ZonedDateTimeType", ZonedDateTime.class.getSimpleName(), ZonedDateTime.class.getName());
}
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class TypeDefinition method createResolution.
private static BasicValue.Resolution<?> createResolution(String name, Class<?> typeImplementorClass, Properties parameters, Map<?, ?> usageSiteProperties, JdbcTypeIndicators indicators, MetadataBuildingContext context) {
final BootstrapContext bootstrapContext = context.getBootstrapContext();
final TypeConfiguration typeConfiguration = bootstrapContext.getTypeConfiguration();
final BeanInstanceProducer instanceProducer = bootstrapContext.getBeanInstanceProducer();
final boolean isKnownType = Type.class.isAssignableFrom(typeImplementorClass) || UserType.class.isAssignableFrom(typeImplementorClass);
// support for AttributeConverter would be nice too
if (isKnownType) {
final Object typeInstance = instantiateType(bootstrapContext.getServiceRegistry(), name, typeImplementorClass, instanceProducer);
if (typeInstance instanceof TypeConfigurationAware) {
((TypeConfigurationAware) typeInstance).setTypeConfiguration(typeConfiguration);
}
final Properties combinedTypeParameters;
if (CollectionHelper.isNotEmpty(usageSiteProperties)) {
combinedTypeParameters = new Properties(parameters);
combinedTypeParameters.putAll(usageSiteProperties);
} else {
combinedTypeParameters = parameters;
}
injectParameters(typeInstance, combinedTypeParameters);
if (typeInstance instanceof UserType) {
final UserType<Object> userType = (UserType<Object>) typeInstance;
final CustomType<Object> customType = new CustomType<>(userType, typeConfiguration);
return new UserTypeResolution(customType, null, combinedTypeParameters);
}
if (typeInstance instanceof BasicType) {
final BasicType resolvedBasicType = (BasicType) typeInstance;
return new BasicValue.Resolution<Object>() {
@Override
public JdbcMapping getJdbcMapping() {
return resolvedBasicType;
}
@Override
public BasicType getLegacyResolvedBasicType() {
return resolvedBasicType;
}
@Override
public Properties getCombinedTypeParameters() {
return combinedTypeParameters;
}
@Override
public JavaType<Object> getDomainJavaType() {
return resolvedBasicType.getMappedJavaType();
}
@Override
public JavaType<?> getRelationalJavaType() {
return resolvedBasicType.getMappedJavaType();
}
@Override
public JdbcType getJdbcType() {
return resolvedBasicType.getJdbcType();
}
@Override
public BasicValueConverter getValueConverter() {
return null;
}
@Override
public MutabilityPlan<Object> getMutabilityPlan() {
// a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
return resolvedBasicType.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
}
};
}
}
if (Serializable.class.isAssignableFrom(typeImplementorClass)) {
final JavaType<Serializable> jtd = typeConfiguration.getJavaTypeRegistry().resolveDescriptor(typeImplementorClass);
final JdbcType jdbcType = typeConfiguration.getJdbcTypeRegistry().getDescriptor(Types.VARBINARY);
final BasicType<Serializable> resolved = typeConfiguration.getBasicTypeRegistry().resolve(jtd, jdbcType);
final SerializableType legacyType = new SerializableType(typeImplementorClass);
return new BasicValue.Resolution<Object>() {
@Override
public JdbcMapping getJdbcMapping() {
return resolved;
}
@Override
public BasicType getLegacyResolvedBasicType() {
return legacyType;
}
@Override
public JavaType<Object> getDomainJavaType() {
return (JavaType) resolved.getMappedJavaType();
}
@Override
public JavaType<?> getRelationalJavaType() {
return resolved.getMappedJavaType();
}
@Override
public JdbcType getJdbcType() {
return resolved.getJdbcType();
}
@Override
public BasicValueConverter getValueConverter() {
return null;
}
@Override
public MutabilityPlan<Object> getMutabilityPlan() {
// a TypeDefinition does not explicitly provide a MutabilityPlan (yet?)
return resolved.isMutable() ? getDomainJavaType().getMutabilityPlan() : ImmutableMutabilityPlan.instance();
}
};
}
throw new IllegalArgumentException("Named type [" + typeImplementorClass + "] did not implement BasicType nor UserType");
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class InferredBasicValueResolver method ordinalEnumValueResolution.
private static <E extends Enum<E>> InferredBasicValueResolution<E, Integer> ordinalEnumValueResolution(EnumJavaType<E> enumJavaType, BasicJavaType<?> explicitJavaType, JdbcType explicitJdbcType, TypeConfiguration typeConfiguration) {
final JavaType<Integer> relationalJtd;
if (explicitJavaType != null) {
if (!Integer.class.isAssignableFrom(explicitJavaType.getJavaTypeClass())) {
throw new MappingException("Explicit JavaType [" + explicitJavaType + "] applied to enumerated value with EnumType#ORDINAL" + " should handle `java.lang.Integer` as its relational type descriptor");
}
// noinspection unchecked
relationalJtd = (BasicJavaType<Integer>) explicitJavaType;
} else {
relationalJtd = typeConfiguration.getJavaTypeRegistry().getDescriptor(Integer.class);
}
final JdbcType jdbcType = explicitJdbcType != null ? explicitJdbcType : typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.TINYINT);
final OrdinalEnumValueConverter<E> valueConverter = new OrdinalEnumValueConverter<>(enumJavaType, jdbcType, relationalJtd);
return new InferredBasicValueResolution<>(typeConfiguration.getBasicTypeRegistry().resolve(relationalJtd, jdbcType), enumJavaType, relationalJtd, jdbcType, valueConverter, new CustomType<>(new org.hibernate.type.EnumType<>(enumJavaType.getJavaTypeClass(), valueConverter, typeConfiguration), typeConfiguration), ImmutableMutabilityPlan.instance());
}
use of org.hibernate.type.descriptor.jdbc.JdbcType in project hibernate-orm by hibernate.
the class InferredBasicValueResolver method from.
/**
* Create an inference-based resolution
*/
public static <T> BasicValue.Resolution<T> from(Function<TypeConfiguration, BasicJavaType> explicitJavaTypeAccess, Function<TypeConfiguration, JdbcType> explicitSqlTypeAccess, Type resolvedJavaType, Supplier<JavaType<T>> reflectedJtdResolver, JdbcTypeIndicators stdIndicators, Table table, Selectable selectable, String ownerName, String propertyName, TypeConfiguration typeConfiguration) {
final BasicJavaType<T> explicitJavaType = explicitJavaTypeAccess != null ? explicitJavaTypeAccess.apply(typeConfiguration) : null;
final JdbcType explicitJdbcType = explicitSqlTypeAccess != null ? explicitSqlTypeAccess.apply(typeConfiguration) : null;
return InferredBasicValueResolver.from(explicitJavaType, explicitJdbcType, resolvedJavaType, reflectedJtdResolver, stdIndicators, table, selectable, ownerName, propertyName, typeConfiguration);
}
Aggregations