bookmodel.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package model
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. "github.com/tal-tech/go-zero/core/stores/cache"
  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. bookFieldNames = builderx.FieldNames(&Book{})
  14. bookRows = strings.Join(bookFieldNames, ",")
  15. bookRowsExpectAutoSet = strings.Join(stringx.Remove(bookFieldNames, "create_time", "update_time"), ",")
  16. bookRowsWithPlaceHolder = strings.Join(stringx.Remove(bookFieldNames, "book", "create_time", "update_time"), "=?,") + "=?"
  17. cacheBookPrefix = "cache#Book#book#"
  18. )
  19. type (
  20. BookModel interface {
  21. Insert(data Book) (sql.Result, error)
  22. FindOne(book string) (*Book, error)
  23. Update(data Book) error
  24. Delete(book string) error
  25. }
  26. defaultBookModel struct {
  27. sqlc.CachedConn
  28. table string
  29. }
  30. Book struct {
  31. Book string `db:"book"` // book name
  32. Price int64 `db:"price"` // book price
  33. }
  34. )
  35. func NewBookModel(conn sqlx.SqlConn, c cache.CacheConf) BookModel {
  36. return &defaultBookModel{
  37. CachedConn: sqlc.NewConn(conn, c),
  38. table: "book",
  39. }
  40. }
  41. func (m *defaultBookModel) Insert(data Book) (sql.Result, error) {
  42. query := fmt.Sprintf("insert into %s (%s) values (?, ?)", m.table, bookRowsExpectAutoSet)
  43. ret, err := m.ExecNoCache(query, data.Book, data.Price)
  44. return ret, err
  45. }
  46. func (m *defaultBookModel) FindOne(book string) (*Book, error) {
  47. bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, book)
  48. var resp Book
  49. err := m.QueryRow(&resp, bookKey, func(conn sqlx.SqlConn, v interface{}) error {
  50. query := fmt.Sprintf("select %s from %s where book = ? limit 1", bookRows, m.table)
  51. return conn.QueryRow(v, query, book)
  52. })
  53. switch err {
  54. case nil:
  55. return &resp, nil
  56. case sqlc.ErrNotFound:
  57. return nil, ErrNotFound
  58. default:
  59. return nil, err
  60. }
  61. }
  62. func (m *defaultBookModel) Update(data Book) error {
  63. bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, data.Book)
  64. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  65. query := fmt.Sprintf("update %s set %s where book = ?", m.table, bookRowsWithPlaceHolder)
  66. return conn.Exec(query, data.Price, data.Book)
  67. }, bookKey)
  68. return err
  69. }
  70. func (m *defaultBookModel) Delete(book string) error {
  71. bookKey := fmt.Sprintf("%s%v", cacheBookPrefix, book)
  72. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  73. query := fmt.Sprintf("delete from %s where book = ?", m.table)
  74. return conn.Exec(query, book)
  75. }, bookKey)
  76. return err
  77. }
  78. func (m *defaultBookModel) formatPrimary(primary interface{}) string {
  79. return fmt.Sprintf("%s%v", cacheBookPrefix, primary)
  80. }
  81. func (m *defaultBookModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
  82. query := fmt.Sprintf("select %s from %s where book = ? limit 1", bookRows, m.table)
  83. return conn.QueryRow(v, query, primary)
  84. }