db/schema/column_definition.go

116 lines
3.5 KiB
Go
Raw Permalink Normal View History

2023-04-12 15:58:25 +08:00
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
}
// StoredAs Create a stored generated column (MySQL)
func (c *ColumnDefinition) StoredAs(as string) *ColumnDefinition {
c.storedAs = as
return c
}
// Unsigned Set INTEGER columns as UNSIGNED (MySQL)
func (c *ColumnDefinition) Unsigned() *ColumnDefinition {
c.unsigned = true
return c
}
// First Place the column "first" in the table (MySQL)
func (c *ColumnDefinition) First() *ColumnDefinition {
c.first = true
return c
}
// Default Specify a "default" value for the column
func (c *ColumnDefinition) Default(def any) *ColumnDefinition {
c.def = def
return c
}
// Comment Add a comment to a column (MySQL/PostgreSQL)
func (c *ColumnDefinition) Comment(comm string) *ColumnDefinition {
c.comment = comm
return c
}
// Collaction Specify a collation for the column (MySQL/PostgreSQL/SQL Server)
func (c *ColumnDefinition) Collaction(coll string) *ColumnDefinition {
c.collation = coll
return c
}
// Charset Specify a character set for the column (MySQL)
func (c *ColumnDefinition) Charset(chars string) *ColumnDefinition {
c.charset = chars
return c
}
// AutoIncrement set INTEGER columns as auto-increment (primary key)
func (c *ColumnDefinition) AutoIncrement() *ColumnDefinition {
c.autoIncrement = true
return c
}
// After place the column "after" another column (MySQL)
func (c *ColumnDefinition) After(column string) *ColumnDefinition {
c.after = column
return c
}
// Nullable makes a column nullable
func (c *ColumnDefinition) Nullable() *ColumnDefinition {
c.nullable = true
return c
}
// 修改字段, 默认新增
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
}