Class JdbcTableFacet.Builder

java.lang.Object
guru.mikelue.foxglove.jdbc.JdbcTableFacet.Builder
All Implemented Interfaces:
SettingAware<JdbcTableFacet.Builder>
Enclosing class:
JdbcTableFacet

public static class JdbcTableFacet.Builder extends Object implements SettingAware<JdbcTableFacet.Builder>
Builds a JdbcTableFacet with table name.

Number of rows

You can only use one of following ways to decide the number of rows to be generated:

Basic example

Example(builds 10 rows):

 var facet = JdbcTableFacet.builder(TABLE_CAR)
     .numberOfRows(10)
     // Round robin among several brands
     .column("cr_brand")
         .roundRobin("Toyota", "Honda", "Ford", "BMW", "Audi")
     // Generates year between 2015 and 2025
     .column("cr_year")
         .useSpec(() -> gen().shorts().range((short)2015, (short)2025))
     // Random choice for number of seats
     .column("cr_seats")
         .useSpec(() -> gen().oneOf(2, 4, 5, 7))
     // Color with nullable property get nullable values
     .<String>column("cr_color")
         .decideSupplier(columnMeta -> {
             var colorGenerator = gen().oneOf("Red", "Blue", "Green");

             if (columnMeta.properties().contains(Property.NULLABLE)) {
                 return colorGenerator.nullable();
             }

             return colorGenerator;
         })
     // Fixed value for status column
     .column("cr_status")
         .fixed(null)
     // Includes these two columns by auto-generating their values
     .includeColumns("cr_license_plate", "cr_daily_rate", "cr_created_at", "cr_model")
     .build();
 

Example for deciding number of rows

Key column

Use keyOfInt(String) to configure a key column as integral value,

Example(builds 10 rows by number of key values):


 var facet = JdbcTableFacet.builder(TABLE_CAR)
     .keyOfInt("cr_id").limit(1000, 10)
     .build();
 

Cartesian product

Use cartesianProduct(String) to configure columns for Cartesian product.

Example(builds 3 * 3 = 9 rows):


 var facet = JdbcTableFacet.builder(TABLE_CAR)
     .cartesianProduct("cr_brand")
         .domain("Toyota", "Honda", "Ford")
     .cartesianProduct("cr_year")
         .domain(2020, 2021, 2022)
     .build();
 

Referencing

Use referencing(String) to configure a column referencing to another table,

Example(builds 10 * 2 = 20 rows by number of key values):


 // Prepares parent facet
 var carFacet = JdbcTableFacet.builder(TABLE_CAR)
     .numberOfRows(10)
     .build();

 // Generates rows referencing to the parent facet.
 var featureFacet = JdbcTableFacet.builder(TABLE_CAR_FEATURE)
     .referencing("cf_cr_id").parent(carFacet, "cr_id")
         // Every car is referenced by one feature.
         .cardinality(2)
     .column("cf_feature_name")
         .fixed("Sunroof")
     .build();
 

Other features

Change row values before insertion

Use onTupleGenerated(Consumer) to change generated values before insertion.

 var facet = JdbcTableFacet.builder(TABLE_CAR)
     .numberOfRows(RANDOM_SIZE)
     .column("cr_brand")
         .roundRobin("Toyota", "Honda", "Ford", "BMW", "Audi")
     // Sets the value of "cr_license_plate" by combination of "cr_brand" and a random number
     .onTupleGenerated(tuple -> tuple.setValue(
         "cr_license_plate",
         tuple.getValue("cr_brand") + "-" + randomNumber.get()
     ))
     .build();
 

Applies DataSetting

withSetting(DataSettingInfo) can be used to apply DataSettingInfo locally.

 var setting = new DataSetting()
     // For any column with type of "VARCHAR", using the supplier
     .givenType(JDBCType.VARCHAR)
         .useSupplier(() -> sampleText + suffixSupplier.get())

 var facet = JdbcTableFacet.builder(TABLE_CAR)
     // Uses the setting on the whole table
     .withSetting(setting)
     .numberOfRows(RANDOM_SIZE)
     .build();
 
See Also: