/
Main.hs
50 lines (42 loc) · 1.27 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{-#LANGUAGE TemplateHaskell #-}
{-#LANGUAGE QuasiQuotes #-}
module Main where
import Database.YeshQL
import qualified Database.HDBC as H
import Database.HDBC.MySQL
[yesh|
-- name:countClientSQL :: (Int)
SELECT count(id) FROM clients;
;;;
-- name:insertClientSQL
-- :client_name :: String
-- :subdomain :: String
INSERT INTO clients (name, subdomain) VALUES (:client_name, :subdomain);
|]
getConn :: IO Connection
getConn = do
connectMySQL defaultMySQLConnectInfo {
mysqlHost = "localhost",
mysqlDatabase = "hashmir_test",
mysqlUser = "hashmir_user",
mysqlPassword = "shei7AnganeihaeF",
mysqlUnixSocket = "/tmp/mysql.sock"
}
withConn :: (Connection -> IO b) -> IO b
withConn f = do
conn <- getConn
result <- f conn
H.commit conn
H.disconnect conn
return result
insertClient :: String -> String -> IO Integer
insertClient name subdomain = do
withConn $ do insertClientSQL name subdomain
countClient :: IO (Maybe Int)
countClient = do withConn countClientSQL
main :: IO ()
main = do
clientId <- insertClient "TestClient" "testclient"
putStrLn $ "New client's id is " ++ show clientId
Just clientCount <- countClient
putStrLn $ "There are " ++ show clientCount ++ " records."