on dev help sign in
Merge pull request #43 from echatav/instances
b6df2afEitan Chatav
squeal-postgresql/squeal-postgresql.cabal+1
@@ -24,6 +24,7 @@ library
24 aeson
24 aeson
25 , base
25 , base
26 , bytestring
26 , bytestring
27+ , deepseq
27 , generics-sop
28 , generics-sop
28 , lifted-base
29 , lifted-base
29 , monad-control
30 , monad-control
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+14
@@ -1,5 +1,8 @@
1{-# LANGUAGE
1{-# LANGUAGE
2 DataKinds
2 DataKinds
3+ , DeriveAnyClass
4+ , DeriveDataTypeable
5+ , DeriveGeneric
3 , FlexibleContexts
6 , FlexibleContexts
4 , FlexibleInstances
7 , FlexibleInstances
5 , GADTs
8 , GADTs
@@ -9,6 +12,7 @@
9 , PolyKinds
12 , PolyKinds
10 , RankNTypes
13 , RankNTypes
11 , ScopedTypeVariables
14 , ScopedTypeVariables
15+ , StandaloneDeriving
12 , TypeApplications
16 , TypeApplications
13 , TypeFamilies
17 , TypeFamilies
14 , TypeOperators
18 , TypeOperators
@@ -44,9 +48,12 @@ module Squeal.PostgreSQL.Schema
44 , module GHC.TypeLits
48 , module GHC.TypeLits
45 ) where
49 ) where
46
50
51+import Control.DeepSeq
47import Data.ByteString
52import Data.ByteString
53+import Data.Data (Data)
48import Data.Monoid
54import Data.Monoid
49import Data.String
55import Data.String
56+import GHC.Generics (Generic)
50import GHC.Exts
57import GHC.Exts
51import GHC.OverloadedLabels
58import GHC.OverloadedLabels
52import GHC.TypeLits
59import GHC.TypeLits
@@ -96,6 +103,7 @@ instance PGFloating 'PGfloat8
96type (:::) (alias :: Symbol) (ty :: polykind) = '(alias,ty)
103type (:::) (alias :: Symbol) (ty :: polykind) = '(alias,ty)
97
104
98data Alias (alias :: Symbol) = Alias
105data Alias (alias :: Symbol) = Alias
106+ deriving (Eq,Generic,Data,Ord,Show,NFData)
99
107
100renderAlias :: KnownSymbol alias => Alias alias -> ByteString
108renderAlias :: KnownSymbol alias => Alias alias -> ByteString
101renderAlias = fromString . symbolVal
109renderAlias = fromString . symbolVal
@@ -109,6 +117,12 @@ data Aliased expression aliased where
109 => expression ty
117 => expression ty
110 -> Alias alias
118 -> Alias alias
111 -> Aliased expression (alias ::: ty)
119 -> Aliased expression (alias ::: ty)
120+deriving instance Show (expression ty)
121+ => Show (Aliased expression (alias ::: ty))
122+deriving instance Eq (expression ty)
123+ => Eq (Aliased expression (alias ::: ty))
124+deriving instance Ord (expression ty)
125+ => Ord (Aliased expression (alias ::: ty))
112
126
113renderAliased
127renderAliased
114 :: (forall ty. expression ty -> ByteString)
128 :: (forall ty. expression ty -> ByteString)
squeal-postgresql/src/Squeal/PostgreSQL/Statement.hs+123−48
@@ -1,16 +1,20 @@
1{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
1{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
2{-# LANGUAGE
2{-# LANGUAGE
3 DataKinds
3 DataKinds
4+ , DeriveGeneric
5+ , DeriveDataTypeable
4 , FlexibleContexts
6 , FlexibleContexts
5 , FlexibleInstances
7 , FlexibleInstances
6 , FunctionalDependencies
8 , FunctionalDependencies
7 , GADTs
9 , GADTs
10+ , GeneralizedNewtypeDeriving
8 , LambdaCase
11 , LambdaCase
9 , MagicHash
12 , MagicHash
10 , OverloadedStrings
13 , OverloadedStrings
11 , PolyKinds
14 , PolyKinds
12 , RankNTypes
15 , RankNTypes
13 , ScopedTypeVariables
16 , ScopedTypeVariables
17+ , StandaloneDeriving
14 , TypeApplications
18 , TypeApplications
15 , TypeFamilies
19 , TypeFamilies
16 , TypeOperators
20 , TypeOperators
@@ -20,10 +24,12 @@
20module Squeal.PostgreSQL.Statement where
24module Squeal.PostgreSQL.Statement where
21
25
22import Control.Category
26import Control.Category
27+import Control.DeepSeq
23import Data.ByteString (ByteString)
28import Data.ByteString (ByteString)
29+import Data.Data (Data)
24import Data.Foldable
30import Data.Foldable
25import Data.Maybe
31import Data.Maybe
26−import Data.Monoid
32+import Data.Monoid hiding (All)
27import Data.Ratio
33import Data.Ratio
28import Data.String
34import Data.String
29import Data.Word
35import Data.Word
@@ -33,6 +39,7 @@ import GHC.OverloadedLabels
33import GHC.TypeLits
39import GHC.TypeLits
34import Prelude hiding (RealFrac(..), id, (.))
40import Prelude hiding (RealFrac(..), id, (.))
35
41
42+import qualified GHC.Generics as GHC
36import qualified Data.ByteString as ByteString
43import qualified Data.ByteString as ByteString
37
44
38import Squeal.PostgreSQL.Schema
45import Squeal.PostgreSQL.Schema
@@ -47,7 +54,7 @@ newtype Expression
47 (grouping :: Grouping)
54 (grouping :: Grouping)
48 (ty :: ColumnType)
55 (ty :: ColumnType)
49 = UnsafeExpression { renderExpression :: ByteString }
56 = UnsafeExpression { renderExpression :: ByteString }
50− deriving (Show,Eq)
57+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
51
58
52class (PGTyped (BaseType ty), KnownNat n)
59class (PGTyped (BaseType ty), KnownNat n)
53 => HasParameter (n :: Nat) params ty | n params -> ty where
60 => HasParameter (n :: Nat) params ty | n params -> ty where
@@ -80,6 +87,18 @@ instance (HasTable table tables columns, HasColumn column columns ty)
80 table ! column = UnsafeExpression $
87 table ! column = UnsafeExpression $
81 renderAlias table <> "." <> renderAlias column
88 renderAlias table <> "." <> renderAlias column
82
89
90+data Column
91+ (columns :: [(Symbol,ColumnType)])
92+ (columnty :: (Symbol,ColumnType))
93+ where
94+ Column
95+ :: HasColumn column columns ty
96+ => Alias column
97+ -> Column columns (column ::: ty)
98+deriving instance Show (Column columns columnty)
99+deriving instance Eq (Column columns columnty)
100+deriving instance Ord (Column columns columnty)
101+
83def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
102def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
84def = UnsafeExpression "DEFAULT"
103def = UnsafeExpression "DEFAULT"
85
104
@@ -418,6 +437,7 @@ data SortExpression params tables grouping where
418 DescNullsLast
437 DescNullsLast
419 :: Expression params tables grouping ('Required ('Null ty))
438 :: Expression params tables grouping ('Required ('Null ty))
420 -> SortExpression params tables grouping
439 -> SortExpression params tables grouping
440+deriving instance Show (SortExpression params tables grouping)
421
441
422renderSortExpression :: SortExpression params tables grouping -> ByteString
442renderSortExpression :: SortExpression params tables grouping -> ByteString
423renderSortExpression = \case
443renderSortExpression = \case
@@ -437,7 +457,7 @@ aggregation
437
457
438data By
458data By
439 (tables :: [(Symbol,[(Symbol,ColumnType)])])
459 (tables :: [(Symbol,[(Symbol,ColumnType)])])
440− (tabcol :: (Symbol,Symbol)) where
460+ (by :: (Symbol,Symbol)) where
441 By
461 By
442 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
462 :: (tables ~ '[table ::: columns], HasColumn column columns ty)
443 => Alias column
463 => Alias column
@@ -446,6 +466,9 @@ data By
446 :: (HasTable table tables columns, HasColumn column columns ty)
466 :: (HasTable table tables columns, HasColumn column columns ty)
447 => (Alias table, Alias column)
467 => (Alias table, Alias column)
448 -> By tables '(table, column)
468 -> By tables '(table, column)
469+deriving instance Show (By tables by)
470+deriving instance Eq (By tables by)
471+deriving instance Ord (By tables by)
449
472
450renderBy :: By tables tabcolty -> ByteString
473renderBy :: By tables tabcolty -> ByteString
451renderBy = \case
474renderBy = \case
@@ -470,6 +493,9 @@ data HavingClause params tables grouping where
470 Having
493 Having
471 :: [Condition params tables ('Grouped bys)]
494 :: [Condition params tables ('Grouped bys)]
472 -> HavingClause params tables ('Grouped bys)
495 -> HavingClause params tables ('Grouped bys)
496+deriving instance Show (HavingClause params tables grouping)
497+deriving instance Eq (HavingClause params tables grouping)
498+deriving instance Ord (HavingClause params tables grouping)
473
499
474renderHavingClause :: HavingClause params tables grouping -> ByteString
500renderHavingClause :: HavingClause params tables grouping -> ByteString
475renderHavingClause = \case
501renderHavingClause = \case
@@ -515,74 +541,97 @@ instance
515 ) => IsTableColumn table column
541 ) => IsTableColumn table column
516 (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
542 (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
517
543
544+data AllOrDistinct = All | Distinct
545+ deriving (GHC.Generic,Show,Eq,Ord,Data)
546+instance NFData AllOrDistinct
547+
548+renderAllOrDistinct :: AllOrDistinct -> ByteString
549+renderAllOrDistinct = \case
550+ All -> "ALL"
551+ Distinct -> "DISTINCT"
552+
518unsafeAggregate
553unsafeAggregate
519 :: ByteString
554 :: ByteString
555+ -> AllOrDistinct
520 -> Expression params tables 'Ungrouped ('Required xty)
556 -> Expression params tables 'Ungrouped ('Required xty)
521 -> Expression params tables ('Grouped bys) ('Required yty)
557 -> Expression params tables ('Grouped bys) ('Required yty)
522−unsafeAggregate fun x = UnsafeExpression $ mconcat
558+unsafeAggregate fun allOrDistinct x = UnsafeExpression $ mconcat
523− [fun, "(", renderExpression x, ")"]
559+ [fun, "(", renderAllOrDistinct allOrDistinct <+> renderExpression x, ")"]
524
560
525sum_
561sum_
526 :: PGNum ty
562 :: PGNum ty
563+ => AllOrDistinct
527− => Expression params tables 'Ungrouped ('Required (nullity ty))
564+ -> Expression params tables 'Ungrouped ('Required (nullity ty))
528 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
565 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
529sum_ = unsafeAggregate "sum"
566sum_ = unsafeAggregate "sum"
530
567
531avgInt
568avgInt
532 :: PGIntegral int
569 :: PGIntegral int
570+ => AllOrDistinct
533− => Expression params tables 'Ungrouped ('Required (nullity int))
571+ -> Expression params tables 'Ungrouped ('Required (nullity int))
534 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
572 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
535avgInt = unsafeAggregate "avg"
573avgInt = unsafeAggregate "avg"
536
574
537avgFloat
575avgFloat
538 :: PGFloating float
576 :: PGFloating float
577+ => AllOrDistinct
539− => Expression params tables 'Ungrouped ('Required (nullity float))
578+ -> Expression params tables 'Ungrouped ('Required (nullity float))
540 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
579 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
541avgFloat = unsafeAggregate "avg"
580avgFloat = unsafeAggregate "avg"
542
581
543bitAnd
582bitAnd
544 :: PGIntegral int
583 :: PGIntegral int
584+ => AllOrDistinct
545− => Expression params tables 'Ungrouped ('Required (nullity int))
585+ -> Expression params tables 'Ungrouped ('Required (nullity int))
546 -> Expression params tables ('Grouped bys) ('Required (nullity int))
586 -> Expression params tables ('Grouped bys) ('Required (nullity int))
547bitAnd = unsafeAggregate "bit_and"
587bitAnd = unsafeAggregate "bit_and"
548
588
549bitOr
589bitOr
550 :: PGIntegral int
590 :: PGIntegral int
591+ => AllOrDistinct
551− => Expression params tables 'Ungrouped ('Required (nullity int))
592+ -> Expression params tables 'Ungrouped ('Required (nullity int))
552 -> Expression params tables ('Grouped bys) ('Required (nullity int))
593 -> Expression params tables ('Grouped bys) ('Required (nullity int))
553bitOr = unsafeAggregate "bit_or"
594bitOr = unsafeAggregate "bit_or"
554
595
555boolAnd
596boolAnd
597+ :: AllOrDistinct
556− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
598+ -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
557 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
599 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
558boolAnd = unsafeAggregate "bool_and"
600boolAnd = unsafeAggregate "bool_and"
559
601
560boolOr
602boolOr
603+ :: AllOrDistinct
561− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
604+ -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
562 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
605 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
563boolOr = unsafeAggregate "bool_or"
606boolOr = unsafeAggregate "bool_or"
564
607
565countStar
608countStar
609+ :: AllOrDistinct
566− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
610+ -> Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
567−countStar = UnsafeExpression "count(*)"
611+countStar allOrDistinct = UnsafeExpression $
612+ "count(" <> renderAllOrDistinct allOrDistinct <+> "*)"
568
613
569count
614count
615+ :: AllOrDistinct
570− :: Expression params tables 'Ungrouped ('Required ty)
616+ -> Expression params tables 'Ungrouped ('Required ty)
571 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
617 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
572count = unsafeAggregate "count"
618count = unsafeAggregate "count"
573
619
574every
620every
621+ :: AllOrDistinct
575− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
622+ -> Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
576 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
623 -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
577every = unsafeAggregate "every"
624every = unsafeAggregate "every"
578
625
579jsonAgg
626jsonAgg
627+ :: AllOrDistinct
580− :: Expression params tables 'Ungrouped ('Required ty)
628+ -> Expression params tables 'Ungrouped ('Required ty)
581 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
629 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
582jsonAgg = unsafeAggregate "json_agg"
630jsonAgg = unsafeAggregate "json_agg"
583
631
584jsonbAgg
632jsonbAgg
633+ :: AllOrDistinct
585− :: Expression params tables 'Ungrouped ('Required ty)
634+ -> Expression params tables 'Ungrouped ('Required ty)
586 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
635 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
587jsonbAgg = unsafeAggregate "jsonb_agg"
636jsonbAgg = unsafeAggregate "jsonb_agg"
588
637
@@ -595,20 +644,23 @@ jsonObjectAgg k v = UnsafeExpression $
595 <> ", " <> renderExpression v <> ")"
644 <> ", " <> renderExpression v <> ")"
596
645
597jsonbObjectAgg
646jsonbObjectAgg
647+ :: AllOrDistinct
598− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
648+ -> Expression params tables 'Ungrouped ('Required ('NotNull keyty))
599 -> Expression params tables 'Ungrouped ('Required valty)
649 -> Expression params tables 'Ungrouped ('Required valty)
600 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
650 -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
601−jsonbObjectAgg k v = UnsafeExpression $
651+jsonbObjectAgg allOrDistinct k v = UnsafeExpression $
602− "jsonb_object_agg(" <> renderExpression k
652+ "jsonb_object_agg(" <> renderAllOrDistinct allOrDistinct
603− <> ", " <> renderExpression v <> ")"
653+ <+> renderExpression k <> ", " <> renderExpression v <> ")"
604
654
605max_
655max_
656+ :: AllOrDistinct
606− :: Expression params tables 'Ungrouped ('Required (nullity ty))
657+ -> Expression params tables 'Ungrouped ('Required (nullity ty))
607 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
658 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
608max_ = unsafeAggregate "max"
659max_ = unsafeAggregate "max"
609
660
610min_
661min_
662+ :: AllOrDistinct
611− :: Expression params tables 'Ungrouped ('Required (nullity ty))
663+ -> Expression params tables 'Ungrouped ('Required (nullity ty))
612 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
664 -> Expression params tables ('Grouped bys) ('Required (nullity ty))
613min_ = unsafeAggregate "min"
665min_ = unsafeAggregate "min"
614
666
@@ -620,7 +672,7 @@ newtype Table
620 (schema :: [(Symbol,[(Symbol,ColumnType)])])
672 (schema :: [(Symbol,[(Symbol,ColumnType)])])
621 (columns :: [(Symbol,ColumnType)])
673 (columns :: [(Symbol,ColumnType)])
622 = UnsafeTable { renderTable :: ByteString }
674 = UnsafeTable { renderTable :: ByteString }
623− deriving (Show,Eq)
675+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
624
676
625class KnownSymbol table => HasTable table tables columns
677class KnownSymbol table => HasTable table tables columns
626 | table tables -> columns where
678 | table tables -> columns where
@@ -785,7 +837,7 @@ type expressions
785
837
786newtype TypeExpression (ty :: ColumnType)
838newtype TypeExpression (ty :: ColumnType)
787 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
839 = UnsafeTypeExpression { renderTypeExpression :: ByteString }
788− deriving (Show,Eq)
840+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
789
841
790bool :: TypeExpression ('Required ('Null 'PGbool))
842bool :: TypeExpression ('Required ('Null 'PGbool))
791bool = UnsafeTypeExpression "bool"
843bool = UnsafeTypeExpression "bool"
@@ -911,6 +963,7 @@ newtype Definition
911 (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
963 (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
912 (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
964 (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
913 = UnsafeDefinition { renderDefinition :: ByteString }
965 = UnsafeDefinition { renderDefinition :: ByteString }
966+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
914
967
915instance Category Definition where
968instance Category Definition where
916 id = UnsafeDefinition ";"
969 id = UnsafeDefinition ";"
@@ -922,12 +975,14 @@ newtype Manipulation
922 (params :: [ColumnType])
975 (params :: [ColumnType])
923 (columns :: [(Symbol,ColumnType)])
976 (columns :: [(Symbol,ColumnType)])
924 = UnsafeManipulation { renderManipulation :: ByteString }
977 = UnsafeManipulation { renderManipulation :: ByteString }
978+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
925
979
926newtype Query
980newtype Query
927 (schema :: [(Symbol,[(Symbol,ColumnType)])])
981 (schema :: [(Symbol,[(Symbol,ColumnType)])])
928 (params :: [ColumnType])
982 (params :: [ColumnType])
929 (columns :: [(Symbol,ColumnType)])
983 (columns :: [(Symbol,ColumnType)])
930 = UnsafeQuery { renderQuery :: ByteString }
984 = UnsafeQuery { renderQuery :: ByteString }
985+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
931
986
932queryStatement
987queryStatement
933 :: Query schema params columns
988 :: Query schema params columns
@@ -977,12 +1032,29 @@ select list tabs = UnsafeQuery $
977 <+> renderCommaSeparated (renderAliased renderExpression) list
1032 <+> renderCommaSeparated (renderAliased renderExpression) list
978 <+> renderTableExpression tabs
1033 <+> renderTableExpression tabs
979
1034
1035+selectDistinct
1036+ :: SListI columns
1037+ => NP (Aliased (Expression params tables grouping)) (column ': columns)
1038+ -> TableExpression params schema tables grouping
1039+ -> Query schema params (column ': columns)
1040+selectDistinct list tabs = UnsafeQuery $
1041+ "SELECT DISTINCT"
1042+ <+> renderCommaSeparated (renderAliased renderExpression) list
1043+ <+> renderTableExpression tabs
1044+
980selectStar
1045selectStar
981 :: tables ~ '[table ::: columns]
1046 :: tables ~ '[table ::: columns]
982 => TableExpression params schema tables 'Ungrouped
1047 => TableExpression params schema tables 'Ungrouped
983 -> Query schema params columns
1048 -> Query schema params columns
984selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
1049selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
985
1050
1051+selectDistinctStar
1052+ :: tables ~ '[table ::: columns]
1053+ => TableExpression params schema tables 'Ungrouped
1054+ -> Query schema params columns
1055+selectDistinctStar tabs = UnsafeQuery $
1056+ "SELECT DISTINCT" <+> "*" <+> renderTableExpression tabs
1057+
986selectDotStar
1058selectDotStar
987 :: HasTable table tables columns
1059 :: HasTable table tables columns
988 => Alias table
1060 => Alias table
@@ -991,12 +1063,14 @@ selectDotStar
991selectDotStar table tables = UnsafeQuery $
1063selectDotStar table tables = UnsafeQuery $
992 "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
1064 "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
993
1065
994−selectRow
1066+selectDistinctDotStar
995− :: SListI columns
1067+ :: HasTable table tables columns
996− => NP (Aliased (Expression params '[] 'Ungrouped)) columns
1068+ => Alias table
1069+ -> TableExpression params schema tables 'Ungrouped
997 -> Query schema params columns
1070 -> Query schema params columns
998−selectRow row = UnsafeQuery $
1071+selectDistinctDotStar table tables = UnsafeQuery $
999− "SELECT" <+> renderCommaSeparated (renderAliased renderExpression) row
1072+ "SELECT DISTINCT" <+> renderAlias table <> ".*"
1073+ <+> renderTableExpression tables
1000
1074
1001{-----------------------------------------
1075{-----------------------------------------
1002CREATE statements
1076CREATE statements
@@ -1023,10 +1097,11 @@ createTable table columns constraints = UnsafeDefinition $
1023 [] -> ""
1097 [] -> ""
1024 _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1098 _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1025
1099
1026−data TableConstraint
1100+newtype TableConstraint
1027 (schema :: [(Symbol,[(Symbol,ColumnType)])])
1101 (schema :: [(Symbol,[(Symbol,ColumnType)])])
1028 (columns :: [(Symbol,ColumnType)])
1102 (columns :: [(Symbol,ColumnType)])
1029− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1103+ = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1104+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1030
1105
1031check
1106check
1032 :: Condition '[] '[table ::: columns] 'Ungrouped
1107 :: Condition '[] '[table ::: columns] 'Ungrouped
@@ -1034,15 +1109,6 @@ check
1034check condition = UnsafeTableConstraint $
1109check condition = UnsafeTableConstraint $
1035 "CHECK" <+> parenthesized (renderExpression condition)
1110 "CHECK" <+> parenthesized (renderExpression condition)
1036
1111
1037−data Column
1038− (columns :: [(Symbol,ColumnType)])
1039− (columnty :: (Symbol,ColumnType))
1040− where
1041− Column
1042− :: HasColumn column columns ty
1043− => Alias column
1044− -> Column columns (column ::: ty)
1045−
1046renderColumn :: Column columns columnty -> ByteString
1112renderColumn :: Column columns columnty -> ByteString
1047renderColumn (Column column) = renderAlias column
1113renderColumn (Column column) = renderAlias column
1048
1114
@@ -1084,6 +1150,8 @@ data OnDelete
1084 = OnDeleteNoAction
1150 = OnDeleteNoAction
1085 | OnDeleteRestrict
1151 | OnDeleteRestrict
1086 | OnDeleteCascade
1152 | OnDeleteCascade
1153+ deriving (GHC.Generic,Show,Eq,Ord,Data)
1154+instance NFData OnDelete
1087
1155
1088renderOnDelete :: OnDelete -> ByteString
1156renderOnDelete :: OnDelete -> ByteString
1089renderOnDelete = \case
1157renderOnDelete = \case
@@ -1095,6 +1163,8 @@ data OnUpdate
1095 = OnUpdateNoAction
1163 = OnUpdateNoAction
1096 | OnUpdateRestrict
1164 | OnUpdateRestrict
1097 | OnUpdateCascade
1165 | OnUpdateCascade
1166+ deriving (GHC.Generic,Show,Eq,Ord,Data)
1167+instance NFData OnUpdate
1098
1168
1099renderOnUpdate :: OnUpdate -> ByteString
1169renderOnUpdate :: OnUpdate -> ByteString
1100renderOnUpdate = \case
1170renderOnUpdate = \case
@@ -1136,10 +1206,11 @@ alterTableRename table0 table1 = UnsafeDefinition $
1136 "ALTER TABLE" <+> renderAlias table0
1206 "ALTER TABLE" <+> renderAlias table0
1137 <+> "RENAME TO" <+> renderAlias table1 <> ";"
1207 <+> "RENAME TO" <+> renderAlias table1 <> ";"
1138
1208
1139−data AlterTable
1209+newtype AlterTable
1140 (columns0 :: [(Symbol, ColumnType)])
1210 (columns0 :: [(Symbol, ColumnType)])
1141 (columns1 :: [(Symbol, ColumnType)]) =
1211 (columns1 :: [(Symbol, ColumnType)]) =
1142 UnsafeAlterTable {renderAlterTable :: ByteString}
1212 UnsafeAlterTable {renderAlterTable :: ByteString}
1213+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1143
1214
1144addColumnDefault
1215addColumnDefault
1145 :: KnownSymbol column
1216 :: KnownSymbol column
@@ -1180,8 +1251,9 @@ alterColumn
1180alterColumn column alteration = UnsafeAlterTable $
1251alterColumn column alteration = UnsafeAlterTable $
1181 "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1252 "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1182
1253
1183−data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1254+newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1184 UnsafeAlterColumn {renderAlterColumn :: ByteString}
1255 UnsafeAlterColumn {renderAlterColumn :: ByteString}
1256+ deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1185
1257
1186setDefault
1258setDefault
1187 :: Expression '[] '[] 'Ungrouped ('Required ty)
1259 :: Expression '[] '[] 'Ungrouped ('Required ty)
@@ -1226,10 +1298,10 @@ data ValuesClause
1226 (schema :: [(Symbol,[(Symbol,ColumnType)])])
1298 (schema :: [(Symbol,[(Symbol,ColumnType)])])
1227 (params :: [ColumnType])
1299 (params :: [ColumnType])
1228 (columns :: [(Symbol,ColumnType)])
1300 (columns :: [(Symbol,ColumnType)])
1229− = Values
1301+ = Values
1230− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1302+ (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1231− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1303+ [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1232− | ValuesQuery (Query schema params columns)
1304+ | ValuesQuery (Query schema params columns)
1233
1305
1234renderValuesClause
1306renderValuesClause
1235 :: SListI columns
1307 :: SListI columns
@@ -1299,6 +1371,9 @@ UPDATE statements
1299data UpdateExpression params columns ty
1371data UpdateExpression params columns ty
1300 = Same
1372 = Same
1301 | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1373 | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1374+deriving instance Show (UpdateExpression params columns ty)
1375+deriving instance Eq (UpdateExpression params columns ty)
1376+deriving instance Ord (UpdateExpression params columns ty)
1302
1377
1303renderUpdateExpression
1378renderUpdateExpression
1304 :: Aliased (UpdateExpression params columns) column
1379 :: Aliased (UpdateExpression params columns) column
squeal-postgresql/test/Squeal/PostgreSQL/StatementSpec.hs+4−4
@@ -88,15 +88,15 @@ spec = do
88 let
88 let
89 statement :: Query Tables '[] SumAndCol1
89 statement :: Query Tables '[] SumAndCol1
90 statement =
90 statement =
91− select (sum_ #col2 `As` #sum :* #col1 `As` #col1 :* Nil)
91+ select (sum_ All #col2 `As` #sum :* #col1 `As` #col1 :* Nil)
92 ( from (Table (#table1 `As` #table1))
92 ( from (Table (#table1 `As` #table1))
93 & group (By #col1 :* Nil)
93 & group (By #col1 :* Nil)
94− & having (#col1 + sum_ #col2 >* 1) )
94+ & having (#col1 + sum_ All #col2 >* 1) )
95 statement `queryRenders`
95 statement `queryRenders`
96− "SELECT sum(col2) AS sum, col1 AS col1\
96+ "SELECT sum(ALL col2) AS sum, col1 AS col1\
97 \ FROM table1 AS table1\
97 \ FROM table1 AS table1\
98 \ GROUP BY col1\
98 \ GROUP BY col1\
99− \ HAVING ((col1 + sum(col2)) > 1);"
99+ \ HAVING ((col1 + sum(ALL col2)) > 1);"
100 it "correctly renders simple INSERTs" $ do
100 it "correctly renders simple INSERTs" $ do
101 let
101 let
102 statement :: Manipulation Tables '[] '[]
102 statement :: Manipulation Tables '[] '[]