| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package model
- import (
- "context"
- "fmt"
- "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) ([]TKey, int64, 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),
- }
- }
- func (m *customTKeyModel) FindPage(ctx context.Context, page, pageSize int64) ([]TKey, int64, error) {
- var total int64
- if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil {
- return nil, 0, err
- }
- if total == 0 {
- return nil, 0, nil
- }
- offset := (page - 1) * pageSize
- query := fmt.Sprintf("select %s from %s order by `id` limit ? offset ?", tKeyRows, m.table)
- var resp []TKey
- if err := m.conn.QueryRowsCtx(ctx, &resp, query, pageSize, offset); err != nil {
- return nil, 0, err
- }
- return resp, total, nil
- }
- func (m *customTKeyModel) withSession(session sqlx.Session) TKeyModel {
- return NewTKeyModel(sqlx.NewSqlConnFromSession(session))
- }
|