私は、特定のルートディレクトリの下にあるすべてのファイルのクロール結果を保存するために使用しているMariaDB 10.2.8データベースを持っています。そのためファイル(file
テーブル内)には親ディレクトリ(directory
テーブル)があります。この親ディレクトリは、ディレクトリクロールが開始された元のポイントまで独自の親を持つことができます。私は/home
からクロールを行った場合MariaDB再帰的CTEの順序を逆にする
ので、ファイル/home/tim/projects/foo/bar.py
は親ディレクトリprojects
などを持っているでしょう親ディレクトリfoo
を、持っているでしょう。 /home
(クロールのルート)にはnull
の親があります。
私は次の再帰CTEを持っている:
(予想通り)@FileID
はファイルの主キーであるため、中に結果を返します
with recursive tree as (
select id, name, parent from directory where id =
(select parent from file where id = @FileID)
union
select d.id, d.name, d.parent from directory d, tree t
where t.parent = d.id
) select name from tree;
。例えば
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.2.8-MariaDB-10.2.8+maria~jessie-log mariadb.org binary distribution
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use inventory;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [inventory]> with recursive tree as (
-> select id, name, parent from directory where id =
-> (select parent from file where id = 3790)
-> union
-> select d.id, d.name, d.parent from directory d, tree t
-> where t.parent = d.id
->) select name from tree;
+----------+
| name |
+----------+
| b8 |
| objects |
| .git |
| fresnel |
| Projects |
| metatron |
+----------+
6 rows in set (0.00 sec)
MariaDB [inventory]> Bye
[email protected]:~$
そこで、この場合には、ファイルID 3790は、(/metatron
は、もちろん、クロールのルートである)ディレクトリ/metatron/Projects/fresnel/.git/objects/b8
内のファイルに対応します。
出力の順序を逆転させる信頼できる方法があります(完全なパスを生成するために一緒に連結したいので)。私はorder by id
することができますが、これは子供が彼らの両親よりも高いIDを持っていることを私が知っているとしても、信頼できるとは感じません。これはいつもCTE 。