shorturlmodel.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. shorturlFieldNames = builderx.FieldNames(&Shorturl{})
  14. shorturlRows = strings.Join(shorturlFieldNames, ",")
  15. shorturlRowsExpectAutoSet = strings.Join(stringx.Remove(shorturlFieldNames, "create_time", "update_time"), ",")
  16. shorturlRowsWithPlaceHolder = strings.Join(stringx.Remove(shorturlFieldNames, "shorten", "create_time", "update_time"), "=?,") + "=?"
  17. cacheShorturlShortenPrefix = "cache#Shorturl#shorten#"
  18. )
  19. type (
  20. ShorturlModel struct {
  21. sqlc.CachedConn
  22. table string
  23. }
  24. Shorturl struct {
  25. Shorten string `db:"shorten"` // shorten key
  26. Url string `db:"url"` // original url
  27. }
  28. )
  29. func NewShorturlModel(conn sqlx.SqlConn, c cache.CacheConf, table string) *ShorturlModel {
  30. return &ShorturlModel{
  31. CachedConn: sqlc.NewConn(conn, c),
  32. table: table,
  33. }
  34. }
  35. func (m *ShorturlModel) Insert(data Shorturl) (sql.Result, error) {
  36. query := `insert into ` + m.table + ` (` + shorturlRowsExpectAutoSet + `) values (?, ?)`
  37. return m.ExecNoCache(query, data.Shorten, data.Url)
  38. }
  39. func (m *ShorturlModel) FindOne(shorten string) (*Shorturl, error) {
  40. shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, shorten)
  41. var resp Shorturl
  42. err := m.QueryRow(&resp, shorturlShortenKey, func(conn sqlx.SqlConn, v interface{}) error {
  43. query := `select ` + shorturlRows + ` from ` + m.table + ` where shorten = ? limit 1`
  44. return conn.QueryRow(v, query, shorten)
  45. })
  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 *ShorturlModel) Update(data Shorturl) error {
  56. shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, data.Shorten)
  57. _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  58. query := `update ` + m.table + ` set ` + shorturlRowsWithPlaceHolder + ` where shorten = ?`
  59. return conn.Exec(query, data.Url, data.Shorten)
  60. }, shorturlShortenKey)
  61. return err
  62. }
  63. func (m *ShorturlModel) Delete(shorten string) error {
  64. _, err := m.FindOne(shorten)
  65. if err != nil {
  66. return err
  67. }
  68. shorturlShortenKey := fmt.Sprintf("%s%v", cacheShorturlShortenPrefix, shorten)
  69. _, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
  70. query := `delete from ` + m.table + ` where shorten = ?`
  71. return conn.Exec(query, shorten)
  72. }, shorturlShortenKey)
  73. return err
  74. }