Class AbstractTableBuilder<T extends AbstractTableBuilder<T>>

java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractTableBuilder<T>
Type Parameters:
T - The implementation type for fluent chaining.
Direct Known Subclasses:
TableBuilder, TableBuilder, TableBuilder, TableBuilder, TableBuilder

public abstract class AbstractTableBuilder<T extends AbstractTableBuilder<T>> extends Object
A fluent-style builder for programmatically creating database tables. This class handles column definitions, keys, constraints, and table options, abstracting away the differences between SQL dialects.
  • Constructor Details

    • AbstractTableBuilder

      public AbstractTableBuilder(Connection connection, String table)
      Constructs a new AbstractTableBuilder.
      Parameters:
      connection - The JDBC connection to use.
      table - The name of the table to build.
  • Method Details

    • ifNotExists

      public T ifNotExists()
      Configures the builder to include the "IF NOT EXISTS" clause.
      Returns:
      The current instance cast to T.
    • column

      public T column(String name, String type)
      Defines a column in the table.
      Parameters:
      name - The column name.
      type - The SQL data type string (e.g., "INT", "TEXT", "VARCHAR(32)").
      Returns:
      The current instance cast to T.
    • primaryKey

      public T primaryKey(String... keys)
      Sets one or more columns as the table's primary key.
      Parameters:
      keys - The column names to include in the primary key.
      Returns:
      The current instance cast to T.
    • autoIncrement

      public T autoIncrement(String column)
      Redefines a column to include the auto-increment property.
      Parameters:
      column - The name of the column to modify.
      Returns:
      The current instance cast to T.
    • foreignKey

      public T foreignKey(String column, String referencesTable, String referencesColumn)
      Adds a foreign key constraint to the table.
      Parameters:
      column - The local column name.
      referencesTable - The table name being referenced.
      referencesColumn - The column name being referenced in the target table.
      Returns:
      The current instance cast to T.
    • unique

      public T unique(String... columns)
      Adds a unique constraint on the specified columns.
      Parameters:
      columns - The columns that must contain unique data.
      Returns:
      The current instance cast to T.
    • check

      public T check(String expression)
      Adds a CHECK constraint to ensure valid data.
      Parameters:
      expression - The SQL boolean expression to check.
      Returns:
      The current instance cast to T.
    • defaultValue

      public T defaultValue(String column, String defaultValue)
      Assigns a default value to a column.
      Parameters:
      column - The name of the column.
      defaultValue - The default value as a string (e.g., "'Unknown'" or "0").
      Returns:
      The current instance cast to T.
    • dropIfExists

      public void dropIfExists()
      Drops the table if it exists in the database.
      Throws:
      RuntimeException - If the drop operation fails.
    • execute

      public void execute()
      Compiles and executes the CREATE TABLE SQL statement based on the provided configuration.
      Throws:
      RuntimeException - If the table creation fails.