Package guru.mikelue.foxglove.jdbc
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
Applies
JdbcTableFacet with table name.
Number of rows
You can only use one of following ways to decide the number of rows to be generated:- Using
numberOfRows(int)in the builder. - Using
keyOfInt(String)by limit the amount of generated keys. - Using
cartesianProduct(String)on at least one column. - Using
referencing(String)with cardinality(self side) to parent rows.
Basic example
- Use
column(String)to configure value generator for a column. - Use
numberOfRows(int)to configure fixed number of rows to be generated. - Use
includeColumns(String...)to include those columns only(others are excluded). - Use
excludeColumns(String...)to exclude some columns.
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
UsekeyOfInt(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
UsecartesianProduct(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
Usereferencing(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
UseonTupleGenerated(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:
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds theJdbcTableFacet.cartesianProduct(String columnName) Starts to configure cartesian product for a column.Starts to configure value generator by column name.excludeColumns(String... columns) Excludes the columns by their name.includeColumns(String... columns) These columns would be generated automatically in spite of their properties.Starts to configure key column as integral value, which would limit for number of generated rows for this table.numberOfRows(int numberOfRows) Sets the number of rows to be generated for the table.onTupleGenerated(Consumer<TupleAccessor> tupleGeneratedConsumer) Sets the consumer to handle generated tuple.referencing(String columnName) Starts to configure table reference for a column.withSetting(DataSettingInfo setting) This object has highest priority thanJdbcDataGenerator.withSetting(DataSettingInfo)andDataSetting.defaults().
-
Method Details
-
numberOfRows
Sets the number of rows to be generated for the table.- Parameters:
numberOfRows- The number of rows to be generated- Returns:
- This builder
-
keyOfInt
Starts to configure key column as integral value, which would limit for number of generated rows for this table. If you don't have need to limit the number of generated values, you can usecolumn(String)withInt4SequenceSupplier, etc.- Parameters:
columnName- The name of column- Returns:
- The next step to configure value generator for the key column
- See Also:
-
referencing
Starts to configure table reference for a column.This is differ from
ColumnSettingSteps.ColumnSimpleStep.from(JdbcTableFacet, String)that this method would decide the number of rows to be generated.- Type Parameters:
T- The type of value generated for the column- Parameters:
columnName- The name of column referencing to another table- Returns:
- The next step to configure table reference for the column
-
cartesianProduct
Starts to configure cartesian product for a column.- Type Parameters:
T- The type of value generated for the column- Parameters:
columnName- The name of column- Returns:
- The next step to configure cartesian product for the column
-
column
Starts to configure value generator by column name.- Type Parameters:
T- The type of value generated for the column- Parameters:
columnName- The name of column- Returns:
- The next step to configure value generator for the column
-
includeColumns
These columns would be generated automatically in spite of their properties.This setting would take precedence over
DataSettingInfo.isAutoGenerating(ColumnMeta).- Parameters:
columns- The column names- Returns:
- This builder
-
excludeColumns
Excludes the columns by their name.This method is differ from
includeColumns(String...), the the value generation for un-set columns is decided byDataSetting.- Parameters:
columns- The column names- Returns:
- This builder
-
onTupleGenerated
Sets the consumer to handle generated tuple. You can use this callback to change generated values before insertion,- Parameters:
tupleGeneratedConsumer- The consumer to handle generated tuple- Returns:
- This builder
-
withSetting
This object has highest priority thanJdbcDataGenerator.withSetting(DataSettingInfo)andDataSetting.defaults().- Specified by:
withSettingin interfaceSettingAware<JdbcTableFacet.Builder>- Parameters:
setting- The setting to use- Returns:
- This instance
-
build
Builds theJdbcTableFacet.- Returns:
- The built
JdbcTableFacet
-