Skip to content

Commit e223a46

Browse files
committed
hit some stuff in o.h.t.d.jdbc with Extract Method
and use a record for TypeConstructedJdbcTypeKey
1 parent a514b26 commit e223a46

File tree

6 files changed

+242
-212
lines changed

6 files changed

+242
-212
lines changed

Diff for: hibernate-core/src/main/java/org/hibernate/type/descriptor/JdbcTypeNameMapper.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
import java.lang.reflect.Field;
88
import java.sql.Types;
9-
import java.util.Collections;
109
import java.util.HashMap;
1110
import java.util.Map;
1211

1312
import org.hibernate.HibernateException;
1413
import org.hibernate.internal.CoreMessageLogger;
1514
import org.hibernate.type.SqlTypes;
1615

16+
import static java.util.Collections.unmodifiableMap;
1717
import static org.hibernate.internal.CoreLogging.messageLogger;
1818

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

3131
private static Map<Integer, String> buildJdbcTypeMap(Class<?> typesClass) {
32-
HashMap<Integer, String> map = new HashMap<>();
32+
final HashMap<Integer, String> map = new HashMap<>();
3333
for ( Field field : typesClass.getFields() ) {
3434
try {
3535
final int code = field.getInt( null );
36-
String old = map.put( code, field.getName() );
36+
final String old = map.put( code, field.getName() );
3737
if ( old != null ) {
3838
LOG.JavaSqlTypesMappedSameCodeMultipleTimes( code, old, field.getName() );
3939
}
@@ -42,11 +42,11 @@ private static Map<Integer, String> buildJdbcTypeMap(Class<?> typesClass) {
4242
throw new HibernateException( "Unable to access JDBC type mapping [" + field.getName() + "]", e );
4343
}
4444
}
45-
return Collections.unmodifiableMap( map );
45+
return unmodifiableMap( map );
4646
}
4747

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

6262
/**
@@ -94,11 +94,8 @@ public static boolean isStandardTypeCode(Integer typeCode) {
9494
* @return The type name.
9595
*/
9696
public static String getTypeName(Integer typeCode) {
97-
String name = SQL_TYPE_MAP.get( typeCode );
98-
if ( name == null ) {
99-
return "UNKNOWN(" + typeCode + ")";
100-
}
101-
return name;
97+
final String name = SQL_TYPE_MAP.get( typeCode );
98+
return name == null ? "UNKNOWN(" + typeCode + ")" : name;
10299
}
103100

104101
/**

Diff for: hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlArrayJdbcType.java

+19-16
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
108108
return getObject( statement.getSQLXML( name ), options );
109109
}
110110

111+
private XmlArrayJdbcType getXmlArrayJdbcType() {
112+
return (XmlArrayJdbcType) getJdbcType();
113+
}
114+
111115
private X getObject(SQLXML sqlxml, WrapperOptions options) throws SQLException {
112-
if ( sqlxml == null ) {
113-
return null;
114-
}
115-
return ( (XmlArrayJdbcType ) getJdbcType() ).fromString(
116-
sqlxml.getString(),
117-
getJavaType(),
118-
options
119-
);
116+
return sqlxml == null ? null
117+
: getXmlArrayJdbcType().fromString( sqlxml.getString(), getJavaType(), options );
120118
}
121119

122120
};
@@ -127,22 +125,27 @@ public XmlArrayBinder(JavaType<X> javaType, XmlArrayJdbcType jdbcType) {
127125
super( javaType, jdbcType );
128126
}
129127

128+
private XmlArrayJdbcType getXmlArrayJdbcType() {
129+
return (XmlArrayJdbcType) getJdbcType();
130+
}
131+
132+
private SQLXML getSqlxml(PreparedStatement st, X value, WrapperOptions options) throws SQLException {
133+
final String xml = getXmlArrayJdbcType().toString( value, getJavaType(), options );
134+
SQLXML sqlxml = st.getConnection().createSQLXML();
135+
sqlxml.setString( xml );
136+
return sqlxml;
137+
}
138+
130139
@Override
131140
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
132141
throws SQLException {
133-
final String xml = ( (XmlArrayJdbcType) getJdbcType() ).toString( value, getJavaType(), options );
134-
SQLXML sqlxml = st.getConnection().createSQLXML();
135-
sqlxml.setString( xml );
136-
st.setSQLXML( index, sqlxml );
142+
st.setSQLXML( index, getSqlxml( st, value, options ) );
137143
}
138144

139145
@Override
140146
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
141147
throws SQLException {
142-
final String xml = ( (XmlArrayJdbcType ) getJdbcType() ).toString( value, getJavaType(), options );
143-
SQLXML sqlxml = st.getConnection().createSQLXML();
144-
sqlxml.setString( xml );
145-
st.setSQLXML( name, sqlxml );
148+
st.setSQLXML( name, getSqlxml( st, value, options ) );
146149
}
147150
}
148151
}

Diff for: hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlAsStringArrayJdbcType.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,26 @@ protected boolean needsLob(JdbcTypeIndicators indicators) {
7979
if ( length > maxLength ) {
8080
return true;
8181
}
82-
83-
final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
84-
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
85-
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
82+
else {
83+
final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
84+
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
85+
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
8686
|| typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.NCLOB, dialect ) );
87+
}
8788
}
8889

8990
@Override
9091
public <X> ValueBinder<X> getBinder(JavaType<X> javaType) {
9192
return new BasicBinder<>( javaType, this ) {
93+
94+
private XmlAsStringArrayJdbcType getXmlAsStringArrayJdbcType() {
95+
return (XmlAsStringArrayJdbcType) getJdbcType();
96+
}
97+
9298
@Override
9399
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
94100
throws SQLException {
95-
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
101+
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
96102
final String xml = jdbcType.toString( value, getJavaType(), options );
97103
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
98104
st.setNString( index, xml );
@@ -105,7 +111,7 @@ protected void doBind(PreparedStatement st, X value, int index, WrapperOptions o
105111
@Override
106112
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
107113
throws SQLException {
108-
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
114+
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
109115
final String xml = jdbcType.toString( value, getJavaType(), options );
110116
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
111117
st.setNString( name, xml );
@@ -120,9 +126,14 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
120126
@Override
121127
public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) {
122128
return new BasicExtractor<>( javaType, this ) {
129+
130+
private XmlAsStringArrayJdbcType getXmlAsStringArrayJdbcType() {
131+
return (XmlAsStringArrayJdbcType) getJdbcType();
132+
}
133+
123134
@Override
124135
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
125-
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
136+
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
126137
final String value;
127138
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
128139
value = rs.getNString( paramIndex );
@@ -136,7 +147,7 @@ protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) thro
136147
@Override
137148
protected X doExtract(CallableStatement statement, int index, WrapperOptions options)
138149
throws SQLException {
139-
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
150+
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
140151
final String value;
141152
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
142153
value = statement.getNString( index );
@@ -150,7 +161,7 @@ protected X doExtract(CallableStatement statement, int index, WrapperOptions opt
150161
@Override
151162
protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
152163
throws SQLException {
153-
final XmlAsStringArrayJdbcType jdbcType = (XmlAsStringArrayJdbcType) getJdbcType();
164+
final XmlAsStringArrayJdbcType jdbcType = getXmlAsStringArrayJdbcType();
154165
final String value;
155166
if ( jdbcType.nationalized && options.getDialect().supportsNationalizedMethods() ) {
156167
value = statement.getNString( name );

Diff for: hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlAsStringJdbcType.java

+38-43
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,31 @@ protected boolean needsLob(JdbcTypeIndicators indicators) {
109109
if ( length > maxLength ) {
110110
return true;
111111
}
112-
113-
final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
114-
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
115-
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
116-
|| typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.NCLOB, dialect ) );
112+
else {
113+
final DdlTypeRegistry ddlTypeRegistry = indicators.getTypeConfiguration().getDdlTypeRegistry();
114+
final String typeName = ddlTypeRegistry.getTypeName( getDdlTypeCode(), dialect );
115+
return typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.CLOB, dialect ) )
116+
|| typeName.equals( ddlTypeRegistry.getTypeName( SqlTypes.NCLOB, dialect ) );
117+
}
117118
}
118119

119120
@Override
120121
public <X> ValueBinder<X> getBinder(JavaType<X> javaType) {
121122
if ( nationalized ) {
122123
return new BasicBinder<>( javaType, this ) {
124+
125+
private XmlAsStringJdbcType getXmlAsStringJdbcType() {
126+
return (XmlAsStringJdbcType) getJdbcType();
127+
}
128+
129+
private String getXml(X value, WrapperOptions options) throws SQLException {
130+
return getXmlAsStringJdbcType().toString( value, getJavaType(), options );
131+
}
132+
123133
@Override
124134
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
125135
throws SQLException {
126-
final String xml = ( (XmlAsStringJdbcType) getJdbcType() ).toString(
127-
value,
128-
getJavaType(),
129-
options
130-
);
136+
final String xml = getXml( value, options );
131137
if ( options.getDialect().supportsNationalizedMethods() ) {
132138
st.setNString( index, xml );
133139
}
@@ -139,11 +145,7 @@ protected void doBind(PreparedStatement st, X value, int index, WrapperOptions o
139145
@Override
140146
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
141147
throws SQLException {
142-
final String xml = ( (XmlAsStringJdbcType) getJdbcType() ).toString(
143-
value,
144-
getJavaType(),
145-
options
146-
);
148+
final String xml = getXml( value, options );
147149
if ( options.getDialect().supportsNationalizedMethods() ) {
148150
st.setNString( name, xml );
149151
}
@@ -155,26 +157,25 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
155157
}
156158
else {
157159
return new BasicBinder<>( javaType, this ) {
160+
161+
private XmlAsStringJdbcType getXmlAsStringJdbcType() {
162+
return (XmlAsStringJdbcType) getJdbcType();
163+
}
164+
165+
private String getXml(X value, WrapperOptions options) throws SQLException {
166+
return getXmlAsStringJdbcType().toString( value, getJavaType(), options );
167+
}
168+
158169
@Override
159170
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
160171
throws SQLException {
161-
final String xml = ( (XmlAsStringJdbcType) getJdbcType() ).toString(
162-
value,
163-
getJavaType(),
164-
options
165-
);
166-
st.setString( index, xml );
172+
st.setString( index, getXml( value, options ) );
167173
}
168174

169175
@Override
170176
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
171177
throws SQLException {
172-
final String xml = ( (XmlAsStringJdbcType) getJdbcType() ).toString(
173-
value,
174-
getJavaType(),
175-
options
176-
);
177-
st.setString( name, xml );
178+
st.setString( name, getXml( value, options ) );
178179
}
179180
};
180181
}
@@ -218,20 +219,21 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
218219
}
219220

220221
private X getObject(String xml, WrapperOptions options) throws SQLException {
221-
if ( xml == null ) {
222-
return null;
223-
}
224-
return ( (XmlAsStringJdbcType) getJdbcType() ).fromString(
225-
xml,
226-
getJavaType(),
227-
options
228-
);
222+
return xml == null ? null : getXmlAsStringJdbcType().fromString( xml, getJavaType(), options );
223+
}
224+
225+
private XmlAsStringJdbcType getXmlAsStringJdbcType() {
226+
return (XmlAsStringJdbcType) getJdbcType();
229227
}
230228
};
231229
}
232230
else {
233231
return new BasicExtractor<>( javaType, this ) {
234232

233+
private XmlAsStringJdbcType getXmlAsStringJdbcType() {
234+
return (XmlAsStringJdbcType) getJdbcType();
235+
}
236+
235237
@Override
236238
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
237239
return getObject( rs.getString( paramIndex ), options );
@@ -249,14 +251,7 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
249251
}
250252

251253
private X getObject(String xml, WrapperOptions options) throws SQLException {
252-
if ( xml == null ) {
253-
return null;
254-
}
255-
return ( (XmlAsStringJdbcType) getJdbcType() ).fromString(
256-
xml,
257-
getJavaType(),
258-
options
259-
);
254+
return xml == null ? null : getXmlAsStringJdbcType().fromString( xml, getJavaType(), options );
260255
}
261256
};
262257
}

Diff for: hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/XmlJdbcType.java

+19-16
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,12 @@ protected X doExtract(CallableStatement statement, String name, WrapperOptions o
128128
}
129129

130130
private X getObject(SQLXML sqlxml, WrapperOptions options) throws SQLException {
131-
if ( sqlxml == null ) {
132-
return null;
133-
}
134-
return ( (XmlJdbcType) getJdbcType() ).fromString(
135-
sqlxml.getString(),
136-
getJavaType(),
137-
options
138-
);
131+
return sqlxml == null ? null
132+
: getXmlJdbcType().fromString( sqlxml.getString(), getJavaType(), options );
133+
}
134+
135+
private XmlJdbcType getXmlJdbcType() {
136+
return (XmlJdbcType) getJdbcType();
139137
}
140138
};
141139
}
@@ -145,22 +143,27 @@ public XmlValueBinder(JavaType<X> javaType, JdbcType jdbcType) {
145143
super( javaType, jdbcType );
146144
}
147145

146+
private XmlJdbcType getXmlJdbcType() {
147+
return (XmlJdbcType) getJdbcType();
148+
}
149+
150+
private SQLXML getSqlxml(PreparedStatement st, X value, WrapperOptions options) throws SQLException {
151+
final String xml = getXmlJdbcType().toString( value, getJavaType(), options );
152+
SQLXML sqlxml = st.getConnection().createSQLXML();
153+
sqlxml.setString( xml );
154+
return sqlxml;
155+
}
156+
148157
@Override
149158
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
150159
throws SQLException {
151-
final String xml = ( (XmlJdbcType) getJdbcType() ).toString( value, getJavaType(), options );
152-
SQLXML sqlxml = st.getConnection().createSQLXML();
153-
sqlxml.setString( xml );
154-
st.setSQLXML( index, sqlxml );
160+
st.setSQLXML( index, getSqlxml( st, value, options ) );
155161
}
156162

157163
@Override
158164
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
159165
throws SQLException {
160-
final String xml = ( (XmlJdbcType) getJdbcType() ).toString( value, getJavaType(), options );
161-
SQLXML sqlxml = st.getConnection().createSQLXML();
162-
sqlxml.setString( xml );
163-
st.setSQLXML( name, sqlxml );
166+
st.setSQLXML( name, getSqlxml( st, value, options ) );
164167
}
165168
}
166169
}

0 commit comments

Comments
 (0)