Skip to content

Commit 999e4c5

Browse files
committed
Add IsSqlValue instance for Maybe, move SqlValue to its own module
1 parent 5a46c71 commit 999e4c5

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/Database/Postgres.purs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ module Database.Postgres
33
, Client()
44
, DB()
55
, ConnectionInfo()
6-
, SqlValue()
7-
, IsSqlValue
8-
, toSql
96
, connect
107
, end
118
, execute, execute_
@@ -23,13 +20,14 @@ import Data.Array
2320
import Data.Foreign
2421
import Data.Foreign.Class
2522
import Data.Maybe
26-
import Data.Int
2723
import Control.Monad.Aff
2824
import Control.Monad.Eff.Class
2925
import Control.Monad.Eff.Exception(Error(), error)
3026
import Control.Monad.Error.Class (throwError)
3127
import Data.Traversable (sequence)
3228

29+
import Database.Postgres.SqlValue
30+
3331
newtype Query a = Query String
3432

3533
foreign import data Client :: *
@@ -123,26 +121,6 @@ finally a sequel = do
123121
sequel
124122
either throwError pure res
125123

126-
foreign import data SqlValue :: *
127-
128-
foreign import unsafeToSqlValue """
129-
function unsafeToSqlValue(x) {
130-
return x;
131-
}
132-
""" :: forall a. a -> SqlValue
133-
134-
class IsSqlValue a where
135-
toSql :: a -> SqlValue
136-
137-
instance isSqlValueString :: IsSqlValue String where
138-
toSql = unsafeToSqlValue
139-
140-
instance isSqlValueNumber :: IsSqlValue Number where
141-
toSql = unsafeToSqlValue
142-
143-
instance isSqlValueInt :: IsSqlValue Int where
144-
toSql = unsafeToSqlValue <<< toNumber
145-
146124

147125
foreign import connect' """
148126
function connect$prime(conString) {

src/Database/Postgres/SqlValue.purs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Database.Postgres.SqlValue
2+
( SqlValue()
3+
, IsSqlValue
4+
, toSql
5+
) where
6+
7+
import Data.Int
8+
import Data.Maybe
9+
10+
foreign import data SqlValue :: *
11+
12+
class IsSqlValue a where
13+
toSql :: a -> SqlValue
14+
15+
instance isSqlValueString :: IsSqlValue String where
16+
toSql = unsafeToSqlValue
17+
18+
instance isSqlValueNumber :: IsSqlValue Number where
19+
toSql = unsafeToSqlValue
20+
21+
instance isSqlValueInt :: IsSqlValue Int where
22+
toSql = unsafeToSqlValue <<< toNumber
23+
24+
instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where
25+
toSql Nothing = nullSqlValue
26+
toSql (Just x) = toSql x
27+
28+
foreign import unsafeToSqlValue """
29+
function unsafeToSqlValue(x) {
30+
return x;
31+
}
32+
""" :: forall a. a -> SqlValue
33+
34+
foreign import nullSqlValue "var nullSqlValue = null;" :: SqlValue

test/Main.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Test.Main where
22

33
import Database.Postgres
4+
import Database.Postgres.SqlValue
45
import Debug.Trace
56
import Control.Monad.Eff
67
import Control.Monad.Eff.Class

0 commit comments

Comments
 (0)