Files
phylum/server/internal/sql/resources.sql.go
2024-08-04 22:00:20 +05:30

339 lines
8.0 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
// source: resources.sql
package sql
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const createResource = `-- name: CreateResource :one
INSERT INTO resources(
id, parent, name, dir, created, modified
) VALUES (
$1, $2, $3, $4, NOW(), NOW()
) RETURNING id, parent, name, dir, created, modified, deleted, size, etag
`
type CreateResourceParams struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
}
func (q *Queries) CreateResource(ctx context.Context, arg CreateResourceParams) (Resource, error) {
row := q.db.QueryRow(ctx, createResource,
arg.ID,
arg.Parent,
arg.Name,
arg.Dir,
)
var i Resource
err := row.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Etag,
)
return i, err
}
const deleteRecursive = `-- name: DeleteRecursive :exec
WITH RECURSIVE nodes(id, parent) AS (
SELECT r.id, r.parent
FROM resources r WHERE r.id = $1::uuid
UNION ALL
SELECT r.id, r.parent
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
)
UPDATE resources
SET modified = NOW(), deleted = NOW()
WHERE id in (SELECT id FROM nodes)
`
func (q *Queries) DeleteRecursive(ctx context.Context, id uuid.UUID) error {
_, err := q.db.Exec(ctx, deleteRecursive, id)
return err
}
const hardDeleteRecursive = `-- name: HardDeleteRecursive :many
WITH RECURSIVE nodes(id, parent) AS (
SELECT r.id, r.parent
FROM resources r WHERE r.id = $1::uuid
UNION ALL
SELECT r.id, r.parent
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
)
DELETE FROM resources
WHERE id in (SELECT id FROM nodes)
RETURNING id, dir
`
type HardDeleteRecursiveRow struct {
ID uuid.UUID
Dir bool
}
func (q *Queries) HardDeleteRecursive(ctx context.Context, id uuid.UUID) ([]HardDeleteRecursiveRow, error) {
rows, err := q.db.Query(ctx, hardDeleteRecursive, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []HardDeleteRecursiveRow
for rows.Next() {
var i HardDeleteRecursiveRow
if err := rows.Scan(&i.ID, &i.Dir); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const readDir = `-- name: ReadDir :many
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, size, etag, depth, path, permission) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, 0, ''::text, p.permission
FROM resources r
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $2::int
WHERE r.id = $3::uuid
UNION ALL
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, n.depth + 1, concat(n.path, '/', r.name), CASE WHEN (p.permission IS NULL OR p.permission < n.permission) THEN n.permission ELSE p.permission END
FROM resources r JOIN nodes n on r.parent = n.id
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $2::int
WHERE deleted IS NULL
AND CASE WHEN $4::boolean THEN true ELSE depth < 1 END
)
SELECT id, parent, name, dir, created, modified, size, etag, depth, path, permission from nodes
WHERE CASE WHEN $1::boolean THEN true ELSE depth > 0 END
`
type ReadDirParams struct {
IncludeRoot bool
UserID int32
ID uuid.UUID
Recursive bool
}
type ReadDirRow struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Size pgtype.Int8
Etag pgtype.Text
Depth int32
Path string
Permission pgtype.Int4
}
func (q *Queries) ReadDir(ctx context.Context, arg ReadDirParams) ([]ReadDirRow, error) {
rows, err := q.db.Query(ctx, readDir,
arg.IncludeRoot,
arg.UserID,
arg.ID,
arg.Recursive,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ReadDirRow
for rows.Next() {
var i ReadDirRow
if err := rows.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Size,
&i.Etag,
&i.Depth,
&i.Path,
&i.Permission,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const resourceById = `-- name: ResourceById :one
SELECT id, parent, name, dir, created, modified, deleted, size, etag from resources WHERE id = $1
`
func (q *Queries) ResourceById(ctx context.Context, id uuid.UUID) (Resource, error) {
row := q.db.QueryRow(ctx, resourceById, id)
var i Resource
err := row.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Etag,
)
return i, err
}
const resourceByPath = `-- name: ResourceByPath :one
WITH RECURSIVE nodes(id, parent, name, dir, created, modified, size, etag, depth, path, search, permission) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, 0, ''::text, $1::text[], p.permission
FROM resources r
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $2::int
WHERE r.id = $3::uuid
UNION ALL
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, n.depth + 1, concat(n.path, '/', r.name), n.search, CASE WHEN (p.permission IS NULL OR p.permission < n.permission) THEN n.permission ELSE p.permission END
FROM resources r
JOIN nodes n ON r.parent = n.id
LEFT JOIN permissions p
ON r.id = p.resource_id
AND p.user_id = $2::int
WHERE deleted IS NULL
AND r.name = n.search[n.depth + 1]
)
SELECT id, parent, name, dir, created, modified, size, etag, depth, path, search, permission FROM nodes WHERE cardinality(search) = depth
`
type ResourceByPathParams struct {
Search []string
UserID int32
Root uuid.UUID
}
type ResourceByPathRow struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Size pgtype.Int8
Etag pgtype.Text
Depth int32
Path string
Search []string
Permission pgtype.Int4
}
func (q *Queries) ResourceByPath(ctx context.Context, arg ResourceByPathParams) (ResourceByPathRow, error) {
row := q.db.QueryRow(ctx, resourceByPath, arg.Search, arg.UserID, arg.Root)
var i ResourceByPathRow
err := row.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Size,
&i.Etag,
&i.Depth,
&i.Path,
&i.Search,
&i.Permission,
)
return i, err
}
const updateResourceContents = `-- name: UpdateResourceContents :exec
UPDATE resources
SET
size = $1,
etag = $2,
modified = NOW()
WHERE id = $3
`
type UpdateResourceContentsParams struct {
Size pgtype.Int8
Etag pgtype.Text
ID uuid.UUID
}
func (q *Queries) UpdateResourceContents(ctx context.Context, arg UpdateResourceContentsParams) error {
_, err := q.db.Exec(ctx, updateResourceContents, arg.Size, arg.Etag, arg.ID)
return err
}
const updateResourceModified = `-- name: UpdateResourceModified :exec
UPDATE resources
SET
modified = NOW()
WHERE id = $1
`
func (q *Queries) UpdateResourceModified(ctx context.Context, id uuid.UUID) error {
_, err := q.db.Exec(ctx, updateResourceModified, id)
return err
}
const updateResourceName = `-- name: UpdateResourceName :exec
UPDATE resources
SET
name = CASE WHEN ($1::text = '') THEN name ELSE $1::text END,
modified = NOW()
WHERE id = $2::uuid
`
type UpdateResourceNameParams struct {
Name string
ID uuid.UUID
}
func (q *Queries) UpdateResourceName(ctx context.Context, arg UpdateResourceNameParams) error {
_, err := q.db.Exec(ctx, updateResourceName, arg.Name, arg.ID)
return err
}
const updateResourceParent = `-- name: UpdateResourceParent :exec
UPDATE resources
SET
parent = $1::uuid,
modified = NOW()
WHERE id = $2::uuid
`
type UpdateResourceParentParams struct {
Parent uuid.UUID
ID uuid.UUID
}
func (q *Queries) UpdateResourceParent(ctx context.Context, arg UpdateResourceParentParams) error {
_, err := q.db.Exec(ctx, updateResourceParent, arg.Parent, arg.ID)
return err
}