Skip to content

Commit 320380f

Browse files
Session construction demo
1 parent 0f1eaad commit 320380f

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

src/Database/PostgreSQL/Query.hs

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,11 @@ import qualified Data.HashSet as HS
1010
import Data.Word
1111
import Data.Proxy
1212
import Data.Monoid
13+
import Data.Coerce
1314

1415
-- import Database.PostgreSQL.Session
1516
import Database.PostgreSQL.Other
1617

17-
---------------
18-
-- Result Parser
19-
---------------
20-
21-
data ResultParser
22-
= OneResurtParser
23-
| MaybeResultParser
24-
| ManyParser
25-
26-
data ResultParserError
27-
= ResultNoRows
28-
| ResultToManyRows
2918

3019
---------------------
3120
-- Row parser
@@ -74,64 +63,90 @@ composite name xs = undefined
7463
class ToPostgres a where
7564
toPostgres :: Encoder a
7665

77-
-- Params
78-
7966
class IsParams a where
8067
params :: a -> [QueryM EncodeResult]
8168

8269
instance (ToPostgres a, ToPostgres b) => IsParams (a, b) where
8370
params (a, b) = [toPostgres a, toPostgres b]
8471

85-
type Context = [QueryM EncodeResult]
72+
----------------------
73+
-- Results
74+
-------------------
8675

87-
class ToParams a where
88-
type ParamType a :: *
76+
data ResultParser a
77+
= OneRowParser
78+
| MaybeRowParser
79+
| ManyRowsParser
8980

90-
derive :: Proxy a -> Context -> ParamType a
81+
data ResultParserError
82+
= ResultNoRows
83+
| ResultToManyRows
9184

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
9488

95-
derive p ctx = ctx <> params
89+
class FromResult a where
90+
type Result a :: *
9691

97-
instance (ToPostgres x, ToParams xs) => ToParams (x ': xs) where
98-
type ParamType (x ': xs) = x -> ParamType xs
92+
resultParser :: ResultParser (Result a)
9993

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
10196

102-
instance ToParams '[] where
103-
type ParamType a = [QueryM EncodeResult]
97+
resultParser = OneRowParser
10498

105-
derive p ctx = ctx
99+
instance FromPostgres a => FromResult (MaybeRow a) where
100+
type Result (MaybeRow a) = Maybe a
106101

107-
getParams :: ToParams a => SessionQuery a b -> ParamType a
108-
getParams _ = derive (Proxy :: Proxy a) []
102+
resultParser = MaybeRowParser
109103

104+
instance FromPostgres a => FromResult (ManyRows a) where
105+
type Result (ManyRows a) = V.Vector a
110106

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
116108

117-
----------------------
118-
-- Results
119-
-------------------
120-
data ResultType a
121-
= SingleRow a
122-
| MaybeRow a
123-
| ManyRows a
109+
------------------------
110+
-- Session
111+
-----------------------
124112

125-
data SessionQuery a (b :: ResultType *) = SessionQuery { sqStatement :: B.ByteString }
113+
data SessionQuery a b = SessionQuery { sqStatement :: B.ByteString }
126114
deriving (Show)
127115

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
132132

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
135150

136151

137152
tq :: SessionQuery '[Int, Char, Word] b

0 commit comments

Comments
 (0)