fix(change model template file type): All model template variables ar… (#2573)
* fix(change model template file type): All model template variables are stored in tpl format files with the same name as the template generated using the template init command * fix(change model template file type): All model template variables are stored in tpl format files with the same name as the template generated using the template init command Co-authored-by: qilvge <qilvge@.qilvge.com> Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
// Field defines a filed template for types
|
|
||||||
const Field = `{{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}}`
|
|
||||||
@@ -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) `
|
|
||||||
)
|
|
||||||
@@ -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"
|
|
||||||
)
|
|
||||||
`
|
|
||||||
)
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
`
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
// Tag defines a tag template text
|
|
||||||
const Tag = "`db:\"{{.field}}\"`"
|
|
||||||
129
tools/goctl/model/sql/template/template.go
Normal file
129
tools/goctl/model/sql/template/template.go
Normal file
@@ -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
|
||||||
@@ -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 {
|
func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error {
|
||||||
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
|
{{if .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, {{.lowerStartCamelPrimaryKey}})
|
||||||
if err!=nil{
|
if err!=nil{
|
||||||
@@ -17,8 +12,3 @@ func (m *default{{.upperStartCamelObject}}Model) Delete(ctx context.Context, {{.
|
|||||||
_,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}}
|
_,err:=m.conn.ExecCtx(ctx, query, {{.lowerStartCamelPrimaryKey}}){{end}}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
`
|
|
||||||
|
|
||||||
// DeleteMethod defines a delete template for interface method
|
|
||||||
DeleteMethod = `Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error`
|
|
||||||
)
|
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
package template
|
package {{.pkg}}
|
||||||
|
|
||||||
// Error defines an error template
|
|
||||||
const Error = `package {{.pkg}}
|
|
||||||
|
|
||||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||||
|
|
||||||
var ErrNotFound = sqlx.ErrNotFound
|
var ErrNotFound = sqlx.ErrNotFound
|
||||||
`
|
|
||||||
1
tools/goctl/model/sql/template/tpl/field.tpl
Normal file
1
tools/goctl/model/sql/template/tpl/field.tpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{{.name}} {{.type}} {{.tag}} {{if .hasComment}}// {{.comment}}{{end}}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
30
tools/goctl/model/sql/template/tpl/find-one-by-field.tpl
Normal file
30
tools/goctl/model/sql/template/tpl/find-one-by-field.tpl
Normal file
@@ -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}}
|
||||||
26
tools/goctl/model/sql/template/tpl/find-one.tpl
Normal file
26
tools/goctl/model/sql/template/tpl/find-one.tpl
Normal file
@@ -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}}
|
||||||
|
}
|
||||||
13
tools/goctl/model/sql/template/tpl/import-no-cache.tpl
Normal file
13
tools/goctl/model/sql/template/tpl/import-no-cache.tpl
Normal file
@@ -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"
|
||||||
|
)
|
||||||
14
tools/goctl/model/sql/template/tpl/import.tpl
Normal file
14
tools/goctl/model/sql/template/tpl/import.tpl
Normal file
@@ -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"
|
||||||
|
)
|
||||||
@@ -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) {
|
func (m *default{{.upperStartCamelObject}}Model) Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error) {
|
||||||
{{if .withCache}}{{.keys}}
|
{{if .withCache}}{{.keys}}
|
||||||
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
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}}
|
ret,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||||
return ret,err
|
return ret,err
|
||||||
}
|
}
|
||||||
`
|
|
||||||
|
|
||||||
// InsertMethod defines an interface method template for insert code in model
|
|
||||||
InsertMethod = `Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)`
|
|
||||||
)
|
|
||||||
1
tools/goctl/model/sql/template/tpl/interface-delete.tpl
Normal file
1
tools/goctl/model/sql/template/tpl/interface-delete.tpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Delete(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) error
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
FindOneBy{{.upperField}}(ctx context.Context, {{.in}}) (*{{.upperStartCamelObject}}, error)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
FindOne(ctx context.Context, {{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)
|
||||||
1
tools/goctl/model/sql/template/tpl/interface-insert.tpl
Normal file
1
tools/goctl/model/sql/template/tpl/interface-insert.tpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
|
||||||
1
tools/goctl/model/sql/template/tpl/interface-update.tpl
Normal file
1
tools/goctl/model/sql/template/tpl/interface-update.tpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Update(ctx context.Context, {{if .containsIndexCache}}newData{{else}}data{{end}} *{{.upperStartCamelObject}}) error
|
||||||
@@ -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 {
|
func new{{.upperStartCamelObject}}Model(conn sqlx.SqlConn{{if .withCache}}, c cache.CacheConf{{end}}) *default{{.upperStartCamelObject}}Model {
|
||||||
return &default{{.upperStartCamelObject}}Model{
|
return &default{{.upperStartCamelObject}}Model{
|
||||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||||
table: {{.table}},
|
table: {{.table}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
|
||||||
@@ -1,13 +1,4 @@
|
|||||||
package template
|
package {{.pkg}}
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ModelCustom defines a template for extension
|
|
||||||
const ModelCustom = `package {{.pkg}}
|
|
||||||
{{if .withCache}}
|
{{if .withCache}}
|
||||||
import (
|
import (
|
||||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
"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}}),
|
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)
|
|
||||||
3
tools/goctl/model/sql/template/tpl/table-name.tpl
Normal file
3
tools/goctl/model/sql/template/tpl/table-name.tpl
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
func (m *default{{.upperStartCamelObject}}Model) tableName() string {
|
||||||
|
return m.table
|
||||||
|
}
|
||||||
1
tools/goctl/model/sql/template/tpl/tag.tpl
Normal file
1
tools/goctl/model/sql/template/tpl/tag.tpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
`db:"{{.field}}"`
|
||||||
@@ -1,7 +1,3 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
// Types defines a template for types in model.
|
|
||||||
const Types = `
|
|
||||||
type (
|
type (
|
||||||
{{.lowerStartCamelObject}}Model interface{
|
{{.lowerStartCamelObject}}Model interface{
|
||||||
{{.method}}
|
{{.method}}
|
||||||
@@ -16,4 +12,3 @@ type (
|
|||||||
{{.fields}}
|
{{.fields}}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
`
|
|
||||||
@@ -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 {
|
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 .withCache}}{{if .containsIndexCache}}data, err:=m.FindOne(ctx, newData.{{.upperStartCamelPrimaryKey}})
|
||||||
if err!=nil{
|
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}}
|
_,err:=m.conn.ExecCtx(ctx, query, {{.expressionValues}}){{end}}
|
||||||
return err
|
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`
|
|
||||||
)
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
import _ "embed"
|
|
||||||
|
|
||||||
// Vars defines a template for var block in model
|
|
||||||
//go:embed vars.tpl
|
|
||||||
var Vars string
|
|
||||||
Reference in New Issue
Block a user