-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: list files by parentId explicitly
- Loading branch information
Showing
2 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
internal/database/migrations/20240607114052_modify_functions.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE OR REPLACE FUNCTION teldrive.get_file_from_path(full_path text,u_id bigint) | ||
RETURNS setof teldrive.files AS $$ | ||
DECLARE | ||
target_id text; | ||
begin | ||
|
||
IF full_path = '/' then | ||
RETURN QUERY select * from teldrive.files as root where root.parent_id = 'root' and root.user_id = u_id; | ||
END IF; | ||
|
||
WITH RECURSIVE dir_hierarchy AS ( | ||
SELECT | ||
root.id, | ||
root.name, | ||
root.parent_id, | ||
0 AS depth, | ||
'' as path | ||
FROM | ||
teldrive.files as root | ||
WHERE | ||
root.parent_id = 'root' AND root.user_id = u_id | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
f.id, | ||
f.name, | ||
f.parent_id, | ||
dh.depth + 1 AS depth, | ||
dh.path || '/' || f.name | ||
FROM | ||
teldrive.files f | ||
JOIN | ||
dir_hierarchy dh ON dh.id = f.parent_id | ||
WHERE f.type = 'folder' AND f.user_id = u_id | ||
) | ||
|
||
SELECT id into target_id FROM dir_hierarchy dh | ||
WHERE dh.path = full_path | ||
ORDER BY dh.depth DESC | ||
LIMIT 1; | ||
|
||
RETURN QUERY select * from teldrive.files where id=target_id; | ||
|
||
END; | ||
$$ LANGUAGE plpgsql; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters