{-# Language DefaultSignatures, FlexibleContexts, DeriveAnyClass,
  StandaloneDeriving #-}
------------------------------------------------------------------------------
-- |
-- Module:      Database.SQLite.Simple.ToRow
-- Copyright:   (c) 2011 MailRank, Inc.
--              (c) 2011-2012 Leon P Smith
--              (c) 2012-2013 Janne Hellsten
-- License:     BSD3
-- Maintainer:  Janne Hellsten <jjhellst@gmail.com>
-- Portability: portable
--
-- 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.SQLite.Simple.ToRow
    ( GToRow(..)
    , ToRow(..)
    ) where

import GHC.Generics

import Database.SQLite.Simple.ToField (ToField(..))
import Database.SQLite.Simple.Types (Only(..), (:.)(..))

import Database.SQLite3 (SQLData(..))

-- | Generic derivation of 'ToRow'.  For details about what can be
-- derived refer to 'Database.Sqlite.Simple.FromRow.GFromRow'.
--
-- @since 0.4.18.1
class GToRow f where
  gtoRow :: (f a) -> [SQLData]

instance GToRow U1 where
  gtoRow :: forall a. U1 a -> [SQLData]
gtoRow U1 a
U1 = [SQLData]
forall a. Monoid a => a
mempty

instance ToField a => GToRow (K1 i a) where
  gtoRow :: forall a. K1 i a a -> [SQLData]
gtoRow (K1 a
a) = SQLData -> [SQLData]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SQLData -> [SQLData]) -> SQLData -> [SQLData]
forall a b. (a -> b) -> a -> b
$ a -> SQLData
forall a. ToField a => a -> SQLData
toField a
a

instance (GToRow a, GToRow b) => GToRow (a :*: b) where
  gtoRow :: forall a. (:*:) a b a -> [SQLData]
gtoRow (a a
a :*: b a
b) = a a -> [SQLData]
forall a. a a -> [SQLData]
forall (f :: * -> *) a. GToRow f => f a -> [SQLData]
gtoRow a a
a [SQLData] -> [SQLData] -> [SQLData]
forall a. Monoid a => a -> a -> a
`mappend` b a -> [SQLData]
forall a. b a -> [SQLData]
forall (f :: * -> *) a. GToRow f => f a -> [SQLData]
gtoRow b a
b

instance GToRow a => GToRow (M1 i c a) where
  gtoRow :: forall a. M1 i c a a -> [SQLData]
gtoRow (M1 a a
a) = a a -> [SQLData]
forall a. a a -> [SQLData]
forall (f :: * -> *) a. GToRow f => f a -> [SQLData]
gtoRow a a
a

-- | A collection type that can be turned into a list of 'SQLData'
-- elements.
--
-- Since version 0.4.18.1 it is possible in some cases to derive a
-- generic implementation for 'ToRow'.  Refer to the documentation for
-- 'Database.Sqlite.Simple.FromRow.FromRow' to see how this can be
-- done.
class ToRow a where
    toRow :: a -> [SQLData]
    -- ^ 'ToField' a collection of values.

    default toRow :: Generic a => GToRow (Rep a) => a -> [SQLData]
    toRow a
a = Rep a Any -> [SQLData]
forall a. Rep a a -> [SQLData]
forall (f :: * -> *) a. GToRow f => f a -> [SQLData]
gtoRow (Rep a Any -> [SQLData]) -> Rep a Any -> [SQLData]
forall a b. (a -> b) -> a -> b
$ a -> Rep a Any
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from a
a

deriving instance ToRow ()
deriving instance (ToField a) => ToRow (Only a)
deriving instance (ToField a, ToField b) => ToRow (a,b)
deriving instance (ToField a, ToField b, ToField c) => ToRow (a,b,c)
deriving instance (ToField a, ToField b, ToField c, ToField d) => ToRow (a,b,c,d)
deriving instance (ToField a, ToField b, ToField c, ToField d, ToField e) => ToRow (a,b,c,d,e)
deriving instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f) => ToRow (a,b,c,d,e,f)
deriving instance (ToField a, ToField b, ToField c, ToField d, ToField e, ToField f, ToField g) => ToRow (a,b,c,d,e,f,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) -> [SQLData]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h) =
        [a -> SQLData
forall a. ToField a => a -> SQLData
toField a
a, b -> SQLData
forall a. ToField a => a -> SQLData
toField b
b, c -> SQLData
forall a. ToField a => a -> SQLData
toField c
c, d -> SQLData
forall a. ToField a => a -> SQLData
toField d
d, e -> SQLData
forall a. ToField a => a -> SQLData
toField e
e, f -> SQLData
forall a. ToField a => a -> SQLData
toField f
f,
         g -> SQLData
forall a. ToField a => a -> SQLData
toField g
g, h -> SQLData
forall a. ToField a => a -> SQLData
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) -> [SQLData]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i) =
        [a -> SQLData
forall a. ToField a => a -> SQLData
toField a
a, b -> SQLData
forall a. ToField a => a -> SQLData
toField b
b, c -> SQLData
forall a. ToField a => a -> SQLData
toField c
c, d -> SQLData
forall a. ToField a => a -> SQLData
toField d
d, e -> SQLData
forall a. ToField a => a -> SQLData
toField e
e, f -> SQLData
forall a. ToField a => a -> SQLData
toField f
f,
         g -> SQLData
forall a. ToField a => a -> SQLData
toField g
g, h -> SQLData
forall a. ToField a => a -> SQLData
toField h
h, i -> SQLData
forall a. ToField a => a -> SQLData
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) -> [SQLData]
toRow (a
a,b
b,c
c,d
d,e
e,f
f,g
g,h
h,i
i,j
j) =
        [a -> SQLData
forall a. ToField a => a -> SQLData
toField a
a, b -> SQLData
forall a. ToField a => a -> SQLData
toField b
b, c -> SQLData
forall a. ToField a => a -> SQLData
toField c
c, d -> SQLData
forall a. ToField a => a -> SQLData
toField d
d, e -> SQLData
forall a. ToField a => a -> SQLData
toField e
e, f -> SQLData
forall a. ToField a => a -> SQLData
toField f
f,
         g -> SQLData
forall a. ToField a => a -> SQLData
toField g
g, h -> SQLData
forall a. ToField a => a -> SQLData
toField h
h, i -> SQLData
forall a. ToField a => a -> SQLData
toField i
i, j -> SQLData
forall a. ToField a => a -> SQLData
toField j
j]

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

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