diff --git a/src/Database/Persist/Local/Sql.hs b/src/Database/Persist/Local/Sql.hs index 14b2137..a0fcfc6 100644 --- a/src/Database/Persist/Local/Sql.hs +++ b/src/Database/Persist/Local/Sql.hs @@ -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 + , ")" + ] diff --git a/src/Database/Persist/Sql/Graph/Connects.hs b/src/Database/Persist/Sql/Graph/Connects.hs index 5bbc86f..f20aadb 100644 --- a/src/Database/Persist/Sql/Graph/Connects.hs +++ b/src/Database/Persist/Sql/Graph/Connects.hs @@ -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] diff --git a/src/Database/Persist/Sql/Graph/Cyclic.hs b/src/Database/Persist/Sql/Graph/Cyclic.hs index 98ea756..83578be 100644 --- a/src/Database/Persist/Sql/Graph/Cyclic.hs +++ b/src/Database/Persist/Sql/Graph/Cyclic.hs @@ -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] diff --git a/src/Database/Persist/Sql/Graph/Path.hs b/src/Database/Persist/Sql/Graph/Path.hs index b3f2f1e..3ba166d 100644 --- a/src/Database/Persist/Sql/Graph/Path.hs +++ b/src/Database/Persist/Sql/Graph/Path.hs @@ -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] diff --git a/src/Database/Persist/Sql/Graph/Reachable.hs b/src/Database/Persist/Sql/Graph/Reachable.hs index 7b9e136..90c1236 100644 --- a/src/Database/Persist/Sql/Graph/Reachable.hs +++ b/src/Database/Persist/Sql/Graph/Reachable.hs @@ -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]