modelRT/sql/topologic.go

30 lines
1.0 KiB
Go

// Package sql define database sql statement
package sql
// RecursiveSQL define topologic table recursive query statement
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
SELECT uuid_from,uuid_to,flag
FROM "topologic"
WHERE uuid_from = ?
UNION ALL
SELECT t.uuid_from,t.uuid_to,t.flag
FROM "topologic" t
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
)
SELECT * FROM recursive_tree;`
// 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;`