tkeymodel.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. FindOneByIden(ctx context.Context, iden string) (*TKey, error)
  16. UpdateStatus(ctx context.Context, id int64, status string) error
  17. withSession(session sqlx.Session) TKeyModel
  18. }
  19. customTKeyModel struct {
  20. *defaultTKeyModel
  21. }
  22. )
  23. // NewTKeyModel returns a model for the database table.
  24. func NewTKeyModel(conn sqlx.SqlConn) TKeyModel {
  25. return &customTKeyModel{
  26. defaultTKeyModel: newTKeyModel(conn),
  27. }
  28. }
  29. // escapeLike escapes LIKE wildcards so the search value matches literally:
  30. // "50%_x" searches for the exact characters, not "50<any>..." patterns.
  31. func escapeLike(name string) string {
  32. return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(name)
  33. }
  34. // trimName normalizes the search keyword; empty means no filtering.
  35. func trimName(name string) (string, bool) {
  36. name = strings.TrimSpace(name)
  37. return name, name != ""
  38. }
  39. // FindPage returns one page of rows ordered by id together with the total
  40. // count. When name is non-empty, rows are filtered by a name LIKE match;
  41. // when status is non-empty, rows are filtered by exact status ("1"/"2").
  42. func (m *customTKeyModel) FindPage(ctx context.Context, page, pageSize int64, name, status string) ([]TKey, int64, error) {
  43. keyword, nameFilter := trimName(name)
  44. var conds []string
  45. var args []any
  46. if nameFilter {
  47. conds = append(conds, "`name` like ? escape '\\\\'")
  48. args = append(args, "%"+escapeLike(keyword)+"%")
  49. }
  50. if status != "" {
  51. conds = append(conds, "`status` = ?")
  52. args = append(args, status)
  53. }
  54. where := ""
  55. if len(conds) > 0 {
  56. where = " where " + strings.Join(conds, " and ")
  57. }
  58. var total int64
  59. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
  60. return nil, 0, err
  61. }
  62. if total == 0 {
  63. return nil, 0, nil
  64. }
  65. offset := (page - 1) * pageSize
  66. args = append(args, pageSize, offset)
  67. query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tKeyRows, m.table, where)
  68. var resp []TKey
  69. if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
  70. return nil, 0, err
  71. }
  72. return resp, total, nil
  73. }
  74. // FindOneByIden looks up a key row by its `iden` (密钥标识) column.
  75. func (m *customTKeyModel) FindOneByIden(ctx context.Context, iden string) (*TKey, error) {
  76. query := fmt.Sprintf("select %s from %s where `iden` = ? limit 1", tKeyRows, m.table)
  77. var resp TKey
  78. err := m.conn.QueryRowCtx(ctx, &resp, query, iden)
  79. switch err {
  80. case nil:
  81. return &resp, nil
  82. case sqlx.ErrNotFound:
  83. return nil, ErrNotFound
  84. default:
  85. return nil, err
  86. }
  87. }
  88. // UpdateStatus enables (status "1") or disables (status "2") a key
  89. // without touching any other column.
  90. func (m *customTKeyModel) UpdateStatus(ctx context.Context, id int64, status string) error {
  91. query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
  92. if _, err := m.conn.ExecCtx(ctx, query, status, id); err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func (m *customTKeyModel) withSession(session sqlx.Session) TKeyModel {
  98. return NewTKeyModel(sqlx.NewSqlConnFromSession(session))
  99. }