Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selectOne #265

Merged
merged 9 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.5.1.0
=======
- @ibarrae
- [#265](https://github.com/bitemyapp/esqueleto/pull/265)
- Added `selectOne`

3.5.0.0
=======
- @belevy
Expand Down
14 changes: 7 additions & 7 deletions esqueleto.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 1.12

name: esqueleto
version: 3.5.0.0
version: 3.5.1.0
synopsis: Type-safe EDSL for SQL queries on persistent backends.
description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime.
.
Expand Down Expand Up @@ -102,7 +102,7 @@ test-suite specs
, attoparsec
, blaze-html
, bytestring
, conduit
, conduit
, containers
, esqueleto
, exceptions
Expand All @@ -118,11 +118,11 @@ test-suite specs
, persistent-sqlite
, postgresql-simple
, QuickCheck
, resourcet
, tagged
, text
, resourcet
, tagged
, text
, time
, transformers
, transformers
, unliftio
, unordered-containers
, unordered-containers
default-language: Haskell2010
1 change: 1 addition & 0 deletions src/Database/Esqueleto.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ module Database.Esqueleto {-# WARNING "This module will switch over to the Exper
, SqlExpr
, SqlEntity
, select
, selectOne
, selectSource
, delete
, deleteCount
Expand Down
1 change: 1 addition & 0 deletions src/Database/Esqueleto/Experimental.hs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ module Database.Esqueleto.Experimental
, SqlExpr
, SqlEntity
, select
, selectOne
, selectSource
, delete
, deleteCount
Expand Down
29 changes: 29 additions & 0 deletions src/Database/Esqueleto/Internal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,35 @@ select query = do
conn <- R.ask
liftIO $ with res $ flip R.runReaderT conn . runSource

-- | Execute an @esqueleto@ @SELECT@ query inside @persistent@'s
-- 'SqlPersistT' monad and return the first entry wrapped in a @Maybe@.
-- @since 3.5.1.0
--
-- === __Example usage__
--
-- @
-- firstPerson :: MonadIO m => SqlPersistT m (Maybe (Entity Person))
-- firstPerson =
-- 'selectOne' $ do
-- person <- 'from' $ 'table' @Person
-- return person
-- @
--
-- The above query is equivalent to a 'select' combined with 'limit' but you
-- would still have to transform the results from a list:
--
-- @
-- firstPerson :: MonadIO m => SqlPersistT m [Entity Person]
-- firstPerson =
-- 'select' $ do
-- person <- 'from' $ 'table' @Person
-- 'limit' 1
-- return person
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fantastic 😄

-- @

selectOne :: (SqlSelect a r, MonadIO m) => SqlQuery a -> SqlReadT m (Maybe r)
selectOne query = fmap Maybe.listToMaybe $ select $ limit 1 >> query

-- | (Internal) Run a 'C.Source' of rows.
runSource
:: Monad m
Expand Down
1 change: 1 addition & 0 deletions src/Database/Esqueleto/Legacy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module Database.Esqueleto.Legacy
, SqlExpr
, SqlEntity
, select
, selectOne
, selectSource
, delete
, deleteCount
Expand Down
25 changes: 23 additions & 2 deletions test/Common/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,27 @@ testSubSelect = do
Right xs ->
xs `shouldBe` []

testSelectOne :: SpecDb
testSelectOne =
describe "selectOne" $ do
let personQuery =
selectOne $ do
person <- Experimental.from $ Experimental.table @Person
where_ $ person ^. PersonFavNum >=. val 1
orderBy [asc (person ^. PersonId)]
return $ person ^. PersonId
itDb "returns Just" $ do
person <- insert' p1
_ <- insert' p2
res <- personQuery
asserting $
res `shouldBe` Just (Value $ entityKey person)

itDb "returns Nothing" $ do
res <- personQuery
asserting $
res `shouldBe` (Nothing :: Maybe (Value PersonId))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome


testSelectSource :: SpecDb
testSelectSource = do
describe "selectSource" $ do
Expand Down Expand Up @@ -2271,10 +2292,11 @@ listsEqualOn :: (HasCallStack, Show a1, Eq a1) => [a2] -> [a2] -> (a2 -> a1) ->
listsEqualOn a b f = map f a `shouldBe` map f b

tests :: SpecDb
tests = do
tests =
describe "Esqueleto" $ do
testSelect
testSubSelect
testSelectOne
testSelectSource
testSelectFrom
testSelectJoin
Expand Down Expand Up @@ -2389,4 +2411,3 @@ shouldBeOnClauseWithoutMatchingJoinException ea =
pure ()
_ ->
expectationFailure $ "Expected OnClauseWithMatchingJoinException, got: " <> show ea