From 77d90803ceb9491f5a21f12b21a3fb9733c4c364 Mon Sep 17 00:00:00 2001 From: Tony DiPasquale Date: Fri, 8 Dec 2017 13:27:37 -0800 Subject: [PATCH] Add IsSqlValue class to Arrays of IsSqlValues --- MODULE.md | 7 +++++++ src/Database/Postgres/SqlValue.purs | 3 +++ test/Main.purs | 11 +++++++++++ 3 files changed, 21 insertions(+) diff --git a/MODULE.md b/MODULE.md index fa84b62..5892e0a 100644 --- a/MODULE.md +++ b/MODULE.md @@ -195,6 +195,13 @@ instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) ``` +#### `isSqlValueArray` + +``` purescript +instance isSqlValueArray :: (IsSqlValue a) => IsSqlValue (Array a) +``` + + ## Module Database.Postgres.Transaction diff --git a/src/Database/Postgres/SqlValue.purs b/src/Database/Postgres/SqlValue.purs index 1292b68..b03415a 100644 --- a/src/Database/Postgres/SqlValue.purs +++ b/src/Database/Postgres/SqlValue.purs @@ -31,6 +31,9 @@ instance isSqlValueInt :: IsSqlValue Int where instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where toSql = unsafeCoerce <<< toNullable <<< (toSql <$> _) +instance isSqlValueArray :: (IsSqlValue a) => IsSqlValue (Array a) where + toSql = unsafeCoerce <<< map toSql + instance isSqlValueDateTime :: IsSqlValue DateTime where toSql = toSql <<< format where diff --git a/test/Main.purs b/test/Main.purs index 733a1ab..5b3491f 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -128,6 +128,17 @@ main = run [consoleReporter] do ) dt liftEff $ end pool + describe "sql arrays as parameters" $ + it "can be passed as a SqlValue" do + pool <- liftEff $ mkPool connectionInfo + withClient pool \c -> do + execute_ (Query "delete from artist") c + execute_ (Query "insert into artist values ('Zed Leppelin', 1967)") c + execute_ (Query "insert into artist values ('Led Zeppelin', 1968)") c + execute_ (Query "insert into artist values ('Deep Purple', 1969)") c + artists <- query (Query "select * from artist where year = any ($1)" :: Query Artist) [toSql [1968, 1969]] c + length artists `shouldEqual` 2 + liftEff $ end pool describe "transactions" do it "does not commit after an error inside a transation" do