Skip to content

Commit 65d09dd

Browse files
committed
Merge pull request #3 from anttih/sqlvalue-maybe
Add IsSqlValue instance for Maybe, move SqlValue stuff to a separate module
2 parents 5a46c71 + 8ff0055 commit 65d09dd

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

MODULE.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ type ConnectionInfo = { password :: String, user :: String, port :: Number, db :
3737
connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client
3838
```
3939

40-
Makes a connection to the database
40+
Makes a connection to the database.
4141

4242
#### `execute`
4343

4444
``` purescript
4545
execute :: forall eff a. Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) Unit
4646
```
4747

48-
Runs a query and returns nothing
48+
Runs a query and returns nothing.
4949

5050
#### `execute_`
5151

@@ -61,7 +61,7 @@ Runs a query and returns nothing
6161
query :: forall eff a p. (IsForeign a) => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) [F a]
6262
```
6363

64-
Runs a query and returns all results
64+
Runs a query and returns all results.
6565

6666
#### `query_`
6767

@@ -93,7 +93,7 @@ Just like `queryOne` but does not make any param replacement
9393
queryValue :: forall eff a. (IsForeign a) => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a)
9494
```
9595

96-
Runs a query and returns a single value, if any
96+
Runs a query and returns a single value, if any.
9797

9898
#### `queryValue_`
9999

@@ -112,6 +112,16 @@ withConnection :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff
112112
Connects to the database, calls the provided function with the client
113113
and returns the results.
114114

115+
#### `end`
116+
117+
``` purescript
118+
end :: forall eff. Client -> Eff (db :: DB | eff) Unit
119+
```
120+
121+
122+
123+
## Module Database.Postgres.SqlValue
124+
115125
#### `SqlValue`
116126

117127
``` purescript
@@ -148,10 +158,10 @@ instance isSqlValueInt :: IsSqlValue Int
148158
```
149159

150160

151-
#### `end`
161+
#### `isSqlValueMaybe`
152162

153163
``` purescript
154-
end :: forall eff. Client -> Eff (db :: DB | eff) Unit
164+
instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a)
155165
```
156166

157167

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)