47
47
import static org .junit .jupiter .api .Assertions .assertNotNull ;
48
48
49
49
/**
50
- * Test that we handle arrays as basic types and the @{@link Array} annotation.
50
+ * Test that we handle arrays as basic types and the @{@link Array} annotation in combination with @{@link Column} .
51
51
* <p>
52
52
* Specifying the length doesn't seem to have any effect at the moment.
53
- * Except for Postgres: Hibernate ORM will use a different SQL query for the creation of the table, but Postgres will
54
- * ignore the limit anyway.
53
+ * We use it when creating the table with Postgres, but Postgres ignore it anyway.
55
54
*/
56
55
@ Timeout (value = 10 , timeUnit = MINUTES )
57
56
@ DisabledFor (value = ORACLE , reason = "Vert.x does not support arrays for Oracle" )
@@ -72,7 +71,7 @@ protected void addServices(StandardServiceRegistryBuilder builder) {
72
71
}
73
72
74
73
private static boolean filterCreateTable (String s ) {
75
- return s .toLowerCase ().startsWith ( "create table basic" );
74
+ return s .toLowerCase ().startsWith ( "create table basic " );
76
75
}
77
76
78
77
@ Override
@@ -387,7 +386,7 @@ public void testBigIntegerArrayType(VertxTestContext context) {
387
386
388
387
testField ( context , basic , found -> {
389
388
assertArrayEquals ( dataArray , found .bigIntegerArray );
390
- validateArrayColumn ( "bigIntegerArray" , null , null );
389
+ validateArrayColumn ( "bigIntegerArray" , null , 5000 );
391
390
} );
392
391
}
393
392
@@ -401,7 +400,7 @@ public void testBigDecimalArrayType(VertxTestContext context) {
401
400
assertEquals ( dataArray .length , found .bigDecimalArray .length );
402
401
assertEquals ( 0 , dataArray [0 ].compareTo ( found .bigDecimalArray [0 ] ) );
403
402
assertEquals ( 0 , dataArray [1 ].compareTo ( found .bigDecimalArray [1 ] ) );
404
- validateArrayColumn ( "bigDecimalArray" , null , null );
403
+ validateArrayColumn ( "bigDecimalArray" , null , 5000 );
405
404
} );
406
405
}
407
406
@@ -415,6 +414,7 @@ public void testBigDecimalArrayTypeWithArrayAnnotation(VertxTestContext context)
415
414
assertEquals ( dataArray .length , found .bigDecimalArrayWithArrayAnnotation .length );
416
415
assertEquals ( 0 , dataArray [0 ].compareTo ( found .bigDecimalArrayWithArrayAnnotation [0 ] ) );
417
416
assertEquals ( 0 , dataArray [1 ].compareTo ( found .bigDecimalArrayWithArrayAnnotation [1 ] ) );
417
+ validateArrayColumn ( "bigDecimalArrayWithArrayAnnotation" , 5 , 5000 );
418
418
} );
419
419
}
420
420
@@ -426,32 +426,60 @@ private void validateArrayColumn(String columnName, Integer arrayLength, Integer
426
426
427
427
// A predicate that checks we apply the right size to the array when required
428
428
private static Predicate <String > arrayColumnPredicate (String columnName , Integer arrayLength , Integer columnLength ) {
429
- final StringBuilder conditionBuilder = new StringBuilder ();
430
- // Only Postgres has a different behaviour if @Array is used
431
- if ( dbType () == POSTGRESQL ) {
432
- conditionBuilder .append ( ".*" );
433
- // Postgres will ignore the length of the array, but we can test the table creation sql query
434
- // and make sure that the limit is defined
435
-
436
- // Example of correct query definition: columnName varchar(123) array[2]
437
- conditionBuilder .append ( columnName ).append ( " \\ w+" );
438
- if ( columnLength != null ) {
439
- conditionBuilder .append ( "\\ (" ).append ( columnLength ).append ( "\\ )" );
440
- }
441
- else {
442
- // for some types we have a default size. For example: `varchar(255)` or `numeric(38,0)`
443
- conditionBuilder .append ( "(\\ (\\ d+(,\\ d+)?\\ ))?" );
444
- }
445
- conditionBuilder .append ( " array" );
446
- if ( arrayLength != null ) {
447
- conditionBuilder .append ( "\\ [" ).append ( arrayLength ).append ( "\\ ]" );
448
- }
449
- conditionBuilder .append ( ".*" );
429
+ switch ( dbType () ) {
430
+ case POSTGRESQL :
431
+ case COCKROACHDB :
432
+ return postgresPredicate ( columnName , arrayLength , columnLength );
433
+ case MYSQL :
434
+ case MARIA :
435
+ case SQLSERVER :
436
+ case DB2 :
437
+ return mysqlPredicate ( columnName , columnLength );
438
+ default :
439
+ return s -> true ;
450
440
}
451
- if ( conditionBuilder .length () == 0 ) {
452
- return s -> true ;
441
+ }
442
+
443
+ /**
444
+ * For Postgres, we expect arrays to be defined as {@code array}.
445
+ * <p>
446
+ * For example: {@code varchar(255) array[2]}
447
+ * </p>
448
+ */
449
+ private static Predicate <String > postgresPredicate (String columnName , Integer arrayLength , Integer columnLength ) {
450
+ StringBuilder regexBuilder = new StringBuilder ();
451
+ regexBuilder .append ( ".*" );
452
+
453
+ regexBuilder .append ( columnName ).append ( " \\ w+" );
454
+ // Column length only affect String types
455
+ if ( columnLength != null && columnName .startsWith ( "string" ) ) {
456
+ regexBuilder .append ( "\\ (" ).append ( columnLength ).append ( "\\ )" );
457
+ }
458
+ else {
459
+ // for some types we have a default size. For example: `varchar(255)` or `numeric(38,0)`
460
+ regexBuilder .append ( "(\\ (\\ d+(,\\ d+)?\\ ))?" );
461
+ }
462
+ regexBuilder .append ( " array" );
463
+ if ( arrayLength != null ) {
464
+ regexBuilder .append ( "\\ [" ).append ( arrayLength ).append ( "\\ ]" );
465
+ }
466
+ regexBuilder .append ( ".*" );
467
+ return s -> s .matches ( regexBuilder .toString () );
468
+ }
469
+
470
+ private static Predicate <String > mysqlPredicate (String columnName , Integer columnLength ) {
471
+ StringBuilder regexBuilder = new StringBuilder ();
472
+ // Example of correct query definition: columnName varbinary(255)
473
+ regexBuilder .append ( columnName ).append ( " varbinary" ).append ( "(" );
474
+ if ( columnLength != null ) {
475
+ regexBuilder .append ( columnLength );
476
+ }
477
+ else {
478
+ // Default size
479
+ regexBuilder .append ( 255 );
453
480
}
454
- return s -> s .matches ( conditionBuilder .toString () );
481
+ regexBuilder .append ( ")" );
482
+ return s -> s .contains ( regexBuilder .toString () );
455
483
}
456
484
457
485
@ Entity (name = "Basic" )
0 commit comments