CREATE TABLE

September 28, 2021 ยท View on GitHub

The CreateTable class of the SQL Statement Builder provides an entry point to defining a CREATE TABLE SQL statement.

Usage

  1. Create an instance of the CreateTable class through the StatementFactory:
CreateTable createTable = StatementFactory.getInstance().createTable("tableName");
  1. Create columns with desired data types using fluent programming.

Construct the columns in the order you want them to appear in the created table.

For example, create a table with three columns (DECIMAL, CHAR and BOOLEAN):

createTable.decimalColumn("col_decimal", 9, 0)
           .charColumn("col_char", 10)
           .booleanColumn("col_boolean");

Please keep in mind that the column name is required when creating a column. Additionally, some column types require extra parameters, for instance VARCHAR.

Currently, the following column types are supported:

Column TypeParameter RequiredExample
BOOLEANfalsecreateTable.booleanColumn("col_bool")
CHARtruecreateTable.charColumn("col_char")
DATEfalsecreateTable.dateColumn("col_date")
DECIMALtruecreateTable.decimalColumn("col_dec", 18, 0)
DOUBLE PRECISIONfalsecreateTable.doublePrecisionColumn("col_double_precision")
INTERVAL DAY TO SECONDtruecreateTable.intervalDayToSecondColumn("col_intdaytosec", 2, 3)
INTERVAL YEAR TO MONTHtruecreateTable.intervalYearToMonthColumn("col_intyeartomonth", 2)
TIMESTAMPfalsecreateTable.timestampColumn("col_timestamp")
TIMESTAMP WITH LOCAL TIME ZONEfalsecreateTable.timestampWithLocalTimeZoneColumn("col_tswithzone")
VARCHARtruecreateTable.varcharColumn("col_varchar", 100)

You can find more information about the column types in the SQL Statement Builder's JavaDoc API description.

  1. Render the instance of CreateTable class. Click here for more information on Rendering SQL Statement.
  • The complete example code

    CreateTable createTable = StatementFactory.getInstance().createTable("tableName");
    
    createTable.decimalColumn("col_decimal", 9, 0)
               .charColumn("col_char", 10)
               .booleanColumn("col_boolean");
    
    // optional step: add configuration
    StringRendererConfig config = StringRendererConfig.builder().lowerCase(true).build();
    CreateTableRenderer renderer = CreateTableRenderer.create(config);
    createTable.accept(renderer);
    
    String renderedString = renderer.render();
    

Please check the API documentation for more details.