Permalink
Browse files

Extract Data logic from Main

  • Loading branch information...
adomokos committed Dec 5, 2017
1 parent 6c6f77b commit 275d45ad1f6abe1f6f5eccb0e67c552543c96c90
Showing with 48 additions and 51 deletions.
  1. +3 −43 app/Main.hs
  2. +1 −1 hashmir.cabal
  3. +1 −1 package.yaml
  4. +43 −0 src/Hashmir/Data.hs
  5. +0 −6 src/Lib.hs
View
@@ -1,50 +1,10 @@
{-#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 =
withConn $ insertClientSQL name subdomain
countClient :: IO (Maybe Int)
countClient = withConn countClientSQL
import qualified Hashmir.Data as D
main :: IO ()
main = do
clientId <- insertClient "TestClient" "testclient"
clientId <- D.insertClient "TestClient" "testclient"
putStrLn $ "New client's id is " ++ show clientId
Just clientCount <- countClient
Just clientCount <- D.countClient
putStrLn $ "There are " ++ show clientCount ++ " records."
View
@@ -23,7 +23,7 @@ library
, HDBC
, HDBC-mysql
exposed-modules:
Lib
Hashmir.Data
other-modules:
Paths_hashmir
default-language: Haskell2010
View
@@ -17,7 +17,7 @@ dependencies:
library:
source-dirs: src/
exposed-modules:
- Lib
- Hashmir.Data
executables:
hashmir-exe:
View
@@ -0,0 +1,43 @@
{-#LANGUAGE TemplateHaskell #-}
{-#LANGUAGE QuasiQuotes #-}
module Hashmir.Data 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 =
withConn $ insertClientSQL name subdomain
countClient :: IO (Maybe Int)
countClient = withConn countClientSQL
View

This file was deleted.

Oops, something went wrong.

0 comments on commit 275d45a

Please sign in to comment.