From 521871c476a83b0ae4e1dd4df7cc1722d046551d Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Tue, 2 Aug 2016 09:29:56 +0000 Subject: [PATCH] Refactor undirected edge SQL to remove repeated filter --- src/Database/Persist/Local/Sql.hs | 48 +++++++++++++++++++------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Database/Persist/Local/Sql.hs b/src/Database/Persist/Local/Sql.hs index a0fcfc6..2a1d4b8 100644 --- a/src/Database/Persist/Local/Sql.hs +++ b/src/Database/Persist/Local/Sql.hs @@ -169,6 +169,9 @@ infixr 5 ?++ uedge :: DBName uedge = DBName "temp_undirected_edge_cte" +ubase :: DBName +ubase = DBName "temp_undirected_base_cte" + temp :: DBName temp = DBName "temp_hierarchy_cte" @@ -188,21 +191,30 @@ 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 - , ")" - ] + sqlBase = mconcat + [ dbname ubase + , " (" + , dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd + , ") AS (SELECT " + , entityDB tEdge ^* fieldDB bwd + , ", " + , entityDB tEdge ^* fieldDB fwd + , " FROM ", dbname $ entityDB tEdge + , filt + , "), " + ] + sqlEdge base = mconcat + [ dbname uedge + , " (" + , dbname $ fieldDB bwd, ", ", dbname $ fieldDB fwd + , ") AS (SELECT " + , base ^* fieldDB bwd, ", ", base ^* fieldDB fwd + , " FROM ", dbname base + , " UNION ALL SELECT " + , base ^* fieldDB fwd, ", ", base ^* fieldDB bwd + , " FROM ", dbname base + , ")" + ] + in if T.null filt + then sqlEdge $ entityDB tEdge + else sqlBase <> sqlEdge ubase