{-# LANGUAGE DefaultSignatures, FlexibleInstances, FlexibleContexts #-}
------------------------------------------------------------------------------
-- |
-- Module:      Database.PostgreSQL.Simple.ToRow
-- Copyright:   (c) 2011 MailRank, Inc.
--              (c) 2011-2012 Leon P Smith
-- License:     BSD3
-- Maintainer:  Leon P Smith <leon@melding-monads.com>
-- Stability:   experimental
--
-- The 'ToRow' typeclass, for rendering a collection of
-- parameters to a SQL query.
--
-- Predefined instances are provided for tuples containing up to ten
-- elements.
--
------------------------------------------------------------------------------

module Database.PostgreSQL.Simple.ToRow
    (
      ToRow(..)
    ) where

import Database.PostgreSQL.Simple.ToField (Action(..), ToField(..))
import Database.PostgreSQL.Simple.Types (Only(..), (:.)(..))
import GHC.Generics

-- | A collection type that can be turned into a list of rendering
-- 'Action's.
--
-- Instances should use the 'toField' method of the 'ToField' class
-- to perform conversion of each element of the collection.
--
-- You can derive 'ToRow' for your data type using GHC generics, like this:
--
-- @
-- \{-# LANGUAGE DeriveAnyClass \#-}
-- \{-# LANGUAGE DeriveGeneric  \#-}
--
-- import "GHC.Generics" ('GHC.Generics.Generic')
-- import "Database.PostgreSQL.Simple" ('ToRow')
--
-- data User = User { name :: String, fileQuota :: Int }
--   deriving ('GHC.Generics.Generic', 'ToRow')
-- @
--
-- Note that this only works for product types (e.g. records) and does not
-- support sum types or recursive types.
class ToRow a where
    toRow :: a -> [Action]
    default toRow :: (Generic a, GToRow (Rep a)) => a -> [Action]
    toRow = forall (f :: * -> *) p. GToRow f => f p -> [Action]
gtoRow forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from
    -- ^ ToField a collection of values.

instance ToRow () where
    toRow :: () -> [Action]
toRow ()
_ = []

instance (ToField a) => ToRow (Only a) where
    toRow :: Only a -> [Action]
toRow (Only a
v) = [forall a. ToField a => a -> Action
toField a
v]

instance (ToField a, ToField b) => ToRow (a,b) where
    toRow :: (a, b) -> [Action]
toRow (a
a,b
b) = [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b]

instance (ToField a, ToField b, ToField c) => ToRow (a,b,c) where
    toRow :: (a, b, c) -> [Action]
toRow (a
a,b
b,c
c) = [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c]

instance (ToField a, ToField b, ToField c, ToField d) => ToRow (a,b,c,d) where
    toRow :: (a, b, c, d) -> [Action]
toRow (a
a,b
b,c
c,d
d) = [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d]

instance (ToField a, ToField b, ToField c, ToField d, ToField e)
    => ToRow (a,b,c,d,e) where
    toRow :: (a, b, c, d, e) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f)
    => ToRow (a,b,c,d,e,f) where
    toRow :: (a, b, c, d, e, f) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g)
    => ToRow (a,b,c,d,e,f,g) where
    toRow :: (a, b, c, d, e, f, g) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h)
    => ToRow (a,b,c,d,e,f,g,h) where
    toRow :: (a, b, c, d, e, f, g, h) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i)
    => ToRow (a,b,c,d,e,f,g,h,i) where
    toRow :: (a, b, c, d, e, f, g, h, i) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j)
    => ToRow (a,b,c,d,e,f,g,h,i,j) where
    toRow :: (a, b, c, d, e, f, g, h, i, j) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o, ToField p)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o,p
p) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o, forall a. ToField a => a -> Action
toField p
p]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o, ToField p, ToField q)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o,p
p,q
q) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o, forall a. ToField a => a -> Action
toField p
p, forall a. ToField a => a -> Action
toField q
q]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o, ToField p, ToField q, ToField r)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r) -> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o,p
p,q
q,r
r) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o, forall a. ToField a => a -> Action
toField p
p, forall a. ToField a => a -> Action
toField q
q, forall a. ToField a => a -> Action
toField r
r]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o, ToField p, ToField q, ToField r,
          ToField s)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)
-> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o,p
p,q
q,r
r,s
s) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o, forall a. ToField a => a -> Action
toField p
p, forall a. ToField a => a -> Action
toField q
q, forall a. ToField a => a -> Action
toField r
r,
         forall a. ToField a => a -> Action
toField s
s]

instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f,
          ToField g, ToField h, ToField i, ToField j, ToField k, ToField l,
          ToField m, ToField n, ToField o, ToField p, ToField q, ToField r,
          ToField s, ToField t)
    => ToRow (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t) where
    toRow :: (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)
-> [Action]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j,k
k,l
l,m
m,n
n,o
o,p
p,q
q,r
r,s
s,t
t) =
        [forall a. ToField a => a -> Action
toField a
a, forall a. ToField a => a -> Action
toField b
b, forall a. ToField a => a -> Action
toField c
c, forall a. ToField a => a -> Action
toField d
d, forall a. ToField a => a -> Action
toField e
e, forall a. ToField a => a -> Action
toField f
f,
         forall a. ToField a => a -> Action
toField g
g, forall a. ToField a => a -> Action
toField h
h, forall a. ToField a => a -> Action
toField i
i, forall a. ToField a => a -> Action
toField j
j, forall a. ToField a => a -> Action
toField k
k, forall a. ToField a => a -> Action
toField l
l,
         forall a. ToField a => a -> Action
toField m
m, forall a. ToField a => a -> Action
toField n
n, forall a. ToField a => a -> Action
toField o
o, forall a. ToField a => a -> Action
toField p
p, forall a. ToField a => a -> Action
toField q
q, forall a. ToField a => a -> Action
toField r
r,
         forall a. ToField a => a -> Action
toField s
s, forall a. ToField a => a -> Action
toField t
t]

instance (ToField a) => ToRow [a] where
    toRow :: [a] -> [Action]
toRow = forall a b. (a -> b) -> [a] -> [b]
map forall a. ToField a => a -> Action
toField

instance (ToRow a, ToRow b) => ToRow (a :. b) where
    toRow :: (a :. b) -> [Action]
toRow (a
a :. b
b) = forall a. ToRow a => a -> [Action]
toRow a
a forall a. [a] -> [a] -> [a]
++ forall a. ToRow a => a -> [Action]
toRow b
b


-- Type class for default implementation of ToRow using generics
class GToRow f where
    gtoRow :: f p -> [Action]

instance GToRow f => GToRow (M1 c i f) where
    gtoRow :: forall p. M1 c i f p -> [Action]
gtoRow (M1 f p
x) = forall (f :: * -> *) p. GToRow f => f p -> [Action]
gtoRow f p
x

instance (GToRow f, GToRow g) => GToRow (f :*: g) where
    gtoRow :: forall p. (:*:) f g p -> [Action]
gtoRow (f p
f :*: g p
g) = forall (f :: * -> *) p. GToRow f => f p -> [Action]
gtoRow f p
f forall a. [a] -> [a] -> [a]
++ forall (f :: * -> *) p. GToRow f => f p -> [Action]
gtoRow g p
g

instance (ToField a) => GToRow (K1 R a) where
    gtoRow :: forall p. K1 R a p -> [Action]
gtoRow (K1 a
a) = [forall a. ToField a => a -> Action
toField a
a]

instance GToRow U1 where
    gtoRow :: forall p. U1 p -> [Action]
gtoRow U1 p
_ = []