1
0
mirror of https://github.com/beego/bee.git synced 2024-11-30 03:11:28 +00:00
bee/internal/app/module/beegopro/schema_model.go

98 lines
2.2 KiB
Go
Raw Normal View History

2020-07-04 14:58:03 +00:00
package beegopro
import (
"github.com/beego/bee/utils"
"strings"
)
2020-07-05 06:54:26 +00:00
// parse get the model info
2020-07-04 14:58:03 +00:00
type ModelInfo struct {
Name string `json:"name"` // mysql name
InputType string `json:"inputType"` // user input type
MysqlType string `json:"mysqlType"` // mysql type
GoType string `json:"goType"` // go type
Orm string `json:"orm"` // orm tag
Comment string `json:"comment"` // mysql comment
Extend string `json:"extend"` // user extend info
}
func (m ModelInfo) GetColumnKey() (columnKey string) {
if m.InputType == "auto" || m.Orm == "pk" {
columnKey = "PRI"
}
return
}
func (m ModelInfo) IsPrimaryKey() (flag bool) {
if m.Orm == "auto" || m.Orm == "pk" || strings.ToLower(m.Name) == "id" {
flag = true
}
return
}
2020-07-05 06:54:26 +00:00
type ModelInfos []ModelInfo
2020-07-04 14:58:03 +00:00
2020-07-05 06:54:26 +00:00
// to render model schemas
func (modelInfos ModelInfos) ToModelSchemas() (output ModelSchemas) {
2020-07-04 14:58:03 +00:00
output = make(ModelSchemas, 0)
2020-07-05 06:54:26 +00:00
for i, value := range modelInfos {
2020-07-04 14:58:03 +00:00
if i == 0 && !value.IsPrimaryKey() {
inputType, goType, mysqlType, ormTag := getModelType("auto")
output = append(output, &ModelSchema{
Name: "id",
InputType: inputType,
ColumnKey: "PRI",
Comment: "ID",
MysqlType: mysqlType,
GoType: goType,
OrmTag: ormTag,
Extend: value.Extend,
})
}
modelSchema := &ModelSchema{
Name: value.Name,
InputType: value.InputType,
ColumnKey: value.GetColumnKey(),
MysqlType: value.MysqlType,
Comment: value.Comment,
GoType: value.GoType,
OrmTag: value.Orm,
}
output = append(output, modelSchema)
}
return
}
type ModelSchema struct {
Name string // column name
InputType string // user input type
MysqlType string // mysql type
ColumnKey string // PRI
Comment string // comment
GoType string // go type
OrmTag string // orm tag
Extend string
}
type ModelSchemas []*ModelSchema
func (m ModelSchemas) IsExistTime() bool {
for _, value := range m {
if value.InputType == "datetime" {
return true
}
}
return false
}
func (m ModelSchemas) GetPrimaryKey() string {
camelPrimaryKey := ""
for _, value := range m {
if value.ColumnKey == "PRI" {
camelPrimaryKey = utils.CamelString(value.Name)
}
}
return camelPrimaryKey
}