Merge pull request #21 from Kamirus/master

type support for jsdate
This commit is contained in:
paluh 2018-09-05 18:19:02 +02:00 committed by GitHub
commit eb937a4a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 7 deletions

View File

@ -13,14 +13,16 @@ import Data.Decimal (Decimal)
import Data.Decimal as Decimal
import Data.Either (Either(..), note)
import Data.Enum (fromEnum, toEnum)
import Foreign (Foreign, isNull, readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign, unsafeFromForeign)
import Data.Int (fromString)
import Data.JSDate (JSDate)
import Data.List (List)
import Data.List as List
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), split)
import Data.Time (Time(..))
import Data.Time.Duration (Milliseconds(..))
import Data.Traversable (traverse)
import Foreign (Foreign, isNull, readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign, unsafeFromForeign)
-- | Convert things to SQL values.
class ToSQLValue a where
@ -112,6 +114,12 @@ instance fromSQLValueDate :: FromSQLValue Date where
note msg result
_ -> Left msg
instance toSQLValueJSDate :: ToSQLValue JSDate where
toSQLValue = unsafeToForeign
instance fromSQLValueJSDate :: FromSQLValue JSDate where
fromSQLValue = Right <<< unsafeFromForeign
instance toSQLValueMaybe :: (ToSQLValue a) => ToSQLValue (Maybe a) where
toSQLValue Nothing = null
toSQLValue (Just x) = toSQLValue x

View File

@ -12,7 +12,7 @@ import Data.DateTime.Instant (Instant, unInstant)
import Data.Decimal as D
import Data.Enum (toEnum)
import Data.Foldable (all, length)
import Data.JSDate (toInstant)
import Data.JSDate (JSDate, jsdate, toInstant)
import Data.JSDate as JSDate
import Data.Maybe (Maybe(..), fromJust)
import Data.Newtype (unwrap)
@ -51,6 +51,13 @@ test conn t a = Test.Unit.test t (withRollback conn a)
now Effect Instant
now = unsafePartial $ (fromJust <<< toInstant) <$> JSDate.now
date Int Int Int Date
date y m d = unsafePartial $ fromJust $ canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d
jsdate_ Number Number Number Number Number Number Number JSDate
jsdate_ year month day hour minute second millisecond =
jsdate { year, month, day, hour, minute, second, millisecond }
main Effect Unit
main = void $ launchAff do
pool <- newPool config
@ -66,6 +73,9 @@ main = void $ launchAff do
CREATE TEMPORARY TABLE dates (
date date NOT NULL
);
CREATE TEMPORARY TABLE timestamps (
timestamp timestamptz NOT NULL
);
""") Row0
liftEffect $ runTest $ do
@ -146,11 +156,9 @@ main = void $ launchAff do
test conn "handling date value" $ do
let
date y m d =
canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d
d1 = unsafePartial $ fromJust $ date 2010 2 31
d2 = unsafePartial $ fromJust $ date 2017 2 1
d3 = unsafePartial $ fromJust $ date 2020 6 31
d1 = date 2010 2 31
d2 = date 2017 2 1
d3 = date 2020 6 31
execute conn (Query """
INSERT INTO dates (date)
@ -165,6 +173,24 @@ main = void $ launchAff do
equal 3 (length dates)
liftEffect <<< assert $ all (\(Tuple (Row1 r) e) -> e == r) $ (zip dates [d1, d2, d3])
test conn "handling jsdate value" $ do
let
jsd1 = jsdate_ 2010.0 2.0 31.0 6.0 23.0 1.0 123.0
jsd2 = jsdate_ 2017.0 2.0 1.0 12.0 59.0 42.0 999.0
jsd3 = jsdate_ 2020.0 6.0 31.0 23.0 3.0 59.0 333.0
execute conn (Query """
INSERT INTO timestamps (timestamp)
VALUES ($1), ($2), ($3)
""") (Row3 jsd1 jsd2 jsd3)
(timestamps :: Array (Row1 JSDate)) <- query conn (Query """
SELECT *
FROM timestamps
ORDER BY timestamp ASC
""") Row0
equal 3 (length timestamps)
liftEffect <<< assert $ all (\(Tuple (Row1 r) e) -> e == r) $ (zip timestamps [jsd1, jsd2, jsd3])
config :: PoolConfiguration
config =