如何使用ltree查询结果创建分层json对象?(PostgreSQL的)

ale*_*exc 5 sql postgresql

我正在尝试使用postgres创建用于自定义类别的存储系统。

在寻找潜在的解决方案之后,我决定尝试使用ltree ;

这是下面的原始数据示例;

+----+---------+---------------------------------+-----------+
| id | user_id |              path               |   name    |
+----+---------+---------------------------------+-----------+
|  1 |       1 | root.test                       | test      |
|  2 |       1 | root.test.inbox                 | inbox     |
|  3 |       1 | root.personal                   | personal  |
|  4 |       1 | root.project                    | project   |
|  5 |       1 | root.project.idea               | idea      |
|  6 |       1 | root.personal.events            | events    |
|  7 |       1 | root.personal.events.janaury    | january   |
|  8 |       1 | root.project.objective          | objective |
|  9 |       1 | root.personal.events.february   | february  |
| 10 |       1 | root.project.objective.january  | january   |
| 11 |       1 | root.project.objective.february | february  |
+----+---------+---------------------------------+-----------+
Run Code Online (Sandbox Code Playgroud)

我认为,首先对结果进行排序,然后从路径返回中删除顶层可能会更容易。使用;

select id, name, subpath(path, 1) as path, nlevel(subpath(path, 1)) as level from testLtree order by level, path
Run Code Online (Sandbox Code Playgroud)

我懂了

+----+-----------+----------------------------+-------+
| id |   name    |            path            | level |
+----+-----------+----------------------------+-------+
|  3 | personal  | personal                   |     1 |
|  4 | project   | project                    |     1 |
|  1 | test      | test                       |     1 |
|  6 | events    | personal.events            |     2 |
|  5 | idea      | project.idea               |     2 |
|  8 | objective | project.objective          |     2 |
|  2 | inbox     | test.inbox                 |     2 |
|  9 | february  | personal.events.february   |     3 |
|  7 | january   | personal.events.january    |     3 |
| 11 | february  | project.objective.february |     3 |
| 10 | january   | project.objective.january  |     3 |
+----+-----------+----------------------------+-------+
Run Code Online (Sandbox Code Playgroud)

我希望能够以某种方式将此结果转换为一组JSON数据。我想要类似的输出;

personal: {
    id: 3,
    name: 'personal',
    children: {
        events: {
            id: 6,
            name: 'events',
            children: {
                january: {
                    id: 7,
                    name: 'january',
                    children: null
                },
                february: {
                    id: 9,
                    name: 'february',
                    children: null
                }
            }
        }
    }
},
project: {
    id: 4,
    name: 'project',
    children: {
        idea: {
            id: 5,
            name: 'idea',
            children: null
        },
        objective: {
            id: 8,
            name: 'objective',
            children: {
                january: {
                    id: 10,
                    name: 'january',
                    children: null
                },
                february: {
                    id: 11,
                    name: 'february',
                    children: null
                }
            }
        }
    }]
},
test: {
    id: 1,
    name: 'test',
    children: {
        inbox: {
            id: 2,
            name: 'inbox',
            children: null
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我一直在寻找实现此目的的最佳方法,但是没有遇到任何对我有意义的解决方案。但是,由于我一般不熟悉Postgres和SQL,因此可以预期。

我想我可能必须使用递归查询?我对最好的方法/执行方式感到困惑。任何帮助/建议都非常感谢!以及任何其他问题,请询问。


我已经将所有内容放入下面的sqlfiddle中;

http://sqlfiddle.com/#!17/1713e/5