Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions plugins/admin/modules/tools/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Param struct {
HideResetButton bool `json:"hide_reset_button"`
HideBackButton bool `json:"hide_back_button"`

TablePageTitle string `json:"table_title"`
TablePageTitle string `json:"table_page_title"`
TableDescription string `json:"table_description"`
FormTitle string `json:"form_title"`
FormDescription string `json:"form_description"`
Expand All @@ -59,6 +59,9 @@ type Param struct {
FormFields Fields `json:"form_fields"`
DetailFields Fields `json:"detail_fields"`

PrimaryKey string `json:"primary_key"`
PrimaryKeyType string `json:"primary_key_type"`

DetailDisplay uint8 `json:"detail_display"`

Output string `json:"output"`
Expand Down Expand Up @@ -123,6 +126,8 @@ func NewParam(cfg Config) *Param {
fields := getFieldsFromConn(cfg.Conn, dbTable, cfg.Driver)
tt := strings.Title(ta)

pkey, ptype := fields.GetPrimaryKey()

return &Param{
Connection: cfg.Connection,
Driver: cfg.Driver,
Expand Down Expand Up @@ -156,6 +161,9 @@ func NewParam(cfg Config) *Param {
TableDescription: utils.SetDefault(cfg.TableDescription, "", tt),
FormTitle: utils.SetDefault(cfg.FormTitle, "", tt),
FormDescription: utils.SetDefault(cfg.FormDescription, "", tt),

PrimaryKey: pkey,
PrimaryKeyType: ptype,
}
}

Expand Down Expand Up @@ -218,6 +226,15 @@ func NewParamWithFields(cfg Config, fields ...Fields) *Param {

type Fields []Field

func (fs Fields) GetPrimaryKey() (string, string) {
for _, field := range fs {
if field.IsPrimaryKey {
return field.Name, field.DBType
}
}
return "", ""
}

type Field struct {
Head string `json:"head"`
Name string `json:"name"`
Expand All @@ -234,6 +251,7 @@ type Field struct {
Default string `json:"default"`
CanAdd bool `json:"can_add"`
ExtraFun string `json:"extra_fun"`
IsPrimaryKey bool `json:"is_primary_key"`
}

func Generate(param *Param) error {
Expand Down Expand Up @@ -442,13 +460,19 @@ func getFieldsFromConn(conn db.Connection, table, driver string) Fields {

for i, model := range columnsModel {
typeName := getType(model[typeField].(string))
isPrimaryKey := false
if columnKey, ok := model["Key"].(string); ok {
isPrimaryKey = columnKey == "PRI"
}

fields[i] = Field{
Head: strings.Title(model[fieldField].(string)),
Name: model[fieldField].(string),
DBType: typeName,
CanAdd: true,
Editable: true,
FormType: form.GetFormTypeFromFieldType(db.DT(strings.ToUpper(typeName)), model[fieldField].(string)),
Head: strings.Title(model[fieldField].(string)),
Name: model[fieldField].(string),
DBType: typeName,
CanAdd: true,
Editable: true,
IsPrimaryKey: isPrimaryKey,
FormType: form.GetFormTypeFromFieldType(db.DT(strings.ToUpper(typeName)), model[fieldField].(string)),
}
if model[fieldField].(string) == "id" {
fields[i].Filterable = true
Expand Down
12 changes: 10 additions & 2 deletions plugins/admin/modules/tools/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ import (
func Get{{.TableTitle}}Table(ctx *context.Context) table.Table {

{{if eq .Connection "default"}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}"))
{{if eq .PrimaryKey ""}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}"))
{{else}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}").SetPrimaryKey("{{.PrimaryKey}}", db.{{.PrimaryKeyType}}))
{{end}}
{{else}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}"))
{{if eq .PrimaryKey ""}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}"))
{{else}}
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}").SetPrimaryKey("{{.PrimaryKey}}", db.{{.PrimaryKeyType}}))
{{end}}
{{end}}

info := {{.Table}}.GetInfo(){{if .HideFilterArea}}.HideFilterArea(){{end}}
Expand Down