|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.dialect; |
| 8 | + |
| 9 | +import org.hibernate.boot.model.relational.Database; |
| 10 | +import org.hibernate.boot.model.relational.NamedAuxiliaryDatabaseObject; |
| 11 | +import org.hibernate.engine.jdbc.Size; |
| 12 | +import org.hibernate.type.descriptor.ValueBinder; |
| 13 | +import org.hibernate.type.descriptor.ValueExtractor; |
| 14 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 15 | +import org.hibernate.type.descriptor.converter.internal.EnumHelper; |
| 16 | +import org.hibernate.type.descriptor.java.JavaType; |
| 17 | +import org.hibernate.type.descriptor.jdbc.BasicBinder; |
| 18 | +import org.hibernate.type.descriptor.jdbc.BasicExtractor; |
| 19 | +import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; |
| 20 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 21 | +import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; |
| 22 | +import org.hibernate.type.spi.TypeConfiguration; |
| 23 | + |
| 24 | +import java.sql.CallableStatement; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.ResultSet; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.sql.Types; |
| 29 | +import java.util.Arrays; |
| 30 | + |
| 31 | +import static java.util.Collections.emptySet; |
| 32 | +import static org.hibernate.type.SqlTypes.NAMED_ENUM; |
| 33 | + |
| 34 | +/** |
| 35 | + * Represents a named {@code enum} type on Oracle 23ai+. |
| 36 | + * <p> |
| 37 | + * Hibernate does <em>not</em> automatically use this for enums |
| 38 | + * mapped as {@link jakarta.persistence.EnumType#STRING}, and |
| 39 | + * instead this type must be explicitly requested using: |
| 40 | + * <pre> |
| 41 | + * @JdbcTypeCode(SqlTypes.NAMED_ENUM) |
| 42 | + * </pre> |
| 43 | + * |
| 44 | + * @see org.hibernate.type.SqlTypes#NAMED_ENUM |
| 45 | + * @see OracleDialect#getEnumTypeDeclaration(String, String[]) |
| 46 | + * @see OracleDialect#getCreateEnumTypeCommand(String, String[]) |
| 47 | + * |
| 48 | + * @author Loïc Lefèvre |
| 49 | + */ |
| 50 | +public class OracleEnumJdbcType implements JdbcType { |
| 51 | + |
| 52 | + public static final OracleEnumJdbcType INSTANCE = new OracleEnumJdbcType(); |
| 53 | + |
| 54 | + @Override |
| 55 | + public int getJdbcTypeCode() { |
| 56 | + return Types.VARCHAR; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int getDefaultSqlTypeCode() { |
| 61 | + return NAMED_ENUM; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { |
| 66 | + return (appender, value, dialect, wrapperOptions) -> appender.appendSql( dialect.getEnumTypeDeclaration( (Class<? extends Enum<?>>) javaType.getJavaType() )+"." + ((Enum<?>) value).name() ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getFriendlyName() { |
| 71 | + return "ENUM"; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + return "EnumTypeDescriptor"; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public <X> ValueBinder<X> getBinder(JavaType<X> javaType) { |
| 81 | + return new BasicBinder<>( javaType, this ) { |
| 82 | + @Override |
| 83 | + protected void doBindNull(PreparedStatement st, int index, WrapperOptions options) throws SQLException { |
| 84 | + st.setNull( index, getJdbcTypeCode() ); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void doBindNull(CallableStatement st, String name, WrapperOptions options) throws SQLException { |
| 89 | + st.setNull( name, getJdbcTypeCode() ); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) |
| 94 | + throws SQLException { |
| 95 | + st.setString( index, ((Enum<?>) value).name() ); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) |
| 100 | + throws SQLException { |
| 101 | + st.setString( name, ((Enum<?>) value).name() ); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) { |
| 108 | + return new BasicExtractor<>( javaType, this ) { |
| 109 | + @Override |
| 110 | + protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { |
| 111 | + return getJavaType().wrap( rs.getString( paramIndex ), options ); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { |
| 116 | + return getJavaType().wrap( statement.getString( index ), options ); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException { |
| 121 | + return getJavaType().wrap( statement.getString( name ), options ); |
| 122 | + } |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void addAuxiliaryDatabaseObjects( |
| 128 | + JavaType<?> javaType, |
| 129 | + Size columnSize, |
| 130 | + Database database, |
| 131 | + JdbcTypeIndicators context) { |
| 132 | + addAuxiliaryDatabaseObjects( javaType, database, true ); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void addAuxiliaryDatabaseObjects( |
| 137 | + JavaType<?> javaType, |
| 138 | + Size columnSize, |
| 139 | + Database database, |
| 140 | + TypeConfiguration typeConfiguration) { |
| 141 | + addAuxiliaryDatabaseObjects( javaType, database, true ); |
| 142 | + } |
| 143 | + |
| 144 | + private void addAuxiliaryDatabaseObjects( |
| 145 | + JavaType<?> javaType, |
| 146 | + Database database, |
| 147 | + boolean sortEnumValues) { |
| 148 | + final Dialect dialect = database.getDialect(); |
| 149 | + final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) javaType.getJavaType(); |
| 150 | + final String enumTypeName = enumClass.getSimpleName(); |
| 151 | + final String[] enumeratedValues = EnumHelper.getEnumeratedValues( enumClass ); |
| 152 | + if ( sortEnumValues ) { |
| 153 | + Arrays.sort( enumeratedValues ); |
| 154 | + } |
| 155 | + final String[] create = getCreateEnumTypeCommand( |
| 156 | + javaType.getJavaTypeClass().getSimpleName(), |
| 157 | + enumeratedValues |
| 158 | + ); |
| 159 | + final String[] drop = dialect.getDropEnumTypeCommand( enumClass ); |
| 160 | + if ( create != null && create.length > 0 ) { |
| 161 | + database.addAuxiliaryDatabaseObject( |
| 162 | + new NamedAuxiliaryDatabaseObject( |
| 163 | + enumTypeName, |
| 164 | + database.getDefaultNamespace(), |
| 165 | + create, |
| 166 | + drop, |
| 167 | + emptySet(), |
| 168 | + true |
| 169 | + ) |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Used to generate the CREATE DDL command for Data Use Case Domain based on VARCHAR2 values. |
| 176 | + * |
| 177 | + * @param name |
| 178 | + * @param values |
| 179 | + * @return the DDL command to create that enum |
| 180 | + */ |
| 181 | + public String[] getCreateEnumTypeCommand(String name, String[] values) { |
| 182 | + final StringBuilder domain = new StringBuilder(); |
| 183 | + domain.append( "create domain " ) |
| 184 | + .append( name ) |
| 185 | + .append( " as enum (" ); |
| 186 | + String separator = ""; |
| 187 | + for ( String value : values ) { |
| 188 | + domain.append( separator ).append( value ).append("='").append(value).append("'"); |
| 189 | + separator = ", "; |
| 190 | + } |
| 191 | + domain.append( ')' ); |
| 192 | + return new String[] { domain.toString() }; |
| 193 | + } |
| 194 | +} |
0 commit comments