tkeymodel.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/zeromicro/go-zero/core/stores/sqlx"
  7. )
  8. var _ TKeyModel = (*customTKeyModel)(nil)
  9. type (
  10. // TKeyModel is an interface to be customized, add more methods here,
  11. // and implement the added methods in customTKeyModel.
  12. TKeyModel interface {
  13. tKeyModel
  14. FindPage(ctx context.Context, page, pageSize int64, name, status string) ([]TKey, int64, error)
  15. UpdateStatus(ctx context.Context, id int64, status string) error
  16. withSession(session sqlx.Session) TKeyModel
  17. }
  18. customTKeyModel struct {
  19. *defaultTKeyModel
  20. }
  21. )
  22. // NewTKeyModel returns a model for the database table.
  23. func NewTKeyModel(conn sqlx.SqlConn) TKeyModel {
  24. return &customTKeyModel{
  25. defaultTKeyModel: newTKeyModel(conn),
  26. }
  27. }
  28. // escapeLike escapes LIKE wildcards so the search value matches literally:
  29. // "50%_x" searches for the exact characters, not "50<any>..." patterns.
  30. func escapeLike(name string) string {
  31. return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(name)
  32. }
  33. // trimName normalizes the search keyword; empty means no filtering.
  34. func trimName(name string) (string, bool) {
  35. name = strings.TrimSpace(name)
  36. return name, name != ""
  37. }
  38. // FindPage returns one page of rows ordered by id together with the total
  39. // count. When name is non-empty, rows are filtered by a name LIKE match;
  40. // when status is non-empty, rows are filtered by exact status ("1"/"2").
  41. func (m *customTKeyModel) FindPage(ctx context.Context, page, pageSize int64, name, status string) ([]TKey, int64, error) {
  42. keyword, nameFilter := trimName(name)
  43. var conds []string
  44. var args []any
  45. if nameFilter {
  46. conds = append(conds, "`name` like ? escape '\\\\'")
  47. args = append(args, "%"+escapeLike(keyword)+"%")
  48. }
  49. if status != "" {
  50. conds = append(conds, "`status` = ?")
  51. args = append(args, status)
  52. }
  53. where := ""
  54. if len(conds) > 0 {
  55. where = " where " + strings.Join(conds, " and ")
  56. }
  57. var total int64
  58. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  59. return nil, 0, err
  60. }
  61. if total == 0 {
  62. return nil, 0, nil
  63. }
  64. offset := (page - 1) * pageSize
  65. args = append(args, pageSize, offset)
  66. query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tKeyRows, m.table, where)
  67. var resp []TKey
  68. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  69. return nil, 0, err
  70. }
  71. return resp, total, nil
  72. }
  73. // UpdateStatus enables (status "1") or disables (status "2") a key
  74. // without touching any other column.
  75. func (m *customTKeyModel) UpdateStatus(ctx context.Context, id int64, status string) error {
  76. query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
  77. if _, err := m.conn.ExecCtx(ctx, query, status, id); err != nil {
  78. return err
  79. }
  80. return nil
  81. }
  82. func (m *customTKeyModel) withSession(session sqlx.Session) TKeyModel {
  83. return NewTKeyModel(sqlx.NewSqlConnFromSession(session))
  84. }