Skip to content

SQL values formatter

The SQL values formatter defines how attribute / column values have to be represented in the SQL INSERT string generated by RedG. RedG offers a default formatter DefaultSQLValuesFormatter that can turn the most common data types into valid SQL. If you are using uncommon or custom data types, you have to provide your own implementation.

Note

If you are not generating SQL INSERT statements with the generateSQLStatements() method, you can ignore the SQL values formatter and take a look at the PreparedStatement parameter setter

Default SQL values formatter

The default formatter provided by RedG formats the input like described in the following table. (Empty fields in the type columns mean that RedG does not check that type)

SQL type Java type Formatting
VARCHAR &VARCHAR2 toString() gets called on value and single quotation marks get escaped. Escaped string gets wrapped in single quotation marks.
DECIMAL & NUMBER Boolean 1 for true, 0 for false.
DECIMAL & NUMBER toString() gets called on value. This works for numbers, as they all implement a correct toString() method. No precision checks are performed.
java.util.Date A timestamp gets constructed from the unix timestamp and inserted as a string using the TO_TIMESTAMP(string, format) SQL function.
TemporalAccessor The temporal value gets formatted and inserted as a string using the TO_TIMESTAMP(string, format) SQL function.

Java API

If you need your own formatter, simply implement the SQLValuesFormatter interface. The formatValues method gets called for every value that has to be formatted (so every attribute in every entity).

public interface SQLValuesFormatter {

    <T> String formatValue(T value, String sqlDataType, String fullTableName, String tableName, String columnName);
}

Source

SQLValuesFormatter.java DefaultSQLValuesFormatter.java