ark-member/internal/model/machine_node_model.go

49 lines
1.4 KiB
Go
Executable File

package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ MachineNodeModel = (*customMachineNodeModel)(nil)
type (
// MachineNodeModel is an interface to be customized, add more methods here,
// and implement the added methods in customMachineNodeModel.
MachineNodeModel interface {
machineNodeModel
FindOneByHostName(ctx context.Context, hostName string) (*MachineNode, error)
}
customMachineNodeModel struct {
*defaultMachineNodeModel
}
)
// NewMachineNodeModel returns a model for the database table.
func NewMachineNodeModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) MachineNodeModel {
return &customMachineNodeModel{
defaultMachineNodeModel: newMachineNodeModel(conn, c, opts...),
}
}
func (m *defaultMachineNodeModel) FindOneByHostName(ctx context.Context, hostName string) (*MachineNode, error) {
machineNodeIdKey := fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, hostName)
var resp MachineNode
err := m.QueryRowCtx(ctx, &resp, machineNodeIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `host_name` = ? limit 1", machineNodeRows, m.table)
return conn.QueryRowCtx(ctx, v, query, hostName)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}