insertInto
Lets imagine we want insert data into table
CREATE TABLE exampleWriteRead
(
`a1` Int64,
`a2` String,
`a3` DateTime,
`a4` UUID,
)= MergeTree
ENGINE PARTITION BY ()
ORDER BY ();
There are a simple “How to do” example:
{-# LANGUAGE
DataKinds
, DeriveAnyClass
, OverloadedStrings
#-}
module Writing where
import ClickHaskell
WritableInto, insertInto
( ChCredential(..), openNativeConnection
, Table, Column
,
)import ClickHaskell.DbTypes
import Data.ByteString (ByteString)
import Data.Int (Int64)
import Data.Word (Word32, Word64)
import GHC.Generics (Generic)
main :: IO ()
= do
main let credentials = MkChCredential
= "default"
{ chLogin = ""
, chPass = "localhost"
, chHost = "default"
, chDatabase = "9000"
, chPort
}<- openNativeConnection credentials
connection
insertInto@ExampleTable
@ExampleData
connectionMkExampleData
[ = 42
{ a1 = "text"
, a2 = toChType (0 :: Word64)
, a4 = 42
, a3
}
]
{- Before GHC 9.8 its better to use standalone deriving
since type errors occures exact on deriving declaration.
-}
deriving instance WritableInto ExampleTable ExampleData
type ExampleTable =
Table
"exampleWriteRead"
Column "a1" ChInt64
'[ Column "a2" ChString
, Column "a3" ChDateTime
, Column "a4" ChUUID
,
]
data ExampleData = MkExampleData
a1 :: Int64
{ a2 :: ByteString
, a4 :: ChUUID
, a3 :: Word32
,
}deriving (Generic, Show)