Skip to content

Commit 1a74a42

Browse files
authored
Merge pull request #20 from tonyd256/td-sql-array
Add IsSqlValue class to Arrays of IsSqlValues
2 parents 59785b0 + 77d9080 commit 1a74a42

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

MODULE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a)
195195
```
196196

197197

198+
#### `isSqlValueArray`
199+
200+
``` purescript
201+
instance isSqlValueArray :: (IsSqlValue a) => IsSqlValue (Array a)
202+
```
203+
204+
198205

199206
## Module Database.Postgres.Transaction
200207

src/Database/Postgres/SqlValue.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ instance isSqlValueInt :: IsSqlValue Int where
3131
instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where
3232
toSql = unsafeCoerce <<< toNullable <<< (toSql <$> _)
3333

34+
instance isSqlValueArray :: (IsSqlValue a) => IsSqlValue (Array a) where
35+
toSql = unsafeCoerce <<< map toSql
36+
3437
instance isSqlValueDateTime :: IsSqlValue DateTime where
3538
toSql = toSql <<< format
3639
where

test/Main.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ main = run [consoleReporter] do
128128
) dt
129129
liftEff $ end pool
130130

131+
describe "sql arrays as parameters" $
132+
it "can be passed as a SqlValue" do
133+
pool <- liftEff $ mkPool connectionInfo
134+
withClient pool \c -> do
135+
execute_ (Query "delete from artist") c
136+
execute_ (Query "insert into artist values ('Zed Leppelin', 1967)") c
137+
execute_ (Query "insert into artist values ('Led Zeppelin', 1968)") c
138+
execute_ (Query "insert into artist values ('Deep Purple', 1969)") c
139+
artists <- query (Query "select * from artist where year = any ($1)" :: Query Artist) [toSql [1968, 1969]] c
140+
length artists `shouldEqual` 2
141+
liftEff $ end pool
131142

132143
describe "transactions" do
133144
it "does not commit after an error inside a transation" do

0 commit comments

Comments
 (0)