mirror of
https://code.naskya.net/repos/ndqEd
synced 2025-01-10 21:16:46 +09:00
SQL: Define the undirected edge table once, use everywhere
This commit is contained in:
parent
c111f66d7d
commit
9cfaabc035
5 changed files with 32 additions and 76 deletions
|
@ -30,6 +30,7 @@ module Database.Persist.Local.Sql
|
|||
, tpath
|
||||
, tcycle
|
||||
, tcontains
|
||||
, sqlUEdge
|
||||
, FollowDirection (..)
|
||||
)
|
||||
where
|
||||
|
@ -182,3 +183,26 @@ tcycle = DBName "cycle"
|
|||
|
||||
tcontains :: DBName
|
||||
tcontains = DBName "contains"
|
||||
|
||||
sqlUEdge
|
||||
:: (DBName -> Text) -> Text -> EntityDef -> FieldDef -> FieldDef -> Text
|
||||
sqlUEdge dbname filt tEdge bwd fwd =
|
||||
let t ^* f = dbname t <> "." <> dbname f
|
||||
in mconcat
|
||||
[ dbname uedge
|
||||
, " ("
|
||||
, dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd
|
||||
, ") AS (SELECT "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, " UNION ALL SELECT "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, ")"
|
||||
]
|
||||
|
|
|
@ -159,25 +159,8 @@ xmconnectsm' follow filter msource mdest mlen proxy = do
|
|||
sql = mconcat
|
||||
[ "WITH RECURSIVE "
|
||||
, case follow of
|
||||
FollowBoth -> mconcat
|
||||
[ dbname uedge
|
||||
, " ("
|
||||
, dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd
|
||||
, ") AS (SELECT "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, " UNION ALL SELECT "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, "), "
|
||||
]
|
||||
_ -> ""
|
||||
FollowBoth -> sqlUEdge dbname filt tEdge bwd fwd <> ", "
|
||||
_ -> T.empty
|
||||
, dbname temp
|
||||
, " ("
|
||||
, T.intercalate "," $ map dbname [tid, tpath, tcycle]
|
||||
|
|
|
@ -154,25 +154,8 @@ xcyclicn' follow filter minitials proxy = do
|
|||
sql = mconcat
|
||||
[ "WITH RECURSIVE "
|
||||
, case follow of
|
||||
FollowBoth -> mconcat
|
||||
[ dbname uedge
|
||||
, " ("
|
||||
, dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd
|
||||
, ") AS (SELECT "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, " UNION ALL SELECT "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, "), "
|
||||
]
|
||||
_ -> ""
|
||||
FollowBoth -> sqlUEdge dbname filt tEdge bwd fwd <> ", "
|
||||
_ -> T.empty
|
||||
, dbname temp
|
||||
, " ("
|
||||
, T.intercalate "," $ map dbname [tid, tpath, tcycle]
|
||||
|
|
|
@ -142,25 +142,8 @@ xmpathm' follow filter msource mdest mlen mlim proxy = do
|
|||
sql = mconcat
|
||||
[ "WITH RECURSIVE "
|
||||
, case follow of
|
||||
FollowBoth -> mconcat
|
||||
[ dbname uedge
|
||||
, " ("
|
||||
, dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd
|
||||
, ") AS (SELECT "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, " UNION ALL SELECT "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, "), "
|
||||
]
|
||||
_ -> ""
|
||||
FollowBoth -> sqlUEdge dbname filt tEdge bwd fwd <> ", "
|
||||
_ -> T.empty
|
||||
, dbname temp
|
||||
, " ("
|
||||
, T.intercalate "," $ map dbname [tid, tpath, tcycle]
|
||||
|
|
|
@ -115,25 +115,8 @@ xreachable' follow filter initials mlen proxy = do
|
|||
sql = mconcat
|
||||
[ "WITH RECURSIVE "
|
||||
, case follow of
|
||||
FollowBoth -> mconcat
|
||||
[ dbname uedge
|
||||
, " ("
|
||||
, dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd
|
||||
, ") AS (SELECT "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, " UNION ALL SELECT "
|
||||
, entityDB tEdge ^* fieldDB fwd
|
||||
, ", "
|
||||
, entityDB tEdge ^* fieldDB bwd
|
||||
, " FROM ", dbname $ entityDB tEdge
|
||||
, filt
|
||||
, "), "
|
||||
]
|
||||
_ -> ""
|
||||
FollowBoth -> sqlUEdge dbname filt tEdge bwd fwd <> ", "
|
||||
_ -> T.empty
|
||||
, dbname temp
|
||||
, " ("
|
||||
, T.intercalate "," $ map dbname [tid, tpath, tcycle]
|
||||
|
|
Loading…
Reference in a new issue