-
Notifications
You must be signed in to change notification settings - Fork 71
/
ToRow.hs
92 lines (76 loc) · 3.22 KB
/
ToRow.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
------------------------------------------------------------------------------
-- |
-- 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(..), (:.)(..))
-- | A collection type that can be turned into a list of rendering
-- 'Action's.
--
-- Instances should use the 'render' method of the 'Param' class
-- to perform conversion of each element of the collection.
class ToRow a where
toRow :: a -> [Action]
-- ^ ToField a collection of values.
instance ToRow () where
toRow _ = []
instance (ToField a) => ToRow (Only a) where
toRow (Only v) = [toField v]
instance (ToField a, ToField b) => ToRow (a,b) where
toRow (a,b) = [toField a, toField b]
instance (ToField a, ToField b, ToField c) => ToRow (a,b,c) where
toRow (a,b,c) = [toField a, toField b, toField c]
instance (ToField a, ToField b, ToField c, ToField d) => ToRow (a,b,c,d) where
toRow (a,b,c,d) = [toField a, toField b, toField c, toField 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) =
[toField a, toField b, toField c, toField d, toField 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) =
[toField a, toField b, toField c, toField d, toField e, toField 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) =
[toField a, toField b, toField c, toField d, toField e, toField f,
toField 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) =
[toField a, toField b, toField c, toField d, toField e, toField f,
toField g, toField 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) =
[toField a, toField b, toField c, toField d, toField e, toField f,
toField g, toField h, toField 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) =
[toField a, toField b, toField c, toField d, toField e, toField f,
toField g, toField h, toField i, toField j]
instance (ToField a) => ToRow [a] where
toRow = map toField
instance (ToRow a, ToRow b) => ToRow (a :. b) where
toRow (a :. b) = toRow a ++ toRow b