| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package model
- import (
- "context"
- "fmt"
- "strings"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- var _ TKeyModel = (*customTKeyModel)(nil)
- type (
- // TKeyModel is an interface to be customized, add more methods here,
- // and implement the added methods in customTKeyModel.
- TKeyModel interface {
- tKeyModel
- FindPage(ctx context.Context, page, pageSize int64, name, status string) ([]TKey, int64, error)
- FindOneByIden(ctx context.Context, iden string) (*TKey, error)
- UpdateStatus(ctx context.Context, id int64, status string) error
- withSession(session sqlx.Session) TKeyModel
- }
- customTKeyModel struct {
- *defaultTKeyModel
- }
- )
- // NewTKeyModel returns a model for the database table.
- func NewTKeyModel(conn sqlx.SqlConn) TKeyModel {
- return &customTKeyModel{
- defaultTKeyModel: newTKeyModel(conn),
- }
- }
- // escapeLike escapes LIKE wildcards so the search value matches literally:
- // "50%_x" searches for the exact characters, not "50<any>..." patterns.
- func escapeLike(name string) string {
- return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(name)
- }
- // trimName normalizes the search keyword; empty means no filtering.
- func trimName(name string) (string, bool) {
- name = strings.TrimSpace(name)
- return name, name != ""
- }
- // FindPage returns one page of rows ordered by id together with the total
- // count. When name is non-empty, rows are filtered by a name LIKE match;
- // when status is non-empty, rows are filtered by exact status ("1"/"2").
- func (m *customTKeyModel) FindPage(ctx context.Context, page, pageSize int64, name, status string) ([]TKey, int64, error) {
- keyword, nameFilter := trimName(name)
- var conds []string
- var args []any
- if nameFilter {
- conds = append(conds, "`name` like ? escape '\\\\'")
- args = append(args, "%"+escapeLike(keyword)+"%")
- }
- if status != "" {
- conds = append(conds, "`status` = ?")
- args = append(args, status)
- }
- where := ""
- if len(conds) > 0 {
- where = " where " + strings.Join(conds, " and ")
- }
- var total int64
- if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s%s", m.table, where), args...); err != nil {
- return nil, 0, err
- }
- if total == 0 {
- return nil, 0, nil
- }
- offset := (page - 1) * pageSize
- args = append(args, pageSize, offset)
- query := fmt.Sprintf("select %s from %s%s order by `id` limit ? offset ?", tKeyRows, m.table, where)
- var resp []TKey
- if err := m.conn.QueryRowsCtx(ctx, &resp, query, args...); err != nil {
- return nil, 0, err
- }
- return resp, total, nil
- }
- // FindOneByIden looks up a key row by its `iden` (密钥标识) column.
- func (m *customTKeyModel) FindOneByIden(ctx context.Context, iden string) (*TKey, error) {
- query := fmt.Sprintf("select %s from %s where `iden` = ? limit 1", tKeyRows, m.table)
- var resp TKey
- err := m.conn.QueryRowCtx(ctx, &resp, query, iden)
- switch err {
- case nil:
- return &resp, nil
- case sqlx.ErrNotFound:
- return nil, ErrNotFound
- default:
- return nil, err
- }
- }
- // UpdateStatus enables (status "1") or disables (status "2") a key
- // without touching any other column.
- func (m *customTKeyModel) UpdateStatus(ctx context.Context, id int64, status string) error {
- query := fmt.Sprintf("update %s set `status` = ? where `id` = ?", m.table)
- if _, err := m.conn.ExecCtx(ctx, query, status, id); err != nil {
- return err
- }
- return nil
- }
- func (m *customTKeyModel) withSession(session sqlx.Session) TKeyModel {
- return NewTKeyModel(sqlx.NewSqlConnFromSession(session))
- }
|