118 lines
4.2 KiB
Go
Executable File
118 lines
4.2 KiB
Go
Executable File
// Code generated by goctl. DO NOT EDIT.
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
)
|
|
|
|
var (
|
|
machineNodeFieldNames = builder.RawFieldNames(&MachineNode{})
|
|
machineNodeRows = strings.Join(machineNodeFieldNames, ",")
|
|
machineNodeRowsExpectAutoSet = strings.Join(stringx.Remove(machineNodeFieldNames, "`id`"), ",")
|
|
machineNodeRowsWithPlaceHolder = strings.Join(stringx.Remove(machineNodeFieldNames, "`id`"), "=?,") + "=?"
|
|
|
|
cacheMachineNodeIdPrefix = "cache:machineNode:id:"
|
|
)
|
|
|
|
type (
|
|
machineNodeModel interface {
|
|
Insert(ctx context.Context, data *MachineNode) (sql.Result, error)
|
|
FindOne(ctx context.Context, id int64) (*MachineNode, error)
|
|
Update(ctx context.Context, data *MachineNode) error
|
|
Delete(ctx context.Context, id int64) error
|
|
}
|
|
|
|
defaultMachineNodeModel struct {
|
|
sqlc.CachedConn
|
|
table string
|
|
}
|
|
|
|
MachineNode struct {
|
|
Id int64 `db:"id"` // 流水號
|
|
CreateTime int64 `db:"create_time"` // 創建時間
|
|
UpdateTime int64 `db:"update_time"` // 更新時間
|
|
HostName string `db:"host_name"` // host name
|
|
}
|
|
)
|
|
|
|
func newMachineNodeModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultMachineNodeModel {
|
|
return &defaultMachineNodeModel{
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
table: "`machine_node`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) withSession(session sqlx.Session) *defaultMachineNodeModel {
|
|
return &defaultMachineNodeModel{
|
|
CachedConn: m.CachedConn.WithSession(session),
|
|
table: "`machine_node`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) Delete(ctx context.Context, id int64) error {
|
|
machineNodeIdKey := fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, id)
|
|
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
return conn.ExecCtx(ctx, query, id)
|
|
}, machineNodeIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) FindOne(ctx context.Context, id int64) (*MachineNode, error) {
|
|
machineNodeIdKey := fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, id)
|
|
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 `id` = ? limit 1", machineNodeRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, id)
|
|
})
|
|
switch err {
|
|
case nil:
|
|
return &resp, nil
|
|
case sqlc.ErrNotFound:
|
|
return nil, ErrNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) Insert(ctx context.Context, data *MachineNode) (sql.Result, error) {
|
|
machineNodeIdKey := fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, data.Id)
|
|
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, machineNodeRowsExpectAutoSet)
|
|
return conn.ExecCtx(ctx, query, data.CreateTime, data.UpdateTime, data.HostName)
|
|
}, machineNodeIdKey)
|
|
return ret, err
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) Update(ctx context.Context, data *MachineNode) error {
|
|
machineNodeIdKey := fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, data.Id)
|
|
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, machineNodeRowsWithPlaceHolder)
|
|
return conn.ExecCtx(ctx, query, data.CreateTime, data.UpdateTime, data.HostName, data.Id)
|
|
}, machineNodeIdKey)
|
|
return err
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) formatPrimary(primary any) string {
|
|
return fmt.Sprintf("%s%v", cacheMachineNodeIdPrefix, primary)
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", machineNodeRows, m.table)
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
}
|
|
|
|
func (m *defaultMachineNodeModel) tableName() string {
|
|
return m.table
|
|
}
|