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>master
parent
1677a4dceb
commit
81831b60a9
@ -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}}\"`"
|
@ -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,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
|
||||
`
|
@ -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)
|
||||
}
|
@ -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}}
|
@ -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}}
|
||||
}
|
@ -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"
|
||||
)
|
@ -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"
|
||||
)
|
@ -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)
|
@ -0,0 +1 @@
|
||||
Insert(ctx context.Context, data *{{.upperStartCamelObject}}) (sql.Result,error)
|
@ -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 {
|
||||
return &default{{.upperStartCamelObject}}Model{
|
||||
{{if .withCache}}CachedConn: sqlc.NewConn(conn, c){{else}}conn:conn{{end}},
|
||||
table: {{.table}},
|
||||
}
|
||||
}
|
||||
`
|
@ -0,0 +1,3 @@
|
||||
func (m *default{{.upperStartCamelObject}}Model) tableName() string {
|
||||
return m.table
|
||||
}
|
@ -0,0 +1 @@
|
||||
`db:"{{.field}}"`
|
@ -1,7 +0,0 @@
|
||||
package template
|
||||
|
||||
import _ "embed"
|
||||
|
||||
// Vars defines a template for var block in model
|
||||
//go:embed vars.tpl
|
||||
var Vars string
|
Loading…
Reference in New Issue