Parametrized view
Lets imagine we have database with parametrized view exampleParametrizedView
CREATE VIEW exampleParametrizedView
AS SELECT *
FROM generateRandom('a1 Int32, a2 Int32, a3 String', 1, 10, 2)
WHERE (a1 > {a1MoreThan:Int32}) AND (a1 < {a1LessThan:Int32})
LIMIT 5;
To perform a SELECT from such view you can use this snippet
{-# LANGUAGE
DataKinds
, OverloadedStrings
#-}
import ClickHaskell.Client (ReadableFrom, ChCredential(..), selectFromView)
import ClickHaskell.Tables (parameter, View, Parameter, Column)
import ClickHaskell.DbTypes (ChInt32, ChString)
import Data.Int (Int32)
import GHC.Generics (Generic)
import Network.HTTP.Client (defaultManagerSettings, newManager)
main :: IO ()
= do
main let credential = MkChCredential
= "default"
{ chLogin = ""
, chPass = "http://localhost:8123"
, chUrl = "default"
, chDatabase
}<- newManager defaultManagerSettings
manager mapM_ print
=<<
selectFromView@ExampleView
@ExampleViewRecord
manager
credential@"a1MoreThan" @ChInt32 ((-100_000) :: Int32)
( parameter . parameter @"a1LessThan" @ChInt32 ((100_000) :: Int32)
)
type ExampleView =
View
"exampleParametrizedView"
Column "a1" ChInt32
'[ Column "a2" ChInt32
, Column "a3" ChString
,
]Parameter "a1MoreThan" ChInt32
'[ Parameter "a1LessThan" ChInt32
,
]
newtype ExampleViewRecord = MkExampleViewRecord
a1 :: Int32
{
}deriving (Generic, Show)
instance ReadableFrom ExampleView ExampleViewRecord
To try out this example in repl: setup development environment and then run
cabal run example-parametrized-view
# output
MkExampleViewRecord {a1 = -23652}
MkExampleViewRecord {a1 = -82405}
MkExampleViewRecord {a1 = 92847}
MkExampleViewRecord {a1 = 6575}
MkExampleViewRecord {a1 = -80663}