tkeymodel.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/zeromicro/go-zero/core/stores/sqlx"
  6. )
  7. var _ TKeyModel = (*customTKeyModel)(nil)
  8. type (
  9. // TKeyModel is an interface to be customized, add more methods here,
  10. // and implement the added methods in customTKeyModel.
  11. TKeyModel interface {
  12. tKeyModel
  13. FindAll(ctx context.Context) ([]TKey, error)
  14. withSession(session sqlx.Session) TKeyModel
  15. }
  16. customTKeyModel struct {
  17. *defaultTKeyModel
  18. }
  19. )
  20. // NewTKeyModel returns a model for the database table.
  21. func NewTKeyModel(conn sqlx.SqlConn) TKeyModel {
  22. return &customTKeyModel{
  23. defaultTKeyModel: newTKeyModel(conn),
  24. }
  25. }
  26. func (m *customTKeyModel) FindAll(ctx context.Context) ([]TKey, error) {
  27. query := fmt.Sprintf("select %s from %s order by `id`", tKeyRows, m.table)
  28. var resp []TKey
  29. if err := m.conn.QueryRowsCtx(ctx, &resp, query); err != nil {
  30. return nil, err
  31. }
  32. return resp, nil
  33. }
  34. func (m *customTKeyModel) withSession(session sqlx.Session) TKeyModel {
  35. return NewTKeyModel(sqlx.NewSqlConnFromSession(session))
  36. }