5050
5151import java .sql .PreparedStatement ;
5252import java .sql .Types ;
53+ import java .util .ArrayList ;
5354import java .util .Collection ;
5455import java .util .Collections ;
5556import java .util .Comparator ;
@@ -67,7 +68,7 @@ public class JdbcAdapter implements DbAdapter {
6768 private PkGenerator pkGenerator ;
6869 protected QuotingStrategy quotingStrategy ;
6970
70- protected Map <Integer , String []> externalTypes ;
71+ protected Map <Integer , NativeColumnType []> nativeColumnTypes ;
7172 protected ExtendedTypeMap extendedTypes ;
7273 protected boolean supportsBatchUpdates ;
7374 protected boolean supportsUniqueConstraints ;
@@ -96,7 +97,7 @@ public JdbcAdapter(@Inject RuntimeProperties runtimeProperties,
9697 this .quotingStrategy = createQuotingStrategy ();
9798
9899 this .ejbqlTranslator = createEJBQLTranslator ();
99- this .externalTypes = createExternalTypes ();
100+ this .nativeColumnTypes = indexBySqlType ( createExternalTypes () );
100101 this .extendedTypes = new ExtendedTypeMap ();
101102 initExtendedTypes (defaultExtendedTypes , userExtendedTypes , extendedTypeFactories , valueObjectTypeRegistry );
102103 }
@@ -119,50 +120,58 @@ public JdbcEventLogger getJdbcEventLogger() {
119120 }
120121
121122 /**
122- * Creates the mapping of JDBC types to RDBMS type names used for DDL generation. The first name in each
123- * array is used for column DDL; some adapters use subsequent names for special cases (e.g. PostgreSQL
124- * serial types for generated columns). Subclasses override this method, mapping every type supported by
125- * the target database explicitly, without calling the superclass.
123+ * Returns the database-native types supported by this adapter.
126124 *
127125 * @since 5.0
128126 */
129- protected Map <Integer , String []> createExternalTypes () {
130- Map <Integer , String []> types = new HashMap <>();
131- types .put (Types .ARRAY , new String []{"ARRAY" });
132- types .put (Types .BIGINT , new String []{"BIGINT" });
133- types .put (Types .ROWID , new String []{"ROWID" });
134- types .put (Types .BINARY , new String []{"BINARY" });
135- types .put (Types .BIT , new String []{"BIT" });
136- types .put (Types .BLOB , new String []{"BLOB" });
137- types .put (Types .BOOLEAN , new String []{"BOOLEAN" });
138- types .put (Types .CHAR , new String []{"CHAR" });
139- types .put (Types .NCHAR , new String []{"NCHAR" });
140- types .put (Types .CLOB , new String []{"CLOB" });
141- types .put (Types .NCLOB , new String []{"NCLOB" });
142- types .put (Types .DATALINK , new String []{"DATALINK" });
143- types .put (Types .DATE , new String []{"DATE" });
144- types .put (Types .DECIMAL , new String []{"DECIMAL" });
145- types .put (Types .DOUBLE , new String []{"DOUBLE" });
146- types .put (Types .FLOAT , new String []{"FLOAT" });
147- types .put (Types .INTEGER , new String []{"INTEGER" });
148- types .put (Types .JAVA_OBJECT , new String []{"JAVA_OBJECT" });
149- types .put (Types .LONGVARBINARY , new String []{"LONGVARBINARY" });
150- types .put (Types .LONGVARCHAR , new String []{"LONGVARCHAR" });
151- types .put (Types .LONGNVARCHAR , new String []{"LONGNVARCHAR" });
152- types .put (Types .NUMERIC , new String []{"NUMERIC" });
153- types .put (Types .OTHER , new String []{"OTHER" });
154- types .put (Types .REAL , new String []{"REAL" });
155- types .put (Types .REF , new String []{"REF" });
156- types .put (Types .SMALLINT , new String []{"SMALLINT" });
157- types .put (Types .STRUCT , new String []{"STRUCT" });
158- types .put (Types .TIME , new String []{"TIME" });
159- types .put (Types .TIMESTAMP , new String []{"TIMESTAMP" });
160- types .put (Types .TINYINT , new String []{"TINYINT" });
161- types .put (Types .VARBINARY , new String []{"VARBINARY" });
162- types .put (Types .VARCHAR , new String []{"VARCHAR" });
163- types .put (Types .NVARCHAR , new String []{"NVARCHAR" });
164- types .put (Types .SQLXML , new String []{"SQLXML" });
165- return types ;
127+ protected NativeColumnType [] createExternalTypes () {
128+ return new NativeColumnType []{
129+ NativeColumnType .of (Types .ARRAY , "ARRAY" ),
130+ NativeColumnType .of (Types .BIGINT , "BIGINT" ),
131+ NativeColumnType .of (Types .ROWID , "ROWID" ),
132+ NativeColumnType .of (Types .BINARY , "BINARY" ),
133+ NativeColumnType .of (Types .BIT , "BIT" ),
134+ NativeColumnType .of (Types .BLOB , "BLOB" ),
135+ NativeColumnType .of (Types .BOOLEAN , "BOOLEAN" ),
136+ NativeColumnType .of (Types .CHAR , "CHAR" ),
137+ NativeColumnType .of (Types .NCHAR , "NCHAR" ),
138+ NativeColumnType .of (Types .CLOB , "CLOB" ),
139+ NativeColumnType .of (Types .NCLOB , "NCLOB" ),
140+ NativeColumnType .of (Types .DATALINK , "DATALINK" ),
141+ NativeColumnType .of (Types .DATE , "DATE" ),
142+ NativeColumnType .of (Types .DECIMAL , "DECIMAL" ),
143+ NativeColumnType .of (Types .DOUBLE , "DOUBLE" ),
144+ NativeColumnType .of (Types .FLOAT , "FLOAT" ),
145+ NativeColumnType .of (Types .INTEGER , "INTEGER" ),
146+ NativeColumnType .of (Types .JAVA_OBJECT , "JAVA_OBJECT" ),
147+ NativeColumnType .of (Types .LONGVARBINARY , "LONGVARBINARY" ),
148+ NativeColumnType .of (Types .LONGVARCHAR , "LONGVARCHAR" ),
149+ NativeColumnType .of (Types .LONGNVARCHAR , "LONGNVARCHAR" ),
150+ NativeColumnType .of (Types .NUMERIC , "NUMERIC" ),
151+ NativeColumnType .of (Types .OTHER , "OTHER" ),
152+ NativeColumnType .of (Types .REAL , "REAL" ),
153+ NativeColumnType .of (Types .REF , "REF" ),
154+ NativeColumnType .of (Types .SMALLINT , "SMALLINT" ),
155+ NativeColumnType .of (Types .STRUCT , "STRUCT" ),
156+ NativeColumnType .of (Types .TIME , "TIME" ),
157+ NativeColumnType .of (Types .TIMESTAMP , "TIMESTAMP" ),
158+ NativeColumnType .of (Types .TINYINT , "TINYINT" ),
159+ NativeColumnType .of (Types .VARBINARY , "VARBINARY" ),
160+ NativeColumnType .of (Types .VARCHAR , "VARCHAR" ),
161+ NativeColumnType .of (Types .NVARCHAR , "NVARCHAR" ),
162+ NativeColumnType .of (Types .SQLXML , "SQLXML" ),
163+ };
164+ }
165+
166+ private static Map <Integer , NativeColumnType []> indexBySqlType (NativeColumnType [] types ) {
167+ Map <Integer , List <NativeColumnType >> grouped = new HashMap <>();
168+ for (NativeColumnType type : types ) {
169+ grouped .computeIfAbsent (type .jdbcType (), key -> new ArrayList <>()).add (type );
170+ }
171+
172+ Map <Integer , NativeColumnType []> indexed = new HashMap <>();
173+ grouped .forEach ((sqlType , variants ) -> indexed .put (sqlType , variants .toArray (new NativeColumnType [0 ])));
174+ return indexed ;
166175 }
167176
168177 /**
@@ -398,15 +407,15 @@ public static String getType(DbAdapter adapter, DbAttribute column) {
398407 return "OTHER" ;
399408 }
400409
401- String [] types = adapter .externalTypesForJdbcType (columnType );
410+ NativeColumnType [] types = adapter .nativeColumnTypes (columnType );
402411 if (types == null || types .length == 0 ) {
403412 String entityName = column .getEntity () != null
404413 ? column .getEntity ().getFullyQualifiedName ()
405414 : "<null>" ;
406415 throw new CayenneRuntimeException ("Undefined type for attribute '%s.%s': %s."
407416 , entityName , column .getName (), column .getType ());
408417 }
409- return types [0 ];
418+ return types [0 ]. nativeType () ;
410419 }
411420
412421 /**
@@ -487,9 +496,29 @@ public String createFkConstraint(DbRelationship rel) {
487496 return buf .toString ();
488497 }
489498
499+ /**
500+ * @deprecated use {@link #nativeColumnTypes(int)}
501+ */
502+ @ Deprecated (since = "5.0" , forRemoval = true )
490503 @ Override
491504 public String [] externalTypesForJdbcType (int type ) {
492- return externalTypes .get (type );
505+ NativeColumnType [] variants = nativeColumnTypes .get (type );
506+ if (variants == null ) {
507+ return null ;
508+ }
509+ String [] names = new String [variants .length ];
510+ for (int i = 0 ; i < variants .length ; i ++) {
511+ names [i ] = variants [i ].nativeType ();
512+ }
513+ return names ;
514+ }
515+
516+ /**
517+ * @since 5.0
518+ */
519+ @ Override
520+ public NativeColumnType [] nativeColumnTypes (int type ) {
521+ return nativeColumnTypes .get (type );
493522 }
494523
495524 @ Override
0 commit comments