-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
119 lines (98 loc) · 3.76 KB
/
Main.hs
File metadata and controls
119 lines (98 loc) · 3.76 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Control.Monad (forM_)
import Control.Monad.IO.Class (liftIO)
import Data.Text (Text)
import qualified Data.ByteString.Char8 as BS
import qualified DPella.Postgres as PG
import qualified DPella.MySQL as MS
import qualified DPella.SQLite as SQL
import Data.String (IsString)
-- Define the data structure for employees
data Employee = Employee
{ empName :: Text
, empAge :: Int
, empIsEmployed :: Bool
}
-- Sample employee data
employees :: [Employee]
employees =
[ Employee "Alice" 30 True
, Employee "Bob" 24 False
, Employee "Charlie" 45 True
, Employee "Diana" 29 True
]
sumQuery :: IsString a => a
sumQuery = "SELECT dpella_sample_random(SUM(CAST(age as FLOAT)),CAST(10 AS FLOAT)) FROM employees"
insertQuery :: IsString a => a
insertQuery = "INSERT INTO employees (name, age, is_employed) VALUES (?, ?, ?)"
createTableQuery :: (Semigroup a, IsString a) => a
createTableQuery =
"CREATE TABLE IF NOT EXISTS employees "
<> "(id SERIAL PRIMARY KEY, "
<> " name TEXT NOT NULL,"
<> " age INTEGER NOT NULL,"
<> " is_employed BOOLEAN NOT NULL)"
-- SQLite Example
runSQLiteExample :: IO ()
runSQLiteExample = SQL.runSQLite ":memory:" $ do
liftIO $ putStrLn "--- Running SQLite Example ---"
-- Create table
_ <- SQL.execute_ createTableQuery
liftIO $ putStrLn "SQLite table 'employees' created."
-- Insert data
forM_ employees $ \emp -> do
SQL.execute insertQuery
(empName emp, empAge emp, empIsEmployed emp)
liftIO $ putStrLn $ "Inserted " <> show (length employees) <> " records into SQLite."
-- Query sum of ages 4 times, to show randomness
forM_ [1 :: Int ..4] $ \_ -> do
[SQL.Only totalAge] :: [SQL.Only Double] <- SQL.query_ sumQuery
liftIO $ putStrLn $ "Sum of ages (with noise) (SQLite): " <> show totalAge
-- PostgreSQL Example
runPostgresExample :: IO ()
runPostgresExample = do
let connStr = "postgres://test:test@localhost:5432/test"
putStrLn "\n--- Running PostgreSQL Example ---"
putStrLn $ "Connecting to: " <> connStr
PG.runPostgres (BS.pack connStr) $ do
-- Drop and Create table
_ <- PG.execute_ "DROP TABLE IF EXISTS employees;"
_ <- PG.execute_ createTableQuery
liftIO $ putStrLn "PostgreSQL table 'employees' created."
-- Insert data
forM_ employees $ \emp -> do
PG.execute insertQuery
(empName emp, empAge emp, empIsEmployed emp)
liftIO $ putStrLn $ "Inserted " <> show (length employees) <> " records into PostgreSQL."
-- Query sum of ages 4 times, to show randomness
forM_ [1 :: Int ..4] $ \_ -> do
[PG.Only totalAge] :: [PG.Only Double] <- PG.query_ sumQuery
liftIO $ putStrLn $ "Sum of ages (PostgreSQL): " <> show totalAge
-- PostgreSQL Example
runMySQLExample :: IO ()
runMySQLExample = do
let connStr = "mysql://test:test@localhost:3306/test"
putStrLn "\n--- Running MySQL Example ---"
putStrLn $ "Connecting to: " <> connStr
MS.runMySQL (BS.pack connStr) $ do
-- Drop and Create table
_ <- MS.execute_ "DROP TABLE IF EXISTS employees;"
_ <- MS.execute_ createTableQuery
liftIO $ putStrLn "MySQL table 'employees' created."
-- Insert data
forM_ employees $ \emp -> do
MS.execute insertQuery
(empName emp, empAge emp, empIsEmployed emp)
liftIO $ putStrLn $ "Inserted " <> show (length employees) <> " records into MySQL."
-- Query sum of ages 4 times, to show randomness
forM_ [1 :: Int ..4] $ \_ -> do
[MS.Only totalAge] :: [MS.Only Double] <- MS.query_ sumQuery
liftIO $ putStrLn $ "Sum of ages (MySQL): " <> show totalAge
main :: IO ()
main = do
runSQLiteExample
runPostgresExample
runMySQLExample