Skip to content

Commit c54f6de

Browse files
committed
Merge branch 'pgsql'
Conflicts: JCU.cabal src/Application.hs
2 parents 6426fd0 + fac9a7a commit c54f6de

File tree

2 files changed

+65
-43
lines changed

2 files changed

+65
-43
lines changed

JCU.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Executable jcu
5454
MonadCatchIO-transformers >= 0.2.1 && < 0.3,
5555
mtl >= 2.0,
5656
NanoProlog >= 0.3,
57+
resource-pool-catchio >= 0.2 && < 0.3,
5758
snap >= 0.6,
5859
snap-core >= 0.6,
5960
snap-server >= 0.6,

src/Application.hs

+64-43
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import Data.ListLike (CharString(..))
2222
import Data.Map (Map)
2323
import qualified Data.Map as DM
2424
import Data.Maybe
25+
import Data.Pool
2526
import Data.String
2627
import Data.Text (Text)
2728
import qualified Data.Text as DT
2829
import qualified Data.Text.Encoding as DT
29-
import Database.HDBC.PostgreSQL
30+
import qualified Database.HDBC as HDBC
31+
import Database.HDBC.MySQL
3032
import JCU.Prolog
3133
import JCU.Templates
3234
import JCU.Types
@@ -37,7 +39,7 @@ import Snap.Core
3739
import Snap.Snaplet
3840
import Snap.Snaplet.Auth
3941
import Snap.Snaplet.Auth.Backends.Hdbc
40-
import Snap.Snaplet.Hdbc
42+
{-import Snap.Snaplet.Hdbc-}
4143
import Snap.Snaplet.Session
4244
import Snap.Snaplet.Session.Backends.CookieSession
4345
import Snap.Util.FileServe
@@ -54,18 +56,15 @@ import qualified Database.HDBC as HDBC
5456
data App = App
5557
{ _authLens :: Snaplet (AuthManager App)
5658
, _sessLens :: Snaplet SessionManager
57-
, _dbLens :: Snaplet (HdbcSnaplet Connection IO)
59+
, pgconn :: Connection
5860
}
5961

6062
makeLens ''App
6163

6264
type AppHandler = Handler App App
6365

64-
instance HasHdbc (Handler b App) Connection IO where
65-
getHdbcState = with dbLens get
66-
67-
-- instance Control.Monad.Trans.Control.MonadBaseControl IO (Handler b App) where
68-
66+
{-instance HasHdbc (Handler b App) Connection IO where-}
67+
{-getHdbcState = with dbLens get-}
6968

