2011-08-22 10 views
9

Nie znalazłem żadnych konkretnych przykładów tego, ale jestem zainteresowany reprezentowaniem całej struktury katalogów z aktualizacjami, itp. Przy użyciu hierarchyid typu danych. Jest to powszechny przypadek użycia cytowany dla hierarchyid, ale nie mogę znaleźć żadnych artykułów budujących taki przykład.Reprezentacja systemu plików w DB (przy użyciu hierarchyid w SQL Server 2008)

prostu chcę reprezentować całą strukturę katalogów, takich jak:

/dir1 
/file1 
/dir2 
/dir2/dir3 
/dir2/dir3/file2 

** I nie próbuję zsynchronizować to z systemu plików na dysku. Jest reprezentowany wyłącznie przez bazę danych. **

+2

Jak można oczekiwać, aby zachować strukturę tabeli w synchronizacji z plikiem system? Jeśli dodaję plik do katalogu/dir2, jak szybko tabela powinna o tym wiedzieć? Natychmiast, pewne opóźnienie, nigdy? Czy nie byłoby sensu po prostu czytać w strukturze katalogów w czasie wykonywania? Naprawdę nie potrzebowałby takiej hierarchii. –

+0

Dzięki, nie próbuję utrzymywać DB zsynchronizowany z systemem plików. Zasadniczo będzie działał jako własny system plików (węzły plików wskażą pliki na dysku, ale nie będzie struktury katalogów). Wszystko czego naprawdę potrzebuję, to tylko ta część. Dzięki. – user8790899800

+0

Dlaczego komentarz @Aarona Bertranda jest przegłosowany, gdy tak naprawdę nie jest przydatny? Wydaje się dziwne. – user8790899800

Odpowiedz

7

Oto przykład przedstawiający system plików przez hierarchyid:

/* 
Setup: 
- Create the table to hold the files 
- nodeDepth is identifier of the depth for readability 
- fullpath is the full path of the file or directory 
- nodePath is the HierarchyID 
- nodePath identifies the row within the tree 
*/ 

DECLARE @t TABLE (
    nodeID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, 
    nodeDepth VARCHAR(10) NOT NULL, 
    fullPath VARCHAR(20) NOT NULL, 
    nodePath HIERARCHYID NOT NULL 
) 

danych Load:

/* 
Load the nodePath value with the Parse command: 
- The root node has a single/
- Every nodePath must begin and end with/
- /1/2/ the second item on level 2 
*/ 

INSERT @t (fullPath, nodeDepth, nodePath) VALUES 
('/','1',HIERARCHYID::Parse('/')), 
('/dir1','1.1',HIERARCHYID::Parse('/1/1/')), 
('/file1','1.2',HIERARCHYID::Parse('/1/2/')), 
('/dir2','1.3',HIERARCHYID::Parse('/1/3/')), 
('/dir2/dir3','1.3.1',HIERARCHYID::Parse('/1/3/1/')), 
('/dir2/dir3/file2','1.3.1.1',HIERARCHYID::Parse('/1/3/1/1/')) 

Lista ścieżek:

SELECT * 
FROM @t 

nodeID  nodeDepth fullPath    nodePath 
----------- ---------- -------------------- -------- 
1   1  /     0x 
2   1.1  /dir1    0x5AC0 
3   1.2  /file1    0x5B40 
4   1.3  /dir2    0x5BC0 
5   1.3.1  /dir2/dir3   0x5BD6 
6   1.3.1.1 /dir2/dir3/file2  0x5BD6B0 

Get przodków plik2 (jeden poziom w górę):

SELECT * 
FROM @t 
WHERE nodePath = 
    (SELECT nodePath.GetAncestor(1) 
    FROM @t 
    WHERE fullPath = '/dir2/dir3/file2') 

nodeID  nodeDepth fullPath    nodePath 
----------- ---------- -------------------- --------- 
5   1.3.1  /dir2/dir3   0x5BD6 

Pobierz wszystkie descentants z dir2:

SELECT * 
FROM @t 
WHERE nodePath.IsDescendantOf(
    (SELECT nodePath 
    FROM @t 
    WHERE fullPath = '/dir2')) = 1 
AND fullPath <> '/dir2' /* Parent is considered its own descendant */ 

nodeID  nodeDepth fullPath    nodePath 
----------- ---------- -------------------- -------- 
5   1.3.1  /dir2/dir3   0x5BD6 
6   1.3.1.1 /dir2/dir3/file2  0x5BD6B0 

uzyskać ścieżkę root:

SELECT * 
FROM @t 
WHERE nodePath = HIERARCHYID::GetRoot() 

nodeID  nodeDepth fullPath    nodePath 
----------- ---------- -------------------- -------- 
1   1  /     0x 

Zdobądź przewagę l plik2:

SELECT nodePath.GetLevel() AS level 
FROM @t 
WHERE fullPath = '/dir2/dir3/file2' 

level 
------ 
4 

Referencje:

Powiązane problemy