takusen-oracle 0.9.1 → 0.9.2
raw patch · 12 files changed
+670/−774 lines, 12 filessetup-changed
Files
- Database/Oracle/Test/Enumerator.hs +17/−11
- Database/Test/Enumerator.hs +421/−0
- Database/Test/Enumerator.lhs +0/−480
- Database/Test/Performance.hs +100/−0
- Database/Test/Performance.lhs +0/−99
- Database/Test/Util.hs +109/−0
- Database/Test/Util.lhs +0/−129
- Foreign/C/Test/UTF8.hs +7/−7
- Foreign/C/UTF8.hs +1/−8
- Main.hs +9/−33
- Setup.hs +5/−6
- takusen-oracle.cabal +1/−1
Database/Oracle/Test/Enumerator.hs view
@@ -42,7 +42,12 @@ {-# LANGUAGE OverlappingInstances #-} - +{-# LANGUAGE UndecidableInstances #-} +{-# LANGUAGE TypeSynonymInstances #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE DeriveDataTypeable #-} +{-# LANGUAGE ScopedTypeVariables #-} module Database.Oracle.Test.Enumerator (runTest) where import qualified Database.Oracle.Test.OCIFunctions as Low @@ -71,7 +76,7 @@ runPerformanceTests = do makeFixture execDrop execDDL_ beginTransaction RepeatableRead - runTestTT "Oracle performance tests" (map (flip catchDB reportRethrow) + runTestTT "Oracle performance tests" (map (`catchDB` reportRethrow) [ timedSelect (prefetch 1000 sqlRows2Power20 []) 30 (2^20) , timedSelect (prefetch 1 sqlRows2Power17 []) 60 (2^17) , timedSelect (prefetch 1000 sqlRows2Power17 []) 6 (2^17) @@ -268,11 +273,12 @@ then assertFailure "selectNestedMultiResultSet: inner value not 101 or 102" else return () result' (i:acc) - withTransaction RepeatableRead $ do - withPreparedStatement (prepareQuery (sql q)) $ \pstmt -> do - withBoundStatement pstmt [] $ \bstmt -> do - rs <- doQuery bstmt iterMain [] - return () + in + withTransaction RepeatableRead $ do + withPreparedStatement (prepareQuery (sql q)) $ \pstmt -> do + withBoundStatement pstmt [] $ \bstmt -> do + rs <- doQuery bstmt iterMain [] + return () selectNestedMultiResultSet2 :: OracleFunctions -> DBM mark Session () @@ -297,10 +303,10 @@ assertBool "processInner2" (i < inner) result' (i:acc) withTransaction RepeatableRead $ do - withPreparedStatement (prepareQuery (sql q)) $ \pstmt -> do - withBoundStatement pstmt [] $ \bstmt -> do - rs <- doQuery bstmt iterMain [] - assertEqual "selectNestedMultiResultSet" [9,8,7,6,5,4,3,2,1] (map fst rs) + withPreparedStatement (prepareQuery (sql q)) $ \pstmt -> do + withBoundStatement pstmt [] $ \bstmt -> do + rs <- doQuery bstmt iterMain [] + assertEqual "selectNestedMultiResultSet" [9,8,7,6,5,4,3,2,1] (map fst rs) --print_ "" selectNestedMultiResultSet3 :: OracleFunctions -> DBM mark Session ()
+ Database/Test/Enumerator.hs view
@@ -0,0 +1,421 @@+{- +| +Module : Database.Test.Enumerator +Copyright : (c) 2004 Oleg Kiselyov, Alistair Bayley +License : BSD-style +Maintainer : oleg@pobox.com, alistair@abayley.org +Stability : experimental +Portability : non-portable + +Simple test harness. Demonstrates possible usage. + +Tests in this module are organised in groups of three/four functions: + sqlXXX + iterXXX + expectXXX + actionXXX + +These have to be tied together by a function which uses backend-specific +functions and types. See the various backend-specific test modules for examples. + +-} + +{-# LANGUAGE OverlappingInstances, ScopedTypeVariables #-} + +module Database.Test.Enumerator where + +import Database.Enumerator +import Database.Util +import Data.Time +import System.Time +import Data.Int +-- import Control.Exception.MonadIO +-- import Control.Monad.Trans (liftIO) +import Test.MiniUnit + +testTable :: String +testTable = "takusen_test" + +sqlDropDual :: String +sqlDropDual = "drop table tdual" +sqlCreateDual = "create table tdual (dummy varchar(1) primary key)" +sqlInsertDual = "insert into tdual values ('X')" +sqlDropTest = "drop table " ++ testTable +sqlCreateTest = "create table " ++ testTable ++ " (id integer, v varchar(250))" + +sqlInsertTest1 = "insert into " ++ testTable ++ " (id, v) values (1, '2')" +sqlInsertTest2 = "insert into " ++ testTable ++ " (id, v) values (2, '2')" +sqlInsertTest3 = "insert into " ++ testTable ++ " (id, v) values (3, '3')" +sqlInsertTest4 = "insert into " ++ testTable ++ " (id, v) values (4, '4')" + + +reportError sql (DBFatal (ssc, sssc) e m) = do + putStrLn ("FATAL: " ++ ssc ++ sssc ++ " - " ++ m) + putStrLn (" " ++ sql) +reportError sql (DBError (ssc, sssc) e m) = do + putStrLn ("ERROR: " ++ ssc ++ sssc ++ " - " ++ m) + putStrLn (" " ++ sql) +reportError sql (DBUnexpectedNull r c) = + putStrLn $ "Unexpected null in row " ++ show r ++ ", column " ++ show c ++ "." +reportError sql (DBNoData) = putStrLn "Fetch: no more data." + +----------------------------------------------------------- + +class DBLiteralValue a where + literalDate :: a -> Int64 -> String + literalInt :: a -> Int -> String + literalInt64 :: a -> Int64 -> String + literalFloat :: a -> Float -> String + literalDouble :: a -> Double -> String + -- default methods + literalInt _ = show + literalInt64 _ = show + literalFloat _ = show + literalDouble _ = show + + +data OracleFunctions = OracleFunctions + +instance DBLiteralValue OracleFunctions where + literalDate _ = dateOracle + +dateOracle :: Int64 -> String +dateOracle i + | i == 0 = "to_date(null)" + | i > 0 = "to_date('" ++ zeroPad 14 i ++ "', 'yyyymmddhh24miss')" + | i < 0 = "to_date('" ++ zeroPad 14 i ++ "', 'syyyymmddhh24miss')" + +dateISO :: Int64 -> String +dateISO i = + let + (year, month, day, hour, minute, second) = int64ToDateParts i + zp = zeroPad + in + if i == 0 then "null" + else zp 4 year ++ "-" ++ zp 2 month ++ "-" ++ zp 2 day + ++ "T" ++ zp 2 hour ++ ":" ++ zp 2 minute ++ ":" ++ zp 2 second + + + +execDDL_ s = catchDB (execDDL s) (reportRethrowMsg ("sql: " ++ s ++ "\n")) +execDML_ s = execDDL_ s + +{- +Use execDrop when the DDL is likely to raise an error. +Note that PostgreSQL + ODBC seems to require you commit or rollback the DDL; +if you don't then the next statement will fail with a 25P02 +("in failed SQL transaction") +I guess that's a result of PostgreSQL's transactional DDL feature. +-} + +execDrop s = catchDB (withTransaction Serializable (execDDL s)) (\e -> return ()) + +makeFixture doDrop doDDL = flip catchDB (reportRethrowMsg "makeFixture: ") $ do + doDrop sqlDropDual + doDrop sqlDropTest + doDDL sqlCreateDual + beginTransaction ReadCommitted + doDDL sqlInsertDual + commit + doDDL sqlCreateTest + withTransaction Serialisable $ do + doDDL sqlInsertTest1 + doDDL sqlInsertTest2 + doDDL sqlInsertTest3 + +destroyFixture execDDL_ = flip catchDB (reportRethrowMsg "destroyFixture: ") $ do + execDDL_ sqlDropDual + execDDL_ sqlDropTest + catchDB commit (const (return ())) + +selectTest query iter expect = do + actual <- doQuery query iter [] + assertEqual query expect actual + + +{- +----------------------------------------------------------- + +This is used in a few tests... + +-} + +sqlSingleValue = "select ? from tdual" + +sqlNoRows = "select dummy from tdual where dummy = 'a' or dummy = '2' " +iterNoRows (c1::String) acc = result $ c1:acc +expectNoRows = []::[String] + +sqlTermEarly = "select 'hello1' from tdual union select 'hello2' from tdual union select 'hello3' from tdual order by 1" +iterTermEarly c1 acc = if c1 == "hello2" + then return (Left (c1:acc)) + else result (c1:acc) +expectTermEarly = ["hello2", "hello1"] + +sqlFloatsAndInts fns = "select " ++ literalDouble fns 4841.3403490431 ++ ", " + ++ literalInt fns (-22340234) ++ " from tdual union select 33311.32332, 23789234 from tdual order by 1" +--iterFloatsAndInts :: (Monad m) => Double -> Int -> IterAct m [(Double, Int)] +iterFloatsAndInts (c1::Double) (c2::Int) acc = result $ (c1, c2):acc +expectFloatsAndInts :: [(Double, Int)] +expectFloatsAndInts = [ (33311.32332, 23789234) , (4841.3403490431, -22340234) ] + +sqlNullString = "select 'hello1', 'hello2', null from tdual" +iterNullString :: (Monad m) => String -> String -> Maybe String + -> IterAct m [(String, String, Maybe String)] +iterNullString c1 c2 c3 acc = result $ (c1, c2, c3):acc +expectNullString = [ ("hello1", "hello2", Nothing) ] + +-- Access can't handle SQL comments (-- or /* */). + +sqlEmptyString = "select 'hello1', 'Oracle always fails this test', '' from tdual" +iterEmptyString :: (Monad m) => String -> String -> Maybe String + -> IterAct m [(String, String, Maybe String)] +iterEmptyString c1 c2 c3 acc = result $ (c1, c2, c3):acc +expectEmptyString = [ ("hello1", "Oracle always fails this test", Just "") ] + +sqlUnhandledNull = "select 'hello1', 'hello2', null from tdual" +iterUnhandledNull :: (Monad m) => String -> String -> UTCTime + -> IterAct m [(String, String, UTCTime)] +iterUnhandledNull c1 c2 c3 acc = result $ (c1, c2, c3):acc +expectUnhandledNull = [] + +sqlNullDate fns = "select 'hello1', 'hello2', " ++ literalDate fns 0 ++ " from tdual" +iterNullDate :: (Monad m) => String -> String -> Maybe UTCTime + -> IterAct m [(String, String, UTCTime)] +iterNullDate c1 c2 c3 acc = result $ (c1, c2, ifNull c3 (int64ToUTCTime 10101000000)):acc +expectNullDate = [ ("hello1", "hello2", int64ToUTCTime 10101000000) ] + +sqlDate fns = "select " ++ literalDate fns 20041224235959 ++ " from tdual" +iterDate :: (Monad m) => UTCTime -> IterAct m [UTCTime] +iterDate c1 acc = result $ c1:acc +expectDate = [ int64ToUTCTime 20041224235959 ] + +iterCalDate :: (Monad m) => CalendarTime -> IterAct m [CalendarTime] +iterCalDate c1 acc = result $ c1:acc +expectCalDate = [ int64ToCalTime 20041224235959 ] + + +-- These are the Oracle date boundary cases. + +sqlBoundaryDates fns = + "select " ++ literalDate fns 99991231000000 ++ " from tdual" + ++ " union select " ++ literalDate fns 10101000000 ++ " from tdual" + ++ " union select " ++ literalDate fns (-10101000000) ++ " from tdual" + ++ " union select " ++ literalDate fns (-47120101000000) ++ " from tdual" + ++ " order by 1 desc" +iterBoundaryDates :: (Monad m) => UTCTime -> IterAct m [UTCTime] +iterBoundaryDates c1 acc = result $ c1:acc +expectBoundaryDates = + [ int64ToUTCTime (-47120101000000) + , int64ToUTCTime (-10101000000) + , int64ToUTCTime 10101000000 + , int64ToUTCTime 99991231000000 + ] + +-- |Goal: exercise the "happy path" throught cursor code +-- i.e. open and fetch all rows, close after last row. + +sqlCursor fns = "select " ++ literalInt fns 1 ++ " from tdual union select " ++ literalInt fns 2 ++ " from tdual" +iterCursor :: (Monad m) => Int -> IterAct m [Int] +iterCursor i acc = result $ i:acc +actionCursor query = withCursor query iterCursor [] $ \c -> do + doneBool <- cursorIsEOF c + assertBool query (not doneBool) + r <- cursorCurrent c + assertEqual query [1] r + -- + cursorNext c + doneBool <- cursorIsEOF c + assertBool query (not doneBool) + r <- cursorCurrent c + assertEqual query [2, 1] r + -- + -- Now that the result-set is exhausted, cursorIsEOF returns True, + -- and cursorCurrent returns the same value as the previous call to cursorCurrent. + cursorNext c + doneBool <- cursorIsEOF c + assertBool query doneBool + r <- cursorCurrent c + assertEqual query [2, 1] r + -- + -- What happens if try to advance again? + -- We get a DBException: the DBNoData case. + --cursorNext c + doneBool <- cursorIsEOF c + assertBool query doneBool + r <- cursorCurrent c + assertEqual query [2, 1] r + -- + return () + +{-|Goal: ensure exception raised when too many rows +fetched from cursor. + +This test will raise an exception, as it tries to +fetch too many rows from the cursor. +The exception handler is coded as if we expect the +exception i.e. it ignores it. +The main action should never finish, so there's +a failure assertion at the bottom, just in case +the exception is not raised. +-} + +actionExhaustCursor query = catchDB ( + withCursor query iterCursor [] $ \c -> do + cursorNext c + cursorNext c + cursorNext c + cursorNext c + assertFailure "selectExhaustCursor" + ) (\e -> return () ) + +sqlBindString = "select ? from tdual union select ? from tdual order by 1" +iterBindString :: (Monad m) => String -> IterAct m [String] +iterBindString i acc = result $ i:acc +expectBindString = ["b1", "a2"] +actionBindString stmt bindVals = withTransaction Serialisable $ + withPreparedStatement stmt $ \pstmt -> + withBoundStatement pstmt bindVals $ \bstmt -> do + actual <- doQuery bstmt iterBindString [] + assertEqual sqlBindString expectBindString actual + +{- +Each back-end has it's own idea of parameter placeholder syntax. +We currently support ?-style as a lowest-common-denominator, +and each back-end converts occurences of "?" +to the back-end-specific style where required. +If you use a back-end specific style, you can expect it to be passed +through unmolested. + ? : ODBC, MS Sql Server, Sqlite + :n : Oracle + $n : Postgres +-} + +sqlBindInt = "select ? from tdual union select ? from tdual order by 1" +iterBindInt :: (Monad m) => Int -> IterAct m [Int] +iterBindInt i acc = result $ i:acc +expectBindInt :: [Int]; expectBindInt = [2, 1] +actionBindInt stmt bindVals = + withTransaction Serialisable $ + withPreparedStatement stmt $ \pstmt -> + withBoundStatement pstmt bindVals $ \bstmt -> do + actual <- doQuery bstmt iterBindInt [] + assertEqual sqlBindInt expectBindInt actual + +sqlBindIntDoubleString = "select ?,?,? from tdual union select ?,?,? from tdual order by 1" +iterBindIntDoubleString :: (Monad m) => Int -> Double -> String -> IterAct m [(Int, Double, String)] +iterBindIntDoubleString i d s acc = result $ (i, d, s):acc +expectBindIntDoubleString :: [(Int, Double, String)] +expectBindIntDoubleString = [(3, 4.4, "row 2"), (1, 2.2, "row 1")] +actionBindIntDoubleString stmt = do + withTransaction Serialisable $ do + actual <- doQuery stmt iterBindIntDoubleString [] + assertEqual sqlBindIntDoubleString expectBindIntDoubleString actual + +sqlBindDate = sqlSingleValue +iterBindDate :: (Monad m) => UTCTime -> IterAct m [UTCTime] +iterBindDate c1 acc = result $ c1:acc +expectBindDate = [ int64ToUTCTime 20041224235959 ] +actionBindDate stmt = do + withTransaction Serialisable $ do + actual <- doQuery stmt iterBindDate [] + assertEqual sqlBindDate expectBindDate actual + +sqlBindBool = "select ?, ? from tdual" +iterBindBool :: (Monad m) => Bool -> Bool -> IterAct m [(Bool, Bool)] +iterBindBool b1 b2 acc = result $ (b1,b2):acc +expectBindBool = [ (True, False) ] +actionBindBool stmt = do + withTransaction Serialisable $ do + actual <- doQuery stmt iterBindBool [] + assertEqual sqlBindBool expectBindBool actual + +sqlBindBoundaryDates = + "select ? from tdual" + ++ " union select ? from tdual" + ++ " union select ? from tdual" + ++ " union select ? from tdual" + ++ " order by 1 desc" +actionBindBoundaryDates stmt = do + withTransaction Serialisable $ do + actual <- doQuery stmt iterBindDate [] + assertEqual sqlBindBoundaryDates expectBoundaryDates actual + + +sqlBoundStmtDML = "insert into " ++ testTable ++ " (id, v) values (?, ?)" +actionBoundStmtDML stmt = do + withPreparedStatement stmt $ \pstmt -> do + -- do it twice, to check that prepared stmt can be reused. + -- Note that with bound statements, the query (or command) + -- already been executed, so it's too late to begin the transaction. + beginTransaction Serialisable + withBoundStatement pstmt [bindP (100::Int), bindP "100"] $ \bstmt -> do + count <- execDML bstmt + rollback + assertEqual sqlBoundStmtDML 1 count + beginTransaction Serialisable + withBoundStatement pstmt [bindP (100::Int), bindP "100"] $ \bstmt -> do + count <- execDML bstmt + rollback + assertEqual sqlBoundStmtDML 1 count + +{- + +With 'MyTree' we test the ability to send and receive arbtrary Show-able +values as Strings i.e. we create our own datatype for the test. + +-} + +data MyTree a = Leaf a | Branch [MyTree a] deriving (Eq, Show, Read) + +sqlPolymorphicFetch = sqlSingleValue +iterPolymorphicFetch :: (Monad m) => MyTree String -> IterAct m (MyTree String) +iterPolymorphicFetch v _ = result' v +expectPolymorphicFetch = Branch [Leaf "a", Leaf "b", Branch [Leaf "c", Leaf "d"], Leaf "e"] +actionPolymorphicFetch stmt = do + actual <- doQuery stmt iterPolymorphicFetch (Leaf "") + assertEqual sqlPolymorphicFetch expectPolymorphicFetch actual + +sqlPolymorphicFetchNull = "select '' from tdual" +iterPolymorphicFetchNull :: (Monad m) => Maybe (MyTree String) -> IterAct m (Maybe (MyTree String)) +iterPolymorphicFetchNull v _ = result' v +expectPolymorphicFetchNull :: Maybe (MyTree String) +expectPolymorphicFetchNull = Nothing +actionPolymorphicFetchNull stmt = do + actual <- doQuery stmt iterPolymorphicFetchNull Nothing + assertEqual sqlPolymorphicFetchNull Nothing actual + +sqlRebind = sqlSingleValue +iterRebind (i::Int) acc = result $ i:acc +expectRebind1 :: [Int]; expectRebind1 = [1] +expectRebind2 :: [Int]; expectRebind2 = [2] +actionRebind stmt bindVal1 bindVal2 = do + withPreparedStatement stmt $ \pstmt -> do + withBoundStatement pstmt bindVal1 $ \bstmt -> do + actual <- doQuery bstmt iterRebind [] + assertEqual sqlRebind expectRebind1 actual + withBoundStatement pstmt bindVal2 $ \bstmt -> do + actual <- doQuery bstmt iterRebind [] + assertEqual sqlRebind expectRebind2 actual + +sqlExceptionRollback = "select count(*) from " ++ testTable +iterExceptionRollback (i::Int) acc = result $ i:acc +actionExceptionRollback insertStmt selectStmt = do + catchDB ( + withTransaction Serialisable $ do + execDML insertStmt + throwDB DBNoData + ) (\e -> return () ) + count <- doQuery selectStmt iterExceptionRollback [] + assertEqual sqlExceptionRollback [3] count + +sqlIterateeMatchesResultSet = "select ?,?,? from tdual" +--iterMatchesResultSet :: (Monad m) => Int -> Double -> String -> IterAct m [(Int, Double, String)] +--iterMatchesResultSet i d s acc = result $ (i, d, s):acc +iterMatchesResultSet :: (Monad m) => Int -> Double -> String -> String -> IterAct m [(Int, Double, String, String)] +iterMatchesResultSet i d s s2 acc = result $ (i, d, s, s2):acc +actionIterateeMatchesResultSet stmt = do + catchDB ( do + actual <- doQuery stmt iterMatchesResultSet [] + assertFailure "actionIterateeMatchesResultSet" + ) (\e -> return () )
− Database/Test/Enumerator.lhs
@@ -1,480 +0,0 @@- -| -Module : Database.Test.Enumerator -Copyright : (c) 2004 Oleg Kiselyov, Alistair Bayley -License : BSD-style -Maintainer : oleg@pobox.com, alistair@abayley.org -Stability : experimental -Portability : non-portable - -Simple test harness. Demonstrates possible usage. - -Tests in this module are organised in groups of three/four functions: - sqlXXX - iterXXX - expectXXX - actionXXX - -These have to be tied together by a function which uses backend-specific -functions and types. See the various backend-specific test modules for examples. - - -> {-# LANGUAGE OverlappingInstances #-} - -> module Database.Test.Enumerator where - -> import Database.Enumerator -> import Database.Util -> import Data.Time -> import System.Time -> import Data.Int -> import Control.Exception.MonadIO -> import Control.Monad.Trans (liftIO) -> import Test.MiniUnit - - -> testTable = "takusen_test" - -> sqlDropDual = "drop table tdual" -> sqlCreateDual = "create table tdual (dummy varchar(1) primary key)" -> sqlInsertDual = "insert into tdual values ('X')" -> sqlDropTest = "drop table " ++ testTable -> sqlCreateTest = "create table " ++ testTable ++ " (id integer, v varchar(250))" - -> sqlInsertTest1 = "insert into " ++ testTable ++ " (id, v) values (1, '2')" -> sqlInsertTest2 = "insert into " ++ testTable ++ " (id, v) values (2, '2')" -> sqlInsertTest3 = "insert into " ++ testTable ++ " (id, v) values (3, '3')" -> sqlInsertTest4 = "insert into " ++ testTable ++ " (id, v) values (4, '4')" - - -> reportError sql (DBFatal (ssc, sssc) e m) = do -> putStrLn ("FATAL: " ++ ssc ++ sssc ++ " - " ++ m) -> putStrLn (" " ++ sql) -> reportError sql (DBError (ssc, sssc) e m) = do -> putStrLn ("ERROR: " ++ ssc ++ sssc ++ " - " ++ m) -> putStrLn (" " ++ sql) -> reportError sql (DBUnexpectedNull r c) = -> putStrLn $ "Unexpected null in row " ++ (show r) ++ ", column " ++ (show c) ++ "." -> reportError sql (DBNoData) = putStrLn "Fetch: no more data." - ------------------------------------------------------------ - -> class DBLiteralValue a where -> literalDate :: a -> Int64 -> String -> literalInt :: a -> Int -> String -> literalInt64 :: a -> Int64 -> String -> literalFloat :: a -> Float -> String -> literalDouble :: a -> Double -> String -> -- default methods -> literalInt _ i = show i -> literalInt64 _ i = show i -> literalFloat _ i = show i -> literalDouble _ i = show i - - -> data SqliteFunctions = SqliteFunctions -> data OracleFunctions = OracleFunctions -> data PGSqlFunctions = PGSqlFunctions -> data ODBCFunctions = ODBCFunctions - -> instance DBLiteralValue SqliteFunctions where -> literalDate _ i = dateSqlite i - -> instance DBLiteralValue OracleFunctions where -> literalDate _ i = dateOracle i - -> instance DBLiteralValue PGSqlFunctions where -> literalDate _ i = datePG i -> literalInt _ i = show i ++ "::int4" -> literalInt64 _ i = show i ++ "::int8" -> literalFloat _ i = show i ++ "::float4" -> literalDouble _ i = show i ++ "::float8" - -> instance DBLiteralValue ODBCFunctions where -> literalDate _ i = dateISO i - - - -> dateSqlite :: Int64 -> String -> dateSqlite i = if i == 0 then "99999999999999" else show i - -> dateOracle :: Int64 -> String -> dateOracle i -> | i == 0 = "to_date(null)" -> | i > 0 = "to_date('" ++ (zeroPad 14 i) ++ "', 'yyyymmddhh24miss')" -> | i < 0 = "to_date('" ++ (zeroPad 14 i) ++ "', 'syyyymmddhh24miss')" - -> dateISO :: Int64 -> String -> dateISO i = -> let -> (year, month, day, hour, minute, second) = int64ToDateParts i -> zp = zeroPad -> in -> if i == 0 then "null" -> else zp 4 year ++ "-" ++ zp 2 month ++ "-" ++ zp 2 day -> ++ "T" ++ zp 2 hour ++ ":" ++ zp 2 minute ++ ":" ++ zp 2 second - - -For postgres: -0000-01-01 AD -> 0001-01-01 BC -0000-12-31 AD -> 0001-12-31 BC -0001-01-01 AD -> 0001-01-01 AD - -Using Doubles as storage (not sure about this?) -4714-11-24 BC -> mindate -5874897-12-31 AD -> maxdate -Using 8-bytes ints: -4713 BC -> 294276 AD - -Postgres only allows specification of -ve years by use of the AD/BC suffix. -Sadly, this differs from the astronomical year number like so: -astro: ...3,2,1,0,-1,-2,-3,... -ad/bc: ...3AD,2AD,1AD,1BC,2BC,3BC,... - -i.e. 0 astro = 1BC, -1 astro = 2BC, etc. - -ISO8601 uses astronomical years, so we ought to be able to write --1000-12-25 (instead of 1001-01-01 BC), but Postgres won't parse this. - -So in datePG we want: -int64 0000-01-01 -> 0001-01-01 AD -int64 0001-01-01 -> 0001-01-01 AD -int64 -0001-01-01 -> 0000-01-01 AD -int64 -0002-01-01 -> 0003-01-01 BC -int64 -4712-01-01 -> 4713-01-01 BC - -> datePG :: Int64 -> String -> datePG i = -> let -> (year1, month, day, hour, minute, second) = int64ToDateParts i -> year = case () of -> _ | year1 == 0 -> 1 -> | year1 < 0 -> abs (year1 - 1) -> | otherwise -> year1 -> suffix = if year1 < 1 then " BC'" else " AD'" -> zp = zeroPad -> in -> if i == 0 then "null::timestamp" -> --else "timestamp with time zone '" ++ zp 4 year ++ "-" ++ zp 2 month ++ "-" ++ zp 2 day -> else "timestamp '" ++ zp 4 year ++ "-" ++ zp 2 month ++ "-" ++ zp 2 day -> ++ " " ++ zp 2 hour ++ ":" ++ zp 2 minute ++ ":" ++ zp 2 second ++ suffix - - - - -> execDDL_ s = catchDB (execDDL s) (reportRethrowMsg ("sql: " ++ s ++ "\n")) -> execDML_ s = execDDL_ s - -Use execDrop when the DDL is likely to raise an error. -Note that PostgreSQL + ODBC seems to require you commit or rollback the DDL; -if you don't then the next statement will fail with a 25P02 -("in failed SQL transaction") -I guess that's a result of PostgreSQL's transactional DDL feature. - -> execDrop s = catchDB (withTransaction Serializable (execDDL s)) (\e -> return ()) - -> makeFixture doDrop doDDL = flip catchDB (reportRethrowMsg "makeFixture: ") $ do -> doDrop sqlDropDual -> doDrop sqlDropTest -> doDDL sqlCreateDual -> beginTransaction ReadCommitted -> doDDL sqlInsertDual -> commit -> doDDL sqlCreateTest -> withTransaction Serialisable $ do -> doDDL sqlInsertTest1 -> doDDL sqlInsertTest2 -> doDDL sqlInsertTest3 - -> destroyFixture execDDL_ = flip catchDB (reportRethrowMsg "destroyFixture: ") $ do -> execDDL_ sqlDropDual -> execDDL_ sqlDropTest -> catchDB commit (const (return ())) - -> selectTest query iter expect = do -> actual <- doQuery query iter [] -> assertEqual query expect actual - - - ------------------------------------------------------------ - -This is used in a few tests... - -> sqlSingleValue = "select ? from tdual" - -> sqlNoRows = "select dummy from tdual where dummy = 'a' or dummy = '2' " -> iterNoRows (c1::String) acc = result $ c1:acc -> expectNoRows = []::[String] - -> sqlTermEarly = "select 'hello1' from tdual union select 'hello2' from tdual union select 'hello3' from tdual order by 1" -> iterTermEarly c1 acc = if c1 == "hello2" -> then return (Left (c1:acc)) -> else result (c1:acc) -> expectTermEarly = ["hello2", "hello1"] - -> sqlFloatsAndInts fns = "select " ++ (literalDouble fns 4841.3403490431) ++ ", " -> ++ (literalInt fns (-22340234)) ++ " from tdual union select 33311.32332, 23789234 from tdual order by 1" -> --iterFloatsAndInts :: (Monad m) => Double -> Int -> IterAct m [(Double, Int)] -> iterFloatsAndInts (c1::Double) (c2::Int) acc = result $ (c1, c2):acc -> expectFloatsAndInts :: [(Double, Int)] -> expectFloatsAndInts = [ (33311.32332, 23789234) , (4841.3403490431, -22340234) ] - -> sqlNullString = "select 'hello1', 'hello2', null from tdual" -> iterNullString :: (Monad m) => String -> String -> Maybe String -> -> IterAct m [(String, String, Maybe String)] -> iterNullString c1 c2 c3 acc = result $ (c1, c2, c3):acc -> expectNullString = [ ("hello1", "hello2", Nothing) ] - -Access can't handle SQL comments (-- or /* */). - -> sqlEmptyString = "select 'hello1', 'Oracle always fails this test', '' from tdual" -> iterEmptyString :: (Monad m) => String -> String -> Maybe String -> -> IterAct m [(String, String, Maybe String)] -> iterEmptyString c1 c2 c3 acc = result $ (c1, c2, c3):acc -> expectEmptyString = [ ("hello1", "Oracle always fails this test", Just "") ] - -> sqlUnhandledNull = "select 'hello1', 'hello2', null from tdual" -> iterUnhandledNull :: (Monad m) => String -> String -> UTCTime -> -> IterAct m [(String, String, UTCTime)] -> iterUnhandledNull c1 c2 c3 acc = result $ (c1, c2, c3):acc -> expectUnhandledNull = [] - -> sqlNullDate fns = "select 'hello1', 'hello2', " ++ (literalDate fns 0) ++ " from tdual" -> iterNullDate :: (Monad m) => String -> String -> Maybe UTCTime -> -> IterAct m [(String, String, UTCTime)] -> iterNullDate c1 c2 c3 acc = result $ (c1, c2, ifNull c3 (int64ToUTCTime 10101000000)):acc -> expectNullDate = [ ("hello1", "hello2", (int64ToUTCTime 10101000000)) ] - -> sqlDate fns = "select " ++ (literalDate fns 20041224235959) ++ " from tdual" -> iterDate :: (Monad m) => UTCTime -> IterAct m [UTCTime] -> iterDate c1 acc = result $ c1:acc -> expectDate = [ (int64ToUTCTime 20041224235959) ] - -> iterCalDate :: (Monad m) => CalendarTime -> IterAct m [CalendarTime] -> iterCalDate c1 acc = result $ c1:acc -> expectCalDate = [ (int64ToCalTime 20041224235959) ] - - -These are the Oracle date boundary cases. - -> sqlBoundaryDates fns = -> "select " ++ (literalDate fns 99991231000000) ++ " from tdual" -> ++ " union select " ++ (literalDate fns 10101000000) ++ " from tdual" -> ++ " union select " ++ (literalDate fns (-10101000000)) ++ " from tdual" -> ++ " union select " ++ (literalDate fns (-47120101000000)) ++ " from tdual" -> ++ " order by 1 desc" -> iterBoundaryDates :: (Monad m) => UTCTime -> IterAct m [UTCTime] -> iterBoundaryDates c1 acc = result $ c1:acc -> expectBoundaryDates = -> [ int64ToUTCTime (-47120101000000) -> , int64ToUTCTime (-10101000000) -> , int64ToUTCTime 10101000000 -> , int64ToUTCTime 99991231000000 -> ] - -|Goal: exercise the "happy path" throught cursor code -i.e. open and fetch all rows, close after last row. - -> sqlCursor fns = "select " ++ literalInt fns 1 ++ " from tdual union select " ++ literalInt fns 2 ++ " from tdual" -> iterCursor :: (Monad m) => Int -> IterAct m [Int] -> iterCursor i acc = result $ i:acc -> actionCursor query = do -> withCursor query iterCursor [] $ \c -> do -> doneBool <- cursorIsEOF c -> assertBool query (not doneBool) -> r <- cursorCurrent c -> assertEqual query [1] r -> -- -> cursorNext c -> doneBool <- cursorIsEOF c -> assertBool query (not doneBool) -> r <- cursorCurrent c -> assertEqual query [2, 1] r -> -- -> -- Now that the result-set is exhausted, cursorIsEOF returns True, -> -- and cursorCurrent returns the same value as the previous call to cursorCurrent. -> cursorNext c -> doneBool <- cursorIsEOF c -> assertBool query doneBool -> r <- cursorCurrent c -> assertEqual query [2, 1] r -> -- -> -- What happens if try to advance again? -> -- We get a DBException: the DBNoData case. -> --cursorNext c -> doneBool <- cursorIsEOF c -> assertBool query doneBool -> r <- cursorCurrent c -> assertEqual query [2, 1] r -> -- -> return () - -|Goal: ensure exception raised when too many rows -fetched from cursor. - -This test will raise an exception, as it tries to -fetch too many rows from the cursor. -The exception handler is coded as if we expect the -exception i.e. it ignores it. -The main action should never finish, so there's -a failure assertion at the bottom, just in case -the exception is not raised. - -> actionExhaustCursor query = catchDB ( -> withCursor query iterCursor [] $ \c -> do -> cursorNext c -> cursorNext c -> cursorNext c -> cursorNext c -> assertFailure "selectExhaustCursor" -> ) (\e -> return () ) - -> sqlBindString = "select ? from tdual union select ? from tdual order by 1" -> iterBindString :: (Monad m) => String -> IterAct m [String] -> iterBindString i acc = result $ i:acc -> expectBindString = ["b1", "a2"] -> actionBindString stmt bindVals = do -> withTransaction Serialisable $ do -> withPreparedStatement stmt $ \pstmt -> do -> withBoundStatement pstmt bindVals $ \bstmt -> do -> actual <- doQuery bstmt iterBindString [] -> assertEqual sqlBindString expectBindString actual - - -Each back-end has it's own idea of parameter placeholder syntax. -We currently support ?-style as a lowest-common-denominator, -and each back-end converts occurences of "?" -to the back-end-specific style where required. -If you use a back-end specific style, you can expect it to be passed -through unmolested. - ? : ODBC, MS Sql Server, Sqlite - :n : Oracle - $n : Postgres - -> sqlBindInt = "select ? from tdual union select ? from tdual order by 1" -> iterBindInt :: (Monad m) => Int -> IterAct m [Int] -> iterBindInt i acc = result $ i:acc -> expectBindInt :: [Int]; expectBindInt = [2, 1] -> actionBindInt stmt bindVals = do -> withTransaction Serialisable $ do -> withPreparedStatement stmt $ \pstmt -> do -> withBoundStatement pstmt bindVals $ \bstmt -> do -> actual <- doQuery bstmt iterBindInt [] -> assertEqual sqlBindInt expectBindInt actual - -> sqlBindIntDoubleString = "select ?,?,? from tdual union select ?,?,? from tdual order by 1" -> iterBindIntDoubleString :: (Monad m) => Int -> Double -> String -> IterAct m [(Int, Double, String)] -> iterBindIntDoubleString i d s acc = result $ (i, d, s):acc -> expectBindIntDoubleString :: [(Int, Double, String)] -> expectBindIntDoubleString = [(3, 4.4, "row 2"), (1, 2.2, "row 1")] -> actionBindIntDoubleString stmt = do -> withTransaction Serialisable $ do -> actual <- doQuery stmt iterBindIntDoubleString [] -> assertEqual sqlBindIntDoubleString expectBindIntDoubleString actual - -> sqlBindDate = sqlSingleValue -> iterBindDate :: (Monad m) => UTCTime -> IterAct m [UTCTime] -> iterBindDate c1 acc = result $ c1:acc -> expectBindDate = [ int64ToUTCTime 20041224235959 ] -> actionBindDate stmt = do -> withTransaction Serialisable $ do -> actual <- doQuery stmt iterBindDate [] -> assertEqual sqlBindDate expectBindDate actual - -> sqlBindBool = "select ?, ? from tdual" -> iterBindBool :: (Monad m) => Bool -> Bool -> IterAct m [(Bool, Bool)] -> iterBindBool b1 b2 acc = result $ (b1,b2):acc -> expectBindBool = [ (True, False) ] -> actionBindBool stmt = do -> withTransaction Serialisable $ do -> actual <- doQuery stmt iterBindBool [] -> assertEqual sqlBindBool expectBindBool actual - -> sqlBindBoundaryDates = -> "select ? from tdual" -> ++ " union select ? from tdual" -> ++ " union select ? from tdual" -> ++ " union select ? from tdual" -> ++ " order by 1 desc" -> actionBindBoundaryDates stmt = do -> withTransaction Serialisable $ do -> actual <- doQuery stmt iterBindDate [] -> assertEqual sqlBindBoundaryDates expectBoundaryDates actual - - -> sqlBoundStmtDML = "insert into " ++ testTable ++ " (id, v) values (?, ?)" -> actionBoundStmtDML stmt = do -> withPreparedStatement stmt $ \pstmt -> do -> -- do it twice, to check that prepared stmt can be reused. -> -- Note that with bound statements, the query (or command) -> -- already been executed, so it's too late to begin the transaction. -> beginTransaction Serialisable -> withBoundStatement pstmt [bindP (100::Int), bindP "100"] $ \bstmt -> do -> count <- execDML bstmt -> rollback -> assertEqual sqlBoundStmtDML 1 count -> beginTransaction Serialisable -> withBoundStatement pstmt [bindP (100::Int), bindP "100"] $ \bstmt -> do -> count <- execDML bstmt -> rollback -> assertEqual sqlBoundStmtDML 1 count - - - -With 'MyTree' we test the ability to send and receive arbtrary Show-able -values as Strings i.e. we create our own datatype for the test. - -> data MyTree a = Leaf a | Branch [MyTree a] deriving (Eq, Show, Read) - -> sqlPolymorphicFetch = sqlSingleValue -> iterPolymorphicFetch :: (Monad m) => MyTree String -> IterAct m (MyTree String) -> iterPolymorphicFetch v _ = result' v -> expectPolymorphicFetch = Branch [Leaf "a", Leaf "b", Branch [Leaf "c", Leaf "d"], Leaf "e"] -> actionPolymorphicFetch stmt = do -> actual <- doQuery stmt iterPolymorphicFetch (Leaf "") -> assertEqual sqlPolymorphicFetch expectPolymorphicFetch actual - -> sqlPolymorphicFetchNull = "select '' from tdual" -> iterPolymorphicFetchNull :: (Monad m) => Maybe (MyTree String) -> IterAct m (Maybe (MyTree String)) -> iterPolymorphicFetchNull v _ = result' v -> expectPolymorphicFetchNull :: Maybe (MyTree String) -> expectPolymorphicFetchNull = Nothing -> actionPolymorphicFetchNull stmt = do -> actual <- doQuery stmt iterPolymorphicFetchNull Nothing -> assertEqual sqlPolymorphicFetchNull Nothing actual - -> sqlRebind = sqlSingleValue -> iterRebind (i::Int) acc = result $ i:acc -> expectRebind1 :: [Int]; expectRebind1 = [1] -> expectRebind2 :: [Int]; expectRebind2 = [2] -> actionRebind stmt bindVal1 bindVal2 = do -> withPreparedStatement stmt $ \pstmt -> do -> withBoundStatement pstmt bindVal1 $ \bstmt -> do -> actual <- doQuery bstmt iterRebind [] -> assertEqual sqlRebind expectRebind1 actual -> withBoundStatement pstmt bindVal2 $ \bstmt -> do -> actual <- doQuery bstmt iterRebind [] -> assertEqual sqlRebind expectRebind2 actual - -> sqlExceptionRollback = "select count(*) from " ++ testTable -> iterExceptionRollback (i::Int) acc = result $ i:acc -> actionExceptionRollback insertStmt selectStmt = do -> catchDB ( -> withTransaction Serialisable $ do -> execDML insertStmt -> throwDB DBNoData -> ) (\e -> return () ) -> count <- doQuery selectStmt iterExceptionRollback [] -> assertEqual sqlExceptionRollback [3] count - -> sqlIterateeMatchesResultSet = "select ?,?,? from tdual" -> --iterMatchesResultSet :: (Monad m) => Int -> Double -> String -> IterAct m [(Int, Double, String)] -> --iterMatchesResultSet i d s acc = result $ (i, d, s):acc -> iterMatchesResultSet :: (Monad m) => Int -> Double -> String -> String -> IterAct m [(Int, Double, String, String)] -> iterMatchesResultSet i d s s2 acc = result $ (i, d, s, s2):acc -> actionIterateeMatchesResultSet stmt = do -> catchDB ( do -> actual <- doQuery stmt iterMatchesResultSet [] -> assertFailure "actionIterateeMatchesResultSet" -> ) (\e -> return () )
+ Database/Test/Performance.hs view
@@ -0,0 +1,100 @@+{-| +Module : Database.Test.Performance +Copyright : (c) 2004 Oleg Kiselyov, Alistair Bayley +License : BSD-style +Maintainer : oleg@pobox.com, alistair@abayley.org +Stability : experimental +Portability : non-portable + +Performance tests. Currently just tests large result sets. +-} + +{-# LANGUAGE OverlappingInstances #-} + +module Database.Test.Performance where + +import Database.Enumerator +import Database.Util +import System.Environment (getArgs) +import Control.Monad +import System.Time +import Test.MiniUnit +import Control.Monad.Reader + +data ShouldRunTests = RunTests | Don'tRunTests deriving (Show, Eq) + +timedCursor stmt limit rows = do + withCursor stmt (rowCounter rows) 0 $ \c -> do + ct1 <- liftIO getClockTime + replicateM_ rows (cursorNext c) + ct2 <- liftIO getClockTime + let diffCt = diffClockTimes ct2 ct1 + let limitDiff = TimeDiff 0 0 0 0 0 limit 0 + print_ $ "timedCursor: " ++ timeDiffToString diffCt + assertBool ("timedCursor: time " ++ (timeDiffToString diffCt) ++ " (limit " ++ (timeDiffToString limitDiff) ++ ")") + (diffCt < limitDiff) + + +timedSelect stmt limit rows = do + ct1 <- liftIO getClockTime + r <- doQuery stmt (rowCounter rows) 0 + ct2 <- liftIO getClockTime + let diffCt = diffClockTimes ct2 ct1 + -- assume it must complete within limit seconds + let limitDiff = TimeDiff 0 0 0 0 0 limit 0 + print_ $ "timedSelect: " ++ timeDiffToString diffCt + assertBool ("timedSelect: time " ++ (timeDiffToString diffCt) ++ " (limit " ++ (timeDiffToString limitDiff) ++ ")") + (diffCt < limitDiff) + assertEqual ("timedSelect: rows " ++ (show r)) rows r + + +{-|This counter takes the maximum number of rows to fetch as its first argument, +so don't forget to curry it when using it as an iteratee function. +We also try to ensure that it is strict in the counter; +we don't want thousands or millions of unevaluated '+' thunks sitting +on the stack. +-} + +rowCounter :: (Monad m) => Int -> Int -> IterAct m Int +rowCounter n _ i = result' (i+1) + --if i >= n then return (Left $! i) else return (Right $! (1 + i)) + --if i >= n then return (Left i) else return (Right (1 + i)) + +{- +2 ^ 16 = 65536 +2 ^ 20 = 1048576 + +Cartesian product. Each instance of the subquery multiplies +the total number of rows by 2, so the number of rows +is 2 ^ (count of subquery instances). +You start to notice the pause around 2^13, +and 2^16 blows out the standard 1M stack +if you use the lazy version of result. Bummer. +-} + +sqlRows2Power17 :: String +sqlRows2Power17 = + "select 1 from" + ++ " ( select 1 x from tdual union select 0 from tdual) t1" + ++ ", ( select 2 x from tdual union select 0 from tdual) t2" + ++ ", ( select 3 x from tdual union select 0 from tdual) t3" + ++ ", ( select 4 x from tdual union select 0 from tdual) t4" + ++ ", ( select 5 x from tdual union select 0 from tdual) t5" + ++ ", ( select 6 x from tdual union select 0 from tdual) t6" + ++ ", ( select 7 x from tdual union select 0 from tdual) t7" + ++ ", ( select 8 x from tdual union select 0 from tdual) t8" + ++ ", ( select 9 x from tdual union select 0 from tdual) t9" + ++ ", ( select 10 x from tdual union select 0 from tdual) t10" + ++ ", ( select 11 x from tdual union select 0 from tdual) t11" + ++ ", ( select 12 x from tdual union select 0 from tdual) t12" + ++ ", ( select 13 x from tdual union select 0 from tdual) t13" + ++ ", ( select 14 x from tdual union select 0 from tdual) t14" + ++ ", ( select 15 x from tdual union select 0 from tdual) t15" + ++ ", ( select 16 x from tdual union select 0 from tdual) t16" + ++ ", ( select 17 x from tdual union select 0 from tdual) t17" + +sqlRows2Power20 :: String +sqlRows2Power20 = sqlRows2Power17 + ++ ", ( select 18 x from tdual union select 0 from tdual) t18" + ++ ", ( select 19 x from tdual union select 0 from tdual) t19" + ++ ", ( select 20 x from tdual union select 0 from tdual) t20"
− Database/Test/Performance.lhs
@@ -1,99 +0,0 @@- -| -Module : Database.Test.Performance -Copyright : (c) 2004 Oleg Kiselyov, Alistair Bayley -License : BSD-style -Maintainer : oleg@pobox.com, alistair@abayley.org -Stability : experimental -Portability : non-portable - -Performance tests. Currently just tests large result sets. - - -> {-# LANGUAGE OverlappingInstances #-} - -> module Database.Test.Performance where - -> import Database.Enumerator -> import Database.Util -> import System.Environment (getArgs) -> import Control.Monad -> import System.Time -> import Test.MiniUnit -> import Control.Monad.Reader - -> data ShouldRunTests = RunTests | Don'tRunTests deriving (Show, Eq) - -> timedCursor stmt limit rows = do -> withCursor stmt (rowCounter rows) 0 $ \c -> do -> ct1 <- liftIO getClockTime -> replicateM_ rows (cursorNext c) -> ct2 <- liftIO getClockTime -> let diffCt = diffClockTimes ct2 ct1 -> let limitDiff = TimeDiff 0 0 0 0 0 limit 0 -> print_ $ "timedCursor: " ++ timeDiffToString diffCt -> assertBool ("timedCursor: time " ++ (timeDiffToString diffCt) ++ " (limit " ++ (timeDiffToString limitDiff) ++ ")") -> (diffCt < limitDiff) - - -> timedSelect stmt limit rows = do -> ct1 <- liftIO getClockTime -> r <- doQuery stmt (rowCounter rows) 0 -> ct2 <- liftIO getClockTime -> let diffCt = diffClockTimes ct2 ct1 -> -- assume it must complete within limit seconds -> let limitDiff = TimeDiff 0 0 0 0 0 limit 0 -> print_ $ "timedSelect: " ++ timeDiffToString diffCt -> assertBool ("timedSelect: time " ++ (timeDiffToString diffCt) ++ " (limit " ++ (timeDiffToString limitDiff) ++ ")") -> (diffCt < limitDiff) -> assertEqual ("timedSelect: rows " ++ (show r)) rows r - - -|This counter takes the maximum number of rows to fetch as its first argument, -so don't forget to curry it when using it as an iteratee function. -We also try to ensure that it is strict in the counter; -we don't want thousands or millions of unevaluated '+' thunks sitting -on the stack. - -> rowCounter :: (Monad m) => Int -> Int -> IterAct m Int -> rowCounter n _ i = result' (i+1) -> --if i >= n then return (Left $! i) else return (Right $! (1 + i)) -> --if i >= n then return (Left i) else return (Right (1 + i)) - - -2 ^ 16 = 65536 -2 ^ 20 = 1048576 - -Cartesian product. Each instance of the subquery multiplies -the total number of rows by 2, so the number of rows -is 2 ^ (count of subquery instances). -You start to notice the pause around 2^13, -and 2^16 blows out the standard 1M stack -if you use the lazy version of result. Bummer. - -> sqlRows2Power17 :: String -> sqlRows2Power17 = -> "select 1 from" -> ++ " ( select 1 x from tdual union select 0 from tdual) t1" -> ++ ", ( select 2 x from tdual union select 0 from tdual) t2" -> ++ ", ( select 3 x from tdual union select 0 from tdual) t3" -> ++ ", ( select 4 x from tdual union select 0 from tdual) t4" -> ++ ", ( select 5 x from tdual union select 0 from tdual) t5" -> ++ ", ( select 6 x from tdual union select 0 from tdual) t6" -> ++ ", ( select 7 x from tdual union select 0 from tdual) t7" -> ++ ", ( select 8 x from tdual union select 0 from tdual) t8" -> ++ ", ( select 9 x from tdual union select 0 from tdual) t9" -> ++ ", ( select 10 x from tdual union select 0 from tdual) t10" -> ++ ", ( select 11 x from tdual union select 0 from tdual) t11" -> ++ ", ( select 12 x from tdual union select 0 from tdual) t12" -> ++ ", ( select 13 x from tdual union select 0 from tdual) t13" -> ++ ", ( select 14 x from tdual union select 0 from tdual) t14" -> ++ ", ( select 15 x from tdual union select 0 from tdual) t15" -> ++ ", ( select 16 x from tdual union select 0 from tdual) t16" -> ++ ", ( select 17 x from tdual union select 0 from tdual) t17" - -> sqlRows2Power20 :: String -> sqlRows2Power20 = sqlRows2Power17 -> ++ ", ( select 18 x from tdual union select 0 from tdual) t18" -> ++ ", ( select 19 x from tdual union select 0 from tdual) t19" -> ++ ", ( select 20 x from tdual union select 0 from tdual) t20"
+ Database/Test/Util.hs view
@@ -0,0 +1,109 @@+{-# LANGUAGE CPP #-} + +module Database.Test.Util where + +import Database.Util +import Test.MiniUnit +import Data.Int +import Data.Char + + +runTest :: a -> [String] -> IO () +runTest _ _ = do + counts <- runTestTT "Util module tests" testlist + return () + +testlist = + [ testSubstr + , testWordsBy + , testInt64ToDatePartsMinDate + , testInt64ToDatePartsMaxDate + , testUtcTimeToInt64MinDate + , testUtcTimeToInt64MaxDate + , testCalTimeToInt64MinDate + , testCalTimeToInt64MaxDate + , testInt64ToCalTimeMinDate + , testInt64ToCalTimeMaxDate + , testPGDatetimeToUTCTimeMinDate + , testPGDatetimeToUTCTimeMaxDate + , testPGDatetimeToUTCTimeBCBoundary + , testPGDatetimeToUTCTimeBCBoundary2 + , testUTCTimeToPGDatetimeMinDate + , testUTCTimeToPGDatetimeMaxDate + ] + +testSubstr = do + assertEqual "substr - 1" "a" (substr 1 1 "abc") + assertEqual "substr - 2" "b" (substr 2 1 "abc") + assertEqual "substr - 3" "c" (substr 3 1 "abc") + assertEqual "substr - 4" "bc" (substr 2 2 "abc") + assertEqual "substr - 5" "ab" (substr 1 2 "abc") + assertEqual "substr - 6" "abc" (substr 1 4 "abc") + assertEqual "substr -76" "" (substr 4 4 "abc") + +testWordsBy = do + assertEqual "wordsBy - 1" [] (wordsBy isDigit "") + assertEqual "wordsBy - 2" [] (wordsBy isDigit " --,") + assertEqual "wordsBy - 2.5" ["5"] (wordsBy isDigit "5") + assertEqual "wordsBy - 2.75" ["5"] (wordsBy isDigit "5 ") + assertEqual "wordsBy - 3" ["5"] (wordsBy isDigit "5 --,") + assertEqual "wordsBy - 4" ["6"] (wordsBy isDigit " --,6") + assertEqual "wordsBy - 5" ["7"] (wordsBy isDigit " --7, ") + assertEqual "wordsBy - 6" ["100", "12", "67", "25"] (wordsBy isDigit "100-12,67--, 25") + + +testInt64ToDatePartsMinDate = do + let expect :: (Int64,Int64,Int64,Int64,Int64,Int64); expect = (-4712,1,1,0,0,0) + assertEqual "testInt64ToDatePartsMinDate" expect (int64ToDateParts (-47120101000000)) + +testInt64ToDatePartsMaxDate = do + let expect :: (Int64,Int64,Int64,Int64,Int64,Int64); expect = (999999,12,31,23,59,59) + assertEqual "testInt64ToDatePartsMaxDate" expect (int64ToDateParts (9999991231235959)) + +testUtcTimeToInt64MinDate = do + let expect :: Int64; expect = (-47120101000000) + assertEqual "testUtcTimeToInt64MinDate" expect (utcTimeToInt64 (mkUTCTime (-4712) 1 1 0 0 0)) + +testUtcTimeToInt64MaxDate = do + let expect :: Int64; expect = (9999991231235959) + assertEqual "testUtcTimeToInt64MaxDate" expect (utcTimeToInt64 (mkUTCTime 999999 12 31 23 59 59)) + +testCalTimeToInt64MinDate = do + let expect :: Int64; expect = (-47120101000000) + assertEqual "testCalTimeToInt64MinDate" expect (calTimeToInt64 (mkCalTime (-4712) 1 1 0 0 0)) + +testCalTimeToInt64MaxDate = do + let expect :: Int64; expect = (9999991231235959) + assertEqual "testCalTimeToInt64MaxDate" expect (calTimeToInt64 (mkCalTime 999999 12 31 23 59 59)) + +testInt64ToCalTimeMinDate = do + let expect = mkCalTime (-4712) 1 1 0 0 0 + assertEqual "testInt64ToCalTimeMinDate" expect (int64ToCalTime (-47120101000000)) + +testInt64ToCalTimeMaxDate = do + let expect = mkCalTime 999999 12 31 23 59 59 + assertEqual "testInt64ToCalTimeMaxDate" expect (int64ToCalTime 9999991231235959) + +testPGDatetimeToUTCTimeMinDate = do + let expect = mkUTCTime (-4712) 1 1 0 0 0 + assertEqual "testPGDatetimeToUTCTimeMinDate" expect (pgDatetimetoUTCTime "4713-01-01 00:00:00 BC") + +testPGDatetimeToUTCTimeMaxDate = do + let expect = mkUTCTime 999999 1 1 0 0 0 + assertEqual "testPGDatetimeToUTCTimeMaxDate" expect (pgDatetimetoUTCTime "999999-01-01 00:00:00+00") + +testPGDatetimeToUTCTimeBCBoundary = do + let expect = mkUTCTime 0 1 1 0 0 0 + assertEqual "testPGDatetimeToUTCTimeBCBoundary" expect (pgDatetimetoUTCTime "0001-01-01 00:00:00 BC") + +testPGDatetimeToUTCTimeBCBoundary2 = do + let expect = mkUTCTime 0 12 31 0 0 0 + assertEqual "testPGDatetimeToUTCTimeBCBoundary2" expect (pgDatetimetoUTCTime "0001-12-31 00:00:00 BC") + +testUTCTimeToPGDatetimeMinDate = do + let expect = "4713-01-01T00:00:00.000000+00 BC" + assertEqual "testUTCTimeToPGDatetimeMinDate" expect (utcTimeToPGDatetime (mkUTCTime (-4712) 1 1 0 0 0)) + +testUTCTimeToPGDatetimeMaxDate = do + let expect = "999999-01-01T00:00:00.300001+00 AD" + assertEqual "testUTCTimeToPGDatetimeMaxDate" expect (utcTimeToPGDatetime (mkUTCTime 999999 1 1 0 0 0.300001))
− Database/Test/Util.lhs
@@ -1,129 +0,0 @@-> {-# LANGUAGE CPP #-} - -> module Database.Test.Util where - -> import Database.Util -> import Test.MiniUnit -> import Data.Int -> import Data.Char - - -> runTest :: a -> [String] -> IO () -> runTest _ _ = do -> counts <- runTestTT "Util module tests" testlist -> return () - -> testlist = -> [ testSubstr -> , testWordsBy -> , testInt64ToDatePartsMinDate -> , testInt64ToDatePartsMaxDate -> , testUtcTimeToInt64MinDate -> , testUtcTimeToInt64MaxDate -> , testCalTimeToInt64MinDate -> , testCalTimeToInt64MaxDate -> , testInt64ToCalTimeMinDate -> , testInt64ToCalTimeMaxDate -> , testPGDatetimeToUTCTimeMinDate -> , testPGDatetimeToUTCTimeMaxDate -> , testPGDatetimeToUTCTimeBCBoundary -> , testPGDatetimeToUTCTimeBCBoundary2 -> , testUTCTimeToPGDatetimeMinDate -> , testUTCTimeToPGDatetimeMaxDate -> ] - -> testSubstr = do -> assertEqual "substr - 1" "a" (substr 1 1 "abc") -> assertEqual "substr - 2" "b" (substr 2 1 "abc") -> assertEqual "substr - 3" "c" (substr 3 1 "abc") -> assertEqual "substr - 4" "bc" (substr 2 2 "abc") -> assertEqual "substr - 5" "ab" (substr 1 2 "abc") -> assertEqual "substr - 6" "abc" (substr 1 4 "abc") -> assertEqual "substr -76" "" (substr 4 4 "abc") - -> testWordsBy = do -> assertEqual "wordsBy - 1" [] (wordsBy isDigit "") -> assertEqual "wordsBy - 2" [] (wordsBy isDigit " --,") -> assertEqual "wordsBy - 2.5" ["5"] (wordsBy isDigit "5") -> assertEqual "wordsBy - 2.75" ["5"] (wordsBy isDigit "5 ") -> assertEqual "wordsBy - 3" ["5"] (wordsBy isDigit "5 --,") -> assertEqual "wordsBy - 4" ["6"] (wordsBy isDigit " --,6") -> assertEqual "wordsBy - 5" ["7"] (wordsBy isDigit " --7, ") -> assertEqual "wordsBy - 6" ["100", "12", "67", "25"] (wordsBy isDigit "100-12,67--, 25") - - --4712 (4713BC?) is a very common minimum date among DBMS implementations. -Maximum varies, but is often 5874897 AD. - -ghc-6.4.1 needs this, because it only has Show instances for -up to 5-tuples. - -#if __GLASGOW_HASKELL__ <= 604 -> instance (Show a, Show b, Show c, Show d, Show e, Show f) -> => Show (a,b,c,d,e,f) where -> show (a,b,c,d,e,f) = -> "(" ++ show a -> ++ "," ++ show a -> ++ "," ++ show b -> ++ "," ++ show c -> ++ "," ++ show d -> ++ "," ++ show e -> ++ "," ++ show f -> ++ ")" -#endif - -> testInt64ToDatePartsMinDate = do -> let expect :: (Int64,Int64,Int64,Int64,Int64,Int64); expect = (-4712,1,1,0,0,0) -> assertEqual "testInt64ToDatePartsMinDate" expect (int64ToDateParts (-47120101000000)) - -> testInt64ToDatePartsMaxDate = do -> let expect :: (Int64,Int64,Int64,Int64,Int64,Int64); expect = (999999,12,31,23,59,59) -> assertEqual "testInt64ToDatePartsMaxDate" expect (int64ToDateParts (9999991231235959)) - -> testUtcTimeToInt64MinDate = do -> let expect :: Int64; expect = (-47120101000000) -> assertEqual "testUtcTimeToInt64MinDate" expect (utcTimeToInt64 (mkUTCTime (-4712) 1 1 0 0 0)) - -> testUtcTimeToInt64MaxDate = do -> let expect :: Int64; expect = (9999991231235959) -> assertEqual "testUtcTimeToInt64MaxDate" expect (utcTimeToInt64 (mkUTCTime 999999 12 31 23 59 59)) - -> testCalTimeToInt64MinDate = do -> let expect :: Int64; expect = (-47120101000000) -> assertEqual "testCalTimeToInt64MinDate" expect (calTimeToInt64 (mkCalTime (-4712) 1 1 0 0 0)) - -> testCalTimeToInt64MaxDate = do -> let expect :: Int64; expect = (9999991231235959) -> assertEqual "testCalTimeToInt64MaxDate" expect (calTimeToInt64 (mkCalTime 999999 12 31 23 59 59)) - -> testInt64ToCalTimeMinDate = do -> let expect = mkCalTime (-4712) 1 1 0 0 0 -> assertEqual "testInt64ToCalTimeMinDate" expect (int64ToCalTime (-47120101000000)) - -> testInt64ToCalTimeMaxDate = do -> let expect = mkCalTime 999999 12 31 23 59 59 -> assertEqual "testInt64ToCalTimeMaxDate" expect (int64ToCalTime 9999991231235959) - -> testPGDatetimeToUTCTimeMinDate = do -> let expect = mkUTCTime (-4712) 1 1 0 0 0 -> assertEqual "testPGDatetimeToUTCTimeMinDate" expect (pgDatetimetoUTCTime "4713-01-01 00:00:00 BC") - -> testPGDatetimeToUTCTimeMaxDate = do -> let expect = mkUTCTime 999999 1 1 0 0 0 -> assertEqual "testPGDatetimeToUTCTimeMaxDate" expect (pgDatetimetoUTCTime "999999-01-01 00:00:00+00") - -> testPGDatetimeToUTCTimeBCBoundary = do -> let expect = mkUTCTime 0 1 1 0 0 0 -> assertEqual "testPGDatetimeToUTCTimeBCBoundary" expect (pgDatetimetoUTCTime "0001-01-01 00:00:00 BC") - -> testPGDatetimeToUTCTimeBCBoundary2 = do -> let expect = mkUTCTime 0 12 31 0 0 0 -> assertEqual "testPGDatetimeToUTCTimeBCBoundary2" expect (pgDatetimetoUTCTime "0001-12-31 00:00:00 BC") - -> testUTCTimeToPGDatetimeMinDate = do -> let expect = "4713-01-01T00:00:00.000000+00 BC" -> assertEqual "testUTCTimeToPGDatetimeMinDate" expect (utcTimeToPGDatetime (mkUTCTime (-4712) 1 1 0 0 0)) - -> testUTCTimeToPGDatetimeMaxDate = do -> let expect = "999999-01-01T00:00:00.300001+00 AD" -> assertEqual "testUTCTimeToPGDatetimeMaxDate" expect (utcTimeToPGDatetime (mkUTCTime 999999 1 1 0 0 0.300001))
Foreign/C/Test/UTF8.hs view
@@ -12,8 +12,9 @@ module Foreign.C.Test.UTF8 where -import Prelude hiding (catch) -import Control.Exception.Extensible +import Prelude +import Control.Exception +import Control.Monad(void) import Data.Char import Foreign.C.String import Foreign.C.UTF8 @@ -25,7 +26,7 @@ runTest :: a -> [String] -> IO () -runTest _ args = runTestTT "UTF8" mkTestList >> return () +runTest _ args = void (runTestTT "UTF8" mkTestList) --mkTestList :: Test --mkTestList = TestList (map TestCase testlist) @@ -43,12 +44,11 @@ ] -instance QC.Arbitrary Char where - arbitrary = QC.choose (chr 1, chr 0x10FFFF) - coarbitrary = undefined +-- instance QC.Arbitrary Char where + -- arbitrary = QC.choose (chr 1, chr 0x10FFFF) prop_roundtrip s = s == fromUTF8 (toUTF8 s) -quickCheckUTF8RoundTrip = QC.test prop_roundtrip +quickCheckUTF8RoundTrip = QC.quickCheck prop_roundtrip testUTF8RoundTrip = do utf8RoundTrip "1ByteLow" 0x000001 [0x01]
Foreign/C/UTF8.hs view
@@ -31,7 +31,7 @@ , lengthUTF8, fromUTF8, toUTF8 ) where -import Control.Monad (when, liftM) +import Control.Monad (liftM) import Data.Bits import Data.Char import Data.Word (Word8) @@ -40,14 +40,7 @@ import Foreign.Ptr import Foreign.Marshal.Array import Foreign.Storable - -#ifdef __GLASGOW_HASKELL__ import GHC.Base (unsafeChr) -#else -unsafeChr :: Int -> Char -unsafeChr i = chr i -#endif - nullCChar :: CChar nullCChar = 0
Main.hs view
@@ -16,21 +16,13 @@ -- -- Invoke main like this (assuming the compiled executable is called @takusen@): -- --- > takusen stub noperf --- > takusen sqlite noperf "" "" dbname --- > takusen oracle noperf "" "" dbname -- no username, so os-authenticated --- > takusen mssql noperf user paswd dbname +-- > takusen noperf +-- > takusen noperf "" "" dbname -- no username, so os-authenticated +-- > takusen noperf user paswd dbname module Main (main) where - --- import Database.Sqlite.Test.Enumerator as Sqlite import Database.Oracle.Test.Enumerator as Oracle ---import Database.Test.MultiConnect as Multi --- import Database.ODBC.Test.Enumerator as ODBC -import Database.Stub.Test.Enumerator as Stub ---import Database.MSSqlServer.Test.Enumerator as MSSql ---import Database.PostgreSQL.Test.Enumerator as PGSql import System.Environment (getArgs) import Database.Test.Performance as Perf import Database.Test.Util as Util @@ -40,30 +32,14 @@ main :: IO () main = do args <- getArgs - if (length args < 2) + if length args < 2 then showUsage else do - let (impl:perf:as) = args - doTests impl perf as + let (perf:as) = args + doTests perf as -showUsage = do - putStrLn "usage: takusen backend {no}perf user pswd database" +showUsage = putStrLn "usage: takusen backend {no}perf user pswd database" -doTests impl perf args = do +doTests perf args = do let runPerf = if perf == "perf" then Perf.RunTests else Perf.Don'tRunTests - case lookup impl backendTests of - Nothing -> putStrLn $ "No backend for " ++ impl ++ "." - Just test -> test runPerf args - -backendTests :: [(String, Perf.ShouldRunTests -> [String] -> IO ())] -backendTests = - [ ("sqlite", Sqlite.runTest) - , ("pgsql", PGSql.runTest) - , ("odbc", ODBC.runTest) - --, ("mssql", MSSql.runTest) - , ("oracle", Oracle.runTest) - --, ("multi", Multi.runTest) - , ("stub", Stub.runTest) - , ("utf8", UTF8.runTest) - , ("util", Util.runTest) - ] + Oracle.runTest runPerf args
Setup.hs view
@@ -110,6 +110,7 @@ -- Otherwise, run the second action over the FilePath. guardPath :: IO (Maybe FilePath) -> String -> Verbosity -> (FilePath -> IO BuildInfo) -> IO BuildInfo guardPath pathAction libName verbose resAction = do + putStrLn $ "Looking for "++libName++"..." mb <- pathAction case mb of Nothing -> warn verbose ("No " ++libName++ " library found") >> return emptyBuildInfo @@ -138,11 +139,9 @@ guardPath (maybeGetEnv "ORACLE_HOME") "Oracle" verbose $ \path -> do let fullInstallDirs = ("lib", "rdbms/public") isFullInstall <- doesDirectoryExist (path </> snd fullInstallDirs) - let (libDir, incDir) = - if isWindows - then ("bin", "oci/include") - else if isFullInstall - then fullInstallDirs - else ("", "sdk/include") + let (libDir, incDir) + | isWindows = ("bin", "oci/include") + | isFullInstall = fullInstallDirs + | otherwise = ("", "sdk/include") makeConfig path libDir incDir
takusen-oracle.cabal view
@@ -1,5 +1,5 @@ Name: takusen-oracle -Version: 0.9.1 +Version: 0.9.2 License: BSD3 License-file: LICENSE Author: Alistair Bayley, Oleg Kiselyov, Pavel Ryzhov