114 lines
3.7 KiB
Go
114 lines
3.7 KiB
Go
|
// 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/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`"), "=?,") + "=?"
|
||
|
)
|
||
|
|
||
|
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 {
|
||
|
conn sqlx.SqlConn
|
||
|
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) *defaultPermissionModel {
|
||
|
return &defaultPermissionModel{
|
||
|
conn: conn,
|
||
|
table: "`permission`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultPermissionModel) withSession(session sqlx.Session) *defaultPermissionModel {
|
||
|
return &defaultPermissionModel{
|
||
|
conn: sqlx.NewSqlConnFromSession(session),
|
||
|
table: "`permission`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultPermissionModel) Delete(ctx context.Context, id int64) error {
|
||
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultPermissionModel) FindOne(ctx context.Context, id int64) (*Permission, error) {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", permissionRows, m.table)
|
||
|
var resp Permission
|
||
|
err := m.conn.QueryRowCtx(ctx, &resp, 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) {
|
||
|
var resp Permission
|
||
|
query := fmt.Sprintf("select %s from %s where `name` = ? limit 1", permissionRows, m.table)
|
||
|
err := m.conn.QueryRowCtx(ctx, &resp, query, name)
|
||
|
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) {
|
||
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, permissionRowsExpectAutoSet)
|
||
|
ret, err := m.conn.ExecCtx(ctx, query, data.Parent, data.Name, data.HttpMethod, data.HttpPath, data.Status, data.Type, data.CreateTime, data.UpdateTime)
|
||
|
return ret, err
|
||
|
}
|
||
|
|
||
|
func (m *defaultPermissionModel) Update(ctx context.Context, newData *Permission) error {
|
||
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, permissionRowsWithPlaceHolder)
|
||
|
_, err := m.conn.ExecCtx(ctx, query, newData.Parent, newData.Name, newData.HttpMethod, newData.HttpPath, newData.Status, newData.Type, newData.CreateTime, newData.UpdateTime, newData.Id)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultPermissionModel) tableName() string {
|
||
|
return m.table
|
||
|
}
|