Skip to content

Commit

Permalink
Merge pull request #20 from tonyd256/td-sql-array
Browse files Browse the repository at this point in the history
Add IsSqlValue class to Arrays of IsSqlValues
  • Loading branch information
anttih authored Dec 14, 2017
2 parents 59785b0 + 77d9080 commit 1a74a42
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions MODULE.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a)
```


#### `isSqlValueArray`

``` purescript
instance isSqlValueArray :: (IsSqlValue a) => IsSqlValue (Array a)
```



## Module Database.Postgres.Transaction

Expand Down
3 changes: 3 additions & 0 deletions src/Database/Postgres/SqlValue.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a74a42

Please sign in to comment.