Files
phylum/internal/sql/resources.sql.go
2024-03-16 23:56:18 +05:30

316 lines
7.1 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: resources.sql
package sql
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const createResource = `-- name: CreateResource :exec
INSERT INTO resources(
id, parent, name, dir, created, modified
) VALUES (
$1, $2, $3, $4, NOW(), NOW()
)
`
type CreateResourceParams struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
}
func (q *Queries) CreateResource(ctx context.Context, arg CreateResourceParams) error {
_, err := q.db.Exec(ctx, createResource,
arg.ID,
arg.Parent,
arg.Name,
arg.Dir,
)
return err
}
const deleteRecursive = `-- name: DeleteRecursive :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
)
UPDATE resources
SET modified = NOW(), deleted = NOW()
WHERE id in (SELECT id FROM nodes)
RETURNING id, parent, name, dir, created, modified, deleted, size, etag
`
func (q *Queries) DeleteRecursive(ctx context.Context, id uuid.UUID) ([]Resource, error) {
rows, err := q.db.Query(ctx, deleteRecursive, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Resource
for rows.Next() {
var i Resource
if err := rows.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Etag,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
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, parent, name, dir, created, modified, deleted, size, etag
`
func (q *Queries) HardDeleteRecursive(ctx context.Context, id uuid.UUID) ([]Resource, error) {
rows, err := q.db.Query(ctx, hardDeleteRecursive, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Resource
for rows.Next() {
var i Resource
if err := rows.Scan(
&i.ID,
&i.Parent,
&i.Name,
&i.Dir,
&i.Created,
&i.Modified,
&i.Deleted,
&i.Size,
&i.Etag,
); 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) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, 0, ''::text
FROM resources r WHERE r.id = $2::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)
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
AND CASE WHEN $3::boolean THEN true ELSE depth < 1 END
)
SELECT id, parent, name, dir, created, modified, size, etag, depth, path from nodes
WHERE CASE WHEN $1::boolean THEN true ELSE depth > 0 END
`
type ReadDirParams struct {
IncludeRoot bool
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.Int4
Etag pgtype.Text
Depth int32
Path string
}
func (q *Queries) ReadDir(ctx context.Context, arg ReadDirParams) ([]ReadDirRow, error) {
rows, err := q.db.Query(ctx, readDir, arg.IncludeRoot, 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,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const rename = `-- name: Rename :exec
UPDATE resources
SET
name = CASE WHEN ($1::text = '') THEN name ELSE $1::text END,
parent = $2::uuid,
modified = NOW()
WHERE id = $3::uuid
`
type RenameParams struct {
Name string
Parent uuid.UUID
ID uuid.UUID
}
func (q *Queries) Rename(ctx context.Context, arg RenameParams) error {
_, err := q.db.Exec(ctx, rename, arg.Name, arg.Parent, arg.ID)
return err
}
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) AS (
SELECT r.id, r.parent, r.name, r.dir, r.created, r.modified, r.size, r.etag, 0, ''::text, $1::text[]
FROM resources r WHERE r.id = $2::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
FROM resources r JOIN nodes n on r.parent = n.id
WHERE deleted IS NULL
AND r.name = n.search[n.depth + 1]
)
SELECT id, parent, name, dir, created, modified, size, etag, depth, path, search FROM nodes WHERE cardinality(search) = depth
`
type ResourceByPathParams struct {
Search []string
Root uuid.UUID
}
type ResourceByPathRow struct {
ID uuid.UUID
Parent *uuid.UUID
Name string
Dir bool
Created pgtype.Timestamp
Modified pgtype.Timestamp
Size pgtype.Int4
Etag pgtype.Text
Depth int32
Path string
Search []string
}
func (q *Queries) ResourceByPath(ctx context.Context, arg ResourceByPathParams) (ResourceByPathRow, error) {
row := q.db.QueryRow(ctx, resourceByPath, arg.Search, 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,
)
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.Int4
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
}