tdirectorymodel.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/zeromicro/go-zero/core/stores/sqlx"
  6. )
  7. var _ TDirectoryModel = (*customTDirectoryModel)(nil)
  8. type (
  9. // TDirectoryModel is an interface to be customized, add more methods here,
  10. // and implement the added methods in customTDirectoryModel.
  11. TDirectoryModel interface {
  12. tDirectoryModel
  13. FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, int64, error)
  14. withSession(session sqlx.Session) TDirectoryModel
  15. }
  16. customTDirectoryModel struct {
  17. *defaultTDirectoryModel
  18. }
  19. )
  20. // NewTDirectoryModel returns a model for the database table.
  21. func NewTDirectoryModel(conn sqlx.SqlConn) TDirectoryModel {
  22. return &customTDirectoryModel{
  23. defaultTDirectoryModel: newTDirectoryModel(conn),
  24. }
  25. }
  26. func (m *customTDirectoryModel) FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, int64, error) {
  27. var total int64
  28. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil {
  29. return nil, 0, err
  30. }
  31. if total == 0 {
  32. return nil, 0, nil
  33. }
  34. offset := (page - 1) * pageSize
  35. query := fmt.Sprintf("select %s from %s order by `id` limit ? offset ?", tDirectoryRows, m.table)
  36. var resp []TDirectory
  37. if err := m.conn.QueryRowsCtx(ctx, &resp, query, pageSize, offset); err != nil {
  38. return nil, 0, err
  39. }
  40. return resp, total, nil
  41. }
  42. func (m *customTDirectoryModel) withSession(session sqlx.Session) TDirectoryModel {
  43. return NewTDirectoryModel(sqlx.NewSqlConnFromSession(session))
  44. }