2020-08-19 10:41:19 +08:00
|
|
|
package template
|
2020-07-29 17:11:41 +08:00
|
|
|
|
2021-02-09 10:58:11 +08:00
|
|
|
// FindOne defines find row by id.
|
2020-07-29 17:11:41 +08:00
|
|
|
var FindOne = `
|
2020-11-24 22:36:23 +08:00
|
|
|
func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
|
2020-07-29 17:11:41 +08:00
|
|
|
{{if .withCache}}{{.cacheKey}}
|
2020-08-19 10:41:19 +08:00
|
|
|
var resp {{.upperStartCamelObject}}
|
2020-07-29 17:11:41 +08:00
|
|
|
err := m.QueryRow(&resp, {{.cacheKeyVariable}}, func(conn sqlx.SqlConn, v interface{}) error {
|
2021-07-23 11:45:15 +08:00
|
|
|
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
2020-08-19 10:41:19 +08:00
|
|
|
return conn.QueryRow(v, query, {{.lowerStartCamelPrimaryKey}})
|
2020-07-29 17:11:41 +08:00
|
|
|
})
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
2021-07-23 11:45:15 +08:00
|
|
|
}{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
2020-08-19 10:41:19 +08:00
|
|
|
var resp {{.upperStartCamelObject}}
|
|
|
|
err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelPrimaryKey}})
|
2020-07-29 17:11:41 +08:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}{{end}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-02-09 10:58:11 +08:00
|
|
|
// FindOneByField defines find row by field.
|
2020-07-29 17:11:41 +08:00
|
|
|
var FindOneByField = `
|
2020-11-24 22:36:23 +08:00
|
|
|
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) {
|
2020-08-19 10:41:19 +08:00
|
|
|
{{if .withCache}}{{.cacheKey}}
|
|
|
|
var resp {{.upperStartCamelObject}}
|
2020-10-11 21:55:44 +08:00
|
|
|
err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
2021-03-01 17:29:07 +08:00
|
|
|
query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
2020-08-19 10:41:19 +08:00
|
|
|
if err := conn.QueryRow(&resp, query, {{.lowerStartCamelField}}); err != nil {
|
2020-07-29 17:11:41 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-19 10:41:19 +08:00
|
|
|
return resp.{{.upperStartCamelPrimaryKey}}, nil
|
2020-10-11 21:55:44 +08:00
|
|
|
}, m.queryPrimary)
|
2020-07-29 17:11:41 +08:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
2020-08-19 10:41:19 +08:00
|
|
|
}
|
|
|
|
}{{else}}var resp {{.upperStartCamelObject}}
|
2021-03-01 17:29:07 +08:00
|
|
|
query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table )
|
2020-08-19 10:41:19 +08:00
|
|
|
err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelField}})
|
2020-07-29 17:11:41 +08:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-19 10:41:19 +08:00
|
|
|
}{{end}}
|
2020-07-29 17:11:41 +08:00
|
|
|
`
|
2021-02-09 10:58:11 +08:00
|
|
|
|
|
|
|
// FindOneByFieldExtraMethod defines find row by field with extras.
|
2020-10-11 21:55:44 +08:00
|
|
|
var FindOneByFieldExtraMethod = `
|
2020-11-24 22:36:23 +08:00
|
|
|
func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string {
|
2020-10-12 14:34:11 +08:00
|
|
|
return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary)
|
2020-10-11 21:55:44 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 22:36:23 +08:00
|
|
|
func (m *default{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
|
2021-07-23 11:45:15 +08:00
|
|
|
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1", {{.lowerStartCamelObject}}Rows, m.table )
|
2020-10-11 21:55:44 +08:00
|
|
|
return conn.QueryRow(v, query, primary)
|
|
|
|
}
|
|
|
|
`
|
2020-11-24 22:36:23 +08:00
|
|
|
|
2021-02-09 10:58:11 +08:00
|
|
|
// FindOneMethod defines find row method.
|
2020-11-24 22:36:23 +08:00
|
|
|
var FindOneMethod = `FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)`
|
2021-02-09 10:58:11 +08:00
|
|
|
|
|
|
|
// FindOneByFieldMethod defines find row by field method.
|
2020-11-24 22:36:23 +08:00
|
|
|
var FindOneByFieldMethod = `FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) `
|