// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: resources.sql

package database

import (
	"context"

	"github.com/lib/pq"
)

const getAllResourcesAll = `-- name: GetAllResourcesAll :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
ORDER BY updated_at DESC, name ASC
`

func (q *Queries) GetAllResourcesAll(ctx context.Context) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getAllResourcesAll)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getAllResourcesInDir = `-- name: GetAllResourcesInDir :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE (path = $1 AND is_dir = true)
   OR (path LIKE $2 AND path NOT LIKE $3)
ORDER BY updated_at DESC, name ASC
`

type GetAllResourcesInDirParams struct {
	DirPath     string `json:"dir_path"`
	ChildPrefix string `json:"child_prefix"`
	DeepPrefix  string `json:"deep_prefix"`
}

func (q *Queries) GetAllResourcesInDir(ctx context.Context, arg GetAllResourcesInDirParams) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getAllResourcesInDir, arg.DirPath, arg.ChildPrefix, arg.DeepPrefix)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getAllResourcesRecursive = `-- name: GetAllResourcesRecursive :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE path = $1 OR path LIKE $2
ORDER BY updated_at DESC, name ASC
`

type GetAllResourcesRecursiveParams struct {
	DirPath     string `json:"dir_path"`
	ChildPrefix string `json:"child_prefix"`
}

func (q *Queries) GetAllResourcesRecursive(ctx context.Context, arg GetAllResourcesRecursiveParams) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getAllResourcesRecursive, arg.DirPath, arg.ChildPrefix)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getAllResourcesRoot = `-- name: GetAllResourcesRoot :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE path NOT LIKE '%/%'
ORDER BY updated_at DESC, name ASC
`

func (q *Queries) GetAllResourcesRoot(ctx context.Context) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getAllResourcesRoot)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserResourceByID = `-- name: GetUserResourceByID :one
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE id = $1
`

func (q *Queries) GetUserResourceByID(ctx context.Context, id int64) (UserResource, error) {
	row := q.db.QueryRowContext(ctx, getUserResourceByID, id)
	var i UserResource
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.Hash,
		&i.Name,
		&i.Path,
		&i.Size,
		&i.IsDir,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getUserResourcesAll = `-- name: GetUserResourcesAll :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = $1
ORDER BY updated_at DESC, name ASC
`

func (q *Queries) GetUserResourcesAll(ctx context.Context, userID int64) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getUserResourcesAll, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserResourcesByIDs = `-- name: GetUserResourcesByIDs :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE id = ANY($1::bigint[])
ORDER BY id ASC
`

func (q *Queries) GetUserResourcesByIDs(ctx context.Context, ids []int64) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getUserResourcesByIDs, pq.Array(ids))
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserResourcesInDir = `-- name: GetUserResourcesInDir :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = $1
  AND ((path = $2 AND is_dir = true)
    OR (path LIKE $3 AND path NOT LIKE $4))
ORDER BY updated_at DESC, name ASC
`

type GetUserResourcesInDirParams struct {
	UserID      int64  `json:"user_id"`
	DirPath     string `json:"dir_path"`
	ChildPrefix string `json:"child_prefix"`
	DeepPrefix  string `json:"deep_prefix"`
}

func (q *Queries) GetUserResourcesInDir(ctx context.Context, arg GetUserResourcesInDirParams) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getUserResourcesInDir,
		arg.UserID,
		arg.DirPath,
		arg.ChildPrefix,
		arg.DeepPrefix,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserResourcesRecursive = `-- name: GetUserResourcesRecursive :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = $1
  AND (path = $2 OR path LIKE $3)
ORDER BY updated_at DESC, name ASC
`

type GetUserResourcesRecursiveParams struct {
	UserID      int64  `json:"user_id"`
	DirPath     string `json:"dir_path"`
	ChildPrefix string `json:"child_prefix"`
}

func (q *Queries) GetUserResourcesRecursive(ctx context.Context, arg GetUserResourcesRecursiveParams) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getUserResourcesRecursive, arg.UserID, arg.DirPath, arg.ChildPrefix)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserResourcesRoot = `-- name: GetUserResourcesRoot :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = $1 AND path NOT LIKE '%/%'
ORDER BY updated_at DESC, name ASC
`

func (q *Queries) GetUserResourcesRoot(ctx context.Context, userID int64) ([]UserResource, error) {
	rows, err := q.db.QueryContext(ctx, getUserResourcesRoot, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []UserResource
	for rows.Next() {
		var i UserResource
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.Hash,
			&i.Name,
			&i.Path,
			&i.Size,
			&i.IsDir,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
