From e88ff0a7d0c65c3cc20b8c95d520898cc7200f15 Mon Sep 17 00:00:00 2001 From: Shane O'Brien Date: Thu, 9 Jun 2022 19:07:46 +0100 Subject: [PATCH] loop --- rel8.cabal | 1 + src/Rel8.hs | 4 ++++ src/Rel8/Query/Loop.hs | 27 +++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 src/Rel8/Query/Loop.hs diff --git a/rel8.cabal b/rel8.cabal index 0eae542d..b0a5f833 100644 --- a/rel8.cabal +++ b/rel8.cabal @@ -111,6 +111,7 @@ library Rel8.Query.Indexed Rel8.Query.Limit Rel8.Query.List + Rel8.Query.Loop Rel8.Query.Maybe Rel8.Query.Null Rel8.Query.Opaleye diff --git a/src/Rel8.hs b/src/Rel8.hs index 727023db..c8b0c029 100644 --- a/src/Rel8.hs +++ b/src/Rel8.hs @@ -245,6 +245,9 @@ module Rel8 , without , withoutBy + -- ** @WITH RECURSIVE@ + , loop + -- ** Aggregation , Aggregate , Aggregates @@ -359,6 +362,7 @@ import Rel8.Query.Filter import Rel8.Query.Indexed import Rel8.Query.Limit import Rel8.Query.List +import Rel8.Query.Loop import Rel8.Query.Maybe import Rel8.Query.Null import Rel8.Query.Order diff --git a/src/Rel8/Query/Loop.hs b/src/Rel8/Query/Loop.hs new file mode 100644 index 00000000..48f282e7 --- /dev/null +++ b/src/Rel8/Query/Loop.hs @@ -0,0 +1,27 @@ +{-# language FlexibleContexts #-} + +module Rel8.Query.Loop + ( loop + ) where + +-- base +import Prelude + +-- opaleye +import Opaleye.Lateral ( lateral ) +import Opaleye.WithRecursive ( withRecursiveExplicit ) + +-- rel8 +import Rel8.Expr ( Expr ) +import Rel8.Query ( Query ) +import Rel8.Query.Opaleye ( fromOpaleye, toOpaleye ) +import Rel8.Table ( Table ) +import Rel8.Table.Opaleye ( binaryspec ) + + +loop :: Table Expr a => Query a -> (a -> Query a) -> Query a +loop base recurse = + fromOpaleye $ withRecursiveExplicit binaryspec base' recurse' + where + base' = toOpaleye base + recurse' = lateral $ toOpaleye . recurse