1
0
Fork 0
mirror of https://code.naskya.net/repos/ndqEd synced 2025-01-10 16:26:46 +09:00

SQL: Define the undirected edge table once, use everywhere

This commit is contained in:
fr33domlover 2016-07-30 11:29:23 +00:00
parent c111f66d7d
commit 9cfaabc035
5 changed files with 32 additions and 76 deletions

View file

@ -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
, ")"
]

View file

@ -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]

View file

@ -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]

View file

@ -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]

View file

@ -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]