I have readonly access to a MSSQLServer database referencing files on the server.
Two tables are used to represents the file structure:
folderInfo
-------------------------------------------------
| id | parentId | Sequence | folderName |
|------------------------------------------------
| 1 | 0 | 1 | folder1 |
| 2 | 0 | 2 | folder2 |
| 3 | 1 | 1 | subfolder1 |
-------------------------------------------------
fileInfo
-----------------------------------------------
| id | folderId | Sequence | fileName |
|----------------------------------------------
| 1 | 3 | 1 | e.xml |
| 2 | 2 | 1 | a.xml |
| 3 | 2 | 2 | f.xml |
-----------------------------------------------
"parentId=0" means that the folder is in the root of the file structure.
I have no clue how to make a query for all files, with a union capable of giving me the filepath (relative to the root).
Given that the order of magnitude of the number of files is of 10000, I don't want to have to query again for each file (or am I wrong to think this is inefficient?).
I have found a beginning of an answer in that post: SQL Tree Structure
with recursive full_tree as (
select id, name, parent, 1 as level
from departments
where parent is null
union all
select c.id, c.name, c.parent, p.level + 1
from departments c
join full_tree p on c.parent = p.id
)
select *
from full_tree;
But I do not know how I can turn this into what I need, which would be something like:
fileInfo
----------------------------------------------------
| id | Sequence | filepath |
|---------------------------------------------------
| 1 | 1 | /folder1/subfolder1/e.xml |
| 2 | 1 | /folder2/a.xml |
| 3 | 2 | /folder2/f.xml |
----------------------------------------------------