@@ -10,22 +10,11 @@ import qualified Data.HashSet as HS
10
10
import Data.Word
11
11
import Data.Proxy
12
12
import Data.Monoid
13
+ import Data.Coerce
13
14
14
15
-- import Database.PostgreSQL.Session
15
16
import Database.PostgreSQL.Other
16
17
17
- ---------------
18
- -- Result Parser
19
- ---------------
20
-
21
- data ResultParser
22
- = OneResurtParser
23
- | MaybeResultParser
24
- | ManyParser
25
-
26
- data ResultParserError
27
- = ResultNoRows
28
- | ResultToManyRows
29
18
30
19
---------------------
31
20
-- Row parser
@@ -74,64 +63,90 @@ composite name xs = undefined
74
63
class ToPostgres a where
75
64
toPostgres :: Encoder a
76
65
77
- -- Params
78
-
79
66
class IsParams a where
80
67
params :: a -> [QueryM EncodeResult ]
81
68
82
69
instance (ToPostgres a , ToPostgres b ) => IsParams (a , b ) where
83
70
params (a, b) = [toPostgres a, toPostgres b]
84
71
85
- type Context = [QueryM EncodeResult ]
72
+ ----------------------
73
+ -- Results
74
+ -------------------
86
75
87
- class ToParams a where
88
- type ParamType a :: *
76
+ data ResultParser a
77
+ = OneRowParser
78
+ | MaybeRowParser
79
+ | ManyRowsParser
89
80
90
- derive :: Proxy a -> Context -> ParamType a
81
+ data ResultParserError
82
+ = ResultNoRows
83
+ | ResultToManyRows
91
84
92
- instance IsParams a => ToParams a where
93
- type ParamType a = a -> [QueryM EncodeResult ]
85
+ data SingleRow a
86
+ data MaybeRow a
87
+ data ManyRows a
94
88
95
- derive p ctx = ctx <> params
89
+ class FromResult a where
90
+ type Result a :: *
96
91
97
- instance (ToPostgres x , ToParams xs ) => ToParams (x ': xs ) where
98
- type ParamType (x ': xs ) = x -> ParamType xs
92
+ resultParser :: ResultParser (Result a )
99
93
100
- derive p ctx v = derive (Proxy :: Proxy xs ) (toPostgres v : ctx)
94
+ instance FromPostgres a => FromResult (SingleRow a ) where
95
+ type Result (SingleRow a ) = a
101
96
102
- instance ToParams '[] where
103
- type ParamType a = [QueryM EncodeResult ]
97
+ resultParser = OneRowParser
104
98
105
- derive p ctx = ctx
99
+ instance FromPostgres a => FromResult (MaybeRow a ) where
100
+ type Result (MaybeRow a ) = Maybe a
106
101
107
- getParams :: ToParams a => SessionQuery a b -> ParamType a
108
- getParams _ = derive (Proxy :: Proxy a ) []
102
+ resultParser = MaybeRowParser
109
103
104
+ instance FromPostgres a => FromResult (ManyRows a ) where
105
+ type Result (ManyRows a ) = V. Vector a
110
106
111
- buildSession :: SessionQuery a b -> [QueryM EncoderResult ] -> Session b
112
- buildSession = undefined
113
- -- makeQuery . runEncodeResult <$> sequence params
114
- -- where
115
- -- makeQuery values = Query
107
+ resultParser = ManyRowsParser
116
108
117
- ----------------------
118
- -- Results
119
- -------------------
120
- data ResultType a
121
- = SingleRow a
122
- | MaybeRow a
123
- | ManyRows a
109
+ ------------------------
110
+ -- Session
111
+ -----------------------
124
112
125
- data SessionQuery a ( b :: ResultType * ) = SessionQuery { sqStatement :: B. ByteString }
113
+ data SessionQuery a b = SessionQuery { sqStatement :: B. ByteString }
126
114
deriving (Show )
127
115
128
- type family Result a where
129
- Result (SingleRow a ) = a
130
- Result (MaybeRow a ) = Maybe a
131
- Result (ManyRows a ) = V. Vector a
116
+ type Context = [QueryM EncodeResult ]
117
+
118
+ class ToSession a where
119
+ type SessionType a :: *
120
+
121
+ derive :: a -> Context -> SessionType a
122
+
123
+ instance (IsParams a , FromResult b ) => ToSession (SessionQuery a b ) where
124
+ type SessionType a = a -> Session (Result b )
125
+
126
+ derive q ctx = buildSession s $ ctx <> params
127
+
128
+ instance (ToPostgres x , ToSession xs , FromResult b )
129
+ => ToSession (SessionQuery (x ': xs ) b ) where
130
+
131
+ type SessionType (x ': xs ) = x -> SessionType xs
132
132
133
- query :: (ToParams a , FromRows b ) => SessionQuery a b -> a -> Session (Result b )
134
- query = undefined
133
+ derive q ctx v = derive (coerce q :: SessionQuery xs b ) (toPostgres v : ctx)
134
+
135
+ instance FromResult b => ToSession (SessionQuery '[] b ) where
136
+ type SessionType a = Session (Result b )
137
+
138
+ derive = buildSession
139
+
140
+ query :: ToSession q => q -> SessionType q
141
+ query q = derive q []
142
+
143
+ buildSession
144
+ :: FromResult b
145
+ => SessionQuery a b -> [QueryM EncoderResult ] -> Session (Result b )
146
+ buildSession = undefined
147
+ -- makeQuery . runEncodeResult <$> sequence params
148
+ -- where
149
+ -- makeQuery values = Query
135
150
136
151
137
152
tq :: SessionQuery '[Int , Char , Word ] b
0 commit comments