7069
jcu :: SnapletInit App App
7170
jcu = makeSnaplet "jcu" "Prolog proof tree practice application" Nothing $ do
@@ -86,13 +85,17 @@ jcu = makeSnaplet "jcu" "Prolog proof tree practice application" Nothing $ do
8685
]
8786
_sesslens' <- nestSnaplet "session" sessLens $ initCookieSessionManager
8887
"config/site_key.txt" "_session" Nothing
89-
let sqli = do connString <- readFile "config/connection_string.conf"
90-
c <- connectPostgreSQL connString
91-
return c
92-
_dblens' <- nestSnaplet "hdbc" dbLens $ hdbcInit sqli
88+
pass <- readFile "config/connection_string.conf"
89+
let pgsql = connectMySql defaultMySQLConnectInfo {
90+
mysqlPassword = pass,
91+
mysqlDatabase = "jcu"
92+
} -- connectPostgreSQL' =<< readFile "config/connection_string.conf"
93+
pg <- liftIO $ pgsql
94+
-- pool <- liftIO $ createPool pgsql HDBC.disconnect 1 500 1
95+
{-_dblens' <- nestSnaplet "hdbc" dbLens $ hdbcInit pgsql-}
9396
_authlens' <- nestSnaplet "auth" authLens $ initHdbcAuthManager
94-
defAuthSettings sessLens sqli defAuthTable defQueries
95-
return $ App _authlens' _sesslens' _dblens'
97+
defAuthSettings sessLens pgsql defAuthTable defQueries
98+
return $ App _authlens' _sesslens' pg
9699

97100

98101
------------------------------------------------------------------------------
@@ -182,7 +185,9 @@ deleteStoredRuleH = restrict forbiddenH $ do
182185
mrid <- getParam "id"
183186
case mrid of
184187
Nothing -> return ()
185-
Just x -> deleteRule x -- TODO: Take user ID into account. we don't want people deleting other users's rules
188+
Just x -> do
189+
uid <- getUserId
190+
deleteRule uid x
186191

187192
addStoredRuleH :: AppHandler ()
188193
addStoredRuleH = restrict forbiddenH $ do
@@ -198,8 +203,7 @@ loadExampleH = restrict forbiddenH $ do
198203
uid <- getUserId
199204
deleteUserRules uid
200205
mapM_ (insertRule uid) exampleData
201-
-- commitSession
202-
-- redirect "/"
206+
redirect "/"
203207

204208

205209
getUserId :: AppHandler UserId
@@ -347,36 +351,53 @@ voidM m = do
347351
return ()
348352

349353
-- TODO: This is just a workaround....
350-
q :: HasHdbc m c s => String -> [SqlValue] -> m ()
351-
q qry vals = withTransaction $ \conn' -> do
352-
stmt <- HDBC.prepare conn' qry
353-
_ <- HDBC.execute stmt vals
354-
return ()
355-
356-
insertRule :: HasHdbc m c s => UserId -> Rule -> m (Maybe Int)
357-
insertRule uid rl = let sqlVals = [toSql $ unUid uid, toSql $ show rl] in do
358-
q "INSERT INTO rules (uid, rule_order, rule) VALUES (?, 1, ?)" sqlVals
359-
rws <- query "SELECT rid FROM rules WHERE uid = ? AND rule = ? ORDER BY rid DESC"
360-
sqlVals
361-
return $ case rws of
362-
[] -> Nothing
363-
(x:_) -> Just $ fromSql $ x DM.! "rid"
364-
365-
deleteRule :: HasHdbc m c s => ByteString -> m ()
366-
deleteRule rid = q "DELETE FROM rules WHERE rid = ?" [toSql rid]
367-
368-
getStoredRules :: HasHdbc m c s => UserId -> m [DBRule]
354+
q :: String -> [HDBC.SqlValue] -> AppHandler ()
355+
q qry vals = do
356+
c <- gets pgconn
357+
c' <- liftIO $ HDBC.clone c
358+
liftIO $ HDBC.withTransaction c' $ \conn' -> do
359+
stmt <- HDBC.prepare conn' qry
360+
voidM $ HDBC.execute stmt vals
361+
HDBC.commit c'
362+
return ()
363+
364+
insertRule :: UserId -> Rule -> AppHandler (Maybe Int)
365+
insertRule uid rl =
366+
let sqlVals = [HDBC.toSql $ unUid uid, HDBC.toSql $ show rl]
367+
in do
368+
q "INSERT INTO rules (uid, rule_order, rule) VALUES (?, 1, ?)" sqlVals
369+
c <- gets pgconn
370+
c' <- liftIO $ HDBC.clone c
371+
rws <- liftIO $ do
372+
stmt <- HDBC.prepare c' "SELECT rid FROM rules WHERE uid = ? AND rule = ? ORDER BY rid DESC"
373+
voidM $ HDBC.execute stmt sqlVals
374+
HDBC.fetchAllRowsMap' stmt
375+
return $ case rws of
376+
[] -> Nothing
377+
(x:_) -> Just $ HDBC.fromSql $ x DM.! "rid"
378+
379+
deleteRule :: UserId -> ByteString -> AppHandler ()
380+
deleteRule uid rid = q "DELETE FROM rules WHERE rid = ? AND uid = ?"
381+
[HDBC.toSql rid, HDBC.toSql uid]
382+
383+
getStoredRules :: UserId -> AppHandler [DBRule]
369384
getStoredRules uid = do
370-
rs <- query "SELECT rid, rule_order, rule FROM rules WHERE uid = ?"
371-
[toSql uid]
372-
return $ map convRow rs
373-
where convRow :: Map String SqlValue -> DBRule
385+
c <- gets pgconn
386+
c' <- liftIO $ HDBC.clone c
387+
rws <- liftIO $ do
388+
stmt <- HDBC.prepare c' "SELECT rid, rule_order, rule FROM rules WHERE uid = ?"
389+
voidM $ HDBC.execute stmt [HDBC.toSql uid]
390+
HDBC.fetchAllRowsMap' stmt
391+
{-rs <- query "SELECT rid, rule_order, rule FROM rules WHERE uid = ?"-}
392+
{-[toSql uid]-}
393+
return $ map convRow rws
394+
where convRow :: Map String HDBC.SqlValue -> DBRule
374395
convRow mp =
375-
let rdSql k = fromSql $ mp DM.! k
396+
let rdSql k = HDBC.fromSql $ mp DM.! k
376397
in DBRule (rdSql "rid")
377398
(rdSql "rule_order")
378399
(fst . startParse pRule $ CS (rdSql "rule"))
379400

380-
deleteUserRules :: HasHdbc m c s => UserId -> m ()
381-
deleteUserRules uid = q "DELETE FROM rules WHERE uid = ?" [toSql uid]
401+
deleteUserRules :: UserId -> AppHandler ()
402+
deleteUserRules uid = q "DELETE FROM rules WHERE uid = ?" [HDBC.toSql uid]
382403

0 commit comments

Comments
 (0)