package schema type ColumnDefinition struct { Type string // kind of column, string / int Name string // name of column ColumnOptions } type ColumnOptions struct { Length int // 字段长度 Allowed []string // enum 选项 Precision int // 日期时间精度 Total int // 小数位数 Places int // 小数精度 rename string // 重命名 useCurrent bool // CURRENT TIMESTAMP after string // Place the column "after" another column (MySQL) always bool // Used as a modifier for generatedAs() (PostgreSQL) autoIncrement bool // Set INTEGER columns as auto-increment (primary key) change bool // Change the column charset string // Specify a character set for the column (MySQL) collation string // Specify a collation for the column (MySQL/PostgreSQL/SQL Server) comment string // Add a comment to the column (MySQL) def any // Specify a "default" value for the column first bool // Place the column "first" in the table (MySQL) nullable bool // Allow NULL values to be inserted into the column storedAs string // Create a stored generated column (MySQL) unsigned bool // Set the INTEGER column as UNSIGNED (MySQL) virtualAs string // Create a virtual generated column (MySQL) unique bool // Add a unique index primary bool // Add a primary index index bool // Add an index spatialIndex bool // Add a spatial index } // VirtualAs Create a virtual generated column (MySQL) func (c *ColumnDefinition) VirtualAs(as string) *ColumnDefinition { c.virtualAs = as return c } func (c *ColumnDefinition) GetVirtualAs() string { return c.virtualAs } // StoredAs Create a stored generated column (MySQL) func (c *ColumnDefinition) StoredAs(as string) *ColumnDefinition { c.storedAs = as return c } func (c *ColumnDefinition) GetStoredAs() string { return c.storedAs } // Unsigned Set INTEGER columns as UNSIGNED (MySQL) func (c *ColumnDefinition) Unsigned() *ColumnDefinition { c.unsigned = true return c } func (c *ColumnDefinition) IsUnsigned() bool { return c.unsigned } // First Place the column "first" in the table (MySQL) func (c *ColumnDefinition) First() *ColumnDefinition { c.first = true return c } func (c *ColumnDefinition) IsFirst() bool { return c.first } // Default Specify a "default" value for the column func (c *ColumnDefinition) Default(def any) *ColumnDefinition { c.def = def return c } func (c *ColumnDefinition) GetDefault() any { return c.def } // Comment Add a comment to a column (MySQL/PostgreSQL) func (c *ColumnDefinition) Comment(comm string) *ColumnDefinition { c.comment = comm return c } func (c *ColumnDefinition) GetComment() string { return c.comment } // Collaction Specify a collation for the column (MySQL/PostgreSQL/SQL Server) func (c *ColumnDefinition) Collaction(coll string) *ColumnDefinition { c.collation = coll return c } func (c *ColumnDefinition) GetCollaction() string { return c.collation } // Charset Specify a character set for the column (MySQL) func (c *ColumnDefinition) Charset(chars string) *ColumnDefinition { c.charset = chars return c } func (c *ColumnDefinition) GetCharset() string { return c.charset } // AutoIncrement set INTEGER columns as auto-increment (primary key) func (c *ColumnDefinition) AutoIncrement() *ColumnDefinition { c.autoIncrement = true return c } func (c *ColumnDefinition) IsAutoIncrement() bool { return c.autoIncrement } // After place the column "after" another column (MySQL) func (c *ColumnDefinition) After(column string) *ColumnDefinition { c.after = column return c } func (c *ColumnDefinition) GetAfter() string { return c.after } // Nullable makes a column nullable func (c *ColumnDefinition) Nullable() *ColumnDefinition { c.nullable = true return c } func (c *ColumnDefinition) IsNullable() bool { return c.nullable } // 修改字段, 默认新增 func (c *ColumnDefinition) Change(param ...string) *ColumnDefinition { if len(param) > 0 && param[0] != "" { c.rename = param[0] } c.change = true return c } // 时间戳 func (c *ColumnDefinition) UseCurrent() *ColumnDefinition { c.useCurrent = true return c } func (c *ColumnDefinition) IsUseCurrent() bool { return c.useCurrent } // 获取重命名 func (c *ColumnDefinition) GetRename() string { return c.rename }