You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.5 KiB
85 lines
1.5 KiB
package ddl |
|
|
|
type TableColumn struct { |
|
Name string |
|
Type string |
|
GoType string |
|
Default string |
|
Comment string |
|
IsPrimary bool |
|
IsNotNull bool |
|
IsDefault bool |
|
} |
|
|
|
func (t *TableColumn) GetDefault() string { |
|
if t.Default == "0" { |
|
return "" |
|
} |
|
if t.Default == "0000-00-00 00:00:00" { |
|
return "" |
|
} |
|
return t.Default |
|
} |
|
func (t *TableColumn) GetTypeStr() string { |
|
switch t.Type { |
|
case "bigint": |
|
return "int64" |
|
case "datetime", "timetime", "date", "timestamp": |
|
return "time.Time" |
|
case "tinyint", "smallint", "int": |
|
return "int32" |
|
case "varchar", "char", "text", "longtext": |
|
return "string" |
|
case "float": |
|
return "float32" |
|
case "decimal": |
|
return "float64" |
|
} |
|
return t.Type |
|
} |
|
|
|
func (t *TableColumn) GetTypeImport() string { |
|
switch t.Type { |
|
case "datetime", "timetime", "date", "timestamp": |
|
return "time" |
|
} |
|
return "" |
|
} |
|
|
|
type TableIndex struct { |
|
Name string |
|
IsPrimary bool |
|
IsUnique bool |
|
ColumnsStr []string |
|
Columns []*TableColumn |
|
} |
|
|
|
func (t *TableIndex) GoColumns() []string { |
|
newArr := []string{} |
|
for _, v := range t.ColumnsStr { |
|
newArr = append(newArr, GoName(v)) |
|
} |
|
return newArr |
|
} |
|
|
|
type Table struct { |
|
Name string |
|
Imports []string |
|
Columns []*TableColumn |
|
Indexes []*TableIndex |
|
Primary *TableColumn |
|
} |
|
|
|
func (t *Table) GetImports() []string { |
|
arr := map[string]string{} |
|
for _, v := range t.Columns { |
|
if imp := v.GetTypeImport(); imp != "" { |
|
arr[imp] = imp |
|
} |
|
} |
|
newArr := []string{} |
|
for _, v := range arr { |
|
newArr = append(newArr, v) |
|
} |
|
return newArr |
|
}
|
|
|