diff --git a/MODULE.md b/MODULE.md index 6bbdf29..1ef75ff 100644 --- a/MODULE.md +++ b/MODULE.md @@ -37,7 +37,7 @@ type ConnectionInfo = { password :: String, user :: String, port :: Number, db : connect :: forall eff. ConnectionInfo -> Aff (db :: DB | eff) Client ``` -Makes a connection to the database +Makes a connection to the database. #### `execute` @@ -45,7 +45,7 @@ Makes a connection to the database execute :: forall eff a. Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) Unit ``` -Runs a query and returns nothing +Runs a query and returns nothing. #### `execute_` @@ -61,7 +61,7 @@ Runs a query and returns nothing query :: forall eff a p. (IsForeign a) => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) [F a] ``` -Runs a query and returns all results +Runs a query and returns all results. #### `query_` @@ -93,7 +93,7 @@ Just like `queryOne` but does not make any param replacement queryValue :: forall eff a. (IsForeign a) => Query a -> [SqlValue] -> Client -> Aff (db :: DB | eff) (Maybe a) ``` -Runs a query and returns a single value, if any +Runs a query and returns a single value, if any. #### `queryValue_` @@ -112,6 +112,16 @@ withConnection :: forall eff a. ConnectionInfo -> (Client -> Aff (db :: DB | eff Connects to the database, calls the provided function with the client and returns the results. +#### `end` + +``` purescript +end :: forall eff. Client -> Eff (db :: DB | eff) Unit +``` + + + +## Module Database.Postgres.SqlValue + #### `SqlValue` ``` purescript @@ -148,10 +158,10 @@ instance isSqlValueInt :: IsSqlValue Int ``` -#### `end` +#### `isSqlValueMaybe` ``` purescript -end :: forall eff. Client -> Eff (db :: DB | eff) Unit +instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) ``` diff --git a/src/Database/Postgres.purs b/src/Database/Postgres.purs index 68aa2e1..e7a1ffb 100644 --- a/src/Database/Postgres.purs +++ b/src/Database/Postgres.purs @@ -3,9 +3,6 @@ module Database.Postgres , Client() , DB() , ConnectionInfo() - , SqlValue() - , IsSqlValue - , toSql , connect , end , execute, execute_ @@ -23,13 +20,14 @@ import Data.Array import Data.Foreign import Data.Foreign.Class import Data.Maybe -import Data.Int import Control.Monad.Aff import Control.Monad.Eff.Class import Control.Monad.Eff.Exception(Error(), error) import Control.Monad.Error.Class (throwError) import Data.Traversable (sequence) +import Database.Postgres.SqlValue + newtype Query a = Query String foreign import data Client :: * @@ -123,26 +121,6 @@ finally a sequel = do sequel either throwError pure res -foreign import data SqlValue :: * - -foreign import unsafeToSqlValue """ - function unsafeToSqlValue(x) { - return x; - } - """ :: forall a. a -> SqlValue - -class IsSqlValue a where - toSql :: a -> SqlValue - -instance isSqlValueString :: IsSqlValue String where - toSql = unsafeToSqlValue - -instance isSqlValueNumber :: IsSqlValue Number where - toSql = unsafeToSqlValue - -instance isSqlValueInt :: IsSqlValue Int where - toSql = unsafeToSqlValue <<< toNumber - foreign import connect' """ function connect$prime(conString) { diff --git a/src/Database/Postgres/SqlValue.purs b/src/Database/Postgres/SqlValue.purs new file mode 100644 index 0000000..4dc6757 --- /dev/null +++ b/src/Database/Postgres/SqlValue.purs @@ -0,0 +1,34 @@ +module Database.Postgres.SqlValue + ( SqlValue() + , IsSqlValue + , toSql + ) where + +import Data.Int +import Data.Maybe + +foreign import data SqlValue :: * + +class IsSqlValue a where + toSql :: a -> SqlValue + +instance isSqlValueString :: IsSqlValue String where + toSql = unsafeToSqlValue + +instance isSqlValueNumber :: IsSqlValue Number where + toSql = unsafeToSqlValue + +instance isSqlValueInt :: IsSqlValue Int where + toSql = unsafeToSqlValue <<< toNumber + +instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where + toSql Nothing = nullSqlValue + toSql (Just x) = toSql x + +foreign import unsafeToSqlValue """ + function unsafeToSqlValue(x) { + return x; + } + """ :: forall a. a -> SqlValue + +foreign import nullSqlValue "var nullSqlValue = null;" :: SqlValue diff --git a/test/Main.purs b/test/Main.purs index 7024ce3..e3f03b1 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,6 +1,7 @@ module Test.Main where import Database.Postgres +import Database.Postgres.SqlValue import Debug.Trace import Control.Monad.Eff import Control.Monad.Eff.Class