Class TableSchema.Builder

java.lang.Object
group.worldstandard.pudel.api.database.TableSchema.Builder
Enclosing class:
TableSchema

public static class TableSchema.Builder extends Object
Builder class for constructing TableSchema instances. Provides a fluent API for defining table structure including columns and indexes. Validates table and column names according to naming conventions. Table names must start with a lowercase letter and contain only lowercase letters, numbers, and underscores. Column names follow the same rules but cannot be reserved words like 'id', 'created_at', or 'updated_at'.
  • Method Details

    • column

      public TableSchema.Builder column(String name, ColumnType type, boolean nullable)
      Add a column to the schema.
      Parameters:
      name - column name
      type - column type
      nullable - whether the column can be null
      Returns:
      this builder
    • column

      public TableSchema.Builder column(String name, ColumnType type, Integer size, boolean nullable)
      Add a column with a size constraint.
      Parameters:
      name - column name
      type - column type
      size - size/length for VARCHAR, etc.
      nullable - whether the column can be null
      Returns:
      this builder
    • column

      public TableSchema.Builder column(String name, ColumnType type, boolean nullable, String defaultValue)
      Add a column with a default value.
      Parameters:
      name - column name
      type - column type
      nullable - whether the column can be null
      defaultValue - SQL default value expression
      Returns:
      this builder
    • column

      public TableSchema.Builder column(String name, ColumnType type, Integer size, boolean nullable, String defaultValue)
      Add a column with all options.
      Parameters:
      name - column name
      type - column type
      size - size/length (nullable)
      nullable - whether the column can be null
      defaultValue - SQL default value expression (nullable)
      Returns:
      this builder
    • index

      public TableSchema.Builder index(String... columnNames)
      Add an index on one or more columns.
      Parameters:
      columnNames - the columns to index
      Returns:
      this builder
    • uniqueIndex

      public TableSchema.Builder uniqueIndex(String... columnNames)
      Add a unique index on one or more columns.
      Parameters:
      columnNames - the columns for the unique constraint
      Returns:
      this builder
    • build

      public TableSchema build()
    • fromEntity

      public TableSchema.Builder fromEntity(Class<?> entityClass)
      Add columns from an entity class annotated with Entity.

      This method reads all non-ignored fields from the entity class and automatically maps them to columns based on field types and Column annotations. The following field types are supported:

      Fields annotated with @Column(ignore = true) are skipped. Fields named "id", "createdAt", "updatedAt" (or their snake_case equivalents) are automatically excluded as they are managed by the database.

      Indexes can be specified via @Column(unique = true) or @Column(index = true).

      Parameters:
      entityClass - the entity class annotated with @Entity
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the class is not annotated with @Entity