{-# LANGUAGE Arrows #-}
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE LambdaCase #-}

module Opaleye.Internal.Values where

import           Opaleye.Internal.Column (Field_(Column))
import qualified Opaleye.Internal.Column as C
import qualified Opaleye.Column as OC
import qualified Opaleye.Internal.Unpackspec as U
import qualified Opaleye.Internal.Tag as T
import qualified Opaleye.Internal.Operators as O
import qualified Opaleye.Internal.PrimQuery as PQ
import qualified Opaleye.Internal.PackMap as PM
import qualified Opaleye.Internal.QueryArr as Q
import qualified Opaleye.Internal.HaskellDB.PrimQuery as HPQ
import qualified Opaleye.Internal.PGTypes
import qualified Opaleye.SqlTypes

import           Control.Arrow (returnA)
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.List.NonEmpty as NEL
import           Data.Profunctor (Profunctor, dimap, rmap, lmap)
import           Data.Profunctor.Product (ProductProfunctor)
import qualified Data.Profunctor.Product as PP
import           Data.Profunctor.Product.Default (Default, def)
import           Data.Semigroup (Semigroup, (<>))

import           Control.Applicative (Applicative, pure, (<*>), liftA2)

nonEmptyValues :: Rowspec columns columns'
               -> NEL.NonEmpty columns
               -> Q.Select columns'
nonEmptyValues :: forall columns columns'.
Rowspec columns columns' -> NonEmpty columns -> Select columns'
nonEmptyValues Rowspec columns columns'
rowspec NonEmpty columns
rows =
  let nerowspec' :: NonEmptyRowspec columns columns'
nerowspec' = case Rowspec columns columns'
rowspec of
        NonEmptyRows NonEmptyRowspec columns columns'
nerowspec -> NonEmptyRowspec columns columns'
nerowspec
        EmptyRows columns'
fields ->
          forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (forall a b. a -> b -> a
const Field SqlInt4
zero) (forall a b. a -> b -> a
const columns'
fields) forall (n :: Nullability) a.
NonEmptyRowspec (Field_ n a) (Field_ n a)
nonEmptyRowspecField
          where zero :: Field SqlInt4
zero = Field SqlInt4
0 :: C.Field Opaleye.SqlTypes.SqlInt4
  in forall fields fields'.
NonEmptyRowspec fields fields' -> NonEmpty fields -> Select fields'
nonEmptyRows NonEmptyRowspec columns columns'
nerowspec' NonEmpty columns
rows

nonEmptyRows :: NonEmptyRowspec fields fields'
             -> NEL.NonEmpty fields
             -> Q.Select fields'
nonEmptyRows :: forall fields fields'.
NonEmptyRowspec fields fields' -> NonEmpty fields -> Select fields'
nonEmptyRows (NonEmptyRowspec fields -> NonEmpty PrimExpr
runRow State Tag (NonEmpty Symbol, fields')
fields) NonEmpty fields
rows =
  forall a. State Tag (a, PrimQuery) -> Query a
Q.productQueryArr forall a b. (a -> b) -> a -> b
$ do
    (NonEmpty Symbol
valuesPEs, fields'
newColumns) <- State Tag (NonEmpty Symbol, fields')
fields
    forall (f :: * -> *) a. Applicative f => a -> f a
pure (fields'
newColumns, forall a. [Symbol] -> NonEmpty [PrimExpr] -> PrimQuery' a
PQ.Values (forall a. NonEmpty a -> [a]
NEL.toList NonEmpty Symbol
valuesPEs) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. NonEmpty a -> [a]
NEL.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. fields -> NonEmpty PrimExpr
runRow) NonEmpty fields
rows))

emptySelectExplicit :: Nullspec columns a -> Q.Select a
emptySelectExplicit :: forall columns a. Nullspec columns a -> Select a
emptySelectExplicit Nullspec columns a
nullspec = proc () -> do
  SelectArr (Field SqlBool) ()
O.restrict -< Bool -> Field SqlBool
Opaleye.SqlTypes.sqlBool Bool
False
  forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< forall a fields. Nullspec a fields -> fields
nullFields Nullspec columns a
nullspec

data NonEmptyRowspec fields fields' =
  NonEmptyRowspec (fields -> NEL.NonEmpty HPQ.PrimExpr)
                  (State.State T.Tag (NEL.NonEmpty HPQ.Symbol, fields'))

-- Some overlap here with extractAttrPE
nonEmptyRowspecField :: NonEmptyRowspec (Field_ n a) (Field_ n a)
nonEmptyRowspecField :: forall (n :: Nullability) a.
NonEmptyRowspec (Field_ n a) (Field_ n a)
nonEmptyRowspecField = forall fields fields'.
(fields -> NonEmpty PrimExpr)
-> State Tag (NonEmpty Symbol, fields')
-> NonEmptyRowspec fields fields'
NonEmptyRowspec (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nullability) a. Field_ n a -> PrimExpr
C.unColumn) forall {n :: Nullability} {sqlType}.
StateT Tag Identity (NonEmpty Symbol, Field_ n sqlType)
s
  where s :: StateT Tag Identity (NonEmpty Symbol, Field_ n sqlType)
s = do
          Tag
t <- State Tag Tag
T.fresh
          let symbol :: Symbol
symbol = String -> Tag -> Symbol
HPQ.Symbol String
"values" Tag
t
          forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall (f :: * -> *) a. Applicative f => a -> f a
pure Symbol
symbol, forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
C.Column (Symbol -> PrimExpr
HPQ.AttrExpr Symbol
symbol))

rowspecField :: Rowspec (Field_ n a) (Field_ n a)
rowspecField :: forall (n :: Nullability) a. Rowspec (Field_ n a) (Field_ n a)
rowspecField = forall fields fields'.
NonEmptyRowspec fields fields' -> Rowspec fields fields'
NonEmptyRows forall (n :: Nullability) a.
NonEmptyRowspec (Field_ n a) (Field_ n a)
nonEmptyRowspecField

data Rowspec fields fields' =
    NonEmptyRows (NonEmptyRowspec fields fields')
  | EmptyRows fields'

data Valuesspec fields fields' =
  ValuesspecSafe (Nullspec fields fields')
                 (Rowspec fields fields')

valuesspecField :: Opaleye.SqlTypes.IsSqlType a
                => Valuesspec (Field_ n a) (Field_ n a)
valuesspecField :: forall a (n :: Nullability).
IsSqlType a =>
Valuesspec (Field_ n a) (Field_ n a)
valuesspecField = forall {n :: Nullability}. Valuesspec (Field_ n a) (Field_ n a)
def_
    where def_ :: Valuesspec (Field_ n a) (Field_ n a)
def_ = forall (n :: Nullability) a.
String -> Valuesspec (Field_ n a) (Field_ n a)
valuesspecFieldType (forall sqlType (proxy :: * -> *).
IsSqlType sqlType =>
proxy sqlType -> String
Opaleye.Internal.PGTypes.showSqlType Maybe a
sqlType)
          sqlType :: Maybe a
sqlType = forall (f :: * -> *) (n :: Nullability) sqlType.
f (Field_ n sqlType) -> Maybe sqlType
columnProxy Valuesspec (Field_ n a) (Field_ n a)
def_
          columnProxy :: f (Field_ n sqlType) -> Maybe sqlType
          columnProxy :: forall (f :: * -> *) (n :: Nullability) sqlType.
f (Field_ n sqlType) -> Maybe sqlType
columnProxy f (Field_ n sqlType)
_ = forall a. Maybe a
Nothing

-- For rel8
valuesspecFieldType :: String -> Valuesspec (Field_ n a) (Field_ n a)
valuesspecFieldType :: forall (n :: Nullability) a.
String -> Valuesspec (Field_ n a) (Field_ n a)
valuesspecFieldType String
sqlType =
  forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe (forall a (n :: Nullability) sqlType.
String -> Nullspec a (Field_ n sqlType)
nullspecFieldType String
sqlType) forall (n :: Nullability) a. Rowspec (Field_ n a) (Field_ n a)
rowspecField

instance forall a n. Opaleye.Internal.PGTypes.IsSqlType a
  => Default Valuesspec (Field_ n a) (Field_ n a) where
  def :: Valuesspec (Field_ n a) (Field_ n a)
def = forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe forall a (n :: Nullability) sqlType.
IsSqlType sqlType =>
Nullspec a (Field_ n sqlType)
nullspecField forall (n :: Nullability) a. Rowspec (Field_ n a) (Field_ n a)
rowspecField

newtype Nullspec fields fields' = Nullspec fields'

nullspecField :: forall a n sqlType.
                 Opaleye.SqlTypes.IsSqlType sqlType
              => Nullspec a (Field_ n sqlType)
nullspecField :: forall a (n :: Nullability) sqlType.
IsSqlType sqlType =>
Nullspec a (Field_ n sqlType)
nullspecField = forall a (n :: Nullability) sqlType.
String -> Nullspec a (Field_ n sqlType)
nullspecFieldType String
ty
  where ty :: String
ty = forall sqlType (proxy :: * -> *).
IsSqlType sqlType =>
proxy sqlType -> String
Opaleye.Internal.PGTypes.showSqlType (forall a. Maybe a
Nothing :: Maybe sqlType)

nullspecFieldType :: String
                  -> Nullspec a (Field_ n sqlType)
nullspecFieldType :: forall a (n :: Nullability) sqlType.
String -> Nullspec a (Field_ n sqlType)
nullspecFieldType String
sqlType =
  (forall fields fields'. fields' -> Nullspec fields fields'
Nullspec
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nullability) a b. String -> Field_ n a -> Field_ n b
C.unsafeCast String
sqlType
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: Nullability) a (n' :: Nullability) b.
Field_ n a -> Field_ n' b
C.unsafeCoerceColumn)
  forall a. Column (Nullable a)
OC.null

nullspecList :: Nullspec a [b]
nullspecList :: forall a b. Nullspec a [b]
nullspecList = forall (f :: * -> *) a. Applicative f => a -> f a
pure []

nullspecEitherLeft :: Nullspec a b
                   -> Nullspec a (Either b b')
nullspecEitherLeft :: forall a b b'. Nullspec a b -> Nullspec a (Either b b')
nullspecEitherLeft = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. a -> Either a b
Left

nullspecEitherRight :: Nullspec a b'
                    -> Nullspec a (Either b b')
nullspecEitherRight :: forall a b' b. Nullspec a b' -> Nullspec a (Either b b')
nullspecEitherRight = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. b -> Either a b
Right

instance Opaleye.SqlTypes.IsSqlType b
  => Default Nullspec a (Field_ n b) where
  def :: Nullspec a (Field_ n b)
def = forall a (n :: Nullability) sqlType.
IsSqlType sqlType =>
Nullspec a (Field_ n sqlType)
nullspecField

-- | All fields @NULL@, even though technically the type may forbid
-- that!  Used to create such fields when we know we will never look
-- at them expecting to find something non-NULL.
nullFields :: Nullspec a fields -> fields
nullFields :: forall a fields. Nullspec a fields -> fields
nullFields (Nullspec fields
v) = fields
v

-- {

-- Boilerplate instance definitions.  Theoretically, these are derivable.

instance Functor (ValuesspecUnsafe a) where
  fmap :: forall a b.
(a -> b) -> ValuesspecUnsafe a a -> ValuesspecUnsafe a b
fmap a -> b
f (Valuesspec PackMap () PrimExpr () a
g) = forall columns columns'.
PackMap () PrimExpr () columns'
-> ValuesspecUnsafe columns columns'
Valuesspec (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f PackMap () PrimExpr () a
g)

instance Applicative (ValuesspecUnsafe a) where
  pure :: forall a. a -> ValuesspecUnsafe a a
pure = forall columns columns'.
PackMap () PrimExpr () columns'
-> ValuesspecUnsafe columns columns'
Valuesspec forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Valuesspec PackMap () PrimExpr () (a -> b)
f <*> :: forall a b.
ValuesspecUnsafe a (a -> b)
-> ValuesspecUnsafe a a -> ValuesspecUnsafe a b
<*> Valuesspec PackMap () PrimExpr () a
x = forall columns columns'.
PackMap () PrimExpr () columns'
-> ValuesspecUnsafe columns columns'
Valuesspec (PackMap () PrimExpr () (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> PackMap () PrimExpr () a
x)

instance Profunctor ValuesspecUnsafe where
  dimap :: forall a b c d.
(a -> b)
-> (c -> d) -> ValuesspecUnsafe b c -> ValuesspecUnsafe a d
dimap a -> b
_ c -> d
g (Valuesspec PackMap () PrimExpr () c
q) = forall columns columns'.
PackMap () PrimExpr () columns'
-> ValuesspecUnsafe columns columns'
Valuesspec (forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap c -> d
g PackMap () PrimExpr () c
q)

instance ProductProfunctor ValuesspecUnsafe where
  purePP :: forall b a. b -> ValuesspecUnsafe a b
purePP = forall (f :: * -> *) a. Applicative f => a -> f a
pure
  **** :: forall a a b.
ValuesspecUnsafe a (a -> b)
-> ValuesspecUnsafe a a -> ValuesspecUnsafe a b
(****) = forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

instance Functor (Valuesspec a) where
  fmap :: forall a b. (a -> b) -> Valuesspec a a -> Valuesspec a b
fmap a -> b
f (ValuesspecSafe Nullspec a a
g Rowspec a a
h) = forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Nullspec a a
g) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Rowspec a a
h)

instance Applicative (Valuesspec a) where
  pure :: forall a. a -> Valuesspec a a
pure a
a = forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a) (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a)
  ValuesspecSafe Nullspec a (a -> b)
f Rowspec a (a -> b)
f' <*> :: forall a b.
Valuesspec a (a -> b) -> Valuesspec a a -> Valuesspec a b
<*> ValuesspecSafe Nullspec a a
x Rowspec a a
x' =
    forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe (Nullspec a (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Nullspec a a
x) (Rowspec a (a -> b)
f' forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Rowspec a a
x')

instance Profunctor Valuesspec where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Valuesspec b c -> Valuesspec a d
dimap a -> b
f c -> d
g (ValuesspecSafe Nullspec b c
q Rowspec b c
q') = forall fields fields'.
Nullspec fields fields'
-> Rowspec fields fields' -> Valuesspec fields fields'
ValuesspecSafe (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g Nullspec b c
q) (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g Rowspec b c
q')

instance ProductProfunctor Valuesspec where
  purePP :: forall b a. b -> Valuesspec a b
purePP = forall (f :: * -> *) a. Applicative f => a -> f a
pure
  **** :: forall a a b.
Valuesspec a (a -> b) -> Valuesspec a a -> Valuesspec a b
(****) = forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

instance Functor (Nullspec a) where
  fmap :: forall a b. (a -> b) -> Nullspec a a -> Nullspec a b
fmap a -> b
f (Nullspec a
g) = forall fields fields'. fields' -> Nullspec fields fields'
Nullspec (a -> b
f a
g)

instance Applicative (Nullspec a) where
  pure :: forall a. a -> Nullspec a a
pure = forall fields fields'. fields' -> Nullspec fields fields'
Nullspec
  Nullspec a -> b
f <*> :: forall a b. Nullspec a (a -> b) -> Nullspec a a -> Nullspec a b
<*> Nullspec a
x = forall fields fields'. fields' -> Nullspec fields fields'
Nullspec (a -> b
f a
x)

instance Profunctor Nullspec where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> Nullspec b c -> Nullspec a d
dimap a -> b
_ c -> d
g (Nullspec c
q) = forall fields fields'. fields' -> Nullspec fields fields'
Nullspec (c -> d
g c
q)

instance ProductProfunctor Nullspec where
  purePP :: forall b a. b -> Nullspec a b
purePP = forall (f :: * -> *) a. Applicative f => a -> f a
pure
  **** :: forall a a b. Nullspec a (a -> b) -> Nullspec a a -> Nullspec a b
(****) = forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

instance Functor (NonEmptyRowspec a) where
  fmap :: forall a b. (a -> b) -> NonEmptyRowspec a a -> NonEmptyRowspec a b
fmap = forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap

instance Profunctor NonEmptyRowspec where
  dimap :: forall a b c d.
(a -> b) -> (c -> d) -> NonEmptyRowspec b c -> NonEmptyRowspec a d
dimap a -> b
f c -> d
g (NonEmptyRowspec b -> NonEmpty PrimExpr
a State Tag (NonEmpty Symbol, c)
b) =
    forall fields fields'.
(fields -> NonEmpty PrimExpr)
-> State Tag (NonEmpty Symbol, fields')
-> NonEmptyRowspec fields fields'
NonEmptyRowspec (forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap a -> b
f b -> NonEmpty PrimExpr
a) ((forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) c -> d
g State Tag (NonEmpty Symbol, c)
b)

instance Functor (Rowspec a) where
  fmap :: forall a b. (a -> b) -> Rowspec a a -> Rowspec a b
fmap = forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap

instance Applicative (Rowspec a) where
  pure :: forall a. a -> Rowspec a a
pure a
x = forall a a. a -> Rowspec a a
EmptyRows a
x
  Rowspec a (a -> b)
r1 <*> :: forall a b. Rowspec a (a -> b) -> Rowspec a a -> Rowspec a b
<*> Rowspec a a
r2 = case (Rowspec a (a -> b)
r1, Rowspec a a
r2) of
    (EmptyRows a -> b
f, EmptyRows a
x) -> forall a a. a -> Rowspec a a
EmptyRows (a -> b
f a
x)
    (EmptyRows a -> b
f, NonEmptyRows (NonEmptyRowspec a -> NonEmpty PrimExpr
x1 State Tag (NonEmpty Symbol, a)
x2)) ->
      forall fields fields'.
NonEmptyRowspec fields fields' -> Rowspec fields fields'
NonEmptyRows (forall fields fields'.
(fields -> NonEmpty PrimExpr)
-> State Tag (NonEmpty Symbol, fields')
-> NonEmptyRowspec fields fields'
NonEmptyRowspec a -> NonEmpty PrimExpr
x1 ((forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) a -> b
f State Tag (NonEmpty Symbol, a)
x2))
    (NonEmptyRows (NonEmptyRowspec a -> NonEmpty PrimExpr
f1 State Tag (NonEmpty Symbol, a -> b)
f2), EmptyRows a
x) ->
     forall fields fields'.
NonEmptyRowspec fields fields' -> Rowspec fields fields'
NonEmptyRows (forall fields fields'.
(fields -> NonEmpty PrimExpr)
-> State Tag (NonEmpty Symbol, fields')
-> NonEmptyRowspec fields fields'
NonEmptyRowspec a -> NonEmpty PrimExpr
f1 ((forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (forall a b. (a -> b) -> a -> b
$ a
x) State Tag (NonEmpty Symbol, a -> b)
f2))
    (NonEmptyRows (NonEmptyRowspec a -> NonEmpty PrimExpr
f1 State Tag (NonEmpty Symbol, a -> b)
f2),
     NonEmptyRows (NonEmptyRowspec a -> NonEmpty PrimExpr
x1 State Tag (NonEmpty Symbol, a)
x2)) ->
      forall fields fields'.
NonEmptyRowspec fields fields' -> Rowspec fields fields'
NonEmptyRows (forall fields fields'.
(fields -> NonEmpty PrimExpr)
-> State Tag (NonEmpty Symbol, fields')
-> NonEmptyRowspec fields fields'
NonEmptyRowspec
            (a -> NonEmpty PrimExpr
f1 forall a. Semigroup a => a -> a -> a
<> a -> NonEmpty PrimExpr
x1)
            ((forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m a' b c.
Semigroup m =>
(a' -> b -> c) -> (m, a') -> (m, b) -> (m, c)
liftF2) forall a b. (a -> b) -> a -> b
($) State Tag (NonEmpty Symbol, a -> b)
f2 State Tag (NonEmpty Symbol, a)
x2))

    where -- Instead of depending on Apply
          -- https://www.stackage.org/haddock/lts-19.16/semigroupoids-5.3.7/Data-Functor-Apply.html#v:liftF2
          liftF2 :: Semigroup m
                 => (a' -> b -> c) -> (m, a') -> (m, b) -> (m, c)
          liftF2 :: forall m a' b c.
Semigroup m =>
(a' -> b -> c) -> (m, a') -> (m, b) -> (m, c)
liftF2 a' -> b -> c
f (m
ys1, a'
x1) (m
ys2, b
x2) = (m
ys1 forall a. Semigroup a => a -> a -> a
<> m
ys2, a' -> b -> c
f a'
x1 b
x2)

instance Profunctor Rowspec where
  dimap :: forall a b c d. (a -> b) -> (c -> d) -> Rowspec b c -> Rowspec a d
dimap a -> b
f c -> d
g = \case
    EmptyRows c
x -> forall a a. a -> Rowspec a a
EmptyRows (c -> d
g c
x)
    NonEmptyRows NonEmptyRowspec b c
x -> forall fields fields'.
NonEmptyRowspec fields fields' -> Rowspec fields fields'
NonEmptyRows (forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap a -> b
f c -> d
g NonEmptyRowspec b c
x)

instance ProductProfunctor Rowspec where
  purePP :: forall b a. b -> Rowspec a b
purePP = forall (f :: * -> *) a. Applicative f => a -> f a
pure
  **** :: forall a a b. Rowspec a (a -> b) -> Rowspec a a -> Rowspec a b
(****) = forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

-- }

{-# DEPRECATED valuesU "Will be removed in 0.10" #-}
valuesU :: U.Unpackspec columns columns'
        -> ValuesspecUnsafe columns columns'
        -> [columns]
        -> ((), T.Tag) -> (columns', PQ.PrimQuery)
valuesU :: forall columns columns'.
Unpackspec columns columns'
-> ValuesspecUnsafe columns columns'
-> [columns]
-> ((), Tag)
-> (columns', PrimQuery)
valuesU Unpackspec columns columns'
unpack ValuesspecUnsafe columns columns'
valuesspec [columns]
rows ((), Tag
t) = (columns'
newColumns, PrimQuery
primQ')
  where runRow :: columns -> [PrimExpr]
runRow columns
row = [PrimExpr]
valuesRow
           where (columns'
_, [PrimExpr]
valuesRow) =
                   forall a r. PM [a] r -> (r, [a])
PM.run (forall (f :: * -> *) columns b.
Applicative f =>
Unpackspec columns b -> (PrimExpr -> f PrimExpr) -> columns -> f b
U.runUnpackspec Unpackspec columns columns'
unpack PrimExpr -> PM [PrimExpr] PrimExpr
extractValuesEntry columns
row)

        (columns'
newColumns, [(Symbol, ())]
valuesPEs_nulls) =
          forall a r. PM [a] r -> (r, [a])
PM.run (forall (f :: * -> *) columns columns'.
Applicative f =>
ValuesspecUnsafe columns columns'
-> (() -> f PrimExpr) -> f columns'
runValuesspec ValuesspecUnsafe columns columns'
valuesspec (forall primExpr.
Tag -> primExpr -> PM [(Symbol, primExpr)] PrimExpr
extractValuesField Tag
t))

        valuesPEs :: [Symbol]
valuesPEs = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Symbol, ())]
valuesPEs_nulls

        values :: [[HPQ.PrimExpr]]
        values :: [[PrimExpr]]
values = forall a b. (a -> b) -> [a] -> [b]
map columns -> [PrimExpr]
runRow [columns]
rows

        primQ' :: PrimQuery
primQ' = case forall a. [a] -> Maybe (NonEmpty a)
NEL.nonEmpty [[PrimExpr]]
values of
          Maybe (NonEmpty [PrimExpr])
Nothing      -> forall a. a -> PrimQuery' a
PQ.Empty ()
          Just NonEmpty [PrimExpr]
values' -> forall a. [Symbol] -> NonEmpty [PrimExpr] -> PrimQuery' a
PQ.Values [Symbol]
valuesPEs NonEmpty [PrimExpr]
values'

{-# DEPRECATED extractValuesEntry "Will be removed in 0.10" #-}
extractValuesEntry :: HPQ.PrimExpr -> PM.PM [HPQ.PrimExpr] HPQ.PrimExpr
extractValuesEntry :: PrimExpr -> PM [PrimExpr] PrimExpr
extractValuesEntry PrimExpr
pe = do
  forall a. a -> PM [a] ()
PM.write PrimExpr
pe
  forall (m :: * -> *) a. Monad m => a -> m a
return PrimExpr
pe

{-# DEPRECATED extractValuesField "Will be removed in 0.10" #-}
extractValuesField :: T.Tag -> primExpr
                   -> PM.PM [(HPQ.Symbol, primExpr)] HPQ.PrimExpr
extractValuesField :: forall primExpr.
Tag -> primExpr -> PM [(Symbol, primExpr)] PrimExpr
extractValuesField = forall primExpr.
String -> Tag -> primExpr -> PM [(Symbol, primExpr)] PrimExpr
PM.extractAttr String
"values"

{-# DEPRECATED runValuesspec "Will be removed in 0.10" #-}
runValuesspec :: Applicative f => ValuesspecUnsafe columns columns'
              -> (() -> f HPQ.PrimExpr) -> f columns'
runValuesspec :: forall (f :: * -> *) columns columns'.
Applicative f =>
ValuesspecUnsafe columns columns'
-> (() -> f PrimExpr) -> f columns'
runValuesspec (Valuesspec PackMap () PrimExpr () columns'
v) () -> f PrimExpr
f = forall (f :: * -> *) a b s t.
Applicative f =>
PackMap a b s t -> (a -> f b) -> s -> f t
PM.traversePM PackMap () PrimExpr () columns'
v () -> f PrimExpr
f ()

newtype ValuesspecUnsafe columns columns' =
  Valuesspec (PM.PackMap () HPQ.PrimExpr () columns')

instance Default ValuesspecUnsafe (Field_ n a) (Field_ n a) where
  def :: ValuesspecUnsafe (Field_ n a) (Field_ n a)
def = forall columns columns'.
PackMap () PrimExpr () columns'
-> ValuesspecUnsafe columns columns'
Valuesspec (forall s a b t. (s -> a) -> (b -> t) -> PackMap a b s t
PM.iso forall a. a -> a
id forall (n :: Nullability) sqlType. PrimExpr -> Field_ n sqlType
Column)

{-# DEPRECATED ValuesspecSafe "Use Valuesspec instead.  Will be removed in version 0.10." #-}
type ValuesspecSafe = Valuesspec