Skip to content

Commit

Permalink
added transactions support. exported nullable.
Browse files Browse the repository at this point in the history
darcs-hash:20040222110547-cca28-be90d3f8b42c685fd12261dd209c253bda81dd1a.gz
  • Loading branch information
d00bring committed Feb 22, 2004
1 parent df48431 commit 483c635
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Database support:
HaskellDB only works with MySQL version >= 4.1 since earlier
versions don't support for nested subqueries.

MySQL only supports transactions on transaction-safe table types,
such as InnoDB and BDB. The default table type, MyISAM does not
support transactions. See the MySQL manual for more information.

- PostgreSQL

Should work.
4 changes: 2 additions & 2 deletions src/Database/HaskellDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Database.HaskellDB
, (.*.) , (./.), (.%.), (.+.), (.-.), (.++.)
, _not, like, cat
, isNull, notNull
, constant
, constant, nullable

, count, _sum, _max, _min, avg
, stddev, stddevP, variance, varianceP
Expand All @@ -41,7 +41,7 @@ module Database.HaskellDB
, (!.)
, query, lazyQuery, strictQuery
, insert, delete, update, insertQuery
, tables, describe
, tables, describe, transaction

, showQ, showOpt, showSql

Expand Down
11 changes: 10 additions & 1 deletion src/Database/HaskellDB/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Database.HaskellDB.Database (
-- * Function declarations
, query, lazyQuery, strictQuery
, insert, delete, update, insertQuery
, tables, describe
, tables, describe, transaction
) where

import Database.HaskellDB.HDBRec
Expand Down Expand Up @@ -63,6 +63,7 @@ data Database db row
, dbUpdate :: db -> TableName -> [PrimExpr] -> Assoc -> IO ()
, dbTables :: db -> IO [TableName]
, dbDescribe :: db -> TableName -> IO [(Attribute,FieldDef)]
, dbTransaction :: forall a. db -> IO a -> IO a
, database :: db
}

Expand Down Expand Up @@ -158,3 +159,11 @@ describe :: Database db row -- ^ Database
-> TableName -- ^ Name of the tables whose columns are to be listed
-> IO [(Attribute,FieldDef)] -- ^ Name and type info for each column
describe db = dbInvoke dbDescribe db


-- | Performs some database action in a transaction. If no exception is thrown,
-- the changes are committed.
transaction :: Database db row -- ^ Database
-> IO a -- ^ Action to run
-> IO a
transaction db = dbInvoke dbTransaction db
5 changes: 5 additions & 0 deletions src/Database/HaskellDB/HSQL/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ newHSQL connection
dbUpdate = hsqlUpdate,
dbTables = hsqlTables,
dbDescribe = hsqlDescribe,
dbTransaction = hsqlTransaction,
database = connection
}

Expand Down Expand Up @@ -126,6 +127,10 @@ hsqlDescribe conn table = liftM (map toFieldDef) (HSQL.describe conn table)
where
toFieldDef (name,sqlType,nullable) = (name,(toFieldType sqlType, nullable))

-- | HSQL implementation of 'Database.dbTransaction'.
hsqlTransaction :: Connection -> IO a -> IO a
hsqlTransaction conn action = inTransaction conn (\_ -> action)

toFieldType :: SqlType -> FieldType
toFieldType (SqlDecimal _ _) = DoubleT
toFieldType (SqlNumeric _ _) = DoubleT
Expand Down
8 changes: 4 additions & 4 deletions src/Database/HaskellDB/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Database.HaskellDB.Query (
, union, intersect, divide, minus
, _not, like, cat
, isNull, notNull
, constant
, constant, nullable
, count, _sum, _max, _min, avg
, stddev, stddevP, variance, varianceP
, asc, desc, order
Expand Down Expand Up @@ -318,10 +318,10 @@ instance ShowConstant a => ShowConstant (Maybe a) where
constant :: ShowConstant a => a -> Expr a
constant x = Expr (ConstExpr (showConstant x))

-- This function is not exported and is not used anywhere..
-- Either export it or delete it :)
-- | Turn constant data into a nullable expression.
-- Same as @constant . Just@
nullable :: ShowConstant a => a -> Expr (Maybe a)
nullable x = Expr (ConstExpr (showConstant x))
nullable x = constant (Just x)


-----------------------------------------------------------
Expand Down

0 comments on commit 483c635

Please sign in to comment.