on a8f61ac help sign in
53 lines 1.4 kB history raw
1{-# language FlexibleContexts #-}
2{-# language MonoLocalBinds #-}
4module Rel8.Statement.View
5 ( createView
6 )
7where
9-- base
10import Prelude
12-- hasql
13import qualified Hasql.Decoders as Hasql
14import qualified Hasql.Encoders as Hasql
15import qualified Hasql.Statement as Hasql
17-- rel8
18import Rel8.Query ( Query )
19import Rel8.Schema.Name ( Selects )
20import Rel8.Schema.Table ( TableSchema )
21import Rel8.Statement.Insert ( ppInto )
22import Rel8.Statement.Select ( ppSelect )
24-- pretty
25import Text.PrettyPrint ( Doc, (<+>), ($$), text )
27-- text
28import qualified Data.Text as Text
29import Data.Text.Encoding ( encodeUtf8 )
32-- | Given a 'TableSchema' and 'Query', @createView@ runs a @CREATE VIEW@
33-- statement that will save the given query as a view. This can be useful if
34-- you want to share Rel8 queries with other applications.
35createView :: Selects names exprs
36 => TableSchema names -> Query exprs -> Hasql.Statement () ()
37createView schema query = Hasql.Statement bytes params decode prepare
38 where
39 bytes = encodeUtf8 (Text.pack sql)
40 params = Hasql.noParams
41 decode = Hasql.noResult
42 prepare = False
43 sql = show doc
44 doc = ppCreateView schema query
47ppCreateView :: Selects names exprs
48 => TableSchema names -> Query exprs -> Doc
49ppCreateView schema query =
50 text "CREATE VIEW" <+>
51 ppInto schema $$
52 text "AS" <+>
53 ppSelect query