|
| 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.type.descriptor.jdbc; |
| 8 | + |
| 9 | +import java.sql.CallableStatement; |
| 10 | +import java.sql.PreparedStatement; |
| 11 | +import java.sql.ResultSet; |
| 12 | +import java.sql.SQLException; |
| 13 | + |
| 14 | +import org.hibernate.dialect.MySQLDialect; |
| 15 | +import org.hibernate.type.descriptor.ValueBinder; |
| 16 | +import org.hibernate.type.descriptor.ValueExtractor; |
| 17 | +import org.hibernate.type.descriptor.WrapperOptions; |
| 18 | +import org.hibernate.type.descriptor.java.JavaType; |
| 19 | +import org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterCharacterData; |
| 20 | + |
| 21 | +import static org.hibernate.type.SqlTypes.ENUM; |
| 22 | +import static org.hibernate.type.SqlTypes.VARCHAR; |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents an {@code enum} type for databases like MySQL and H2. |
| 26 | + * <p> |
| 27 | + * Hibernate will automatically use this for enums mapped |
| 28 | + * as {@link jakarta.persistence.EnumType#STRING}. |
| 29 | + * |
| 30 | + * @see org.hibernate.type.SqlTypes#ENUM |
| 31 | + * @see MySQLDialect#getEnumTypeDeclaration(String, String[]) |
| 32 | + * |
| 33 | + * @author Gavin King |
| 34 | + */ |
| 35 | +public class EnumJdbcType implements JdbcType { |
| 36 | + |
| 37 | + public static final EnumJdbcType INSTANCE = new EnumJdbcType(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public int getJdbcTypeCode() { |
| 41 | + return VARCHAR; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int getDefaultSqlTypeCode() { |
| 46 | + return ENUM; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { |
| 51 | + return new JdbcLiteralFormatterCharacterData<>( javaType ); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getFriendlyName() { |
| 56 | + return "ENUM"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public <X> ValueBinder<X> getBinder(JavaType<X> javaType) { |
| 61 | + return new BasicBinder<>( javaType, this ) { |
| 62 | + @Override |
| 63 | + protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) |
| 64 | + throws SQLException { |
| 65 | + st.setString( index, getJavaType().unwrap( value, String.class, options ) ); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) |
| 70 | + throws SQLException { |
| 71 | + st.setString( name, getJavaType().unwrap( value, String.class, options ) ); |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) { |
| 78 | + return new BasicExtractor<>( javaType, this ) { |
| 79 | + @Override |
| 80 | + protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { |
| 81 | + return getJavaType().wrap( rs.getString( paramIndex ), options ); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { |
| 86 | + return getJavaType().wrap( statement.getString( index ), options ); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException { |
| 91 | + return getJavaType().wrap( statement.getString( name ), options ); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | +} |
0 commit comments