1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.setting

File DefaultSetting.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
17
1
1
194
124
1
0.06
17
1
1

Classes

Class Line # Actions
DefaultSetting 24 17 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 116 tests. .

Source view

1    package guru.mikelue.foxglove.setting;
2   
3    import java.math.BigDecimal;
4    import java.sql.JDBCType;
5    import java.time.*;
6    import java.util.EnumSet;
7    import java.util.Set;
8    import java.util.UUID;
9   
10    import org.instancio.Instancio;
11    import org.instancio.generator.specs.BooleanSpec;
12    import org.instancio.generator.specs.FloatSpec;
13    import org.instancio.generator.specs.StringSpec;
14    import org.slf4j.Logger;
15    import org.slf4j.LoggerFactory;
16   
17    import guru.mikelue.foxglove.ColumnMeta;
18    import guru.mikelue.foxglove.functional.SupplierDecider;
19    import guru.mikelue.foxglove.instancio.ByteArraySpec;
20   
21    import static java.sql.JDBCType.*;
22    import static java.util.Collections.unmodifiableSet;
23   
 
24    class DefaultSetting {
25    /**
26    * The default number of rows to be generated for a table(if not set by it).
27    */
28    final static int DEFAULT_NUMBER_OF_ROWS = 1024;
29   
30    /**
31    * The min length for generating value of large text types.
32    */
33    final static int LARGE_TEXT_MIN_LENGTH = 1024;
34    /**
35    * The max length for generating value of large text types.
36    */
37    final static int LARGE_TEXT_MAX_LENGTH = 2048;
38   
39    /**
40    * The default number of sides for dice roll.
41    */
42    final static int DEFAULT_DICE_SIDES = 6;
43   
44    /**
45    * Whether or not to generate possible null value by for nullable columns.
46    */
47    final static boolean DEFAULT_GENERATE_NULL = false;
48   
49    /**
50    * The default column properties used for auto-generating data.
51    */
52    final static Set<ColumnMeta.Property> DEFAULT_COLUMN_PROPERTIES_FOR_AUTO_GENERATING =
53    unmodifiableSet(EnumSet.of(
54    ColumnMeta.Property.NULLABLE,
55    ColumnMeta.Property.DEFAULT_VALUE
56    ));
57   
58    private final static Logger logger = LoggerFactory.getLogger(DefaultSetting.class);
59   
60    private final static FloatSpec DEFAULT_FLOAT_SPEC =
61    Instancio.gen().floats().min(0.0f).max(Float.MAX_VALUE);
62    private final static BooleanSpec DEFAULT_BOOLEAN_SPEC =
63    Instancio.gen().booleans();
64    private final static StringSpec LARGE_TEXT_SPEC =
65    Instancio.gen().string().alphaNumeric()
66    .length(LARGE_TEXT_MIN_LENGTH, LARGE_TEXT_MAX_LENGTH); // minus prefix length
67   
68    private final static SupplierDecider<String> CHAR_SUPPLIER_DECIDER = columnMeta -> {
69  280 logger.trace("Deciding [{}][{}] spec for column: {}",
70    columnMeta.jdbcType(), columnMeta.size(), columnMeta.name());
71   
72  280 int columnSize = columnMeta.size();
73  280 if (columnSize <= 8) {
74  10 return Instancio.gen().string().alphaNumeric().length(columnSize);
75  270 } else if (columnSize <= 32) {
76  105 return Instancio.gen().string().alphaNumeric()
77    .length(8, columnSize);
78    }
79   
80  165 return Instancio.gen().string().alphaNumeric()
81    .length(32, Math.min(128, columnSize));
82    };
83   
84    private final static SupplierDecider<byte[]> BYTE_ARRAY_SUPPLIER_DECIDER = columnMeta -> {
85  12 logger.trace("Deciding [{}][{}] spec for column: {}",
86    columnMeta.jdbcType(), columnMeta.size(), columnMeta.name());
87   
88  12 int columnSize = columnMeta.size();
89  12 if (columnSize <= 16) {
90  8 return new ByteArraySpec().length(columnSize);
91  4 } else if (columnSize <= 64) {
92  2 return new ByteArraySpec().minLength(16).maxLength(columnSize);
93    }
94   
95  2 return new ByteArraySpec().minLength(64).maxLength(Math.min(256, columnSize));
96    };
97   
98    private final static ByteArraySpec LARGE_BINARY_SPEC = new ByteArraySpec()
99    .minLength(1024).maxLength(2048);
100   
101    private final static SupplierDecider<BigDecimal> BIG_DECIMAL_SUPPLIER_DECIDER = columnMeta -> {
102  72 logger.trace("Deciding [{}] <{}.{}> spec for column: {}",
103    columnMeta.jdbcType(),
104    columnMeta.size(), columnMeta.decimalDigits(),
105    columnMeta.name());
106   
107  72 return Instancio.gen().math().bigDecimal()
108    .precision(columnMeta.size())
109    .scale(columnMeta.decimalDigits());
110    };
111   
112    private static final Set<JDBCType> NOT_SUPPORTED_JDBC_TYPES = EnumSet.of(
113    ARRAY, STRUCT,
114    ROWID, NULL, JAVA_OBJECT, DISTINCT, OTHER,
115    SQLXML,
116    REF, REF_CURSOR, DATALINK
117    );
118   
119    private final static DataSetting DEFAULT_SETTING = new DataSetting()
120    /*
121    * Integral types
122    */
123    .<Boolean>givenType(BIT).useSupplier(DEFAULT_BOOLEAN_SPEC)
124    .<Boolean>givenType(BOOLEAN).useSupplier(DEFAULT_BOOLEAN_SPEC)
125    .<Byte>givenType(TINYINT).useSupplier(Instancio.gen().bytes().min((byte) 0).max(Byte.MAX_VALUE))
126    .<Short>givenType(SMALLINT).useSupplier(Instancio.gen().shorts().min((short) 0).max(Short.MAX_VALUE))
127    .<Integer>givenType(INTEGER).useSupplier(Instancio.gen().ints().min(0).max(Integer.MAX_VALUE))
128    .<Long>givenType(BIGINT).useSupplier(Instancio.gen().longs().min(0L).max(Long.MAX_VALUE))
129    // :~)
130    /*
131    * Binary types
132    */
133    .<byte[]>givenType(BINARY)
134    .decideSupplier(BYTE_ARRAY_SUPPLIER_DECIDER)
135    .<byte[]>givenType(VARBINARY)
136    .decideSupplier(BYTE_ARRAY_SUPPLIER_DECIDER)
137    .<byte[]>givenType(LONGVARBINARY)
138    .useSupplier(LARGE_BINARY_SPEC)
139    .<byte[]>givenType(BLOB)
140    .useSupplier(LARGE_BINARY_SPEC)
141    // :~)
142    /*
143    * Floating point types
144    */
145    .<Float>givenType(FLOAT).useSupplier(DEFAULT_FLOAT_SPEC)
146    .<Float>givenType(REAL).useSupplier(DEFAULT_FLOAT_SPEC)
147    .<Double>givenType(DOUBLE).useSupplier(Instancio.gen().doubles().min(0.0).max(Double.MAX_VALUE))
148    .<BigDecimal>givenType(DECIMAL).decideSupplier(BIG_DECIMAL_SUPPLIER_DECIDER)
149    .<BigDecimal>givenType(NUMERIC).decideSupplier(BIG_DECIMAL_SUPPLIER_DECIDER)
150    // :~)
151    /*
152    * Character types
153    */
154    .<String>givenType(CHAR).decideSupplier(CHAR_SUPPLIER_DECIDER)
155    .<String>givenType(VARCHAR).decideSupplier(CHAR_SUPPLIER_DECIDER)
156    .<String>givenType(NCHAR).decideSupplier(CHAR_SUPPLIER_DECIDER)
157    .<String>givenType(NVARCHAR).decideSupplier(CHAR_SUPPLIER_DECIDER)
158    .<String>givenType(LONGVARCHAR).useSupplier(LARGE_TEXT_SPEC)
159    .<String>givenType(CLOB).useSupplier(LARGE_TEXT_SPEC)
160    .<String>givenType(LONGNVARCHAR).useSupplier(LARGE_TEXT_SPEC)
161    .<String>givenType(NCLOB).useSupplier(LARGE_TEXT_SPEC)
162    // :~)
163    /*
164    * Temporal types
165    */
166    .<LocalDate>givenType(DATE)
167    .useSupplier(Instancio.gen().temporal().localDate())
168    .<LocalTime>givenType(TIME)
169    .useSupplier(Instancio.gen().temporal().localTime())
170    .<LocalDateTime>givenType(TIMESTAMP)
171    .useSupplier(Instancio.gen().temporal().localDateTime())
172    .<OffsetTime>givenType(TIME_WITH_TIMEZONE)
173    .useSupplier(Instancio.gen().temporal().offsetTime())
174    .<ZonedDateTime>givenType(TIMESTAMP_WITH_TIMEZONE)
175    .useSupplier(Instancio.gen().temporal().zonedDateTime())
176    // :~)
177    /*
178    * Specific types
179    */
180    .<UUID>givenType("UUID")
181    .useSupplier(Instancio.gen().uuid())
182    .notSupportedJdbcTypes(NOT_SUPPORTED_JDBC_TYPES);
183    // :~)
184   
185    /**
186    * Internal implementation of default {@link DataSetting}.
187    *
188    * @return The default data setting
189    */
 
190  177 toggle static DataSetting instance()
191    {
192  177 return DEFAULT_SETTING;
193    }
194    }