Drop demo

This commit is contained in:
Nikita Volkov 2015-11-21 09:39:38 +03:00
parent 75df827903
commit f1fb618d0c
2 changed files with 0 additions and 149 deletions

View File

@ -1,121 +0,0 @@
module Main where
import BasePrelude hiding (assert, isRight, isLeft)
import Data.Text (Text)
import Data.ByteString (ByteString)
import Data.Functor.Contravariant
import Data.Default.Class
import Contravariant.Extras
import qualified Hasql as H
import qualified Hasql.Serialization as HS
import qualified Hasql.Deserialization as HD
main =
do
connectionEither <- H.connect settings
case connectionEither of
Left e -> print e
Right connection -> do
result <- H.query connection sumQuery (1, 2)
print result
where
settings =
H.ParametricSettings "localhost" 5432 "postgres" "" "postgres"
sumQuery =
(,,,) template serializer deserializer True
where
template =
"SELECT $1 + $2"
serializer =
contramap fst (HS.value HS.int8) <>
contramap snd (HS.value HS.int8)
deserializer =
HD.singleRow (HD.value HD.int8)
-- * Model
-------------------------
data Account =
Account {
email :: Text,
password :: ByteString,
firstName :: Text,
lastName :: Text
}
-- * Queries
-------------------------
updateMenu :: H.Query (Text, Int64) Int64
updateMenu =
(,,,) template serializer deserializer True
where
template =
"UPDATE menu SET title = $1 WHERE id = $2"
serializer =
contrazip2 (HS.value HS.text)
(HS.value HS.int8)
deserializer =
HD.rowsAffected
accountByEmail :: H.Query Text (Maybe (Int64, Account))
accountByEmail =
(,,,) template serializer deserializer True
where
template =
"SELECT id, email, password, first_name, last_name \
\FROM account WHERE email = $1"
serializer =
HS.value HS.text
deserializer =
HD.maybeRow (identifiedDeserializer accountDeserializer)
insertAccount :: H.Query Account Int64
insertAccount =
(,,,) template serializer deserializer True
where
template =
"INSERT INTO account (email, password, first_name, last_name) \
\VALUES ($1, $2, $3, $4) \
\RETURNING id"
serializer =
accountSerializer
deserializer =
HD.singleRow idDeserializer
-- * Deserializers
-------------------------
idDeserializer :: HD.Row Int64
idDeserializer =
HD.value HD.int8
accountDeserializer :: HD.Row Account
accountDeserializer =
liftM4 Account (HD.value def) (HD.value def) (HD.value def) (HD.value def)
identifiedDeserializer :: HD.Row a -> HD.Row (Int64, a)
identifiedDeserializer aDeserializer =
liftM2 (,) idDeserializer aDeserializer
-- * Serializers
-------------------------
accountSerializer :: HS.Params Account
accountSerializer =
contramap (\(Account a b c d) -> (a, b, c, d)) $
contrazip4 (HS.value HS.text)
(HS.value HS.bytea)
(HS.value HS.text)
(HS.value HS.text)

View File

@ -142,34 +142,6 @@ test-suite tasty
base
executable demo
hs-source-dirs:
demo
main-is:
Main.hs
ghc-options:
-O2
-threaded
"-with-rtsopts=-N"
ghc-prof-options:
-O2
-threaded
-fprof-auto
"-with-rtsopts=-N -p -s -h -i0.1"
default-extensions:
Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples
default-language:
Haskell2010
build-depends:
data-default-class,
contravariant-extras,
contravariant,
hasql,
text,
bytestring,
base-prelude
benchmark benchmark
type:
exitcode-stdio-1.0