gameServer/OfficialServer/DBInteraction.hs
author unc0rr
Mon, 23 Feb 2009 20:25:07 +0000
changeset 1833 e901ec5644b4
parent 1804 4e78ad846fb6
child 1834 71cb978dc85f
permissions -rw-r--r--
Add options for configuring database access

module OfficialServer.DBInteraction
(
	startDBConnection,
	DBQuery(HasRegistered, CheckPassword)
) where

import Database.HDBC
import Database.HDBC.MySQL
import System.IO
import Control.Concurrent
import Control.Exception
import Monad
------------------------
import CoreTypes

dbInteractionLoop queries dbConn = do
	q <- readChan queries
	case q of
		HasRegistered queryStr -> putStrLn queryStr
		CheckPassword queryStr -> putStrLn queryStr

	dbInteractionLoop queries dbConn

dbConnectionLoop serverInfo = do
	Control.Exception.handle (\e -> print e) $ handleSqlError $
		bracket
			(connectMySQL defaultMySQLConnectInfo {mysqlHost = dbHost serverInfo, mysqlDatabase = "hedge_main", mysqlUser = dbLogin serverInfo, mysqlPassword = dbPassword serverInfo })
			(disconnect)
			(dbInteractionLoop $ dbQueries serverInfo)

	threadDelay (15 * 10^6)
	dbConnectionLoop serverInfo

startDBConnection serverInfo =
	when (not . null $ dbHost serverInfo) ((forkIO $ dbConnectionLoop serverInfo) >> return ())