testuserinfomodel.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package nocache
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/tal-tech/go-zero/core/stores/sqlc"
  8. "github.com/tal-tech/go-zero/core/stores/sqlx"
  9. "github.com/tal-tech/go-zero/core/stringx"
  10. "github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
  11. )
  12. var (
  13. testUserInfoFieldNames = builderx.FieldNames(&TestUserInfo{})
  14. testUserInfoRows = strings.Join(testUserInfoFieldNames, ",")
  15. testUserInfoRowsExpectAutoSet = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), ",")
  16. testUserInfoRowsWithPlaceHolder = strings.Join(stringx.Remove(testUserInfoFieldNames, "id", "create_time", "update_time"), "=?,") + "=?"
  17. )
  18. type (
  19. TestUserInfoModel struct {
  20. conn sqlx.SqlConn
  21. table string
  22. }
  23. TestUserInfo struct {
  24. Id int64 `db:"id"`
  25. Nanosecond int64 `db:"nanosecond"`
  26. Data string `db:"data"`
  27. CreateTime time.Time `db:"create_time"`
  28. UpdateTime time.Time `db:"update_time"`
  29. }
  30. )
  31. func NewTestUserInfoModel(conn sqlx.SqlConn) *TestUserInfoModel {
  32. return &TestUserInfoModel{
  33. conn: conn,
  34. table: "test_user_info",
  35. }
  36. }
  37. func (m *TestUserInfoModel) Insert(data TestUserInfo) (sql.Result, error) {
  38. query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, testUserInfoRowsExpectAutoSet)
  39. ret, err := m.conn.Exec(query, data.Nanosecond, data.Data)
  40. return ret, err
  41. }
  42. func (m *TestUserInfoModel) FindOne(id int64) (*TestUserInfo, error) {
  43. query := fmt.Sprintf("select %s from %s where id = ? limit 1", testUserInfoRows, m.table)
  44. var resp TestUserInfo
  45. err := m.conn.QueryRow(&resp, query, id)
  46. switch err {
  47. case nil:
  48. return &resp, nil
  49. case sqlc.ErrNotFound:
  50. return nil, ErrNotFound
  51. default:
  52. return nil, err
  53. }
  54. }
  55. func (m *TestUserInfoModel) FindOneByNanosecond(nanosecond int64) (*TestUserInfo, error) {
  56. var resp TestUserInfo
  57. query := fmt.Sprintf("select %s from %s where nanosecond = ? limit 1", testUserInfoRows, m.table)
  58. err := m.conn.QueryRow(&resp, query, nanosecond)
  59. switch err {
  60. case nil:
  61. return &resp, nil
  62. case sqlc.ErrNotFound:
  63. return nil, ErrNotFound
  64. default:
  65. return nil, err
  66. }
  67. }
  68. func (m *TestUserInfoModel) Update(data TestUserInfo) error {
  69. query := fmt.Sprintf("update %s set %s where id = ?", m.table, testUserInfoRowsWithPlaceHolder)
  70. _, err := m.conn.Exec(query, data.Nanosecond, data.Data, data.Id)
  71. return err
  72. }
  73. func (m *TestUserInfoModel) Delete(id int64) error {
  74. query := fmt.Sprintf("delete from %s where id = ?", m.table)
  75. _, err := m.conn.Exec(query, id)
  76. return err
  77. }