-
Notifications
You must be signed in to change notification settings - Fork 107
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
Add selectOne
#265
Changes from 5 commits
b04b88b
b9fbd02
0d266aa
7d55bfb
2780c86
9693f21
206fb82
b1c6fc0
7622e68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2539,6 +2539,34 @@ 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@. | ||||||||||||||||||
-- | ||||||||||||||||||
-- === __Example usage__ | ||||||||||||||||||
-- | ||||||||||||||||||
-- @ | ||||||||||||||||||
-- firstPerson :: MonadIO m => SqlPersistT m (Maybe Person) | ||||||||||||||||||
-- firstPerson = | ||||||||||||||||||
-- 'selectFirst' $ | ||||||||||||||||||
-- 'from' $ \person -> | ||||||||||||||||||
-- return person | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as below - let's use the |
||||||||||||||||||
-- @ | ||||||||||||||||||
-- | ||||||||||||||||||
-- 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 [Person] | ||||||||||||||||||
-- firstPerson = | ||||||||||||||||||
-- 'select' $ | ||||||||||||||||||
-- 'from' $ \person -> do | ||||||||||||||||||
-- 'limit' 1 | ||||||||||||||||||
-- return person | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use experimental syntax for the new examples.
Suggested change
|
||||||||||||||||||
-- @ | ||||||||||||||||||
|
||||||||||||||||||
selectFirst :: (SqlSelect a r, MonadIO m) => SqlQuery a -> SqlReadT m (Maybe r) | ||||||||||||||||||
selectFirst query = fmap Maybe.listToMaybe $ select $ limit 1 >> query | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also like the idea of naming this function It's such a useful function, I'd like to expose it now. We can do this with a minor version bump (3.5.1.0), as long as it does not conflict. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds great to me 🎉 will rename it accordingly and remove the unnecessary |
||||||||||||||||||
|
||||||||||||||||||
-- | (Internal) Run a 'C.Source' of rows. | ||||||||||||||||||
runSource | ||||||||||||||||||
:: Monad m | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -98,6 +98,7 @@ module Database.Esqueleto.Legacy | |||||
, SqlExpr | ||||||
, SqlEntity | ||||||
, select | ||||||
, selectFirst | ||||||
, selectSource | ||||||
, delete | ||||||
, deleteCount | ||||||
|
@@ -130,7 +131,7 @@ module Database.Esqueleto.Legacy | |||||
) where | ||||||
|
||||||
import Database.Esqueleto.Internal.Internal | ||||||
import Database.Esqueleto.Internal.PersistentImport | ||||||
import Database.Esqueleto.Internal.PersistentImport hiding (selectFirst) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue here (and elsewhere) - let's keep this an open import and hide the export at the source.
Suggested change
|
||||||
|
||||||
|
||||||
-- $setup | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -297,6 +297,27 @@ testSubSelect = do | |
Right xs -> | ||
xs `shouldBe` [] | ||
|
||
testSelectFirst :: SpecDb | ||
testSelectFirst = | ||
describe "selectFirst" $ do | ||
let personQuery = | ||
selectFirst $ | ||
from $ \person -> do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you change this test query to use the Experimental syntax please. Were in the process of deprecating the old syntax so it will eventually need to change anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome |
||
|
||
testSelectSource :: SpecDb | ||
testSelectSource = do | ||
describe "selectSource" $ do | ||
|
@@ -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 | ||
testSelectFirst | ||
testSelectSource | ||
testSelectFrom | ||
testSelectJoin | ||
|
@@ -2389,4 +2411,3 @@ shouldBeOnClauseWithoutMatchingJoinException ea = | |
pure () | ||
_ -> | ||
expectationFailure $ "Expected OnClauseWithMatchingJoinException, got: " <> show ea | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of hiding the import, we should just not re-export it from
PersistentImpot