app-cloudep-permission-server/internal/model/permission_model_gen.go

158 lines
5.8 KiB
Go
Raw Normal View History

// 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 (
permissionFieldNames = builder.RawFieldNames(&Permission{})
permissionRows = strings.Join(permissionFieldNames, ",")
permissionRowsExpectAutoSet = strings.Join(stringx.Remove(permissionFieldNames, "`id`"), ",")
permissionRowsWithPlaceHolder = strings.Join(stringx.Remove(permissionFieldNames, "`id`"), "=?,") + "=?"
cachePermissionIdPrefix = "cache:permission:id:"
cachePermissionNamePrefix = "cache:permission:name:"
)
type (
permissionModel interface {
Insert(ctx context.Context, data *Permission) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*Permission, error)
FindOneByName(ctx context.Context, name string) (*Permission, error)
Update(ctx context.Context, data *Permission) error
Delete(ctx context.Context, id int64) error
}
defaultPermissionModel struct {
sqlc.CachedConn
table string
}
Permission struct {
Id int64 `db:"id"` // PK
Parent sql.NullInt64 `db:"parent"`
Name string `db:"name"`
HttpMethod string `db:"http_method"`
HttpPath string `db:"http_path"`
Status int64 `db:"status"` // 狀態 1: 啟用, 2: 關閉
Type int64 `db:"type"` // 狀態 1: 後台, 2: 前台
CreateTime int64 `db:"create_time"` // 創建時間
UpdateTime int64 `db:"update_time"` // 更新時間
}
)
func newPermissionModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultPermissionModel {
return &defaultPermissionModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`permission`",
}
}
func (m *defaultPermissionModel) withSession(session sqlx.Session) *defaultPermissionModel {
return &defaultPermissionModel{
CachedConn: m.CachedConn.WithSession(session),
table: "`permission`",
}
}
func (m *defaultPermissionModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
permissionIdKey := fmt.Sprintf("%s%v", cachePermissionIdPrefix, id)
permissionNameKey := fmt.Sprintf("%s%v", cachePermissionNamePrefix, data.Name)
_, 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)
}, permissionIdKey, permissionNameKey)
return err
}
func (m *defaultPermissionModel) FindOne(ctx context.Context, id int64) (*Permission, error) {
permissionIdKey := fmt.Sprintf("%s%v", cachePermissionIdPrefix, id)
var resp Permission
err := m.QueryRowCtx(ctx, &resp, permissionIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", permissionRows, 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 *defaultPermissionModel) FindOneByName(ctx context.Context, name string) (*Permission, error) {
permissionNameKey := fmt.Sprintf("%s%v", cachePermissionNamePrefix, name)
var resp Permission
err := m.QueryRowIndexCtx(ctx, &resp, permissionNameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `name` = ? limit 1", permissionRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, name); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultPermissionModel) Insert(ctx context.Context, data *Permission) (sql.Result, error) {
permissionIdKey := fmt.Sprintf("%s%v", cachePermissionIdPrefix, data.Id)
permissionNameKey := fmt.Sprintf("%s%v", cachePermissionNamePrefix, data.Name)
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, permissionRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Parent, data.Name, data.HttpMethod, data.HttpPath, data.Status, data.Type, data.CreateTime, data.UpdateTime)
}, permissionIdKey, permissionNameKey)
return ret, err
}
func (m *defaultPermissionModel) Update(ctx context.Context, newData *Permission) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
permissionIdKey := fmt.Sprintf("%s%v", cachePermissionIdPrefix, data.Id)
permissionNameKey := fmt.Sprintf("%s%v", cachePermissionNamePrefix, data.Name)
_, 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, permissionRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.Parent, newData.Name, newData.HttpMethod, newData.HttpPath, newData.Status, newData.Type, newData.CreateTime, newData.UpdateTime, newData.Id)
}, permissionIdKey, permissionNameKey)
return err
}
func (m *defaultPermissionModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cachePermissionIdPrefix, primary)
}
func (m *defaultPermissionModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", permissionRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultPermissionModel) tableName() string {
return m.table
}