1804
|
1 |
module OfficialServer.DBInteraction
|
|
2 |
(
|
|
3 |
startDBConnection,
|
|
4 |
DBQuery(HasRegistered, CheckPassword)
|
|
5 |
) where
|
|
6 |
|
|
7 |
import Database.HDBC
|
|
8 |
import Database.HDBC.MySQL
|
|
9 |
|
|
10 |
import System.IO
|
|
11 |
import Control.Concurrent
|
|
12 |
import Control.Concurrent.STM
|
|
13 |
import Control.Exception
|
|
14 |
|
|
15 |
data DBQuery =
|
|
16 |
HasRegistered String
|
|
17 |
| CheckPassword String
|
|
18 |
|
|
19 |
dbInteractionLoop queries dbConn = do
|
|
20 |
q <- atomically $ readTChan queries
|
|
21 |
case q of
|
|
22 |
HasRegistered queryStr -> putStrLn queryStr
|
|
23 |
CheckPassword queryStr -> putStrLn queryStr
|
|
24 |
|
|
25 |
dbInteractionLoop queries dbConn
|
|
26 |
|
|
27 |
dbConnectionLoop queries = do
|
|
28 |
Control.Exception.handle (\e -> print e) $ handleSqlError $
|
|
29 |
bracket
|
|
30 |
(connectMySQL defaultMySQLConnectInfo { mysqlHost = "192.168.50.5", mysqlDatabase = "glpi" })
|
|
31 |
(disconnect)
|
|
32 |
(dbInteractionLoop queries)
|
|
33 |
|
|
34 |
threadDelay (15 * 10^6)
|
|
35 |
dbConnectionLoop queries
|
|
36 |
|
|
37 |
startDBConnection queries = forkIO $ dbConnectionLoop queries
|