on e150ff4 help sign in
rel8/src/Rel8/Statement/Select.hs
61 lines 1.8 kB history raw
1{-# language ScopedTypeVariables #-}
2{-# language TypeApplications #-}
4module Rel8.Statement.Select
5 ( select
6 , selectWithNames
7 )
8where
10-- base
11import Control.Exception ( throwIO )
12import Control.Monad ( (>=>) )
13import Prelude
15-- hasql
16import Hasql.Connection ( Connection )
17import qualified Hasql.Decoders as Hasql
18import qualified Hasql.Encoders as Hasql
19import qualified Hasql.Session as Hasql
20import qualified Hasql.Statement as Hasql
22-- rel8
23import Rel8.Query ( Query )
24import Rel8.Query.SQL ( sqlForQuery, sqlForQueryWithNames )
25import Rel8.Table.Recontextualize ( Selects )
26import Rel8.Table.Serialize ( Serializable, parse )
28-- text
29import qualified Data.Text as Text
30import Data.Text.Encoding ( encodeUtf8 )
33select :: forall exprs a. Serializable exprs a
34 => Query exprs -> Connection -> IO [a]
35select query = case sqlForQuery query of
36 Nothing -> const $ pure []
37 Just sql -> Hasql.run session >=> either throwIO pure
38 where
39 session = Hasql.statement () statement
40 statement = Hasql.Statement bytes params decode prepare
41 bytes = encodeUtf8 (Text.pack sql)
42 params = Hasql.noParams
43 decode = Hasql.rowList (parse @exprs @a)
44 prepare = False
47selectWithNames :: forall exprs a names.
48 ( Selects names exprs
49 , Serializable exprs a
50 )
51 => names -> Query exprs -> Connection -> IO [a]
52selectWithNames names query = case sqlForQueryWithNames names query of
53 Nothing -> const $ pure []
54 Just sql -> Hasql.run session >=> either throwIO pure
55 where
56 session = Hasql.statement () statement
57 statement = Hasql.Statement bytes params decode prepare
58 bytes = encodeUtf8 (Text.pack sql)
59 params = Hasql.noParams
60 decode = Hasql.rowList (parse @exprs @a)
61 prepare = False