find.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package template
  2. // FindOne defines find row by id.
  3. var FindOne = `
  4. func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error) {
  5. {{if .withCache}}{{.cacheKey}}
  6. var resp {{.upperStartCamelObject}}
  7. err := m.QueryRow(&resp, {{.cacheKeyVariable}}, func(conn sqlx.SqlConn, v interface{}) error {
  8. query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
  9. return conn.QueryRow(v, query, {{.lowerStartCamelPrimaryKey}})
  10. })
  11. switch err {
  12. case nil:
  13. return &resp, nil
  14. case sqlc.ErrNotFound:
  15. return nil, ErrNotFound
  16. default:
  17. return nil, err
  18. }{{else}}query := fmt.Sprintf("select %s from %s where {{.originalPrimaryKey}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table)
  19. var resp {{.upperStartCamelObject}}
  20. err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelPrimaryKey}})
  21. switch err {
  22. case nil:
  23. return &resp, nil
  24. case sqlc.ErrNotFound:
  25. return nil, ErrNotFound
  26. default:
  27. return nil, err
  28. }{{end}}
  29. }
  30. `
  31. // FindOneByField defines find row by field.
  32. var FindOneByField = `
  33. func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) {
  34. {{if .withCache}}{{.cacheKey}}
  35. var resp {{.upperStartCamelObject}}
  36. err := m.QueryRowIndex(&resp, {{.cacheKeyVariable}}, m.formatPrimary, func(conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
  37. query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table)
  38. if err := conn.QueryRow(&resp, query, {{.lowerStartCamelField}}); err != nil {
  39. return nil, err
  40. }
  41. return resp.{{.upperStartCamelPrimaryKey}}, nil
  42. }, m.queryPrimary)
  43. switch err {
  44. case nil:
  45. return &resp, nil
  46. case sqlc.ErrNotFound:
  47. return nil, ErrNotFound
  48. default:
  49. return nil, err
  50. }
  51. }{{else}}var resp {{.upperStartCamelObject}}
  52. query := fmt.Sprintf("select %s from %s where {{.originalField}} limit 1", {{.lowerStartCamelObject}}Rows, m.table )
  53. err := m.conn.QueryRow(&resp, query, {{.lowerStartCamelField}})
  54. switch err {
  55. case nil:
  56. return &resp, nil
  57. case sqlc.ErrNotFound:
  58. return nil, ErrNotFound
  59. default:
  60. return nil, err
  61. }
  62. }{{end}}
  63. `
  64. // FindOneByFieldExtraMethod defines find row by field with extras.
  65. var FindOneByFieldExtraMethod = `
  66. func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface{}) string {
  67. return fmt.Sprintf("%s%v", {{.primaryKeyLeft}}, primary)
  68. }
  69. func (m *default{{.upperStartCamelObject}}Model) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
  70. query := fmt.Sprintf("select %s from %s where {{.originalPrimaryField}} = ? limit 1", {{.lowerStartCamelObject}}Rows, m.table )
  71. return conn.QueryRow(v, query, primary)
  72. }
  73. `
  74. // FindOneMethod defines find row method.
  75. var FindOneMethod = `FindOne({{.lowerStartCamelPrimaryKey}} {{.dataType}}) (*{{.upperStartCamelObject}}, error)`
  76. // FindOneByFieldMethod defines find row by field method.
  77. var FindOneByFieldMethod = `FindOneBy{{.upperField}}({{.in}}) (*{{.upperStartCamelObject}}, error) `