Skip to content

Commit 1d5f6b5

Browse files
committed
finish off jdoc for DDLTypes
1 parent 867b114 commit 1d5f6b5

File tree

5 files changed

+122
-25
lines changed

5 files changed

+122
-25
lines changed

hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@
2424
* {@link org.hibernate.type.descriptor.java.JavaType#getRecommendedJdbcType},
2525
* or when the {@link org.hibernate.annotations.JdbcTypeCode @JdbcTypeCode}
2626
* annotation is used, for example.
27+
* <p>
28+
* A type code may also be used as a key to obtain a dialect-specific
29+
* {@link org.hibernate.type.descriptor.sql.DdlType} for the purposes of
30+
* generating DDL.
2731
*
2832
* @see org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry
33+
* @see org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry
2934
*
3035
* @author Christian Beikov
3136
*/

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/ArrayJdbcType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public class ArrayJdbcType implements JdbcType {
3939

4040
public static final ArrayJdbcType INSTANCE = new ArrayJdbcType( ObjectJdbcType.INSTANCE );
41-
private static final ClassValue<Method> NAME_BINDER = new ClassValue<Method>() {
41+
private static final ClassValue<Method> NAME_BINDER = new ClassValue<>() {
4242
@Override
4343
protected Method computeValue(Class<?> type) {
4444
try {
@@ -137,7 +137,7 @@ protected void doBind(CallableStatement st, X value, String name, WrapperOptions
137137
throw new HibernateException( "JDBC driver does not support named parameters for setArray. Use positional.", ex );
138138
}
139139
}
140-
// Not that it's supposed to have setArray(String,Array) by standard.
140+
// Note that it's supposed to have setArray(String,Array) by standard.
141141
// There are numerous missing methods that only have versions for positional parameter,
142142
// but not named ones.
143143

hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/DdlType.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
package org.hibernate.type.descriptor.sql;
88

99
import java.io.Serializable;
10-
import java.sql.Types;
1110

1211
import org.hibernate.Incubating;
12+
import org.hibernate.dialect.Dialect;
1313
import org.hibernate.engine.jdbc.Size;
14-
import org.hibernate.metamodel.mapping.JdbcMapping;
1514
import org.hibernate.metamodel.mapping.SqlExpressible;
1615
import org.hibernate.type.SqlTypes;
1716
import org.hibernate.type.descriptor.java.JavaType;
1817
import org.hibernate.type.descriptor.jdbc.JdbcType;
1918

2019
/**
21-
* Descriptor for a DDL type.
20+
* Descriptor for a DDL column type. An instance of this type abstracts over
21+
* a parameterized family of {@linkplain Dialect dialect-specific} SQL types
22+
* with the same {@linkplain #getSqlTypeCode() type code} but varying length,
23+
* precision, and scale. Usually, the types belonging to the family share a
24+
* single {@linkplain #getRawTypeName() type name} in SQL, but in certain
25+
* cases, most notably, in the case of the MySQL LOB types {@code text} and
26+
* {@code blob}, it's the type name itself which is parameter-dependent.
2227
*
2328
* @author Christian Beikov
2429
*/
@@ -46,20 +51,46 @@ default String[] getRawTypeNames() {
4651

4752
String getTypeNamePattern();
4853

54+
/**
55+
* Return a type with length, precision, and scale specified by the given
56+
* {@linkplain Size size object}.
57+
*/
4958
default String getTypeName(Size size) {
5059
return getTypeName( size.getLength(), size.getPrecision(), size.getScale() );
5160
}
5261

62+
/**
63+
* Return a type with the given length, precision, and scale.
64+
*/
5365
String getTypeName(Long size, Integer precision, Integer scale);
5466

67+
/**
68+
* Return the database type corresponding to the given {@link JdbcType}
69+
* that may be used as a target type in casting operations using the SQL
70+
* {@code CAST()} function, using the given {@link JavaType} to help
71+
* determine the appropriate precision and scale. The length is usually
72+
* chosen to be the maximum possible length for the dialect.
73+
*
74+
* @see JavaType#getDefaultSqlScale(Dialect, JdbcType)
75+
* @see JavaType#getDefaultSqlPrecision(Dialect, JdbcType)
76+
* @see Dialect#getMaxVarcharLength()
77+
*
78+
* @return The SQL type name
79+
*/
5580
String getCastTypeName(JdbcType jdbcType, JavaType<?> javaType);
5681

5782
/**
58-
* Get the name of the database type appropriate for casting operations
59-
* (via the CAST() SQL function) for the given {@link SqlExpressible}
60-
* SQL type.
83+
* Return the database type with the given length, precision, and scale,
84+
* if specified, corresponding to the {@link SqlExpressible#getJdbcMapping()
85+
* JdbcMapping} of the given {@link SqlExpressible}, that may be used as a
86+
* target type in casting operations using the SQL {@code CAST()} function.
87+
*
88+
* @param type the {@link SqlExpressible}
89+
* @param length the length, or null, if unspecified
90+
* @param precision the precision, or null, if unspecified
91+
* @param scale the scale, or null, if unspecified
6192
*
62-
* @return The database type name
93+
* @return The SQL type name
6394
*/
6495
default String getCastTypeName(SqlExpressible type, Long length, Integer precision, Integer scale) {
6596
return getCastTypeName(
@@ -71,5 +102,20 @@ default String getCastTypeName(SqlExpressible type, Long length, Integer precisi
71102
);
72103
}
73104

105+
/**
106+
* Return the database type with the given length, precision, and scale,
107+
* if specified, corresponding to the given {@link JdbcType}, that may
108+
* be used as a target type in casting operations using the SQL
109+
* {@code CAST()} function, using the given {@link JavaType} to help
110+
* determine the appropriate precision and scale. The length, if not
111+
* explicitly specified, is usually chosen to be the maximum possible
112+
* length for the dialect.
113+
*
114+
* @see JavaType#getDefaultSqlScale(Dialect, JdbcType)
115+
* @see JavaType#getDefaultSqlPrecision(Dialect, JdbcType)
116+
* @see Dialect#getMaxVarcharLength()
117+
*
118+
* @return The SQL type name
119+
*/
74120
String getCastTypeName(JdbcType jdbcType, JavaType<?> javaType, Long length, Integer precision, Integer scale);
75121
}

hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import org.jboss.logging.Logger;
2424

2525
/**
26-
* Basically a map from SQL type code (int) -> {@link DdlType}
26+
* A registry mapping {@link org.hibernate.type.SqlTypes JDBC type codes}
27+
* to instances of the {@link DdlType} interface.
2728
*
2829
* @author Christian Beikov
2930
*
@@ -42,10 +43,17 @@ public DdlTypeRegistry(TypeConfiguration typeConfiguration) {
4243
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4344
// baseline descriptors
4445

46+
/**
47+
* Add a mapping from the {@linkplain DdlType#getSqlTypeCode() type code}
48+
* of the given {@link DdlType} to the given {@code DdlType}.
49+
*/
4550
public void addDescriptor(DdlType ddlType) {
4651
addDescriptor( ddlType.getSqlTypeCode(), ddlType );
4752
}
4853

54+
/**
55+
* Add a mapping from the given type code to the given {@link DdlType}.
56+
*/
4957
public void addDescriptor(int sqlTypeCode, DdlType ddlType) {
5058
final DdlType previous = ddlTypes.put( sqlTypeCode, ddlType );
5159
if ( previous != null && previous != ddlType ) {
@@ -57,16 +65,29 @@ public void addDescriptor(int sqlTypeCode, DdlType ddlType) {
5765
addSqlType( ddlType, sqlTypeCode );
5866
}
5967

68+
/**
69+
* Add a mapping from the {@linkplain DdlType#getSqlTypeCode() type code}
70+
* of the given {@link DdlType} to the given {@code DdlType}, if there
71+
* is no mapping already present for that type code.
72+
*/
6073
public void addDescriptorIfAbsent(DdlType ddlType) {
6174
addDescriptorIfAbsent( ddlType.getSqlTypeCode(), ddlType );
6275
}
6376

77+
/**
78+
* Add a mapping from the given type code to the given {@link DdlType},
79+
* if there is no mapping already present for the given type code.
80+
*/
6481
public void addDescriptorIfAbsent(int sqlTypeCode, DdlType ddlType) {
6582
if ( ddlTypes.putIfAbsent( sqlTypeCode, ddlType ) == null ) {
6683
addSqlType( ddlType, sqlTypeCode );
6784
}
6885
}
6986

87+
/**
88+
* Add a mapping from the given type code to the raw type name of the
89+
* given {@link DdlType}.
90+
*/
7091
private void addSqlType(DdlType ddlType, int sqlTypeCode) {
7192
for ( String rawTypeName : ddlType.getRawTypeNames() ) {
7293
final Integer previousSqlTypeCode = sqlTypes.put( rawTypeName, sqlTypeCode );
@@ -78,7 +99,8 @@ private void addSqlType(DdlType ddlType, int sqlTypeCode) {
7899
}
79100

80101
/**
81-
* Returns the {@link SqlTypes} type code for the given DDL raw type name, or <code>null</code> if it is unknown.
102+
* Returns the {@link SqlTypes} type code for the given DDL raw type name, or
103+
* {@code null} if the type code cannot be determined from the registrations.
82104
*/
83105
public Integer getSqlTypeCode(String rawTypeName) {
84106
return sqlTypes.get( rawTypeName );
@@ -87,11 +109,11 @@ public Integer getSqlTypeCode(String rawTypeName) {
87109
/**
88110
* Returns the registered {@link DdlType} for the given SQL type code.
89111
* <p>
90-
* Not that the "long" types {@link Types#LONGVARCHAR}, {@link Types#LONGNVARCHAR}
91-
* and {@link Types#LONGVARBINARY} are considered synonyms for their
92-
* non-{@code LONG} counterparts, with the only difference being that
93-
* a different default length is used: {@link org.hibernate.Length#LONG}
94-
* instead of {@link org.hibernate.Length#DEFAULT}.
112+
* Note that the "long" types {@link Types#LONGVARCHAR}, {@link Types#LONGNVARCHAR},
113+
* and {@link Types#LONGVARBINARY} are considered synonyms for their non-{@code LONG}
114+
* counterparts, with the only difference being that a different default length is
115+
* used by default: {@link org.hibernate.Length#LONG} instead of
116+
* {@link org.hibernate.Length#DEFAULT}.
95117
*
96118
*/
97119
public DdlType getDescriptor(int sqlTypeCode) {
@@ -113,6 +135,15 @@ public DdlType getDescriptor(int sqlTypeCode) {
113135
return ddlType;
114136
}
115137

138+
/**
139+
* Get the SQL type name for the specified {@link java.sql.Types JDBC type code},
140+
* filling in the placemarkers {@code $l}, {@code $p}, and {@code $s}
141+
* with the default length, precision, and scale for the given SQL dialect.
142+
*
143+
* @param typeCode the JDBC type code
144+
* @param dialect the dialect which determines the default length, precision, and scale
145+
* @return a SQL column type
146+
*/
116147
public String getTypeName(int typeCode, Dialect dialect) {
117148
// explicitly enforce dialect's default precisions
118149
switch ( typeCode ) {
@@ -133,22 +164,36 @@ public String getTypeName(int typeCode, Dialect dialect) {
133164
}
134165
}
135166

167+
/**
168+
* Get the SQL type name for the specified {@link java.sql.Types JDBC type code}
169+
* and size, filling in the placemarkers {@code $l}, {@code $p}, and {@code $s}
170+
* with the length, precision, and scale determined by the given {@linkplain Size
171+
* size object}. The returned type name should be of a SQL type large enough to
172+
* accommodate values of the specified size.
173+
*
174+
* @param typeCode the JDBC type code
175+
* @param size an object which determines the length, precision, and scale
176+
*
177+
* @return the associated type name with the smallest capacity that accommodates
178+
* the given size, if available, and the default type name otherwise
179+
*/
136180
public String getTypeName(int typeCode, Size size) {
137181
return getTypeName( typeCode, size.getLength(), size.getPrecision(), size.getScale() );
138182
}
139183

140184
/**
141185
* Get the SQL type name for the specified {@link java.sql.Types JDBC type code}
142186
* and size, filling in the placemarkers {@code $l}, {@code $p}, and {@code $s}
143-
* with the given length, precision, and scale.
187+
* with the given length, precision, and scale. The returned type name should be
188+
* of a SQL type large enough to accommodate values of the specified size.
144189
*
145190
* @param typeCode the JDBC type code
146191
* @param size the SQL length, if any
147192
* @param precision the SQL precision, if any
148193
* @param scale the SQL scale, if any
149194
*
150-
* @return the associated name with smallest capacity >= size, if available and
151-
* the default type name otherwise
195+
* @return the associated type name with the smallest capacity that accommodates
196+
* the given size, if available, and the default type name otherwise
152197
*/
153198
public String getTypeName(int typeCode, Long size, Integer precision, Integer scale) {
154199
final DdlType descriptor = getDescriptor( typeCode );
@@ -165,12 +210,13 @@ public String getTypeName(int typeCode, Long size, Integer precision, Integer sc
165210
}
166211

167212
/**
168-
* Whether or not the given type name has been registered for this dialect (including both hibernate type names and
169-
* custom-registered type names).
213+
* Determines if there is a registered {@link DdlType} whose {@linkplain
214+
* DdlType#getRawTypeName() raw type name} matches the given type name,
215+
* taking into account DDL types registered by Hibernate.
170216
*
171217
* @param typeName the type name.
172218
*
173-
* @return true if the given string has been registered either as a hibernate type or as a custom-registered one
219+
* @return {@code true} if there is a DDL type with the given raw type name
174220
*/
175221
public boolean isTypeNameRegistered(final String typeName) {
176222
for ( DdlType value : ddlTypes.values() ) {

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/ParameterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void testNonPrimitiveArrayParameterBinding() {
6262
ParameterExpression<Integer[]> param = em.getCriteriaBuilder().parameter( Integer[].class, "theIntegers" );
6363
criteria.where( em.getCriteriaBuilder().equal( thePath, param ) );
6464
TypedQuery<MultiTypedBasicAttributesEntity> query = em.createQuery( criteria );
65-
query.setParameter( param, new Integer[] { Integer.valueOf(1), Integer.valueOf(1), Integer.valueOf(1) } );
65+
query.setParameter( param, new Integer[] {1, 1, 1} );
6666
assertThat( query.getParameterValue( param.getName() ), instanceOf( Integer[].class ) );
6767
query.getResultList();
6868
em.getTransaction().commit();
@@ -85,7 +85,7 @@ public void testNamedParameterMetadata() {
8585
);
8686

8787
TypedQuery<MultiTypedBasicAttributesEntity> query = em.createQuery( criteria );
88-
Parameter parameter = query.getParameter( "id" );
88+
Parameter<?> parameter = query.getParameter( "id" );
8989
assertEquals( "id", parameter.getName() );
9090

9191
em.getTransaction().commit();
@@ -140,7 +140,7 @@ public void testParameterInParameterList2() {
140140
}
141141

142142
@Override
143-
public Class[] getAnnotatedClasses() {
143+
public Class<?>[] getAnnotatedClasses() {
144144
return new Class[] { MultiTypedBasicAttributesEntity.class };
145145
}
146146
}

0 commit comments

Comments
 (0)