| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package model
- import (
- "context"
- "fmt"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- var _ TDirectoryModel = (*customTDirectoryModel)(nil)
- type (
- // TDirectoryModel is an interface to be customized, add more methods here,
- // and implement the added methods in customTDirectoryModel.
- TDirectoryModel interface {
- tDirectoryModel
- FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, int64, error)
- DeleteByServiceId(ctx context.Context, serviceId int64) error
- WithSession(session sqlx.Session) TDirectoryModel
- }
- customTDirectoryModel struct {
- *defaultTDirectoryModel
- }
- )
- // NewTDirectoryModel returns a model for the database table.
- func NewTDirectoryModel(conn sqlx.SqlConn) TDirectoryModel {
- return &customTDirectoryModel{
- defaultTDirectoryModel: newTDirectoryModel(conn),
- }
- }
- func (m *customTDirectoryModel) FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, 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 ?", tDirectoryRows, m.table)
- var resp []TDirectory
- if err := m.conn.QueryRowsCtx(ctx, &resp, query, pageSize, offset); err != nil {
- return nil, 0, err
- }
- return resp, total, nil
- }
- // DeleteByServiceId 删除某个服务下的全部保护目录,配合服务删除做级联清理。
- func (m *customTDirectoryModel) DeleteByServiceId(ctx context.Context, serviceId int64) error {
- query := fmt.Sprintf("delete from %s where `service_id` = ?", m.table)
- _, err := m.conn.ExecCtx(ctx, query, serviceId)
- return err
- }
- func (m *customTDirectoryModel) WithSession(session sqlx.Session) TDirectoryModel {
- return NewTDirectoryModel(sqlx.NewSqlConnFromSession(session))
- }
|