mssql-simple 0.5.0.0 → 0.5.0.1
raw patch · 3 files changed
+201/−12 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- mssql-simple.cabal +2/−2
- src/Database/MSSQLServer/Connection.hs +44/−9
- src/Database/MSSQLServer/Query.hs +155/−1
mssql-simple.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: f7f0ea35f8fb135356688fa555518383d0e49559703ffc5bf54c001806938f54+-- hash: e2009f3f703bbefbce95e2f4d5d870bab30e35886b962643c3bacda549dc8c78 name: mssql-simple-version: 0.5.0.0+version: 0.5.0.1 synopsis: SQL Server client library implemented in Haskell description: Please see the README on GitHub at <https://github.com/mitsuji/mssql-simple#readme> category: Database
src/Database/MSSQLServer/Connection.hs view
@@ -1,16 +1,27 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveDataTypeable #-} -module Database.MSSQLServer.Connection ( ConnectInfo(..)- , defaultConnectInfo- , Connection(..)- , connect- , connectWithoutEncryption- , close- , ProtocolError(..)- , AuthError(..)- ) where+-- |+-- SQL Server client library implemented in Haskell+--+-- [Usage Example](https://github.com/mitsuji/mssql-simple-example/blob/master/app/Main.hs) ++module Database.MSSQLServer.Connection+ (+ -- * Connect with the SQL Server+ -- $use++ ConnectInfo(..)+ , defaultConnectInfo+ , Connection(..)+ , connect+ , connectWithoutEncryption+ , close+ , ProtocolError(..)+ , AuthError(..)+ ) where+ import qualified Network.Socket as Socket import Network.Socket (AddrInfo(..),SocketType(..),Socket(..)) import Network.Socket.ByteString (recv)@@ -282,3 +293,27 @@ +-- $use+-- 'connect' and 'close' function could be used as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- > module Main where+-- >+-- > import Network.Socket (withSocketsDo)+-- > import Control.Exception (bracket)+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > main :: IO ()+-- > main = do+-- > let info = defaultConnectInfo { connectHost = "192.168.0.1"+-- > , connectPort = "1433"+-- > , connectDatabase = "some_database"+-- > , connectUser = "some_user"+-- > , connectPassword = "some_password"+-- > }+-- > withSocketsDo $+-- > bracket (connect info) close $ \conn -> do+-- > rs <- sql conn "SELECT 2 + 2" :: IO [Only Int]+-- > print rs
src/Database/MSSQLServer/Query.hs view
@@ -1,7 +1,15 @@ {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE CPP #-} -module Database.MSSQLServer.Query ( -- * SQL Text Query+-- |+-- SQL Server client library implemented in Haskell+--+-- [Usage Example](https://github.com/mitsuji/mssql-simple-example/blob/master/app/Main.hs)+++module Database.MSSQLServer.Query (+ -- * SQL Text Query+ -- $use sql -- ** ResultSet@@ -199,3 +207,149 @@ decoder' -> readMessage sock decoder' +++-- $use+-- A 'sql' function accepts valid 'Connection' and SQL text.+-- And the expression could be evaluated as a instance of 'ResultSet' type class.+--+-- A 'Row' represents a record included in a query result.+-- Also list of 'Row' is a instance of 'ResultSet'.+-- So the result of select query could be obtained as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_select1 :: Connection -> IO Int+-- > query_select1 conn = do+-- > [Only i] <- sql conn "SELECT 2 + 2" :: IO [Only Int]+-- > return i+--+-- In SQL , uncomputable expression could be evaluated as NULL.+-- In that case, 'Maybe' type could be used.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_select2 :: Connection -> IO (Maybe Int)+-- > query_select2 conn = do+-- > [Only mi] <- sql conn "SELECT 6 / 2" :: IO [Only (Maybe Int)]+-- > return mi+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_select3 :: Connection -> IO (Maybe Int)+-- > query_select3 conn = do+-- > [Only mi] <- sql conn "SELECT 6 / 0" :: IO [Only (Maybe Int)]+-- > return mi+--+-- The result of select query from SQL table could be obtained as follows.+-- A member of tuple must be a instance of 'Data' type class+-- and convertible with the SQL data type of a SQL table column.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > import qualified Data.Text.Lazy as LT+-- > import Database.Tds.Message+-- > import Data.Time (UTCTime(..))+-- >+-- > query_select4 :: Connection -> IO [(Int,String,LT.Text,Money,UTCTime,Maybe UTCTime,Maybe UTCTime)]+-- > query_select4 conn = sql conn "SELECT * FROM TSome ORDER BY somePrice"+--+-- Any type could be a instance of 'Row' and used as the result of select query as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- > {-# LANGUAGE BangPatterns #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > import qualified Data.Text.Lazy as LT+-- > import Database.Tds.Message+-- > import Data.Time (UTCTime(..))+-- >+-- > data Some = Some { someID :: Int+-- > , someTitle :: LT.Text+-- > , someContent :: LT.Text+-- > , somePrice :: Money+-- > , someCreated:: UTCTime+-- > , someModified:: Maybe UTCTime+-- > , someDeleted:: Maybe UTCTime+-- > }+-- > deriving (Show)+-- >+-- > instance Row Some where+-- > fromListOfRawBytes [m1,m2,m3,m4,m5,m6,m7] [b1,b2,b3,b4,b5,b6,b7] = Some d1 d2 d3 d4 d5 d6 d7+-- > where+-- > !d1 = fromRawBytes (mcdTypeInfo m1) b1+-- > !d2 = fromRawBytes (mcdTypeInfo m2) b2+-- > !d3 = fromRawBytes (mcdTypeInfo m3) b3+-- > !d4 = fromRawBytes (mcdTypeInfo m4) b4+-- > !d5 = fromRawBytes (mcdTypeInfo m5) b5+-- > !d6 = fromRawBytes (mcdTypeInfo m6) b6+-- > !d7 = fromRawBytes (mcdTypeInfo m7) b7+-- >+-- > mcdTypeInfo :: MetaColumnData -> TypeInfo+-- > mcdTypeInfo (MetaColumnData _ _ ti _ _) = ti+-- >+-- > fromListOfRawBytes _ _ = error "fromListOfRawBytes: List length must be 7"+-- >+-- > query_select5 :: Connection -> IO [Some]+-- > query_select5 conn = sql conn "SELECT TOP 10 * FROM TSome ORDER BY somePrice DESC"+--+-- 'Row' is also a instance of 'Result' and tuple of 'Result' is a instance of 'ResultSet'.+-- So the result of multiple SQL query could be obtained simultaneously as follows.+--+-- > import Data.Monoid (mconcat)+-- >+-- > query_select6 :: Connection -> IO ([Some],[Some])+-- > query_select6 conn =+-- > sql conn $ mconcat ["SELECT * FROM TSome WHERE someID < 8 ORDER BY someID;",+-- > "SELECT * FROM TSome WHERE 8 <= someID AND someID < 12 ORDER BY someID DESC;"]+--+-- 'RowCount' is a instance of 'ResultSet'.+-- So the count of SQL table record affected with the SQL query could be obtainde as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_count1 :: Connection -> IO Int+-- > query_count1 conn = do+-- > RowCount rc <- sql conn "UPDATE TSome SET somePrice = somePrice + 100 WHERE someID < 5"+-- > return rc+--+-- Also () is a instance of 'ResultSet'.+-- So the result of SQL query could be discarded as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_discard1 :: Connection -> IO ()+-- > query_discard1 conn = sql conn "UPDATE TSome SET somePrice = somePrice + 100 WHERE someID < 5"+--+-- 'ReturnStatus' is a instance of 'ResultSet'.+-- So when a stored procedure executed in SQL text, Return status of the execution could be obtained as follows.+--+-- > {-# LANGUAGE OverloadedStrings #-}+-- >+-- > import Database.MSSQLServer.Connection+-- > import Database.MSSQLServer.Query+-- >+-- > query_status1 :: Connection -> IO Int+-- > query_status1 conn = do+-- > ReturnStatus rets <- sql conn "EXEC SP_Input1 @Val1=3"+-- > return rets