表示数据库中的文件系统(在SQL Server 2008中使用hierarchyid)

发布于 2024-12-01 02:42:47 字数 244 浏览 0 评论 0原文

我还没有找到任何具体的例子,但我有兴趣使用 Hierarchyid 数据类型来表示带有更新的整个目录结构等。这是为 hierarchyid 引用的常见用例,但我找不到任何构建此类示例的文章。

我只想表示整个目录结构,例如:

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

** 我不想将其与磁盘上的文件系统同步。它纯粹通过数据库来表示。 **

I haven't found any specific examples of this but I am interested in representing a whole directory structure with updates, etc using the hierarchyid datatype. This is a common use case cited for the hierarchyid but I can't find any articles building such an example.

I just want to represent a whole directory structure such as:

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

** I am not trying to synch this with a file system on disk. It is purely represented through the database. **

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-12-08 02:42:47

下面是通过 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
) 

加载数据:

/*
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/'))

显示路径:

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

获取 file2 的祖先(上一级) level):

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

获取 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

获取根路径:

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

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

获取 file2 的级别:

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

level
------
4

参考文献: < /强>

Here is an example of representing a file system through 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
) 

Load data:

/*
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/'))

Show the paths:

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 ancestors of file2 (up one level):

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

Get all descentants of 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

Get the root path:

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

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

Get the level of file2:

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

level
------
4

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文