tianyuan-api-server/apps/sentinel/internal/model/whitelistmodel_gen.go

115 lines
4.1 KiB
Go
Raw Normal View History

2024-10-02 00:57:17 +08:00
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.7.2
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"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 (
whitelistFieldNames = builder.RawFieldNames(&Whitelist{})
whitelistRows = strings.Join(whitelistFieldNames, ",")
whitelistRowsExpectAutoSet = strings.Join(stringx.Remove(whitelistFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
whitelistRowsWithPlaceHolder = strings.Join(stringx.Remove(whitelistFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheWhitelistIdPrefix = "cache:whitelist:id:"
)
type (
whitelistModel interface {
Insert(ctx context.Context, data *Whitelist) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*Whitelist, error)
Update(ctx context.Context, data *Whitelist) error
Delete(ctx context.Context, id int64) error
}
defaultWhitelistModel struct {
sqlc.CachedConn
table string
}
Whitelist struct {
Id int64 `db:"id"` // 白名单ID
UserId int64 `db:"user_id"` // 用户ID
WhitelistIp string `db:"whitelist_ip"` // 白名单IP
CreatedAt time.Time `db:"created_at"` // 创建时间
UpdatedAt time.Time `db:"updated_at"` // 更新时间
}
)
func newWhitelistModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultWhitelistModel {
return &defaultWhitelistModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`whitelist`",
}
}
func (m *defaultWhitelistModel) Delete(ctx context.Context, id int64) error {
whitelistIdKey := fmt.Sprintf("%s%v", cacheWhitelistIdPrefix, 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)
}, whitelistIdKey)
return err
}
func (m *defaultWhitelistModel) FindOne(ctx context.Context, id int64) (*Whitelist, error) {
whitelistIdKey := fmt.Sprintf("%s%v", cacheWhitelistIdPrefix, id)
var resp Whitelist
err := m.QueryRowCtx(ctx, &resp, whitelistIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", whitelistRows, 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 *defaultWhitelistModel) Insert(ctx context.Context, data *Whitelist) (sql.Result, error) {
whitelistIdKey := fmt.Sprintf("%s%v", cacheWhitelistIdPrefix, 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, whitelistRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.UserId, data.WhitelistIp)
}, whitelistIdKey)
return ret, err
}
func (m *defaultWhitelistModel) Update(ctx context.Context, data *Whitelist) error {
whitelistIdKey := fmt.Sprintf("%s%v", cacheWhitelistIdPrefix, 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, whitelistRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, data.UserId, data.WhitelistIp, data.Id)
}, whitelistIdKey)
return err
}
func (m *defaultWhitelistModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheWhitelistIdPrefix, primary)
}
func (m *defaultWhitelistModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", whitelistRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultWhitelistModel) tableName() string {
return m.table
}