24
24
import com .mongodb .hibernate .internal .FeatureNotSupportedException ;
25
25
import com .mongodb .hibernate .jdbc .MongoArray ;
26
26
import java .lang .reflect .Array ;
27
- import java .lang .reflect .ParameterizedType ;
28
27
import java .math .BigDecimal ;
29
28
import java .sql .JDBCType ;
30
29
import java .sql .PreparedStatement ;
31
30
import java .sql .ResultSet ;
32
31
import java .sql .SQLException ;
33
32
import java .sql .SQLFeatureNotSupportedException ;
34
33
import java .util .ArrayList ;
35
- import java .util .Collection ;
36
34
import org .bson .BsonArray ;
37
35
import org .bson .BsonBinary ;
38
36
import org .bson .BsonBoolean ;
@@ -82,12 +80,8 @@ private ValueConversions() {}
82
80
return toBsonValue (v );
83
81
} else if (value instanceof ObjectId v ) {
84
82
return toBsonValue (v );
85
- } else if (value instanceof MongoArray v ) {
86
- return toBsonValue (v );
87
- } else if (value .getClass ().isArray ()) {
88
- return arrayToBsonValue (value );
89
- } else if (value instanceof Collection <?> v ) {
90
- return toBsonValue (v );
83
+ } else if (value instanceof Object [] v ) {
84
+ return arrayToBsonValue (v );
91
85
} else {
92
86
throw new SQLFeatureNotSupportedException (format (
93
87
"Value [%s] of type [%s] is not supported" ,
@@ -148,7 +142,7 @@ public static BsonBinary toBsonValue(byte[] value) {
148
142
* href="https://docs.jboss.org/hibernate/orm/6.6/userguide/html_single/Hibernate_User_Guide.html#basic-chararray">
149
143
* Hibernate ORM maps {@code char[]} to {@link java.sql.JDBCType#VARCHAR} by default</a>.
150
144
*
151
- * @see #toDomainValue(BsonString, Type.ArrayOrCollection )
145
+ * @see #toDomainValue(BsonString)
152
146
*/
153
147
private static BsonString toBsonValue (char [] value ) {
154
148
return new BsonString (String .valueOf (value ));
@@ -165,9 +159,6 @@ public static BsonArray toBsonValue(java.sql.Array value) throws SQLFeatureNotSu
165
159
} catch (SQLException e ) {
166
160
throw fail (e .toString ());
167
161
}
168
- // Hibernate ORM always passes an `java.sql.Array` with contents of the Java array type to
169
- // `PreparedStatement.setArray`
170
- assertTrue (contents .getClass ().isArray ());
171
162
return arrayToBsonValue (contents );
172
163
}
173
164
@@ -180,15 +171,7 @@ private static BsonArray arrayToBsonValue(Object value) throws SQLFeatureNotSupp
180
171
return new BsonArray (elements );
181
172
}
182
173
183
- private static BsonArray toBsonValue (Collection <?> value ) throws SQLFeatureNotSupportedException {
184
- var elements = new ArrayList <BsonValue >(value .size ());
185
- for (var e : value ) {
186
- elements .add (toBsonValue (e ));
187
- }
188
- return new BsonArray (elements );
189
- }
190
-
191
- static Object toDomainValue (BsonValue value , Type domainType ) throws SQLFeatureNotSupportedException {
174
+ static Object toDomainValue (BsonValue value , Class <?> domainType ) throws SQLFeatureNotSupportedException {
192
175
// TODO-HIBERNATE-48 decide if `value` is nullable and the method may return `null`
193
176
assertNotNull (value );
194
177
if (value instanceof BsonDocument v ) {
@@ -204,26 +187,21 @@ static Object toDomainValue(BsonValue value, Type domainType) throws SQLFeatureN
204
187
} else if (value instanceof BsonDecimal128 v ) {
205
188
return toDomainValue (v );
206
189
} else if (value instanceof BsonString v ) {
207
- if (domainType instanceof Type .Simple simpleType ) {
208
- return toDomainValue (v , simpleType .type ());
209
- } else if (domainType instanceof Type .ArrayOrCollection arrayOrCollectionType ) {
210
- return toDomainValue (v , arrayOrCollectionType ).getArray ();
190
+ if (domainType .isArray ()) {
191
+ return toDomainValue (v );
211
192
} else {
212
- throw fail ( domainType . toString () );
193
+ return toDomainValue ( v , domainType );
213
194
}
214
195
} else if (value instanceof BsonBinary v ) {
215
196
return toDomainValue (v );
216
197
} else if (value instanceof BsonObjectId v ) {
217
198
return toDomainValue (v );
218
- } else if (value instanceof BsonArray v ) {
219
- if (!(domainType instanceof Type .ArrayOrCollection arrayOrCollectionDomainType )) {
220
- throw fail ("VAKOTODO SQLException/RuntimeException about invalid data" );
221
- }
222
- return toDomainValue (v , arrayOrCollectionDomainType ).getArray ();
199
+ } else if (value instanceof BsonArray v && domainType .isArray ()) {
200
+ return toDomainValue (v , assertNotNull (domainType .getComponentType ()));
223
201
} else {
224
202
throw new SQLFeatureNotSupportedException (format (
225
- "Value [%s] of type [%s] is not supported" ,
226
- value , value .getClass ().getTypeName ()));
203
+ "Value [%s] of type [%s] is not supported for the domain type [%s] " ,
204
+ value , value .getClass ().getTypeName (), domainType ));
227
205
}
228
206
}
229
207
@@ -313,109 +291,29 @@ private static ObjectId toDomainValue(BsonObjectId value) {
313
291
return value .getValue ();
314
292
}
315
293
316
- public static MongoArray toArrayDomainValue (BsonValue value , Type .ArrayOrCollection domainArrayContentsType )
317
- throws SQLFeatureNotSupportedException {
318
- if (value instanceof BsonArray v ) {
319
- return toDomainValue (v , domainArrayContentsType );
320
- } else if (value instanceof BsonString v ) {
321
- return toDomainValue (v , domainArrayContentsType );
322
- } else {
323
- throw new RuntimeException (
324
- format ("Invalid data: value [%s], domainArrayContentsType [%s]" , value , domainArrayContentsType ));
325
- }
294
+ public static MongoArray toArrayDomainValue (BsonValue value ) throws SQLFeatureNotSupportedException {
295
+ return new MongoArray (toDomainValue (value .asArray (), Object .class ));
326
296
}
327
297
328
- private static MongoArray toDomainValue (BsonArray value , Type .ArrayOrCollection domainArrayContentsType )
329
- throws SQLFeatureNotSupportedException {
298
+ private static Object toDomainValue (BsonArray value , Class <?> elementType ) throws SQLFeatureNotSupportedException {
330
299
var size = value .size ();
331
- var domainValueBuilder = MongoArray . builder ( domainArrayContentsType , size );
300
+ var result = Array . newInstance ( elementType , size );
332
301
for (int i = 0 ; i < size ; i ++) {
333
- var element = toDomainValue (value .get (i ), domainArrayContentsType . elementType () );
334
- domainValueBuilder . add ( i , element );
302
+ var element = toDomainValue (value .get (i ), elementType );
303
+ Array . set ( result , i , element );
335
304
}
336
- return domainValueBuilder . build () ;
305
+ return result ;
337
306
}
338
307
339
308
/** @see #toBsonValue(char[]) */
340
- private static MongoArray toDomainValue (BsonString value , Type . ArrayOrCollection domainArrayContentsType ) {
309
+ private static char [] toDomainValue (BsonString value ) {
341
310
var charsAsString = toDomainValue (value , String .class );
342
311
var size = charsAsString .length ();
343
- var domainValueBuilder = MongoArray . builder ( domainArrayContentsType , size ) ;
312
+ var result = new char [ size ] ;
344
313
for (int i = 0 ; i < size ; i ++) {
345
314
var element = charsAsString .charAt (i );
346
- domainValueBuilder .add (i , element );
347
- }
348
- return domainValueBuilder .build ();
349
- }
350
-
351
- public sealed interface Type permits Type .Simple , Type .ArrayOrCollection {
352
- sealed interface Simple extends Type permits SimpleType {}
353
-
354
- // VAKOTODO rename to Plural
355
- sealed interface ArrayOrCollection extends Type permits ArrayType , CollectionType {
356
- Simple elementType ();
357
- }
358
-
359
- static Type of (java .lang .reflect .Type type ) {
360
- if (type instanceof Class <?> klass ) {
361
- return of (klass );
362
- } else if (type instanceof ParameterizedType parameterizedType ) {
363
- if (!(parameterizedType .getRawType () instanceof Class <?> rawKlass )) {
364
- throw fail (type .toString ());
365
- }
366
- if (Collection .class .isAssignableFrom (rawKlass )) { // VAKOTODO equals?
367
- var collectionTypeArguments = parameterizedType .getActualTypeArguments ();
368
- assertTrue (collectionTypeArguments .length == 1 );
369
- return new CollectionType (rawKlass , collectionTypeArguments [0 ]);
370
- } else {
371
- return of (rawKlass );
372
- }
373
- } else {
374
- throw fail (type .toString ());
375
- }
376
- }
377
-
378
- private static Type of (Class <?> type ) {
379
- if (type .isArray () && !type .equals (byte [].class )) {
380
- return array (type );
381
- } else {
382
- return simple (type );
383
- }
384
- }
385
-
386
- private static Simple simple (Class <?> type ) {
387
- return new SimpleType (type );
388
- }
389
-
390
- static ArrayOrCollection array (Class <?> type ) {
391
- return new ArrayType (type );
392
- }
393
-
394
- Class <?> type ();
395
- }
396
-
397
- private record SimpleType (Class <?> type ) implements Type .Simple {}
398
-
399
- private record ArrayType (Class <?> type , Simple elementType ) implements Type .ArrayOrCollection {
400
- ArrayType (Class <?> arrayType ) {
401
- this (arrayType , elementType (arrayType ));
402
- }
403
-
404
- private static Simple elementType (Class <?> arrayType ) {
405
- return Type .simple (assertNotNull (arrayType .componentType ()));
406
- }
407
- }
408
-
409
- private record CollectionType (Class <?> type , Simple elementType ) implements Type .ArrayOrCollection {
410
- CollectionType (Class <?> rawCollectionType , java .lang .reflect .Type collectionTypeArgument ) {
411
- this (rawCollectionType , elementType (collectionTypeArgument ));
412
- }
413
-
414
- private static Simple elementType (java .lang .reflect .Type collectionTypeArgument ) {
415
- if (!(collectionTypeArgument instanceof Class <?> collectionTypeArgumentClass )) {
416
- throw fail (); // VAKOTODO try Collection<?>/Collection<? extends>/Collection<? super>
417
- }
418
- return Type .simple (collectionTypeArgumentClass );
315
+ result [i ] = element ;
419
316
}
317
+ return result ;
420
318
}
421
319
}
0 commit comments