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

Mattp/incorporate cte aliasing #410

Merged
merged 8 commits into from
Dec 26, 2024
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
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
- @blujupiter32
- [#379](https://github.com/bitemyapp/esqueleto/pull/379)
- Fix a bug where `not_ (a &&. b)` would be interpeted as `(not_ a) &&. b`
- @RikvanToor
- [#373](https://github.com/bitemyapp/esqueleto/pull/373), [#410](https://github.com/bitemyapp/esqueleto/pull/410)
- Fix name clashes when using CTEs multiple times
- @TeofilC
- [#394](https://github.com/bitemyapp/esqueleto/pull/394)
- Use TH quotes to eliminate some CPP.
Expand Down
1 change: 1 addition & 0 deletions esqueleto.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ test-suite specs
main-is: Spec.hs
other-modules:
Common.Test
Common.Test.CTE
Common.Test.Models
Common.Test.Import
Common.Test.Select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ with query = do
let clause = CommonTableExpressionClause NormalCommonTableExpression ident (\info -> toRawSql SELECT info aliasedQuery)
Q $ W.tell mempty{sdCteClause = [clause]}
ref <- toAliasReference ident aliasedValue
pure $ From $ pure (ref, (\_ info -> (useIdent info ident, mempty)))
pure $ From $ do
newIdent <- newIdentFor (DBName "cte")
localRef <- toAliasReference newIdent ref
let makeLH info = useIdent info ident <> " AS " <> useIdent info newIdent
pure (localRef, (\_ info -> (makeLH info, mempty)))

-- | @WITH@ @RECURSIVE@ allows one to make a recursive subquery, which can
-- reference itself. Like @WITH@, this is supported in most modern SQL engines.
Expand Down
2 changes: 2 additions & 0 deletions test/Common/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@
import qualified Database.Esqueleto.Internal.ExprParser as P
import qualified Database.Esqueleto.Internal.Internal as EI
import Database.Esqueleto.PostgreSQL as EP
import Database.Persist.Class.PersistEntity

Check warning on line 89 in test/Common/Test.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.6)

The import of ‘Database.Persist.Class.PersistEntity’ is redundant
import qualified UnliftIO.Resource as R

import Common.Record (testDeriveEsqueletoRecord)
import Common.Test.Select
import qualified Common.Test.CTE as CTESpec

-- Test schema
-- | this could be achieved with S.fromList, but not all lists
Expand Down Expand Up @@ -2426,6 +2427,7 @@
testLocking
testOverloadedRecordDot
testDeriveEsqueletoRecord
CTESpec.testCTE

insert' :: ( Functor m
, BaseBackend backend ~ PersistEntityBackend val
Expand Down
35 changes: 35 additions & 0 deletions test/Common/Test/CTE.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{-# language TypeApplications #-}

module Common.Test.CTE where

import Common.Test.Models
import Common.Test.Import
import Database.Persist.TH

Check warning on line 7 in test/Common/Test/CTE.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.6)

The import of ‘Database.Persist.TH’ is redundant

Check warning on line 7 in test/Common/Test/CTE.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8)

The import of ‘Database.Persist.TH’ is redundant

Check warning on line 7 in test/Common/Test/CTE.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10)

The import of ‘Database.Persist.TH’ is redundant

Check warning on line 7 in test/Common/Test/CTE.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0)

The import of ‘Database.Persist.TH’ is redundant

Check warning on line 7 in test/Common/Test/CTE.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2)

The import of ‘Database.Persist.TH’ is redundant

testCTE :: SpecDb
testCTE = describe "CTE" $ do
itDb "can refer to the same CTE twice" $ do
let q :: SqlQuery (SqlExpr (Value Int), SqlExpr (Value Int))
q = do
bCte <- with $ do
b <- from $ table @B
pure b

a :& b1 :& b2 <- from $
table @A
`innerJoin` bCte
`on` do
\(a :& b) ->
a ^. AK ==. b ^. BK
`innerJoin` bCte
`on` do
\(a :& _ :& b2) ->
a ^. AK ==. b2 ^. BK
pure (a ^. AK, a ^. AV +. b1 ^. BV +. b2 ^. BV)
insert_ $ A { aK = 1, aV = 2 }
insert_ $ B { bK = 1, bV = 3 }
ret <- select q
asserting $ do
ret `shouldMatchList`
[ (Value 1, Value (2 + 3 + 3))
]
10 changes: 10 additions & 0 deletions test/Common/Test/Models.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

import Data.Time
import Database.Esqueleto.Experimental
import Database.Persist.Sql

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.6)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The import of ‘Database.Persist.Sql’ is redundant

Check warning on line 27 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The import of ‘Database.Persist.Sql’ is redundant
import Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistUpperCase|

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators

Check warning on line 30 in test/Common/Test/Models.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6)

The use of ‘~’ without TypeOperators
Foo
name Int
Primary name
Expand Down Expand Up @@ -182,6 +182,16 @@
address String
deriving Show
deriving Eq

A
k Int
v Int
Primary k

B
k Int
v Int
Primary k
|]

-- Unique Test schema
Expand Down
2 changes: 0 additions & 2 deletions test/PostgreSQL/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,6 @@ testPostgresqlLocking = do
EP.forUpdateOf p EP.skipLocked
return p

liftIO $ print nonLockedRowsSpecifiedTable
pure $ length nonLockedRowsSpecifiedTable `shouldBe` 2

withAsync sideThread $ \sideThreadAsync -> do
Expand All @@ -1371,7 +1370,6 @@ testPostgresqlLocking = do
EP.forUpdateOf p EP.skipLocked
return p

liftIO $ print nonLockedRowsAfterUpdate
asserting sideThreadAsserts
asserting $ length nonLockedRowsAfterUpdate `shouldBe` 3

Expand Down
Loading