2024-12-03 16:38:17 +08:00
|
|
|
// Package sql define database sql statement
|
|
|
|
|
package sql
|
|
|
|
|
|
2025-11-19 17:44:08 +08:00
|
|
|
// RecursiveSQL define topologic table recursive query statement
|
2024-12-03 16:38:17 +08:00
|
|
|
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
|
2025-05-13 16:34:25 +08:00
|
|
|
SELECT uuid_from,uuid_to,flag
|
2025-11-19 17:44:08 +08:00
|
|
|
FROM "topologic"
|
2025-05-13 16:34:25 +08:00
|
|
|
WHERE uuid_from = ?
|
2024-12-03 16:38:17 +08:00
|
|
|
UNION ALL
|
2025-10-14 16:12:00 +08:00
|
|
|
SELECT t.uuid_from,t.uuid_to,t.flag
|
2025-11-19 17:44:08 +08:00
|
|
|
FROM "topologic" t
|
2024-12-03 16:38:17 +08:00
|
|
|
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
|
|
|
|
)
|
|
|
|
|
SELECT * FROM recursive_tree;`
|
2026-07-07 10:01:14 +08:00
|
|
|
|
|
|
|
|
// RecursiveTopologicByStartSQL returns every directed edge reachable from the
|
|
|
|
|
// supplied start component. It tracks the visited node path inside PostgreSQL
|
|
|
|
|
// so cycles in topologic data cannot recurse forever.
|
|
|
|
|
var RecursiveTopologicByStartSQL = `WITH RECURSIVE recursive_tree as (
|
|
|
|
|
SELECT uuid_from, uuid_to, flag, ARRAY[uuid_from, uuid_to] AS path
|
|
|
|
|
FROM "topologic"
|
|
|
|
|
WHERE uuid_from = ?
|
|
|
|
|
UNION ALL
|
|
|
|
|
SELECT t.uuid_from, t.uuid_to, t.flag, rt.path || t.uuid_to
|
|
|
|
|
FROM "topologic" t
|
|
|
|
|
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
|
|
|
|
WHERE NOT t.uuid_to = ANY(rt.path)
|
|
|
|
|
)
|
|
|
|
|
SELECT uuid_from, uuid_to, flag FROM recursive_tree;`
|