Skip to content

Commit bd0329c

Browse files
author
cg33
authored
Merge pull request #536 from chenhg5/master
feat(db): primary key identify
2 parents b12eb42 + e9e375e commit bd0329c

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

plugins/admin/modules/tools/generator.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Param struct {
4545
HideResetButton bool `json:"hide_reset_button"`
4646
HideBackButton bool `json:"hide_back_button"`
4747

48-
TablePageTitle string `json:"table_title"`
48+
TablePageTitle string `json:"table_page_title"`
4949
TableDescription string `json:"table_description"`
5050
FormTitle string `json:"form_title"`
5151
FormDescription string `json:"form_description"`
@@ -59,6 +59,9 @@ type Param struct {
5959
FormFields Fields `json:"form_fields"`
6060
DetailFields Fields `json:"detail_fields"`
6161

62+
PrimaryKey string `json:"primary_key"`
63+
PrimaryKeyType string `json:"primary_key_type"`
64+
6265
DetailDisplay uint8 `json:"detail_display"`
6366

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

129+
pkey, ptype := fields.GetPrimaryKey()
130+
126131
return &Param{
127132
Connection: cfg.Connection,
128133
Driver: cfg.Driver,
@@ -156,6 +161,9 @@ func NewParam(cfg Config) *Param {
156161
TableDescription: utils.SetDefault(cfg.TableDescription, "", tt),
157162
FormTitle: utils.SetDefault(cfg.FormTitle, "", tt),
158163
FormDescription: utils.SetDefault(cfg.FormDescription, "", tt),
164+
165+
PrimaryKey: pkey,
166+
PrimaryKeyType: ptype,
159167
}
160168
}
161169

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

219227
type Fields []Field
220228

229+
func (fs Fields) GetPrimaryKey() (string, string) {
230+
for _, field := range fs {
231+
if field.IsPrimaryKey {
232+
return field.Name, field.DBType
233+
}
234+
}
235+
return "", ""
236+
}
237+
221238
type Field struct {
222239
Head string `json:"head"`
223240
Name string `json:"name"`
@@ -234,6 +251,7 @@ type Field struct {
234251
Default string `json:"default"`
235252
CanAdd bool `json:"can_add"`
236253
ExtraFun string `json:"extra_fun"`
254+
IsPrimaryKey bool `json:"is_primary_key"`
237255
}
238256

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

443461
for i, model := range columnsModel {
444462
typeName := getType(model[typeField].(string))
463+
isPrimaryKey := false
464+
if columnKey, ok := model["Key"].(string); ok {
465+
isPrimaryKey = columnKey == "PRI"
466+
}
467+
445468
fields[i] = Field{
446-
Head: strings.Title(model[fieldField].(string)),
447-
Name: model[fieldField].(string),
448-
DBType: typeName,
449-
CanAdd: true,
450-
Editable: true,
451-
FormType: form.GetFormTypeFromFieldType(db.DT(strings.ToUpper(typeName)), model[fieldField].(string)),
469+
Head: strings.Title(model[fieldField].(string)),
470+
Name: model[fieldField].(string),
471+
DBType: typeName,
472+
CanAdd: true,
473+
Editable: true,
474+
IsPrimaryKey: isPrimaryKey,
475+
FormType: form.GetFormTypeFromFieldType(db.DT(strings.ToUpper(typeName)), model[fieldField].(string)),
452476
}
453477
if model[fieldField].(string) == "id" {
454478
fields[i].Filterable = true

plugins/admin/modules/tools/template.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ import (
1414
func Get{{.TableTitle}}Table(ctx *context.Context) table.Table {
1515
1616
{{if eq .Connection "default"}}
17-
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}"))
17+
{{if eq .PrimaryKey ""}}
18+
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}"))
19+
{{else}}
20+
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriver("{{.Driver}}").SetPrimaryKey("{{.PrimaryKey}}", db.{{.PrimaryKeyType}}))
21+
{{end}}
1822
{{else}}
19-
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}"))
23+
{{if eq .PrimaryKey ""}}
24+
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}"))
25+
{{else}}
26+
{{.Table}} := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("{{.Driver}}", "{{.Connection}}").SetPrimaryKey("{{.PrimaryKey}}", db.{{.PrimaryKeyType}}))
27+
{{end}}
2028
{{end}}
2129
2230
info := {{.Table}}.GetInfo(){{if .HideFilterArea}}.HideFilterArea(){{end}}

0 commit comments

Comments
 (0)