feat: 统一sql命名、函数命名
This commit is contained in:
@@ -10,25 +10,25 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const countSysMenus = `-- name: CountSysMenus :one
|
||||
const countMenus = `-- name: CountMenus :one
|
||||
SELECT COUNT(*)
|
||||
FROM sys_menus
|
||||
`
|
||||
|
||||
func (q *Queries) CountSysMenus(ctx context.Context) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countSysMenus)
|
||||
func (q *Queries) CountMenus(ctx context.Context) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countMenus)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const createSysMenu = `-- name: CreateSysMenu :one
|
||||
const createMenu = `-- name: CreateMenu :one
|
||||
INSERT INTO sys_menus (name, path, component, type, hidden, sort, status, parent_id, icon)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type CreateSysMenuParams struct {
|
||||
type CreateMenuParams struct {
|
||||
Name string `json:"name"`
|
||||
Path *string `json:"path"`
|
||||
Component *string `json:"component"`
|
||||
@@ -40,8 +40,8 @@ type CreateSysMenuParams struct {
|
||||
Icon *int32 `json:"icon"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSysMenu(ctx context.Context, arg CreateSysMenuParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, createSysMenu,
|
||||
func (q *Queries) CreateMenu(ctx context.Context, arg CreateMenuParams) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, createMenu,
|
||||
arg.Name,
|
||||
arg.Path,
|
||||
arg.Component,
|
||||
@@ -57,47 +57,47 @@ func (q *Queries) CreateSysMenu(ctx context.Context, arg CreateSysMenuParams) (i
|
||||
return id, err
|
||||
}
|
||||
|
||||
const createSysMenuPermission = `-- name: CreateSysMenuPermission :exec
|
||||
const createMenuPermission = `-- name: CreateMenuPermission :exec
|
||||
INSERT INTO sys_menu_permission (menu_id, permission_id)
|
||||
VALUES ($1, $2)
|
||||
`
|
||||
|
||||
type CreateSysMenuPermissionParams struct {
|
||||
type CreateMenuPermissionParams struct {
|
||||
MenuID int32 `json:"menu_id"`
|
||||
PermissionID int32 `json:"permission_id"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSysMenuPermission(ctx context.Context, arg CreateSysMenuPermissionParams) error {
|
||||
_, err := q.db.Exec(ctx, createSysMenuPermission, arg.MenuID, arg.PermissionID)
|
||||
func (q *Queries) CreateMenuPermission(ctx context.Context, arg CreateMenuPermissionParams) error {
|
||||
_, err := q.db.Exec(ctx, createMenuPermission, arg.MenuID, arg.PermissionID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSysMenu = `-- name: DeleteSysMenu :execrows
|
||||
const deleteMenu = `-- name: DeleteMenu :execrows
|
||||
DELETE
|
||||
FROM sys_menus
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSysMenu(ctx context.Context, id int32) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteSysMenu, id)
|
||||
func (q *Queries) DeleteMenu(ctx context.Context, id int32) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteMenu, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteSysMenuPermission = `-- name: DeleteSysMenuPermission :exec
|
||||
const deleteMenuPermission = `-- name: DeleteMenuPermission :exec
|
||||
DELETE
|
||||
FROM sys_menu_permission
|
||||
WHERE menu_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSysMenuPermission(ctx context.Context, menuID int32) error {
|
||||
_, err := q.db.Exec(ctx, deleteSysMenuPermission, menuID)
|
||||
func (q *Queries) DeleteMenuPermission(ctx context.Context, menuID int32) error {
|
||||
_, err := q.db.Exec(ctx, deleteMenuPermission, menuID)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSysPermissionByMenuID = `-- name: DeleteSysPermissionByMenuID :exec
|
||||
const deletePermissionsByMenuID = `-- name: DeletePermissionsByMenuID :exec
|
||||
DELETE
|
||||
FROM sys_permissions
|
||||
WHERE id IN (SELECT permission_id
|
||||
@@ -105,79 +105,19 @@ WHERE id IN (SELECT permission_id
|
||||
WHERE menu_id = $1)
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSysPermissionByMenuID(ctx context.Context, menuID int32) error {
|
||||
_, err := q.db.Exec(ctx, deleteSysPermissionByMenuID, menuID)
|
||||
func (q *Queries) DeletePermissionsByMenuID(ctx context.Context, menuID int32) error {
|
||||
_, err := q.db.Exec(ctx, deletePermissionsByMenuID, menuID)
|
||||
return err
|
||||
}
|
||||
|
||||
const getAllSysMenus = `-- name: GetAllSysMenus :many
|
||||
SELECT m.id, m.name, m.path, m.component, m.type, m.hidden, m.sort, m.status, m.parent_id, m.icon, m.created_at, m.updated_at,
|
||||
p.code AS permission_code
|
||||
FROM sys_menus m
|
||||
LEFT JOIN sys_menu_permission mp ON m.id = mp.menu_id
|
||||
LEFT JOIN sys_permissions p ON p.id = mp.permission_id
|
||||
ORDER BY m.sort ASC,
|
||||
m.id ASC
|
||||
`
|
||||
|
||||
type GetAllSysMenusRow struct {
|
||||
ID int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path *string `json:"path"`
|
||||
Component *string `json:"component"`
|
||||
Type int16 `json:"type"`
|
||||
Hidden *bool `json:"hidden"`
|
||||
Sort *int32 `json:"sort"`
|
||||
Status int16 `json:"status"`
|
||||
ParentID *int32 `json:"parent_id"`
|
||||
Icon *int32 `json:"icon"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
PermissionCode *string `json:"permission_code"`
|
||||
}
|
||||
|
||||
func (q *Queries) GetAllSysMenus(ctx context.Context) ([]GetAllSysMenusRow, error) {
|
||||
rows, err := q.db.Query(ctx, getAllSysMenus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []GetAllSysMenusRow{}
|
||||
for rows.Next() {
|
||||
var i GetAllSysMenusRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Path,
|
||||
&i.Component,
|
||||
&i.Type,
|
||||
&i.Hidden,
|
||||
&i.Sort,
|
||||
&i.Status,
|
||||
&i.ParentID,
|
||||
&i.Icon,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PermissionCode,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getSysMenuByID = `-- name: GetSysMenuByID :one
|
||||
const getMenuByID = `-- name: GetMenuByID :one
|
||||
SELECT id, name, path, component, type, hidden, sort, status, parent_id, icon, created_at, updated_at
|
||||
FROM sys_menus
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSysMenuByID(ctx context.Context, id int32) (SysMenu, error) {
|
||||
row := q.db.QueryRow(ctx, getSysMenuByID, id)
|
||||
func (q *Queries) GetMenuByID(ctx context.Context, id int32) (SysMenu, error) {
|
||||
row := q.db.QueryRow(ctx, getMenuByID, id)
|
||||
var i SysMenu
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
@@ -196,21 +136,17 @@ func (q *Queries) GetSysMenuByID(ctx context.Context, id int32) (SysMenu, error)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listSysMenus = `-- name: ListSysMenus :many
|
||||
SELECT m.id, m.name, m.path, m.component, m.type, m.hidden, m.sort, m.status, m.parent_id, m.icon, m.created_at, m.updated_at, p.code AS permission_code
|
||||
const listAllMenus = `-- name: ListAllMenus :many
|
||||
SELECT m.id, m.name, m.path, m.component, m.type, m.hidden, m.sort, m.status, m.parent_id, m.icon, m.created_at, m.updated_at,
|
||||
p.code AS permission_code
|
||||
FROM sys_menus m
|
||||
LEFT JOIN sys_menu_permission mp ON m.id = mp.menu_id
|
||||
LEFT JOIN sys_permissions p ON mp.permission_id = p.id
|
||||
ORDER BY m.id
|
||||
LIMIT $1 OFFSET $2
|
||||
LEFT JOIN sys_permissions p ON p.id = mp.permission_id
|
||||
ORDER BY m.sort ASC,
|
||||
m.id ASC
|
||||
`
|
||||
|
||||
type ListSysMenusParams struct {
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
}
|
||||
|
||||
type ListSysMenusRow struct {
|
||||
type ListAllMenusRow struct {
|
||||
ID int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path *string `json:"path"`
|
||||
@@ -226,15 +162,15 @@ type ListSysMenusRow struct {
|
||||
PermissionCode *string `json:"permission_code"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListSysMenus(ctx context.Context, arg ListSysMenusParams) ([]ListSysMenusRow, error) {
|
||||
rows, err := q.db.Query(ctx, listSysMenus, arg.Limit, arg.Offset)
|
||||
func (q *Queries) ListAllMenus(ctx context.Context) ([]ListAllMenusRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAllMenus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListSysMenusRow{}
|
||||
items := []ListAllMenusRow{}
|
||||
for rows.Next() {
|
||||
var i ListSysMenusRow
|
||||
var i ListAllMenusRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
@@ -260,7 +196,71 @@ func (q *Queries) ListSysMenus(ctx context.Context, arg ListSysMenusParams) ([]L
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateSysMenu = `-- name: UpdateSysMenu :execrows
|
||||
const listMenus = `-- name: ListMenus :many
|
||||
SELECT m.id, m.name, m.path, m.component, m.type, m.hidden, m.sort, m.status, m.parent_id, m.icon, m.created_at, m.updated_at, p.code AS permission_code
|
||||
FROM sys_menus m
|
||||
LEFT JOIN sys_menu_permission mp ON m.id = mp.menu_id
|
||||
LEFT JOIN sys_permissions p ON mp.permission_id = p.id
|
||||
ORDER BY m.id
|
||||
LIMIT $1 OFFSET $2
|
||||
`
|
||||
|
||||
type ListMenusParams struct {
|
||||
Limit int32 `json:"limit"`
|
||||
Offset int32 `json:"offset"`
|
||||
}
|
||||
|
||||
type ListMenusRow struct {
|
||||
ID int32 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path *string `json:"path"`
|
||||
Component *string `json:"component"`
|
||||
Type int16 `json:"type"`
|
||||
Hidden *bool `json:"hidden"`
|
||||
Sort *int32 `json:"sort"`
|
||||
Status int16 `json:"status"`
|
||||
ParentID *int32 `json:"parent_id"`
|
||||
Icon *int32 `json:"icon"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt *time.Time `json:"updated_at"`
|
||||
PermissionCode *string `json:"permission_code"`
|
||||
}
|
||||
|
||||
func (q *Queries) ListMenus(ctx context.Context, arg ListMenusParams) ([]ListMenusRow, error) {
|
||||
rows, err := q.db.Query(ctx, listMenus, arg.Limit, arg.Offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListMenusRow{}
|
||||
for rows.Next() {
|
||||
var i ListMenusRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.Path,
|
||||
&i.Component,
|
||||
&i.Type,
|
||||
&i.Hidden,
|
||||
&i.Sort,
|
||||
&i.Status,
|
||||
&i.ParentID,
|
||||
&i.Icon,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.PermissionCode,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateMenu = `-- name: UpdateMenu :execrows
|
||||
UPDATE sys_menus
|
||||
SET name = coalesce($1, name),
|
||||
path = coalesce($2, path),
|
||||
@@ -274,7 +274,7 @@ SET name = coalesce($1, name),
|
||||
WHERE id = $12
|
||||
`
|
||||
|
||||
type UpdateSysMenuParams struct {
|
||||
type UpdateMenuParams struct {
|
||||
Name *string `json:"name"`
|
||||
Path *string `json:"path"`
|
||||
Component *string `json:"component"`
|
||||
@@ -289,8 +289,8 @@ type UpdateSysMenuParams struct {
|
||||
ID int32 `json:"id"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSysMenu(ctx context.Context, arg UpdateSysMenuParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateSysMenu,
|
||||
func (q *Queries) UpdateMenu(ctx context.Context, arg UpdateMenuParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, updateMenu,
|
||||
arg.Name,
|
||||
arg.Path,
|
||||
arg.Component,
|
||||
@@ -310,7 +310,7 @@ func (q *Queries) UpdateSysMenu(ctx context.Context, arg UpdateSysMenuParams) (i
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const updateSysMenuPermissionCode = `-- name: UpdateSysMenuPermissionCode :exec
|
||||
const updateMenuPermissionCode = `-- name: UpdateMenuPermissionCode :exec
|
||||
UPDATE sys_permissions p
|
||||
SET code = coalesce($2, code)
|
||||
FROM sys_menu_permission mp
|
||||
@@ -318,12 +318,12 @@ WHERE p.id = mp.permission_id
|
||||
AND mp.menu_id = $1
|
||||
`
|
||||
|
||||
type UpdateSysMenuPermissionCodeParams struct {
|
||||
type UpdateMenuPermissionCodeParams struct {
|
||||
MenuID int32 `json:"menu_id"`
|
||||
Code *string `json:"code"`
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateSysMenuPermissionCode(ctx context.Context, arg UpdateSysMenuPermissionCodeParams) error {
|
||||
_, err := q.db.Exec(ctx, updateSysMenuPermissionCode, arg.MenuID, arg.Code)
|
||||
func (q *Queries) UpdateMenuPermissionCode(ctx context.Context, arg UpdateMenuPermissionCodeParams) error {
|
||||
_, err := q.db.Exec(ctx, updateMenuPermissionCode, arg.MenuID, arg.Code)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user