on dev help sign in
use lists for modifier clauses
commitf53dc5b84de8c3236d09100f22f9ec18e7dfad55 authorEitan Chatav <eitan.chatav@gmail.com> dateTue 18 Jul 2017, 11:20 (9 years ago) parent3cf8a5b
2 files changed+34−47
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+33−46 squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+1−1
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+33−46
@@ -19,6 +19,7 @@ module Squeel.PostgreSQL.Statement where
1919import Data.Boolean
2020import Data.Boolean.Numbers
2121import Data.ByteString (ByteString)
22+import Data.Foldable
2223import Data.Maybe
2324import Data.Monoid
2425import Data.Ratio
@@ -425,78 +426,67 @@ instance HasTable table schema columns
425426 => IsLabel table (Join params schema '[table ::: columns]) where
426427 fromLabel p = Table $ fromLabel p
427428
428−data Clauses params tables = Clauses
429− { whereClause :: Maybe (Expression params tables ('Required ('NotNull 'PGBool)))
430− , limitClause :: Maybe (Expression params '[] ('Required ('NotNull 'PGInt8)))
431− , offsetClause :: Maybe (Expression params '[] ('Required ('NotNull 'PGInt8)))
432− }
433−
434−instance Monoid (Clauses params tables) where
435− mempty = Clauses Nothing Nothing Nothing
436− Clauses wh1 lim1 off1 `mappend` Clauses wh2 lim2 off2 = Clauses
437− { whereClause = case (wh1,wh2) of
438− (Nothing,Nothing) -> Nothing
439− (Just w1,Nothing) -> Just w1
440− (Nothing,Just w2) -> Just w2
441− (Just w1,Just w2) -> Just (w1 &&* w2)
442− , limitClause = case (lim1,lim2) of
443− (Nothing,Nothing) -> Nothing
444− (Just l1,Nothing) -> Just l1
445− (Nothing,Just l2) -> Just l2
446− (Just l1,Just l2) -> Just (l1 `minB` l2)
447− , offsetClause = case (off1,off2) of
448− (Nothing,Nothing) -> Nothing
449− (Just o1,Nothing) -> Just o1
450− (Nothing,Just o2) -> Just o2
451− (Just o1,Just o2) -> Just (o1 + o2)
452− }
453−
454429data From
455430 (params :: [ColumnType])
456431 (schema :: [(Symbol,[(Symbol,ColumnType)])])
457432 (tables :: [(Symbol,[(Symbol,ColumnType)])])
458433 = From
459− { fromJoin :: Join params schema tables
460− , fromClauses :: Clauses params tables
434+ { joinClause :: Join params schema tables
435+ , whereClause :: [Expression params tables ('Required ('NotNull 'PGBool))]
436+ , limitClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
437+ , offsetClause :: [Expression params '[] ('Required ('NotNull 'PGInt8))]
461438 }
462439
463440join
464441 :: Join params schema tables
465442 -> From params schema tables
466−join tables = From tables mempty
443+join tables = From tables [] [] []
467444
468445renderFrom :: From params schema tables -> ByteString
469−renderFrom (From tref (Clauses wh lim off))= mconcat
470− [ renderJoin tref
471− , maybe "" ((" WHERE " <>) . renderExpression) wh
472− , maybe "" ((" LIMIT " <>) . renderExpression) lim
473− , maybe "" ((" OFFSET " <>) . renderExpression) off
474− ]
446+renderFrom (From tables whs' lims' offs') = mconcat
447+ [ renderJoin tables
448+ , renderWheres whs'
449+ , renderLimits lims'
450+ , renderOffsets offs'
451+ ] where
452+ renderWheres = \case
453+ [] -> ""
454+ wh:[] -> " WHERE " <> renderExpression wh
455+ wh:whs -> " WHERE " <> renderExpression (foldr (&&*) wh whs)
456+ renderLimits = \case
457+ [] -> ""
458+ lim:[] -> " LIMIT " <> renderExpression lim
459+ lims -> mconcat
460+ [ " LIMIT LEAST("
461+ , ByteString.intercalate ", " (renderExpression <$> reverse lims)
462+ , ")"
463+ ]
464+ renderOffsets = \case
465+ [] -> ""
466+ off:[] -> " OFFSET " <> renderExpression off
467+ off:offs -> " OFFSET " <> renderExpression (foldr (+) off offs)
475468
476469instance (HasTable table schema columns, table ~ table')
477470 => IsLabel table (From params schema '[table' ::: columns]) where
478− fromLabel p = From (fromLabel p) mempty
471+ fromLabel p = join (fromLabel p)
479472
480473where_
481474 :: Expression params tables ('Required ('NotNull 'PGBool))
482475 -> From params schema tables
483476 -> From params schema tables
484−where_ wh (From tabs fromClauses1) = From tabs
485− (fromClauses1 <> Clauses (Just wh) Nothing Nothing)
477+where_ wh (From tabs whs lims offs) = From tabs (wh:whs) lims offs
486478
487479limit
488480 :: Expression params '[] ('Required ('NotNull 'PGInt8))
489481 -> From params schema tables
490482 -> From params schema tables
491−limit lim (From tabs fromClauses1) = From tabs
492− (fromClauses1 <> Clauses Nothing (Just lim) Nothing)
483+limit lim (From tabs whs lims offs) = From tabs whs (lim:lims) offs
493484
494485offset
495486 :: Expression params '[] ('Required ('NotNull 'PGInt8))
496487 -> From params schema tables
497488 -> From params schema tables
498−offset off (From tabs fromClauses1) = From tabs
499− (fromClauses1 <> Clauses Nothing Nothing (Just off))
489+offset off (From tabs whs lims offs) = From tabs whs lims (off:offs)
500490
501491newtype Selection
502492 (params :: [ColumnType])
@@ -540,10 +530,7 @@ select = UnsafeStatement . ("SELECT " <>) . (<> ";") . renderSelection
540530subselect
541531 :: Aliased (Selection params schema) table
542532 -> From params schema '[table]
543−subselect selection = From
544− { fromJoin = Subselect selection
545− , fromClauses = mempty
546− }
533+subselect selection = join (Subselect selection)
547534
548535{-----------------------------------------
549536INSERT statements
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+1−1
@@ -53,7 +53,7 @@ spec = do
5353 statement :: Statement '[] Columns Tables Tables
5454 statement = select $ starFrom (#table1 & limit 1 & limit 2)
5555 statement `shouldRenderAs`
56− "SELECT * FROM table1 AS table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
56+ "SELECT * FROM table1 AS table1 LIMIT LEAST(1, 2);"
5757 it "should render parameters using $ signs" $ do
5858 let
5959 statement :: Statement '[ 'Required ('NotNull 'PGInt8)] Columns Tables Tables