generated from tpl/purs
type support for jsdate
This commit is contained in:
parent
652cb4f78e
commit
eadcc58da1
@ -13,14 +13,16 @@ import Data.Decimal (Decimal)
|
|||||||
import Data.Decimal as Decimal
|
import Data.Decimal as Decimal
|
||||||
import Data.Either (Either(..), note)
|
import Data.Either (Either(..), note)
|
||||||
import Data.Enum (fromEnum, toEnum)
|
import Data.Enum (fromEnum, toEnum)
|
||||||
import Foreign (Foreign, isNull, readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign, unsafeFromForeign)
|
|
||||||
import Data.Int (fromString)
|
import Data.Int (fromString)
|
||||||
|
import Data.JSDate (JSDate)
|
||||||
import Data.List (List)
|
import Data.List (List)
|
||||||
import Data.List as List
|
import Data.List as List
|
||||||
import Data.Maybe (Maybe(..))
|
import Data.Maybe (Maybe(..))
|
||||||
import Data.String (Pattern(..), split)
|
import Data.String (Pattern(..), split)
|
||||||
|
import Data.Time (Time(..))
|
||||||
import Data.Time.Duration (Milliseconds(..))
|
import Data.Time.Duration (Milliseconds(..))
|
||||||
import Data.Traversable (traverse)
|
import Data.Traversable (traverse)
|
||||||
|
import Foreign (Foreign, isNull, readArray, readBoolean, readChar, readInt, readNumber, readString, unsafeToForeign, unsafeFromForeign)
|
||||||
|
|
||||||
-- | Convert things to SQL values.
|
-- | Convert things to SQL values.
|
||||||
class ToSQLValue a where
|
class ToSQLValue a where
|
||||||
@ -112,6 +114,12 @@ instance fromSQLValueDate :: FromSQLValue Date where
|
|||||||
note msg result
|
note msg result
|
||||||
_ -> Left msg
|
_ -> 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
|
instance toSQLValueMaybe :: (ToSQLValue a) => ToSQLValue (Maybe a) where
|
||||||
toSQLValue Nothing = null
|
toSQLValue Nothing = null
|
||||||
toSQLValue (Just x) = toSQLValue x
|
toSQLValue (Just x) = toSQLValue x
|
||||||
|
@ -8,14 +8,16 @@ import Control.Monad.Error.Class (catchError, throwError, try)
|
|||||||
import Control.Monad.Free (Free)
|
import Control.Monad.Free (Free)
|
||||||
import Data.Array (zip)
|
import Data.Array (zip)
|
||||||
import Data.Date (Date, canonicalDate)
|
import Data.Date (Date, canonicalDate)
|
||||||
|
import Data.DateTime (DateTime(..))
|
||||||
import Data.DateTime.Instant (Instant, unInstant)
|
import Data.DateTime.Instant (Instant, unInstant)
|
||||||
import Data.Decimal as D
|
import Data.Decimal as D
|
||||||
import Data.Enum (toEnum)
|
import Data.Enum (toEnum)
|
||||||
import Data.Foldable (all, length)
|
import Data.Foldable (all, length)
|
||||||
import Data.JSDate (toInstant)
|
import Data.JSDate (JSDate, fromDateTime, toInstant)
|
||||||
import Data.JSDate as JSDate
|
import Data.JSDate as JSDate
|
||||||
import Data.Maybe (Maybe(..), fromJust)
|
import Data.Maybe (Maybe(..), fromJust)
|
||||||
import Data.Newtype (unwrap)
|
import Data.Newtype (unwrap)
|
||||||
|
import Data.Time (Time(..))
|
||||||
import Data.Tuple (Tuple(..))
|
import Data.Tuple (Tuple(..))
|
||||||
import Database.PostgreSQL (Connection, PoolConfiguration, Query(Query), Row0(Row0), Row1(Row1), Row2(Row2), Row3(Row3), Row9(Row9), execute, newPool, query, scalar, withConnection, withTransaction)
|
import Database.PostgreSQL (Connection, PoolConfiguration, Query(Query), Row0(Row0), Row1(Row1), Row2(Row2), Row3(Row3), Row9(Row9), execute, newPool, query, scalar, withConnection, withTransaction)
|
||||||
import Effect (Effect)
|
import Effect (Effect)
|
||||||
@ -51,6 +53,12 @@ test conn t a = Test.Unit.test t (withRollback conn a)
|
|||||||
now ∷ Effect Instant
|
now ∷ Effect Instant
|
||||||
now = unsafePartial $ (fromJust <<< toInstant) <$> JSDate.now
|
now = unsafePartial $ (fromJust <<< toInstant) <$> JSDate.now
|
||||||
|
|
||||||
|
date ∷ Int → Int → Int → Date
|
||||||
|
date y m d = unsafePartial $ fromJust $ canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d
|
||||||
|
|
||||||
|
time ∷ Int → Int → Int → Int → Time
|
||||||
|
time h m s ms = unsafePartial $ fromJust $ Time <$> toEnum h <*> toEnum m <*> toEnum s <*> toEnum ms
|
||||||
|
|
||||||
main ∷ Effect Unit
|
main ∷ Effect Unit
|
||||||
main = void $ launchAff do
|
main = void $ launchAff do
|
||||||
pool <- newPool config
|
pool <- newPool config
|
||||||
@ -66,6 +74,9 @@ main = void $ launchAff do
|
|||||||
CREATE TEMPORARY TABLE dates (
|
CREATE TEMPORARY TABLE dates (
|
||||||
date date NOT NULL
|
date date NOT NULL
|
||||||
);
|
);
|
||||||
|
CREATE TEMPORARY TABLE timestamps (
|
||||||
|
timestamp timestamptz NOT NULL
|
||||||
|
);
|
||||||
""") Row0
|
""") Row0
|
||||||
|
|
||||||
liftEffect $ runTest $ do
|
liftEffect $ runTest $ do
|
||||||
@ -146,11 +157,9 @@ main = void $ launchAff do
|
|||||||
|
|
||||||
test conn "handling date value" $ do
|
test conn "handling date value" $ do
|
||||||
let
|
let
|
||||||
date y m d =
|
d1 = date 2010 2 31
|
||||||
canonicalDate <$> toEnum y <*> toEnum m <*> toEnum d
|
d2 = date 2017 2 1
|
||||||
d1 = unsafePartial $ fromJust $ date 2010 2 31
|
d3 = date 2020 6 31
|
||||||
d2 = unsafePartial $ fromJust $ date 2017 2 1
|
|
||||||
d3 = unsafePartial $ fromJust $ date 2020 6 31
|
|
||||||
|
|
||||||
execute conn (Query """
|
execute conn (Query """
|
||||||
INSERT INTO dates (date)
|
INSERT INTO dates (date)
|
||||||
@ -165,6 +174,24 @@ main = void $ launchAff do
|
|||||||
equal 3 (length dates)
|
equal 3 (length dates)
|
||||||
liftEffect <<< assert $ all (\(Tuple (Row1 r) e) -> e == r) $ (zip dates [d1, d2, d3])
|
liftEffect <<< assert $ all (\(Tuple (Row1 r) e) -> e == r) $ (zip dates [d1, d2, d3])
|
||||||
|
|
||||||
|
test conn "handling jsdate value" $ do
|
||||||
|
let
|
||||||
|
jsd1 = fromDateTime $ DateTime (date 2010 2 31) (time 6 23 1 123)
|
||||||
|
jsd2 = fromDateTime $ DateTime (date 2017 2 1) (time 12 59 42 999)
|
||||||
|
jsd3 = fromDateTime $ DateTime (date 2020 6 31) (time 23 3 59 333)
|
||||||
|
|
||||||
|
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 :: PoolConfiguration
|
||||||
config =
|
config =
|
||||||
|
Loading…
Reference in New Issue
Block a user