sql-simple: common middle-level sql client.

[ database, library, mit ] [ Propose Tags ]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.2.0, 0.3.0, 0.3.1
Dependencies base (>=4.6 && <4.8), containers (>=0.5 && <0.6), exceptions (>=0.6 && <0.7), monad-control (>=0.3 && <0.4), tagged (>=0.7 && <0.8), text (>=1.1 && <1.2), transformers (>=0.4 && <0.5), transformers-base (>=0.4 && <0.5) [details]
License MIT
Copyright (c) 2014 Hirotomo Moriwaki
Author HirotomoMoriwaki<philopon.dependence@gmail.com>
Maintainer HirotomoMoriwaki<philopon.dependence@gmail.com>
Category Database
Home page https://github.com/philopon/sql-simple
Bug tracker https://github.com/philopon/sql-simple/issues
Uploaded by HirotomoMoriwaki at 2014-07-10T13:48:09Z
Distributions NixOS:0.3.1
Downloads 2814 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Successful builds reported [all 1 reports]

Readme for sql-simple-0.3.1

[back to package description]

sql-simple Build Status

common middle-level sql client.

tutorial

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ConstraintKinds #-}

import Control.Applicative
import Database.Sql.Simple
import Database.Sql.Simple.SQLite
import Database.Sql.Simple.PostgreSQL
import Database.Sql.Simple.Pool

-- 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 (ConnectionPool def "test.sqlite3" :: ConnectInfo (Pool SQLite)) testQuery
    -- l <- withConnection (def :: ConnectInfo PostgreSQL) testQuery
    print l