|
|
|
@@ -7,6 +7,7 @@ package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
|
|
|
|
)
|
|
|
|
@@ -14,7 +15,7 @@ import (
|
|
|
|
|
const createFile = `-- name: CreateFile :one
|
|
|
|
|
INSERT INTO files (id, name, mime_type, size, storage_key, checksum, created_at, updated_at)
|
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
|
|
|
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id
|
|
|
|
|
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type CreateFileParams struct {
|
|
|
|
@@ -47,14 +48,15 @@ func (q *Queries) CreateFile(ctx context.Context, arg CreateFileParams) (File, e
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
)
|
|
|
|
|
return i, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const createFolder = `-- name: CreateFolder :one
|
|
|
|
|
INSERT INTO files (id, name, created_at, updated_at)
|
|
|
|
|
VALUES ($1, $2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
|
|
|
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id
|
|
|
|
|
INSERT INTO files (id, name, is_folder, created_at, updated_at)
|
|
|
|
|
VALUES ($1, $2, true, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
|
|
|
RETURNING id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
type CreateFolderParams struct {
|
|
|
|
@@ -76,6 +78,7 @@ func (q *Queries) CreateFolder(ctx context.Context, arg CreateFolderParams) (Fil
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
)
|
|
|
|
|
return i, err
|
|
|
|
|
}
|
|
|
|
@@ -91,7 +94,7 @@ func (q *Queries) DeleteFile(ctx context.Context, id string) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getFile = `-- name: GetFile :one
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id FROM files
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder FROM files
|
|
|
|
|
WHERE id = $1 LIMIT 1
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
@@ -109,12 +112,13 @@ func (q *Queries) GetFile(ctx context.Context, id string) (File, error) {
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
)
|
|
|
|
|
return i, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listFiles = `-- name: ListFiles :many
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id FROM files
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder FROM files
|
|
|
|
|
WHERE parent_file_id IS NULL
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
`
|
|
|
|
@@ -139,6 +143,7 @@ func (q *Queries) ListFiles(ctx context.Context) ([]File, error) {
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@@ -154,7 +159,7 @@ func (q *Queries) ListFiles(ctx context.Context) ([]File, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listFilesByID = `-- name: ListFilesByID :many
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id FROM files
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder FROM files
|
|
|
|
|
WHERE id = ANY($1::text[])
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
`
|
|
|
|
@@ -179,6 +184,48 @@ func (q *Queries) ListFilesByID(ctx context.Context, dollar_1 []string) ([]File,
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
); 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 listFilesByParentID = `-- name: ListFilesByParentID :many
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder FROM files
|
|
|
|
|
WHERE parent_file_id = $1
|
|
|
|
|
ORDER BY is_folder DESC, created_at DESC
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
func (q *Queries) ListFilesByParentID(ctx context.Context, parentFileID sql.NullString) ([]File, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, listFilesByParentID, parentFileID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
var items []File
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var i File
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&i.ID,
|
|
|
|
|
&i.Name,
|
|
|
|
|
&i.MimeType,
|
|
|
|
|
&i.Size,
|
|
|
|
|
&i.StorageKey,
|
|
|
|
|
&i.Checksum,
|
|
|
|
|
&i.OcrText,
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@@ -194,8 +241,8 @@ func (q *Queries) ListFilesByID(ctx context.Context, dollar_1 []string) ([]File,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const listFolders = `-- name: ListFolders :many
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id FROM files
|
|
|
|
|
WHERE parent_file_id IS NOT NULL
|
|
|
|
|
SELECT id, name, mime_type, size, storage_key, checksum, ocr_text, created_at, updated_at, parent_file_id, is_folder FROM files
|
|
|
|
|
WHERE is_folder = true
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
@@ -219,6 +266,7 @@ func (q *Queries) ListFolders(ctx context.Context) ([]File, error) {
|
|
|
|
|
&i.CreatedAt,
|
|
|
|
|
&i.UpdatedAt,
|
|
|
|
|
&i.ParentFileID,
|
|
|
|
|
&i.IsFolder,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|