You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.9 KiB
Go
79 lines
2.9 KiB
Go
package template
|
|
|
|
// 通过id查询
|
|
var FindOne = `
|
|
func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
|
|
{{if .withCache}}{{.cacheKey}}
|
|
var resp {{.upperStartCamelObject}}
|
|
err := m.QueryRow(&resp, {{.cacheKeyVariable}}, func(conn sqlx.SqlConn, v interface{}) error {
|
|
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
|
return conn.QueryRow(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}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
|
|
var resp {{.upperStartCamelObject}}
|
|
err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelPrimaryKey}})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}{{end}}
|
|
}
|
|
`
|
|
|
|
// 通过指定字段查询
|
|
var FindOneByField = `
|
|
func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) {
|
|
{{if .withCache}}{{.cacheKey}}
|
|
var resp {{.upperStartCamelObject}}
|
|
err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, m.formatPrimary, func(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.QueryRow(&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.QueryRow(&resp, query, {{.lowerStartCamelField}})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}{{end}}
|
|
`
|
|
var FindOneByFieldExtraMethod = `
|
|
func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string {
|
|
return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary)
|
|
}
|
|
|
|
func (m *default{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
|
|
query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table )
|
|
return conn.QueryRow(v, query, primary)
|
|
}
|
|
`
|
|
|
|
var FindOneMethod = `FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)`
|
|
var FindOneByFieldMethod = `FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) `
|