Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b8a1f83
Revert "allow FromField Text and friends to accept more field types"
jchia Jan 13, 2024
c9cf4c8
Replace .travis.yml with autogenerated github actions config
jhrcek Jan 14, 2024
62363fa
Fix typos in haddock
jhrcek Jan 14, 2024
6c1fe5e
Remove all unused language pragmas
jhrcek Jan 14, 2024
e235123
Improve Errors tests
jhrcek Jan 14, 2024
9b51753
Fix few hlint warnings
jhrcek Jan 14, 2024
448bcf3
Merge pull request #110 from jhrcek/jhrcek/add-github-actions-config-…
jchia Jan 14, 2024
a2d4bbf
Updated stack YAML files
jchia Jan 14, 2024
71379f4
Updated .gitignore
jchia Jan 14, 2024
80be05f
Updated README.markdown for correct GitHub Actions links
jchia Jan 14, 2024
8da8994
Updated README for Hackage badge and list of maintainers
jchia Jan 23, 2024
e529b2f
Bumped version to 0.4.19.0
jchia Jan 23, 2024
c6f91b6
Renamed README.markdown to README.md
jchia Jan 23, 2024
fd9355d
Updated stack resolvers, Haskell CI and some .cabal details
jchia Jan 26, 2024
467e34a
Updated changelog
jchia Mar 3, 2024
1a21d8d
Updated tested-with and haskell-ci.yml
jchia Mar 3, 2024
5c3558e
Fix MonadThrow instance
Shimuuar Apr 2, 2022
7c58a22
Remove noncanonical definitions
rhendric Feb 24, 2025
f9cd5d0
Updated stack resolvers, Haskell CI and some .cabal details
jchia Mar 6, 2026
1887e1e
improve error message for `FromField UTCTime` and `FromField Day`
adamse Oct 6, 2020
749c1f2
Add queryNamedWith and foldNamedWith (#115)
tmcl Mar 17, 2026
6f83e16
Reordered execute, query and fold functions and updated their documen…
jchia Mar 16, 2026
88a6dce
Added returnRowError
jchia Mar 17, 2026
2b7b708
Added 'With' tests
jchia Mar 17, 2026
00292a3
Merge nurpax/sqlite-simple master into sqlcipher
evgeny-simplex Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
cabal-dev/
dist/
TAGS
/*.hs.html
/hpc_*.html
/test.tix
.stack-work/
dist-newstyle/
/dist-newstyle/
/.stack-work/
/stack.yaml.lock
/stack-*.yaml.lock
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

163 changes: 102 additions & 61 deletions Database/SQLite/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,27 @@ module Database.SQLite.Simple (
-- * Queries that return results
, query
, query_
, queryNamed
, queryWith
, queryWith_
, queryNamed
, queryNamedWith
, lastInsertRowId
, changes
, totalChanges
-- * Queries that stream results
, fold
, fold_
, foldNamed
, foldWith
, foldWith_
, foldNamedWith
-- * Statements that do not return results
, execute
, execute_
, executeMany
, executeNamed
, execute_
, field
, returnRowError
-- * Transactions
, withTransaction
, withImmediateTransaction
Expand Down Expand Up @@ -244,7 +249,7 @@ bindNamed (Statement stmt) params = do
reset :: Statement -> IO ()
reset (Statement stmt) = Base.reset stmt

-- | Return the name of a a particular column in the result set of a
-- | Return the name of a particular column in the result set of a
-- 'Statement'. Throws an 'ArrayException' if the colum index is out
-- of bounds.
--
Expand Down Expand Up @@ -314,27 +319,6 @@ withStatementNamedParams :: Connection
withStatementNamedParams conn template namedParams action =
withStatement conn template $ \stmt -> bindNamed stmt namedParams >> action stmt

-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
-- expected to return results.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
execute :: (ToRow q) => Connection -> Query -> q -> IO ()
execute conn template qs =
withStatementParams conn template qs $ \(Statement stmt) ->
void . Base.step $ stmt

-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
-- expected to return results.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
executeMany :: ToRow q => Connection -> Query -> [q] -> IO ()
executeMany conn template paramRows = withStatement conn template $ \stmt -> do
let Statement stmt' = stmt
forM_ paramRows $ \params ->
withBind stmt params
(void . Base.step $ stmt')


doFoldToList :: RowParser row -> Statement -> IO [row]
doFoldToList fromRow_ stmt =
fmap reverse $ doFold fromRow_ stmt [] (\acc e -> return (e : acc))
Expand All @@ -359,41 +343,33 @@ query = queryWith fromRow
query_ :: (FromRow r) => Connection -> Query -> IO [r]
query_ = queryWith_ fromRow

-- | A version of 'query' that takes an explicit 'RowParser'.
queryWith :: (ToRow q) => RowParser r -> Connection -> Query -> q -> IO [r]
queryWith fromRow_ conn templ qs =
withStatementParams conn templ qs $ \stmt -> doFoldToList fromRow_ stmt

-- | A version of 'query' that does not perform query substitution and
-- takes an explicit 'RowParser'.
queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
queryWith_ fromRow_ conn query =
withStatement conn query (doFoldToList fromRow_)

-- | A version of 'query' where the query parameters (placeholders)
-- are named.
-- | A version of 'query' whose query parameters (placeholders) are
-- named.
--
-- Example:
--
-- @
-- r \<- 'queryNamed' c \"SELECT * FROM posts WHERE id=:id AND date>=:date\" [\":id\" ':=' postId, \":date\" ':=' afterDate]
-- @
queryNamed :: (FromRow r) => Connection -> Query -> [NamedParam] -> IO [r]
queryNamed conn templ params =
withStatementNamedParams conn templ params $ \stmt -> doFoldToList fromRow stmt
queryNamed = queryNamedWith fromRow

-- | A version of 'execute' that does not perform query substitution.
execute_ :: Connection -> Query -> IO ()
execute_ conn template =
withStatement conn template $ \(Statement stmt) ->
void $ Base.step stmt
-- | A version of 'query' that takes an explicit 'RowParser'.
queryWith :: (ToRow q) => RowParser r -> Connection -> Query -> q -> IO [r]
queryWith fromRow_ conn templ qs =
withStatementParams conn templ qs $ \stmt -> doFoldToList fromRow_ stmt

-- | A version of 'execute' where the query parameters (placeholders)
-- are named.
executeNamed :: Connection -> Query -> [NamedParam] -> IO ()
executeNamed conn template params =
withStatementNamedParams conn template params $ \(Statement stmt) ->
void $ Base.step stmt
-- | A version of 'query' that takes an explicit 'RowParser' and does
-- not perform query substitution.
queryWith_ :: RowParser r -> Connection -> Query -> IO [r]
queryWith_ fromRow_ conn query =
withStatement conn query (doFoldToList fromRow_)

-- | A version of 'query' whose query parameters (placeholders) are
-- named that takes an explicit 'RowParser'.
queryNamedWith :: RowParser r -> Connection -> Query -> [NamedParam] -> IO [r]
queryNamedWith fromRow_ conn templ params =
withStatementNamedParams conn templ params $ \stmt -> doFoldToList fromRow_ stmt

-- | Perform a @SELECT@ or other SQL query that is expected to return results.
-- Results are converted and fed into the 'action' callback as they are being
Expand All @@ -414,22 +390,18 @@ fold :: ( FromRow row, ToRow params )
-> a
-> (a -> row -> IO a)
-> IO a
fold conn query params initalState action =
withStatementParams conn query params $ \stmt ->
doFold fromRow stmt initalState action
fold = foldWith fromRow

-- | A version of 'fold' which does not perform parameter substitution.
-- | A version of 'fold' that does not perform query substitution.
fold_ :: ( FromRow row )
=> Connection
-> Query
-> a
-> (a -> row -> IO a)
-> IO a
fold_ conn query initalState action =
withStatement conn query $ \stmt ->
doFold fromRow stmt initalState action
fold_ = foldWith_ fromRow

-- | A version of 'fold' where the query parameters (placeholders) are
-- | A version of 'fold' whose query parameters (placeholders) are
-- named.
foldNamed :: ( FromRow row )
=> Connection
Expand All @@ -438,9 +410,45 @@ foldNamed :: ( FromRow row )
-> a
-> (a -> row -> IO a)
-> IO a
foldNamed conn query params initalState action =
foldNamed = foldNamedWith fromRow

-- | A version of 'fold' that takes an explicit 'RowParser'.
foldWith :: ( ToRow params )
=> RowParser row
-> Connection
-> Query
-> params
-> a
-> (a -> row -> IO a)
-> IO a
foldWith fromRow_ conn query params initalState action =
withStatementParams conn query params $ \stmt ->
doFold fromRow_ stmt initalState action

-- | A version of 'fold' that takes an explicit 'RowParser' and does
-- not perform query substitution.
foldWith_ :: RowParser row
-> Connection
-> Query
-> a
-> (a -> row -> IO a)
-> IO a
foldWith_ fromRow_ conn query initalState action =
withStatement conn query $ \stmt ->
doFold fromRow_ stmt initalState action

-- | A version of 'fold' whose query parameters (placeholders) are
-- named that takes an explicit 'RowParser'.
foldNamedWith :: RowParser row
-> Connection
-> Query
-> [NamedParam]
-> a
-> (a -> row -> IO a)
-> IO a
foldNamedWith fromRow_ conn query params initalState action =
withStatementNamedParams conn query params $ \stmt ->
doFold fromRow stmt initalState action
doFold fromRow_ stmt initalState action

doFold :: RowParser row -> Statement -> a -> (a -> row -> IO a) -> IO a
doFold fromRow_ stmt initState action =
Expand All @@ -454,6 +462,39 @@ doFold fromRow_ stmt initState action =
val' `seq` loop val'
Nothing -> return val

-- | Execute an @INSERT@, @UPDATE@, or other SQL query that is not
-- expected to return results.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
execute :: (ToRow q) => Connection -> Query -> q -> IO ()
execute conn template qs =
withStatementParams conn template qs $ \(Statement stmt) ->
void . Base.step $ stmt

-- | Execute a multi-row @INSERT@, @UPDATE@, or other SQL query that is not
-- expected to return results.
--
-- Throws 'FormatError' if the query could not be formatted correctly.
executeMany :: ToRow q => Connection -> Query -> [q] -> IO ()
executeMany conn template paramRows = withStatement conn template $ \stmt -> do
let Statement stmt' = stmt
forM_ paramRows $ \params ->
withBind stmt params
(void . Base.step $ stmt')

-- | A version of 'execute' where the query parameters (placeholders)
-- are named.
executeNamed :: Connection -> Query -> [NamedParam] -> IO ()
executeNamed conn template params =
withStatementNamedParams conn template params $ \(Statement stmt) ->
void $ Base.step stmt

-- | A version of 'execute' that does not perform query substitution.
execute_ :: Connection -> Query -> IO ()
execute_ conn template =
withStatement conn template $ \(Statement stmt) ->
void $ Base.step stmt

-- | Extracts the next row from the prepared statement.
nextRow :: (FromRow r) => Statement -> IO (Maybe r)
nextRow = nextRowWith fromRow
Expand Down Expand Up @@ -806,7 +847,7 @@ getQuery stmt =
--
-- Although SQL can accommodate @NULL@ as a value for any of these
-- types, Haskell cannot. If your result contains columns that may be
-- @NULL@, be sure that you use 'Maybe' in those positions of of your
-- @NULL@, be sure that you use 'Maybe' in those positions of your
-- tuple.
--
-- > (Text, Maybe Int, Int)
Expand Down
12 changes: 6 additions & 6 deletions Database/SQLite/Simple/FromField.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{-# LANGUAGE CPP, DeriveDataTypeable, DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}

------------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -183,7 +183,7 @@ instance FromField UTCTime where
fromField f@(Field (SQLText t) _) =
case parseUTCTime t of
Right t -> Ok t
Left e -> returnError ConversionFailed f ("couldn't parse UTCTime field: " ++ e)
Left e -> returnError ConversionFailed f ("couldn't parse UTCTime field: " ++ e ++ ", field contents: " ++ T.unpack t)

fromField f = returnError ConversionFailed f "expecting SQLText column type"

Expand All @@ -192,7 +192,7 @@ instance FromField Day where
fromField f@(Field (SQLText t) _) =
case parseDay t of
Right t -> Ok t
Left e -> returnError ConversionFailed f ("couldn't parse Day field: " ++ e)
Left e -> returnError ConversionFailed f ("couldn't parse Day field: " ++ e ++ ", field contents: " ++ T.unpack t)

fromField f = returnError ConversionFailed f "expecting SQLText column type"

Expand Down
9 changes: 6 additions & 3 deletions Database/SQLite/Simple/FromRow.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE RecordWildCards, DefaultSignatures, FlexibleContexts,
StandaloneDeriving #-}
{-# LANGUAGE DefaultSignatures, FlexibleContexts #-}

------------------------------------------------------------------------------
-- |
Expand All @@ -24,9 +23,10 @@ module Database.SQLite.Simple.FromRow
, field
, fieldWith
, numFieldsRemaining
, returnRowError
) where

import Control.Exception (SomeException(..))
import Control.Exception (Exception, SomeException(..))
import Control.Monad (replicateM)
import Control.Monad.Trans.State.Strict
import Control.Monad.Trans.Reader
Expand Down Expand Up @@ -135,6 +135,9 @@ numFieldsRemaining = RP $ do
(columnIdx,_) <- lift get
return $! ncols - columnIdx

returnRowError :: Exception err => err -> RowParser a
returnRowError err = RP . lift . lift $ Errors [SomeException err]

instance (FromField a) => FromRow (Only a) where
fromRow = Only <$> field

Expand Down
9 changes: 4 additions & 5 deletions Database/SQLite/Simple/Ok.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-- commonly-used type and typeclass included in @base@.
--
-- Extending the failure case to a list of 'SomeException's enables a
-- more sensible 'Alternative' instance definitions: '<|>' concatinates
-- more sensible 'Alternative' instance definitions: '<|>' concatenates
-- the list of exceptions when both cases fail, and 'empty' is defined as
-- 'Errors []'. Though '<|>' one could pick one of two exceptions, and
-- throw away the other, and have 'empty' provide a generic exception,
Expand Down Expand Up @@ -73,18 +73,17 @@ instance MonadPlus Ok where
mplus = (<|>)

instance Monad Ok where
return = Ok

Errors es >>= _ = Errors es
Ok a >>= f = f a

#if MIN_VERSION_base(4,9,0)
instance MonadFail Ok where
fail str = Errors [SomeException (ErrorCall str)]
#endif

instance MonadThrow Ok where
throwM = fail . show
#endif
throwM = Errors . pure . toException


-- | a way to reify a list of exceptions into a single exception

Expand Down
4 changes: 2 additions & 2 deletions Database/SQLite/Simple/QQ.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TemplateHaskellQuotes #-}

------------------------------------------------------------------------------
-- |
Expand Down Expand Up @@ -27,7 +27,7 @@ import Language.Haskell.TH.Quote (QuasiQuoter (..))
One should consider turning on the @-XQuasiQuotes@ pragma in that module:

@
{-# LANGUAGE QuasiQuoter #-}
{-# LANGUAGE QuasiQuotes #-}

myQuery = query conn [sql|
SELECT
Expand Down
3 changes: 1 addition & 2 deletions Database/SQLite/Simple/ToField.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}

------------------------------------------------------------------------------
-- |
Expand Down
2 changes: 1 addition & 1 deletion Database/SQLite/Simple/Types.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, GeneralizedNewtypeDeriving, CPP #-}
{-# LANGUAGE DeriveDataTypeable, CPP #-}

------------------------------------------------------------------------------
-- |
Expand Down
Loading
Loading