* 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>
27 lines
1.1 KiB
Smarty
27 lines
1.1 KiB
Smarty
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}}
|
|
}
|