sql-simple (empty) → 0.1.0.0
raw patch · 6 files changed
+238/−0 lines, 6 filesdep +basedep +exceptionsdep +taggedsetup-changed
Dependencies added: base, exceptions, tagged, text, transformers, unordered-containers
Files
- Database/Sql/Simple.hs +42/−0
- Database/Sql/Simple/Internal.hs +100/−0
- LICENSE +20/−0
- README.md +44/−0
- Setup.hs +2/−0
- sql-simple.cabal +30/−0
+ Database/Sql/Simple.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE ExplicitNamespaces #-}++module Database.Sql.Simple+ ( -- * data type+ -- ** query+ Query+ , specify+ -- ** parameter+ , ToRow+ , FromRow+ , Only(..)+ , (:.)((:.))+ -- ** other+ , Sql+ , Elem+ , Backend+ -- * connection+ , ConnectInfo+ , withConnection+ , connect+ , close+ -- * execute query+ , execute+ , execute_+ , query+ , query_+ -- ** transaction+ , begin+ , commit+ , rollback+ , withTransaction+ -- * specify backend+ -- | + -- @ + -- sql (sqlite +:+ postgresql) $ query+ -- @+ , sql+ , (+:+)+ , type (++)+ ) where++import Database.Sql.Simple.Internal
+ Database/Sql/Simple/Internal.hs view
@@ -0,0 +1,100 @@+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE ConstraintKinds #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE PolyKinds #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE OverlappingInstances #-}++module Database.Sql.Simple.Internal where++import GHC.Exts (Constraint)+import Control.Monad.Catch+import Control.Monad.IO.Class+import Control.Applicative++import qualified Data.Text as T+import Data.Proxy+import Data.Typeable+import Data.String+import qualified Data.HashMap.Strict as H++data Query = Query T.Text (H.HashMap TypeRep T.Text)+ deriving (Show, Eq)++newtype Sql (l :: [*]) a = Sql { unSql :: IO a }+ deriving (Functor, Applicative, Monad, MonadIO, MonadThrow, MonadCatch, MonadMask)++instance IsString Query where+ fromString s = Query (T.pack s) H.empty++getQuery :: TypeRep -> Query -> T.Text+getQuery t (Query d h) = H.lookupDefault d t h++newtype Only a = Only { fromOnly :: a }++data h :. t = h :. t+infixr 3 :.++class Elem a (as :: [*])+instance Elem a (a ': as)+instance Elem a as => Elem a (a' ': as)++withConnection :: (Backend b, Elem b bs) => ConnectInfo b -> (b -> Sql bs a) -> IO a+withConnection i f = bracket (connect i) close (unSql . f)++-- | specify sql backends.+--+sql :: proxy bs -> Sql bs a -> Sql bs a+sql _ m = m++class Typeable b => Backend b where+ data ConnectInfo b+ type ToRow b :: * -> Constraint+ type FromRow b :: * -> Constraint+ + connect :: ConnectInfo b -> IO b+ close :: b -> IO ()+ + execute :: ToRow b q => b -> Query -> q -> Sql c ()+ execute_ :: b -> Query -> Sql c ()+ + query :: (FromRow b r, ToRow b q) => b -> Query -> q -> Sql c [r]+ query_ :: FromRow b r => b -> Query -> Sql c [r]+ + begin :: b -> Sql c ()+ commit :: b -> Sql c ()+ rollback :: b -> Sql c ()+ withTransaction :: b -> Sql c a -> Sql c a+ withTransaction c action = mask $ \restore -> do+ begin c+ r <- restore action `onException` rollback c+ commit c+ return r++type family (a :: [k]) ++ (b :: [k]) :: [k]+type instance '[] ++ bs = bs+type instance (a ': as) ++ bs = a ': as ++ bs++-- | join sql backends.+(+:+) :: Proxy a -> Proxy b -> Proxy (a ++ b)+_ +:+ _ = Proxy++-- | add specified query string to Query.+--+-- example:+-- +-- @+-- q = specify sqlite "sqlite query" "common query"+-- @+specify :: Backend b => proxy ((b :: *) ': '[]) -> T.Text -> Query -> Query+specify p q (Query t h) = Query t (H.insert (headt p) q h)+ where+ headt :: forall proxy a as. Typeable a => proxy ((a :: *) ': as) -> TypeRep+ headt _ = typeOf (undefined :: a)+
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Hirotomo Moriwaki++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,44 @@+sql-simple [](https://travis-ci.org/philopon/sql-simple)+===+common middle-level sql client.++tutorial+===++```.hs+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE ConstraintKinds #-}++import Control.Applicative+import Database.Sql.Simple+import Database.Sql.Simple.SQLite+import Database.Sql.Simple.PostgreSQL++-- you must specify 1st type variable of Sql Monad.+-- by explicit type signature,+testQuery :: (ToRow conn (Only Int), FromRow conn (Only Int), Backend conn) + => conn -> Sql '[SQLite, PostgreSQL] [Int]+testQuery c = do+ execute_ c "CREATE TABLE test (id int)"+ execute c "INSERT INTO test VALUES (?)" (Only (1 :: Int))+ map fromOnly <$> query_ c "SELECT * FROM test"++-- or sql function(testQuery' equivalent to testQuery).+testQuery' c = sql (sqlite +:+ postgreSQL) $ do+ execute_ c "CREATE TABLE test (id int)"+ execute c "INSERT INTO test VALUES (?)" (Only (1 :: Int))+ i <- map fromOnly <$> query_ c "SELECT * FROM test"+ return (i :: [Int])++-- you can specify backend specific Query.+specificQuery :: Backend conn => conn -> Sql '[SQLite, PostgreSQL] ()+specificQuery c =+ execute_ c (specify sqlite "[sqlite query]" "[common query]")++main :: IO ()+main = do+ l <- withConnection ("test.sqlite3" :: ConnectInfo SQLite) testQuery+ -- l <- withConnection (def :: ConnectInfo PostgreSQL) testQuery+ print l+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sql-simple.cabal view
@@ -0,0 +1,30 @@+name: sql-simple+version: 0.1.0.0+synopsis: common middle-level sql client.+description: please read README.md <https://github.com/philopon/sql-simple/blob/master/README.md>+license: MIT+license-file: LICENSE+author: HirotomoMoriwaki<philopon.dependence@gmail.com>+maintainer: HirotomoMoriwaki<philopon.dependence@gmail.com>+Homepage: https://github.com/philopon/sql-simple+Bug-reports: https://github.com/philopon/sql-simple/issues+copyright: (c) 2014 Hirotomo Moriwaki+category: Database+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: Database.Sql.Simple+ Database.Sql.Simple.Internal+ build-depends: base >=4.6 && <4.8+ , exceptions >=0.6 && <0.7+ , text >=1.1 && <1.2+ , transformers >=0.4 && <0.5+ , unordered-containers >=0.2 && <0.3++ if impl(ghc < 7.8)+ build-depends: tagged >=0.7 && <0.8++ ghc-options: -Wall -O2+ default-language: Haskell2010