| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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)
- FindDirsByServiceId(ctx context.Context, serviceId int64) ([]string, 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
- }
- // FindDirsByServiceId 查询某个服务下全部保护目录路径(剔除空值),
- // 删除服务前的联动卸载用它拿到要卸载的挂载点清单。
- func (m *customTDirectoryModel) FindDirsByServiceId(ctx context.Context, serviceId int64) ([]string, error) {
- // 查全列扫描进 struct 切片(与 FindPage 同款成熟模式),再抽取 directory 字符串
- query := fmt.Sprintf("select %s from %s where `service_id` = ?", tDirectoryRows, m.table)
- var resp []TDirectory
- if err := m.conn.QueryRowsCtx(ctx, &resp, query, serviceId); err != nil {
- return nil, err
- }
- dirs := make([]string, 0, len(resp))
- for _, d := range resp {
- if d.Directory.Valid && d.Directory.String != "" {
- dirs = append(dirs, d.Directory.String)
- }
- }
- return dirs, 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))
- }
|