gameServer/OfficialServer/DBInteraction.hs
author unc0rr
Mon, 23 Feb 2009 20:34:29 +0000
changeset 1834 71cb978dc85f
parent 1833 e901ec5644b4
child 1839 5dd4cb7fd7e5
permissions -rw-r--r--
Add working check for www account existance

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 Maybe
------------------------
import CoreTypes

dbInteractionLoop queries dbConn = do
	q <- readChan queries
	case q of
		HasRegistered name -> do
			statement <- prepare dbConn "SELECT uid FROM users WHERE name=?"
			execute statement [SqlString name]
			uid <- fetchRow statement
			finish statement
			putStrLn (show $ isJust uid)
		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 ())