Skip to content

hit some stuff in o.h.t.d.jdbc with Extract Method #10033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + ( name != null ? name.hashCode() : 0 );
return result;
return Objects.hash( type, name );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
* @author Steve Ebersole
*/
public enum SourceType {
RESOURCE( "resource" ),
FILE( "file" ),
INPUT_STREAM( "input stream" ),
URL( "URL" ),
STRING( "string" ),
DOM( "xml" ),
JAR( "jar" ),
ANNOTATION( "annotation" ),
OTHER( "other" );

private final String legacyTypeText;

SourceType(String legacyTypeText) {
this.legacyTypeText = legacyTypeText;
}
RESOURCE,
FILE,
INPUT_STREAM,
URL,
STRING,
DOM,
JAR,
ANNOTATION,
OTHER;

public String getLegacyTypeText() {
return legacyTypeText;
return switch ( this ) {
case RESOURCE -> "resource";
case FILE -> "file";
case INPUT_STREAM -> "input stream";
case URL -> "URL";
case STRING -> "string";
case DOM -> "xml";
case JAR -> "jar";
case ANNOTATION -> "annotation";
case OTHER -> "other";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
/**
* A JPA {@link CriteriaBuilder} is a source of objects which may be composed
* to express a criteria query. The JPA-standard API defines all the operations
* needed express any query written in standard JPQL. This interface extends
* {@code CriteriaBuilder}, adding operations needed to express features of
* HQL which are not available in standard JPQL. For example:
* needed to express any query written in standard JPQL. This interface extends
* {@code CriteriaBuilder}, adding operations needed to express features of HQL
* which are not available in standard JPQL. For example:
* <ul>
* <li>JPQL does not have a {@code format()} function, so
* {@link #format(Expression, String)} is declared here, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

import java.lang.reflect.Field;
import java.sql.Types;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.type.SqlTypes;

import static java.util.Collections.unmodifiableMap;
import static org.hibernate.internal.CoreLogging.messageLogger;

/**
Expand All @@ -29,11 +29,11 @@ public final class JdbcTypeNameMapper {
private static final Map<String, Integer> SQL_TYPE_NAME_MAP = buildJdbcTypeNameMap( SqlTypes.class );

private static Map<Integer, String> buildJdbcTypeMap(Class<?> typesClass) {
HashMap<Integer, String> map = new HashMap<>();
final HashMap<Integer, String> map = new HashMap<>();
for ( Field field : typesClass.getFields() ) {
try {
final int code = field.getInt( null );
String old = map.put( code, field.getName() );
final String old = map.put( code, field.getName() );
if ( old != null ) {
LOG.JavaSqlTypesMappedSameCodeMultipleTimes( code, old, field.getName() );
}
Expand All @@ -42,11 +42,11 @@ private static Map<Integer, String> buildJdbcTypeMap(Class<?> typesClass) {
throw new HibernateException( "Unable to access JDBC type mapping [" + field.getName() + "]", e );
}
}
return Collections.unmodifiableMap( map );
return unmodifiableMap( map );
}

private static Map<String, Integer> buildJdbcTypeNameMap(Class<?> typesClass) {
HashMap<String, Integer> map = new HashMap<>();
final HashMap<String, Integer> map = new HashMap<>();
for ( Field field : typesClass.getFields() ) {
try {
final int code = field.getInt( null );
Expand All @@ -56,7 +56,7 @@ private static Map<String, Integer> buildJdbcTypeNameMap(Class<?> typesClass) {
throw new HibernateException( "Unable to access JDBC type mapping [" + field.getName() + "]", e );
}
}
return Collections.unmodifiableMap( map );
return unmodifiableMap( map );
}

/**
Expand Down Expand Up @@ -94,11 +94,8 @@ public static boolean isStandardTypeCode(Integer typeCode) {
* @return The type name.
*/
public static String getTypeName(Integer typeCode) {
String name = SQL_TYPE_MAP.get( typeCode );
if ( name == null ) {
return "UNKNOWN(" + typeCode + ")";
}
return name;
final String name = SQL_TYPE_MAP.get( typeCode );
return name == null ? "UNKNOWN(" + typeCode + ")" : name;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,13 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
return getObject( statement.getSQLXML( name ), options );
}

private XmlArrayJdbcType getXmlArrayJdbcType() {
return (XmlArrayJdbcType) getJdbcType();
}

private X getObject(SQLXML sqlxml, WrapperOptions options) throws SQLException {
if ( sqlxml == null ) {
return null;
}
return ( (XmlArrayJdbcType ) getJdbcType() ).fromString(
sqlxml.getString(),
getJavaType(),
options
);
return sqlxml == null ? null
: getXmlArrayJdbcType().fromString( sqlxml.getString(), getJavaType(), options );
}

};
Expand All @@ -127,22 +125,27 @@ public XmlArrayBinder(JavaType<X> javaType, XmlArrayJdbcType jdbcType) {
super( javaType, jdbcType );
}

private XmlArrayJdbcType getXmlArrayJdbcType() {
return (XmlArrayJdbcType) getJdbcType();
}

private SQLXML getSqlxml(PreparedStatement st, X value, WrapperOptions options) throws SQLException {
final String xml = getXmlArrayJdbcType().toString( value, getJavaType(), options );
SQLXML sqlxml = st.getConnection().createSQLXML();
sqlxml.setString( xml );
return sqlxml;
}

@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final String xml = ( (XmlArrayJdbcType) getJdbcType() ).toString( value, getJavaType(), options );
SQLXML sqlxml = st.getConnection().createSQLXML();
sqlxml.setString( xml );
st.setSQLXML( index, sqlxml );
st.setSQLXML( index, getSqlxml( st, value, options ) );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final String xml = ( (XmlArrayJdbcType ) getJdbcType() ).toString( value, getJavaType(), options );
SQLXML sqlxml = st.getConnection().createSQLXML();
sqlxml.setString( xml );
st.setSQLXML( name, sqlxml );
st.setSQLXML( name, getSqlxml( st, value, options ) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,26 @@ protected boolean needsLob(JdbcTypeIndicators indicators) {
if ( length > maxLength ) {
return true;
}

final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
else {
final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
|| typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.NCLOB, dialect ) );
}
}

@Override
public <X> ValueBinder<X> getBinder(JavaType<X> javaType) {
return new BasicBinder<>( javaType, this ) {

private XmlAsStringArrayJdbcType getXmlAsStringArrayJdbcType() {
return (XmlAsStringArrayJdbcType) getJdbcType();
}

@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
throws SQLException {
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
final String xml = jdbcType.toString( value, getJavaType(), options );
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
st.setNString( index, xml );
Expand All @@ -105,7 +111,7 @@ protected void doBind(PreparedStatement st, X value, int index, WrapperOptions o
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
final String xml = jdbcType.toString( value, getJavaType(), options );
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
st.setNString( name, xml );
Expand All @@ -120,9 +126,14 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
@Override
public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
return new BasicExtractor<>( javaType, this ) {

private XmlAsStringArrayJdbcType getXmlAsStringArrayJdbcType() {
return (XmlAsStringArrayJdbcType) getJdbcType();
}

@Override
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
final String value;
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
value = rs.getNString( paramIndex );
Expand All @@ -136,7 +147,7 @@ protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) thro
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options)
throws SQLException {
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
final String value;
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
value = statement.getNString( index );
Expand All @@ -150,7 +161,7 @@ protected X doExtract(CallableStatement statement, int index, WrapperOptions opt
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
throws SQLException {
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
final String value;
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
value = statement.getNString( name );
Expand Down
Loading