Class TableSchema.Builder
java.lang.Object
group.worldstandard.pudel.api.database.TableSchema.Builder
- Enclosing class:
TableSchema
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 Summary
Modifier and TypeMethodDescriptionbuild()column(String name, ColumnType type, boolean nullable) Add a column to the schema.column(String name, ColumnType type, boolean nullable, String defaultValue) Add a column with a default value.column(String name, ColumnType type, Integer size, boolean nullable) Add a column with a size constraint.column(String name, ColumnType type, Integer size, boolean nullable, String defaultValue) Add a column with all options.fromEntity(Class<?> entityClass) Add columns from an entity class annotated withEntity.Add an index on one or more columns.uniqueIndex(String... columnNames) Add a unique index on one or more columns.
-
Method Details
-
column
Add a column to the schema.- Parameters:
name- column nametype- column typenullable- whether the column can be null- Returns:
- this builder
-
column
Add a column with a size constraint.- Parameters:
name- column nametype- column typesize- 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 nametype- column typenullable- whether the column can be nulldefaultValue- 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 nametype- column typesize- size/length (nullable)nullable- whether the column can be nulldefaultValue- SQL default value expression (nullable)- Returns:
- this builder
-
index
Add an index on one or more columns.- Parameters:
columnNames- the columns to index- Returns:
- this builder
-
uniqueIndex
Add a unique index on one or more columns.- Parameters:
columnNames- the columns for the unique constraint- Returns:
- this builder
-
build
-
fromEntity
Add columns from an entity class annotated withEntity.This method reads all non-ignored fields from the entity class and automatically maps them to columns based on field types and
Columnannotations. The following field types are supported:Long,long->ColumnType.BIGINTInteger,int->ColumnType.INTEGERShort,short->ColumnType.SMALLINTBoolean,boolean->ColumnType.BOOLEANString->ColumnType.STRING(size 255) orColumnType.TEXTInstant,LocalDateTime,OffsetDateTime->ColumnType.TIMESTAMPLocalDate->ColumnType.DATELocalTime->ColumnType.TIMEBigDecimal->ColumnType.DECIMALDouble,double->ColumnType.DOUBLEFloat,float->ColumnType.FLOATjava.util.UUID->ColumnType.UUIDbyte[]->ColumnType.BINARY
@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
-