I have only one database, and only one table named WorkingDirectory.
I want to copy folder structure from source (RootFolder1) folder and paste it into destination (RootFolder2) folder in the same table using C#.
Below parameter passed from the UI side or swagger.
[
{
"copySourceFolderId": 1,
"pasteDestinationFolderId": 5
}
]
WorkingDirectory table structure:
| Id | ParentId | Name |
| 1 | null | RootFolder1 |
| 2 | 1 | SubFolder1 |
| 3 | 1 | SubFolder2 |
| 4 | 2 | SubSubFolder1 |
| 5 | null | RootFolder2 |
Step 1: Get the Nth level of tree data structure from the table based on copySourceFolderId = 1.
Getting below result from the query and storing the result into "workingDirectoryList" object.
| Id | ParentId | Name |
| 1 | null | RootFolder1 |
| 2 | 1 | SubFolder1 |
| 3 | 1 | SubFolder2 |
| 4 | 2 | SubSubFolder1 |
with recursive subordinate as
(
select "Id" ,"Name" ,"ParentId" from ol."WorkingDirectory" w where "Id" = 1
union
select wd2."Id" ,wd2."Name" ,wd2."ParentId" from ol."WorkingDirectory" wd2
inner join subordinate s on s."Id"= wd2."ParentId"
)
select s."Id",s."Name",s."ParentId"
from subordinate s
group by s."Id",s."Name",s."ParentId"
order by s."Id" asc
List<WorkingDirectoryModel> workingDirectoryList = new List<WorkingDirectoryModel>();
public class WorkingDirectoryModel
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
}
Step 2: One by one, add or insert records in the "WorkingDirectory" table from "workingDirectoryList" object using C#.
foreach (var item in workingDirectoryList)
{
// What logic should written here?
}
Step 3: output of WorkingDirectory table after copy RootFolder1 and paste it into RootFolder2:
| Id | ParentId | Name |
| 1 | null | RootFolder1 |
| 2 | 1 | SubFolder1 |
| 3 | 1 | SubFolder2 |
| 4 | 2 | SubSubFolder1 |
| 5 | null | RootFolder2 |
| 6 | 5 | RootFolder1 |
| 7 | 6 | SubFolder1 |
| 8 | 6 | SubFolder2 |
| 9 | 7 | SubSubFolder1 |
2
Answers
As per my understanding, you need a parent-child relationship,
Please check this query I prepared in SQL.
Do you want to get data based on the query?
So you have to add a where condition in the CTE.
If I understand your objective then this can be accomplished in a single query. A couple notes first:
With these then you get what you want with:
The
new_root
CTE gets the new row with Root_Folder2 and returns its id. This is later used to generate the copied hierarchy revised ParentId. Theold_parents
just builds an image of the desired hierarchy to be copied. Finally the main query (Insert DML) generates the copy while recalculating theParientId
. (see demo here).