Skip to content

Commit a9e9d4c

Browse files
committed
Get rid of unsafeToSqlValue and use unsafeCoerce, fix tests
1 parent 9f8004d commit a9e9d4c

4 files changed

Lines changed: 13 additions & 19 deletions

File tree

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"purescript-transformers": "^1.0.0",
3434
"purescript-aff": "~0.17.0",
3535
"purescript-integers": "^1.0.0",
36-
"purescript-datetime": "^1.0.0"
36+
"purescript-datetime": "^1.0.0",
37+
"purescript-unsafe-coerce": "~1.0.0"
3738
}
3839
}

src/Database/Postgres/SqlValue.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
'use strict';
22

3-
// module Database.Postgres.SqlValue
4-
5-
exports.unsafeToSqlValue = function (x) {
6-
return x;
7-
}
8-
93
exports.nullSqlValue = null;

src/Database/Postgres/SqlValue.purs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ import Data.Maybe (Maybe(..))
1111
import Data.Date (year, month, day)
1212
import Data.DateTime (DateTime(DateTime))
1313
import Data.Time (hour, minute, second)
14+
import Unsafe.Coerce (unsafeCoerce)
1415

1516
foreign import data SqlValue :: *
1617

1718
class IsSqlValue a where
1819
toSql :: a -> SqlValue
1920

2021
instance isSqlValueString :: IsSqlValue String where
21-
toSql = unsafeToSqlValue
22+
toSql = unsafeCoerce
2223

2324
instance isSqlValueNumber :: IsSqlValue Number where
24-
toSql = unsafeToSqlValue
25+
toSql = unsafeCoerce
2526

2627
instance isSqlValueInt :: IsSqlValue Int where
27-
toSql = unsafeToSqlValue <<< toNumber
28+
toSql = unsafeCoerce <<< toNumber
2829

2930
instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where
3031
toSql Nothing = nullSqlValue
@@ -45,6 +46,4 @@ instance isSqlValueDateTime :: IsSqlValue DateTime where
4546
zeroPad i | i < 10 = "0" <> (show i)
4647
zeroPad i = show i
4748

48-
foreign import unsafeToSqlValue :: forall a. a -> SqlValue
49-
5049
foreign import nullSqlValue :: SqlValue

test/Main.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module Test.Main where
22

3+
import Prelude
34
import Control.Monad.Eff.Console as C
45
import Control.Monad.Aff (Aff, apathize, attempt, runAff)
5-
import Control.Monad.Aff.Console (log, print)
6+
import Control.Monad.Aff.Console (log, logShow)
67
import Control.Monad.Eff (Eff)
78
import Control.Monad.Eff.Class (liftEff)
89
import Control.Monad.Eff.Console (CONSOLE)
@@ -15,11 +16,10 @@ import Data.Maybe (Maybe)
1516
import Database.Postgres (DB, Query(Query), queryOne_, execute_, withConnection, query, withClient, end, query_, connect, queryValue_, disconnect, mkConnectionString)
1617
import Database.Postgres.SqlValue (toSql)
1718
import Database.Postgres.Transaction (withTransaction)
18-
import Prelude (class Show, Unit, return, ($), bind, show, (<>), void, flip, (>>>), const)
1919

2020
main :: forall eff. Eff ( console :: CONSOLE , db :: DB | eff ) Unit
21-
main = runAff C.print (const $ C.log "All ok") $ do
22-
print $ "connecting to " <> mkConnectionString connectionInfo <> "..."
21+
main = runAff C.logShow (const $ C.log "All ok") $ do
22+
logShow $ "connecting to " <> mkConnectionString connectionInfo <> "..."
2323
exampleUsingWithConnection
2424
exampleLowLevel
2525

@@ -51,7 +51,7 @@ exampleUsingWithConnection = withConnection connectionInfo $ \c -> do
5151
execute_ (Query "insert into artist values ('Led Zeppelin', 1968)") c
5252
execute_ (Query "insert into artist values ('Deep Purple', 1968)") c
5353
year <- queryValue_ (Query "insert into artist values ('Fairport Convention', 1967) returning year" :: Query Number) c
54-
print (show year)
54+
logShow (show year)
5555
artists <- query_ (Query "select * from artist" :: Query Artist) c
5656
printRows artists
5757

@@ -83,7 +83,7 @@ exampleTransaction = withConnection connectionInfo $ \c -> do
8383
execute_ (Query "delete from artist") c
8484
apathize $ tryInsert c
8585
one <- queryOne_ (Query "select * from artist" :: Query Artist) c
86-
void $ print one
86+
void $ logShow one
8787
where
8888
tryInsert = withTransaction $ \c -> do
8989
execute_ (Query "insert into artist values ('Not there', 1999)") c
@@ -100,4 +100,4 @@ instance artistIsForeign :: IsForeign Artist where
100100
read obj = do
101101
n <- readProp "name" obj
102102
y <- readProp "year" obj
103-
return $ Artist { name: n, year: y }
103+
pure $ Artist { name: n, year: y }

0 commit comments

Comments
 (0)