diff --git a/tools/goctl/model/sql/template/field.go b/tools/goctl/model/sql/template/field.go deleted file mode 100644 index ad695eb2..00000000 --- a/tools/goctl/model/sql/template/field.go +++ /dev/null @@ -1,4 +0,0 @@ -package template - -// Field defines a filed template for types -const Field = `{{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}}` diff --git a/tools/goctl/model/sql/template/find.go b/tools/goctl/model/sql/template/find.go deleted file mode 100644 index 8154fad2..00000000 --- a/tools/goctl/model/sql/template/find.go +++ /dev/null @@ -1,85 +0,0 @@ -package template - -const ( - // FindOne defines find row by id. - FindOne = ` -func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) { - {{if .withCache}}{{.cacheKey}} - var resp {{.upperStartCamelObject}} - err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { - query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) - return conn.QueryRowCtx(ctx, v, query, {{.lowerStartCamelPrimaryKey}}) - }) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - }{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) - var resp {{.upperStartCamelObject}} - err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelPrimaryKey}}) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - }{{end}} -} -` - - // FindOneByField defines find row by field. - FindOneByField = ` -func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) { - {{if .withCache}}{{.cacheKey}} - var resp {{.upperStartCamelObject}} - err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { - query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) - if err := conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}); err != nil { - return nil, err - } - return resp.{{.upperStartCamelPrimaryKey}}, nil - }, m.queryPrimary) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -}{{else}}var resp {{.upperStartCamelObject}} - query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) - err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}) - switch err { - case nil: - return &resp, nil - case sqlc.ErrNotFound: - return nil, ErrNotFound - default: - return nil, err - } -}{{end}} -` - - // FindOneByFieldExtraMethod defines find row by field with extras. - FindOneByFieldExtraMethod = ` -func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string { - return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary) -} - -func (m *default{{.upperStartCamelObject}}Model) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { - query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) - return conn.QueryRowCtx(ctx, v, query, primary) -} -` - - // FindOneMethod defines find row method. - FindOneMethod = `FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)` - - // FindOneByFieldMethod defines find row by field method. - FindOneByFieldMethod = `FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) ` -) diff --git a/tools/goctl/model/sql/template/import.go b/tools/goctl/model/sql/template/import.go deleted file mode 100644 index cfb6cf0e..00000000 --- a/tools/goctl/model/sql/template/import.go +++ /dev/null @@ -1,35 +0,0 @@ -package template - -const ( - // Imports defines an import template for model in cache case - Imports = `import ( - "context" - "database/sql" - "fmt" - "strings" - {{if .time}}"time"{{end}} - - {{if .containsPQ}}"github.com/lib/pq"{{end}} - "github.com/zeromicro/go-zero/core/stores/builder" - "github.com/zeromicro/go-zero/core/stores/cache" - "github.com/zeromicro/go-zero/core/stores/sqlc" - "github.com/zeromicro/go-zero/core/stores/sqlx" - "github.com/zeromicro/go-zero/core/stringx" -) -` - // ImportsNoCache defines an import template for model in normal case - ImportsNoCache = `import ( - "context" - "database/sql" - "fmt" - "strings" - {{if .time}}"time"{{end}} - - {{if .containsPQ}}"github.com/lib/pq"{{end}} - "github.com/zeromicro/go-zero/core/stores/builder" - "github.com/zeromicro/go-zero/core/stores/sqlc" - "github.com/zeromicro/go-zero/core/stores/sqlx" - "github.com/zeromicro/go-zero/core/stringx" -) -` -) diff --git a/tools/goctl/model/sql/template/tablename.go b/tools/goctl/model/sql/template/tablename.go deleted file mode 100644 index 99210e3a..00000000 --- a/tools/goctl/model/sql/template/tablename.go +++ /dev/null @@ -1,8 +0,0 @@ -package template - -// TableName defines a template that generate the tableName method. -const TableName = ` -func (m *default{{.upperStartCamelObject}}Model) tableName() string { - return m.table -} -` diff --git a/tools/goctl/model/sql/template/tag.go b/tools/goctl/model/sql/template/tag.go deleted file mode 100644 index 5dcaedc5..00000000 --- a/tools/goctl/model/sql/template/tag.go +++ /dev/null @@ -1,4 +0,0 @@ -package template - -// Tag defines a tag template text -const Tag = "`db:\"{{.field}}\"`" diff --git a/tools/goctl/model/sql/template/template.go b/tools/goctl/model/sql/template/template.go new file mode 100644 index 00000000..71b15445 --- /dev/null +++ b/tools/goctl/model/sql/template/template.go @@ -0,0 +1,129 @@ +package template + +import ( + _ "embed" + "fmt" + + "github.com/zeromicro/go-zero/tools/goctl/util" +) + +// Vars defines a template for var block in model +// +//go:embed tpl/var.tpl +var Vars string + +// Types defines a template for types in model. +// +//go:embed tpl/types.tpl +var Types string + +// Tag defines a tag template text +// +//go:embed tpl/tag.tpl +var Tag string + +// TableName defines a template that generate the tableName method. +// +//go:embed tpl/table-name.tpl +var TableName string + +// New defines the template for creating model instance. +// +//go:embed tpl/model-new.tpl +var New string + +// ModelCustom defines a template for extension +// +//go:embed tpl/model.tpl +var ModelCustom string + +// ModelGen defines a template for model +var ModelGen = fmt.Sprintf(`%s + +package {{.pkg}} +{{.imports}} +{{.vars}} +{{.types}} +{{.new}} +{{.delete}} +{{.find}} +{{.insert}} +{{.update}} +{{.extraMethod}} +{{.tableName}} +`, util.DoNotEditHead) + +// Insert defines a template for insert code in model +// +//go:embed tpl/insert.tpl +var Insert string + +// InsertMethod defines an interface method template for insert code in model +// +//go:embed tpl/interface-insert.tpl +var InsertMethod string + +// Update defines a template for generating update codes +// +//go:embed tpl/update.tpl +var Update string + +// UpdateMethod defines an interface method template for generating update codes +// +//go:embed tpl/interface-update.tpl +var UpdateMethod string + +// Imports defines a import template for model in cache case +// +//go:embed tpl/import.tpl +var Imports string + +// ImportsNoCache defines a import template for model in normal case +// +//go:embed tpl/import-no-cache.tpl +var ImportsNoCache string + +// FindOne defines find row by id. +// +//go:embed tpl/find-one.tpl +var FindOne string + +// FindOneByField defines find row by field. +// +//go:embed tpl/find-one-by-field.tpl +var FindOneByField string + +// FindOneByFieldExtraMethod defines find row by field with extras. +// +//go:embed tpl/find-one-by-field-extra-method.tpl +var FindOneByFieldExtraMethod string + +// FindOneMethod defines find row method. +// +//go:embed tpl/interface-find-one.tpl +var FindOneMethod string + +// FindOneByFieldMethod defines find row by field method. +// +//go:embed tpl/interface-find-one-by-field.tpl +var FindOneByFieldMethod string + +// Field defines a filed template for types +// +//go:embed tpl/field.tpl +var Field string + +// Error defines an error template +// +//go:embed tpl/err.tpl +var Error string + +// Delete defines a delete template +// +//go:embed tpl/delete.tpl +var Delete string + +// DeleteMethod defines a delete template for interface method +// +//go:embed tpl/interface-delete.tpl +var DeleteMethod string diff --git a/tools/goctl/model/sql/template/delete.go b/tools/goctl/model/sql/template/tpl/delete.tpl similarity index 77% rename from tools/goctl/model/sql/template/delete.go rename to tools/goctl/model/sql/template/tpl/delete.tpl index f7701a8a..fb995c26 100644 --- a/tools/goctl/model/sql/template/delete.go +++ b/tools/goctl/model/sql/template/tpl/delete.tpl @@ -1,8 +1,3 @@ -package template - -const ( - // Delete defines a delete template - Delete = ` func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error { {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}}) if err!=nil{ @@ -17,8 +12,3 @@ func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{. _,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}} return err } -` - - // DeleteMethod defines a delete template for interface method - DeleteMethod = `Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error` -) diff --git a/tools/goctl/model/sql/template/errors.go b/tools/goctl/model/sql/template/tpl/err.tpl similarity index 51% rename from tools/goctl/model/sql/template/errors.go rename to tools/goctl/model/sql/template/tpl/err.tpl index e9ccc31b..dc5eac4a 100644 --- a/tools/goctl/model/sql/template/errors.go +++ b/tools/goctl/model/sql/template/tpl/err.tpl @@ -1,9 +1,5 @@ -package template - -// Error defines an error template -const Error = `package {{.pkg}} +package {{.pkg}} import "github.com/zeromicro/go-zero/core/stores/sqlx" var ErrNotFound = sqlx.ErrNotFound -` diff --git a/tools/goctl/model/sql/template/tpl/field.tpl b/tools/goctl/model/sql/template/tpl/field.tpl new file mode 100644 index 00000000..6b4ed387 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/field.tpl @@ -0,0 +1 @@ +{{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}} \ No newline at end of file diff --git a/tools/goctl/model/sql/template/tpl/find-one-by-field-extra-method.tpl b/tools/goctl/model/sql/template/tpl/find-one-by-field-extra-method.tpl new file mode 100644 index 00000000..f6d695af --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/find-one-by-field-extra-method.tpl @@ -0,0 +1,8 @@ +func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string { + return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary) +} + +func (m *default{{.upperStartCamelObject}}Model) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { + query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) + return conn.QueryRowCtx(ctx, v, query, primary) +} diff --git a/tools/goctl/model/sql/template/tpl/find-one-by-field.tpl b/tools/goctl/model/sql/template/tpl/find-one-by-field.tpl new file mode 100644 index 00000000..226502d6 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/find-one-by-field.tpl @@ -0,0 +1,30 @@ +func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) { + {{if .withCache}}{{.cacheKey}} + var resp {{.upperStartCamelObject}} + err := m.QueryRowIndexCtx(ctx, &resp, {{.cacheKeyVariable}}, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { + query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) + if err := conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}); err != nil { + return nil, err + } + return resp.{{.upperStartCamelPrimaryKey}}, nil + }, m.queryPrimary) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +}{{else}}var resp {{.upperStartCamelObject}} + query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table ) + err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelField}}) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +}{{end}} diff --git a/tools/goctl/model/sql/template/tpl/find-one.tpl b/tools/goctl/model/sql/template/tpl/find-one.tpl new file mode 100644 index 00000000..4e68b34d --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/find-one.tpl @@ -0,0 +1,26 @@ +func (m *default{{.upperStartCamelObject}}Model) FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) { + {{if .withCache}}{{.cacheKey}} + var resp {{.upperStartCamelObject}} + err := m.QueryRowCtx(ctx, &resp, {{.cacheKeyVariable}}, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { + query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) + return conn.QueryRowCtx(ctx, v, query, {{.lowerStartCamelPrimaryKey}}) + }) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + }{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table) + var resp {{.upperStartCamelObject}} + err := m.conn.QueryRowCtx(ctx, &resp, query, {{.lowerStartCamelPrimaryKey}}) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + }{{end}} +} diff --git a/tools/goctl/model/sql/template/tpl/import-no-cache.tpl b/tools/goctl/model/sql/template/tpl/import-no-cache.tpl new file mode 100644 index 00000000..d99564e1 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/import-no-cache.tpl @@ -0,0 +1,13 @@ +import ( + "context" + "database/sql" + "fmt" + "strings" + {{if .time}}"time"{{end}} + + {{if .containsPQ}}"github.com/lib/pq"{{end}} + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) diff --git a/tools/goctl/model/sql/template/tpl/import.tpl b/tools/goctl/model/sql/template/tpl/import.tpl new file mode 100644 index 00000000..7e0c40f5 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/import.tpl @@ -0,0 +1,14 @@ +import ( + "context" + "database/sql" + "fmt" + "strings" + {{if .time}}"time"{{end}} + + {{if .containsPQ}}"github.com/lib/pq"{{end}} + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) diff --git a/tools/goctl/model/sql/template/insert.go b/tools/goctl/model/sql/template/tpl/insert.tpl similarity index 71% rename from tools/goctl/model/sql/template/insert.go rename to tools/goctl/model/sql/template/tpl/insert.tpl index 6265b294..08e02bce 100644 --- a/tools/goctl/model/sql/template/insert.go +++ b/tools/goctl/model/sql/template/tpl/insert.tpl @@ -1,8 +1,3 @@ -package template - -const ( - // Insert defines a template for insert code in model - Insert = ` func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) { {{if .withCache}}{{.keys}} ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { @@ -12,8 +7,3 @@ func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, dat ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} return ret,err } -` - - // InsertMethod defines an interface method template for insert code in model - InsertMethod = `Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)` -) diff --git a/tools/goctl/model/sql/template/tpl/interface-delete.tpl b/tools/goctl/model/sql/template/tpl/interface-delete.tpl new file mode 100644 index 00000000..d10978b9 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/interface-delete.tpl @@ -0,0 +1 @@ +Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error \ No newline at end of file diff --git a/tools/goctl/model/sql/template/tpl/interface-find-one-by-field.tpl b/tools/goctl/model/sql/template/tpl/interface-find-one-by-field.tpl new file mode 100644 index 00000000..e18ded7a --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/interface-find-one-by-field.tpl @@ -0,0 +1 @@ +FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file diff --git a/tools/goctl/model/sql/template/tpl/interface-find-one.tpl b/tools/goctl/model/sql/template/tpl/interface-find-one.tpl new file mode 100644 index 00000000..a7a54401 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/interface-find-one.tpl @@ -0,0 +1 @@ +FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) \ No newline at end of file diff --git a/tools/goctl/model/sql/template/tpl/interface-insert.tpl b/tools/goctl/model/sql/template/tpl/interface-insert.tpl new file mode 100644 index 00000000..28724037 --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/interface-insert.tpl @@ -0,0 +1 @@ +Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) \ No newline at end of file diff --git a/tools/goctl/model/sql/template/tpl/interface-update.tpl b/tools/goctl/model/sql/template/tpl/interface-update.tpl new file mode 100644 index 00000000..1920425d --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/interface-update.tpl @@ -0,0 +1 @@ +Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error \ No newline at end of file diff --git a/tools/goctl/model/sql/template/new.go b/tools/goctl/model/sql/template/tpl/model-new.tpl similarity index 76% rename from tools/goctl/model/sql/template/new.go rename to tools/goctl/model/sql/template/tpl/model-new.tpl index 4bf89ff3..c73ab593 100644 --- a/tools/goctl/model/sql/template/new.go +++ b/tools/goctl/model/sql/template/tpl/model-new.tpl @@ -1,11 +1,6 @@ -package template - -// New defines the template for creating model instance. -const New = ` func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) *default{{.upperStartCamelObject}}Model { return &default{{.upperStartCamelObject}}Model{ {{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}}, table: {{.table}}, } } -` diff --git a/tools/goctl/model/sql/template/model.go b/tools/goctl/model/sql/template/tpl/model.tpl similarity index 72% rename from tools/goctl/model/sql/template/model.go rename to tools/goctl/model/sql/template/tpl/model.tpl index 9c82262d..2ca914a4 100644 --- a/tools/goctl/model/sql/template/model.go +++ b/tools/goctl/model/sql/template/tpl/model.tpl @@ -1,13 +1,4 @@ -package template - -import ( - "fmt" - - "github.com/zeromicro/go-zero/tools/goctl/util" -) - -// ModelCustom defines a template for extension -const ModelCustom = `package {{.pkg}} +package {{.pkg}} {{if .withCache}} import ( "github.com/zeromicro/go-zero/core/stores/cache" @@ -37,20 +28,3 @@ func New{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c ca default{{.upperStartCamelObject}}Model: new{{.upperStartCamelObject}}Model(conn{{if .withCache}}, c{{end}}), } } -` - -// ModelGen defines a template for model -var ModelGen = fmt.Sprintf(`%s - -package {{.pkg}} -{{.imports}} -{{.vars}} -{{.types}} -{{.new}} -{{.delete}} -{{.find}} -{{.insert}} -{{.update}} -{{.extraMethod}} -{{.tableName}} -`, util.DoNotEditHead) diff --git a/tools/goctl/model/sql/template/tpl/table-name.tpl b/tools/goctl/model/sql/template/tpl/table-name.tpl new file mode 100644 index 00000000..8b14e33a --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/table-name.tpl @@ -0,0 +1,3 @@ +func (m *default{{.upperStartCamelObject}}Model) tableName() string { + return m.table +} diff --git a/tools/goctl/model/sql/template/tpl/tag.tpl b/tools/goctl/model/sql/template/tpl/tag.tpl new file mode 100644 index 00000000..8e1ddf0d --- /dev/null +++ b/tools/goctl/model/sql/template/tpl/tag.tpl @@ -0,0 +1 @@ +`db:"{{.field}}"` \ No newline at end of file diff --git a/tools/goctl/model/sql/template/types.go b/tools/goctl/model/sql/template/tpl/types.tpl similarity index 75% rename from tools/goctl/model/sql/template/types.go rename to tools/goctl/model/sql/template/tpl/types.tpl index 0eeabeae..960cf2b3 100644 --- a/tools/goctl/model/sql/template/types.go +++ b/tools/goctl/model/sql/template/tpl/types.tpl @@ -1,7 +1,3 @@ -package template - -// Types defines a template for types in model. -const Types = ` type ( {{.lowerStartCamelObject}}Model interface{ {{.method}} @@ -16,4 +12,3 @@ type ( {{.fields}} } ) -` diff --git a/tools/goctl/model/sql/template/update.go b/tools/goctl/model/sql/template/tpl/update.tpl similarity index 75% rename from tools/goctl/model/sql/template/update.go rename to tools/goctl/model/sql/template/tpl/update.tpl index 83469519..41b9331c 100644 --- a/tools/goctl/model/sql/template/update.go +++ b/tools/goctl/model/sql/template/tpl/update.tpl @@ -1,8 +1,3 @@ -package template - -const ( - // Update defines a template for generating update codes - Update = ` func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error { {{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}}) if err!=nil{ @@ -17,8 +12,3 @@ func (m *default{{.upperStartCamelObject}}Model) Update(ctx context.Context, {{i _,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}} return err } -` - - // UpdateMethod defines an interface method template for generating update codes - UpdateMethod = `Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error` -) diff --git a/tools/goctl/model/sql/template/vars.tpl b/tools/goctl/model/sql/template/tpl/var.tpl similarity index 100% rename from tools/goctl/model/sql/template/vars.tpl rename to tools/goctl/model/sql/template/tpl/var.tpl diff --git a/tools/goctl/model/sql/template/vars.go b/tools/goctl/model/sql/template/vars.go deleted file mode 100644 index 8db226ff..00000000 --- a/tools/goctl/model/sql/template/vars.go +++ /dev/null @@ -1,7 +0,0 @@ -package template - -import _ "embed" - -// Vars defines a template for var block in model -//go:embed vars.tpl -var Vars string