398 lines
9.7 KiB
Go
398 lines
9.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: rbac.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const AssignPermissionToRole = `-- name: AssignPermissionToRole :exec
|
|
INSERT INTO role_permissions (role_id, permission_id)
|
|
VALUES ($1, $2)
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type AssignPermissionToRoleParams struct {
|
|
RoleID int64 `json:"role_id"`
|
|
PermissionID int64 `json:"permission_id"`
|
|
}
|
|
|
|
func (q *Queries) AssignPermissionToRole(ctx context.Context, arg AssignPermissionToRoleParams) error {
|
|
_, err := q.db.Exec(ctx, AssignPermissionToRole, arg.RoleID, arg.PermissionID)
|
|
return err
|
|
}
|
|
|
|
const BulkAssignPermissionsToRole = `-- name: BulkAssignPermissionsToRole :exec
|
|
INSERT INTO role_permissions (role_id, permission_id)
|
|
SELECT $1, p.id FROM permissions p WHERE p.id = ANY($2::BIGINT[])
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type BulkAssignPermissionsToRoleParams struct {
|
|
RoleID int64 `json:"role_id"`
|
|
Column2 []int64 `json:"column_2"`
|
|
}
|
|
|
|
func (q *Queries) BulkAssignPermissionsToRole(ctx context.Context, arg BulkAssignPermissionsToRoleParams) error {
|
|
_, err := q.db.Exec(ctx, BulkAssignPermissionsToRole, arg.RoleID, arg.Column2)
|
|
return err
|
|
}
|
|
|
|
const CreateRole = `-- name: CreateRole :one
|
|
INSERT INTO roles (name, description, is_system)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING id, name, description, is_system, created_at, updated_at
|
|
`
|
|
|
|
type CreateRoleParams struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
IsSystem bool `json:"is_system"`
|
|
}
|
|
|
|
func (q *Queries) CreateRole(ctx context.Context, arg CreateRoleParams) (Role, error) {
|
|
row := q.db.QueryRow(ctx, CreateRole, arg.Name, arg.Description, arg.IsSystem)
|
|
var i Role
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.IsSystem,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const DeleteRole = `-- name: DeleteRole :exec
|
|
DELETE FROM roles WHERE id = $1 AND is_system = false
|
|
`
|
|
|
|
func (q *Queries) DeleteRole(ctx context.Context, id int64) error {
|
|
_, err := q.db.Exec(ctx, DeleteRole, id)
|
|
return err
|
|
}
|
|
|
|
const GetAllRolesWithPermissions = `-- name: GetAllRolesWithPermissions :many
|
|
SELECT r.id AS role_id, r.name AS role_name, p.key AS permission_key
|
|
FROM roles r
|
|
INNER JOIN role_permissions rp ON rp.role_id = r.id
|
|
INNER JOIN permissions p ON p.id = rp.permission_id
|
|
ORDER BY r.name, p.key
|
|
`
|
|
|
|
type GetAllRolesWithPermissionsRow struct {
|
|
RoleID int64 `json:"role_id"`
|
|
RoleName string `json:"role_name"`
|
|
PermissionKey string `json:"permission_key"`
|
|
}
|
|
|
|
func (q *Queries) GetAllRolesWithPermissions(ctx context.Context) ([]GetAllRolesWithPermissionsRow, error) {
|
|
rows, err := q.db.Query(ctx, GetAllRolesWithPermissions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetAllRolesWithPermissionsRow
|
|
for rows.Next() {
|
|
var i GetAllRolesWithPermissionsRow
|
|
if err := rows.Scan(&i.RoleID, &i.RoleName, &i.PermissionKey); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const GetPermissionByKey = `-- name: GetPermissionByKey :one
|
|
SELECT id, key, name, description, group_name, created_at FROM permissions WHERE key = $1
|
|
`
|
|
|
|
func (q *Queries) GetPermissionByKey(ctx context.Context, key string) (Permission, error) {
|
|
row := q.db.QueryRow(ctx, GetPermissionByKey, key)
|
|
var i Permission
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.GroupName,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetRoleByID = `-- name: GetRoleByID :one
|
|
SELECT id, name, description, is_system, created_at, updated_at FROM roles WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetRoleByID(ctx context.Context, id int64) (Role, error) {
|
|
row := q.db.QueryRow(ctx, GetRoleByID, id)
|
|
var i Role
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.IsSystem,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetRoleByName = `-- name: GetRoleByName :one
|
|
SELECT id, name, description, is_system, created_at, updated_at FROM roles WHERE name = $1
|
|
`
|
|
|
|
func (q *Queries) GetRoleByName(ctx context.Context, name string) (Role, error) {
|
|
row := q.db.QueryRow(ctx, GetRoleByName, name)
|
|
var i Role
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.IsSystem,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const GetRolePermissions = `-- name: GetRolePermissions :many
|
|
SELECT p.id, p.key, p.name, p.description, p.group_name, p.created_at
|
|
FROM permissions p
|
|
INNER JOIN role_permissions rp ON rp.permission_id = p.id
|
|
WHERE rp.role_id = $1
|
|
ORDER BY p.group_name, p.key
|
|
`
|
|
|
|
func (q *Queries) GetRolePermissions(ctx context.Context, roleID int64) ([]Permission, error) {
|
|
rows, err := q.db.Query(ctx, GetRolePermissions, roleID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Permission
|
|
for rows.Next() {
|
|
var i Permission
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.GroupName,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListPermissionGroups = `-- name: ListPermissionGroups :many
|
|
SELECT DISTINCT group_name FROM permissions ORDER BY group_name
|
|
`
|
|
|
|
func (q *Queries) ListPermissionGroups(ctx context.Context) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, ListPermissionGroups)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []string
|
|
for rows.Next() {
|
|
var group_name string
|
|
if err := rows.Scan(&group_name); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, group_name)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListPermissions = `-- name: ListPermissions :many
|
|
SELECT id, key, name, description, group_name, created_at FROM permissions ORDER BY group_name, key
|
|
`
|
|
|
|
func (q *Queries) ListPermissions(ctx context.Context) ([]Permission, error) {
|
|
rows, err := q.db.Query(ctx, ListPermissions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Permission
|
|
for rows.Next() {
|
|
var i Permission
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.GroupName,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const ListRoles = `-- name: ListRoles :many
|
|
SELECT
|
|
COUNT(*) OVER () AS total_count,
|
|
id, name, description, is_system, created_at, updated_at
|
|
FROM roles
|
|
WHERE
|
|
($1::TEXT IS NULL OR name ILIKE '%' || $1::TEXT || '%')
|
|
AND ($2::BOOLEAN IS NULL OR is_system = $2::BOOLEAN)
|
|
ORDER BY name
|
|
LIMIT $4::INT
|
|
OFFSET $3::INT
|
|
`
|
|
|
|
type ListRolesParams struct {
|
|
Query pgtype.Text `json:"query"`
|
|
IsSystem pgtype.Bool `json:"is_system"`
|
|
Offset pgtype.Int4 `json:"offset"`
|
|
Limit pgtype.Int4 `json:"limit"`
|
|
}
|
|
|
|
type ListRolesRow struct {
|
|
TotalCount int64 `json:"total_count"`
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
IsSystem bool `json:"is_system"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) ListRoles(ctx context.Context, arg ListRolesParams) ([]ListRolesRow, error) {
|
|
rows, err := q.db.Query(ctx, ListRoles,
|
|
arg.Query,
|
|
arg.IsSystem,
|
|
arg.Offset,
|
|
arg.Limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListRolesRow
|
|
for rows.Next() {
|
|
var i ListRolesRow
|
|
if err := rows.Scan(
|
|
&i.TotalCount,
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.IsSystem,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const RemovePermissionFromRole = `-- name: RemovePermissionFromRole :exec
|
|
DELETE FROM role_permissions
|
|
WHERE role_id = $1 AND permission_id = $2
|
|
`
|
|
|
|
type RemovePermissionFromRoleParams struct {
|
|
RoleID int64 `json:"role_id"`
|
|
PermissionID int64 `json:"permission_id"`
|
|
}
|
|
|
|
func (q *Queries) RemovePermissionFromRole(ctx context.Context, arg RemovePermissionFromRoleParams) error {
|
|
_, err := q.db.Exec(ctx, RemovePermissionFromRole, arg.RoleID, arg.PermissionID)
|
|
return err
|
|
}
|
|
|
|
const SetRolePermissions = `-- name: SetRolePermissions :exec
|
|
DELETE FROM role_permissions WHERE role_id = $1
|
|
`
|
|
|
|
func (q *Queries) SetRolePermissions(ctx context.Context, roleID int64) error {
|
|
_, err := q.db.Exec(ctx, SetRolePermissions, roleID)
|
|
return err
|
|
}
|
|
|
|
const UpdateRole = `-- name: UpdateRole :exec
|
|
UPDATE roles
|
|
SET name = $2, description = $3, updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = $1 AND is_system = false
|
|
`
|
|
|
|
type UpdateRoleParams struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
func (q *Queries) UpdateRole(ctx context.Context, arg UpdateRoleParams) error {
|
|
_, err := q.db.Exec(ctx, UpdateRole, arg.ID, arg.Name, arg.Description)
|
|
return err
|
|
}
|
|
|
|
const UpsertPermission = `-- name: UpsertPermission :one
|
|
INSERT INTO permissions (key, name, description, group_name)
|
|
VALUES ($1, $2, $3, $4)
|
|
ON CONFLICT (key) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
description = EXCLUDED.description,
|
|
group_name = EXCLUDED.group_name
|
|
RETURNING id, key, name, description, group_name, created_at
|
|
`
|
|
|
|
type UpsertPermissionParams struct {
|
|
Key string `json:"key"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
GroupName string `json:"group_name"`
|
|
}
|
|
|
|
func (q *Queries) UpsertPermission(ctx context.Context, arg UpsertPermissionParams) (Permission, error) {
|
|
row := q.db.QueryRow(ctx, UpsertPermission,
|
|
arg.Key,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.GroupName,
|
|
)
|
|
var i Permission
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Key,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.GroupName,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|