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 |
import System.IO
|
|
10 |
import Control.Concurrent
|
|
11 |
import Control.Exception
|
1833
|
12 |
import Monad
|
|
13 |
------------------------
|
|
14 |
import CoreTypes
|
1804
|
15 |
|
|
16 |
dbInteractionLoop queries dbConn = do
|
1833
|
17 |
q <- readChan queries
|
1804
|
18 |
case q of
|
|
19 |
HasRegistered queryStr -> putStrLn queryStr
|
|
20 |
CheckPassword queryStr -> putStrLn queryStr
|
|
21 |
|
|
22 |
dbInteractionLoop queries dbConn
|
|
23 |
|
1833
|
24 |
dbConnectionLoop serverInfo = do
|
1804
|
25 |
Control.Exception.handle (\e -> print e) $ handleSqlError $
|
|
26 |
bracket
|
1833
|
27 |
(connectMySQL defaultMySQLConnectInfo {mysqlHost = dbHost serverInfo, mysqlDatabase = "hedge_main", mysqlUser = dbLogin serverInfo, mysqlPassword = dbPassword serverInfo })
|
1804
|
28 |
(disconnect)
|
1833
|
29 |
(dbInteractionLoop $ dbQueries serverInfo)
|
1804
|
30 |
|
|
31 |
threadDelay (15 * 10^6)
|
1833
|
32 |
dbConnectionLoop serverInfo
|
1804
|
33 |
|
1833
|
34 |
startDBConnection serverInfo =
|
|
35 |
when (not . null $ dbHost serverInfo) ((forkIO $ dbConnectionLoop serverInfo) >> return ())
|