如何使用ltree查询结果创建hierarchal json对象? (PostgreSQL系统)

时间:2017-11-03 09:42:35

标签: 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  |
+----+---------+---------------------------------+-----------+

我认为首先订购结果可能更容易,并从路径返回中删除顶层。使用;

select id, name, subpath(path, 1) as path, nlevel(subpath(path, 1)) as level from testLtree order by level, path

我明白了;

+----+-----------+----------------------------+-------+
| 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 |
+----+-----------+----------------------------+-------+

我希望能够以某种方式将此结果转换为一组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
        }
    }
}

我一直在寻找最好的方法,但没有遇到任何对我有意义的解决方案。但是,由于我是postgres和SQL的新手,所以这是预期的。

我想我可能不得不使用recursive query?我对这个最好的方法/执行方式有点困惑。任何帮助/建议非常感谢!还有其他问题请问。

我已将所有内容放入下面的平方英尺;

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

1 个答案:

答案 0 :(得分:0)

我遇到了与您相同的问题。我在PostgreSQL中为此付出了巨大的努力,解决起来变得过于复杂。由于我使用的是Django(Python框架),因此我决定使用Python解决它。如果它可以帮助处于相同情况的任何人,我想分享以下代码: https://gist.github.com/eherrerosj/4685e3dc843e94f3ef8645d31dbe490c