on dev help sign in
fix all tests
bcfd6e3Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+25−37
@@ -357,7 +357,7 @@ ALTER statements
357
357
358-- | `alterTable` changes the definition of a table from the schema.
358-- | `alterTable` changes the definition of a table from the schema.
359alterTable
359alterTable
360− :: (KnownSymbol tab, (tab ::: table0) `In` schema)
360+ :: Has tab schema table0
361 => Alias tab -- ^ table to alter
361 => Alias tab -- ^ table to alter
362 -> AlterTable schema table0 table1 -- ^ alteration to perform
362 -> AlterTable schema table0 table1 -- ^ alteration to perform
363 -> Definition schema (Alter tab schema table1)
363 -> Definition schema (Alter tab schema table1)
@@ -424,10 +424,10 @@ newtype AlterTable
424-- >>> :{
424-- >>> :{
425-- let
425-- let
426-- definition :: Definition
426-- definition :: Definition
427−-- '["tab" ::: '["col1" ::: 'Required ('Null 'PGint4)]]
427+-- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
428−-- '["tab" :::
428+-- '["tab" ::: '[] :=>
429−-- '[ "col1" ::: 'Required ('Null 'PGint4)
429+-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
430−-- , "col2" ::: 'Optional ('Null 'PGtext) ]]
430+-- , "col2" ::: 'Def :=> 'Null 'PGtext ]]
431-- definition = alterTable #tab
431-- definition = alterTable #tab
432-- (addColumnDefault #col2 (text & default_ "foo"))
432-- (addColumnDefault #col2 (text & default_ "foo"))
433-- in renderDefinition definition
433-- in renderDefinition definition
@@ -447,10 +447,10 @@ addColumnDefault column ty = UnsafeAlterTable $
447-- >>> :{
447-- >>> :{
448-- let
448-- let
449-- definition :: Definition
449-- definition :: Definition
450−-- '["tab" ::: '["col1" ::: 'Required ('Null 'PGint4)]]
450+-- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
451−-- '["tab" :::
451+-- '["tab" ::: '[] :=>
452−-- '[ "col1" ::: 'Required ('Null 'PGint4)
452+-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
453−-- , "col2" ::: 'Required ('Null 'PGtext) ]]
453+-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ]]
454-- definition = alterTable #tab (addColumnNull #col2 text)
454-- definition = alterTable #tab (addColumnNull #col2 text)
455-- in renderDefinition definition
455-- in renderDefinition definition
456-- :}
456-- :}
@@ -471,10 +471,10 @@ addColumnNull column ty = UnsafeAlterTable $
471-- >>> :{
471-- >>> :{
472-- let
472-- let
473-- definition :: Definition
473-- definition :: Definition
474−-- '["tab" :::
474+-- '["tab" ::: '[] :=>
475−-- '[ "col1" ::: 'Required ('Null 'PGint4)
475+-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
476−-- , "col2" ::: 'Required ('Null 'PGtext) ]]
476+-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ]]
477−-- '["tab" ::: '["col1" ::: 'Required ('Null 'PGint4)]]
477+-- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
478-- definition = alterTable #tab (dropColumn #col2)
478-- definition = alterTable #tab (dropColumn #col2)
479-- in renderDefinition definition
479-- in renderDefinition definition
480-- :}
480-- :}
@@ -488,18 +488,6 @@ dropColumn column = UnsafeAlterTable $
488
488
489-- | Like `dropColumn` but authorizes dropping everything that depends
489-- | Like `dropColumn` but authorizes dropping everything that depends
490-- on the column.
490-- on the column.
491−--
492−-- >>> :{
493−-- let
494−-- definition :: Definition
495−-- '["tab" :::
496−-- '[ "col1" ::: 'Required ('Null 'PGint4)
497−-- , "col2" ::: 'Required ('Null 'PGtext) ]]
498−-- '["tab" ::: '["col1" ::: 'Required ('Null 'PGint4)]]
499−-- definition = alterTable #tab (dropColumnCascade #col2)
500−-- in renderDefinition definition
501−-- :}
502−-- "ALTER TABLE tab DROP COLUMN col2 CASCADE;"
503-- dropColumnCascade
491-- dropColumnCascade
504-- :: KnownSymbol column
492-- :: KnownSymbol column
505-- => Alias column -- ^ column to remove
493-- => Alias column -- ^ column to remove
@@ -512,8 +500,8 @@ dropColumn column = UnsafeAlterTable $
512-- >>> :{
500-- >>> :{
513-- let
501-- let
514-- definition :: Definition
502-- definition :: Definition
515−-- '["tab" ::: '["foo" ::: 'Required ('Null 'PGint4)]]
503+-- '["tab" ::: '[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4]]
516−-- '["tab" ::: '["bar" ::: 'Required ('Null 'PGint4)]]
504+-- '["tab" ::: '[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4]]
517-- definition = alterTable #tab (renameColumn #foo #bar)
505-- definition = alterTable #tab (renameColumn #foo #bar)
518-- in renderDefinition definition
506-- in renderDefinition definition
519-- :}
507-- :}
@@ -548,8 +536,8 @@ newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
548-- >>> :{
536-- >>> :{
549-- let
537-- let
550-- definition :: Definition
538-- definition :: Definition
551−-- '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]]
539+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
552−-- '["tab" ::: '["col" ::: 'Optional ('Null 'PGint4)]]
540+-- '["tab" ::: '[] :=> '["col" ::: 'Def :=> 'Null 'PGint4]]
553-- definition = alterTable #tab (alterColumn #col (setDefault 5))
541-- definition = alterTable #tab (alterColumn #col (setDefault 5))
554-- in renderDefinition definition
542-- in renderDefinition definition
555-- :}
543-- :}
@@ -565,8 +553,8 @@ setDefault expression = UnsafeAlterColumn $
565-- >>> :{
553-- >>> :{
566-- let
554-- let
567-- definition :: Definition
555-- definition :: Definition
568−-- '["tab" ::: '["col" ::: 'Optional ('Null 'PGint4)]]
556+-- '["tab" ::: '[] :=> '["col" ::: 'Def :=> 'Null 'PGint4]]
569−-- '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]]
557+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
570-- definition = alterTable #tab (alterColumn #col dropDefault)
558-- definition = alterTable #tab (alterColumn #col dropDefault)
571-- in renderDefinition definition
559-- in renderDefinition definition
572-- :}
560-- :}
@@ -581,8 +569,8 @@ dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
581-- >>> :{
569-- >>> :{
582-- let
570-- let
583-- definition :: Definition
571-- definition :: Definition
584−-- '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]]
572+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
585−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
573+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
586-- definition = alterTable #tab (alterColumn #col setNotNull)
574-- definition = alterTable #tab (alterColumn #col setNotNull)
587-- in renderDefinition definition
575-- in renderDefinition definition
588-- :}
576-- :}
@@ -595,8 +583,8 @@ setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
595-- >>> :{
583-- >>> :{
596-- let
584-- let
597-- definition :: Definition
585-- definition :: Definition
598−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
586+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
599−-- '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]]
587+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
600-- definition = alterTable #tab (alterColumn #col dropNotNull)
588-- definition = alterTable #tab (alterColumn #col dropNotNull)
601-- in renderDefinition definition
589-- in renderDefinition definition
602-- :}
590-- :}
@@ -611,8 +599,8 @@ dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
611-- >>> :{
599-- >>> :{
612-- let
600-- let
613-- definition :: Definition
601-- definition :: Definition
614−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGint4)]]
602+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
615−-- '["tab" ::: '["col" ::: 'Required ('NotNull 'PGnumeric)]]
603+-- '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric]]
616-- definition =
604-- definition =
617-- alterTable #tab (alterColumn #col (alterType (numeric & notNull)))
605-- alterTable #tab (alterColumn #col (alterType (numeric & notNull)))
618-- in renderDefinition definition
606-- in renderDefinition definition
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+134−25
@@ -452,7 +452,12 @@ instance (PGNum ty, PGFloating ty) => Floating
452 acosh x = log (x + sqrt (x*x - 1))
452 acosh x = log (x + sqrt (x*x - 1))
453 atanh x = log ((1 + x) / (1 - x)) / 2
453 atanh x = log ((1 + x) / (1 - x)) / 2
454
454
455−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ atan2_ pi 2
455+-- | :{
456+-- let
457+-- expression :: Expression tables grouping params (nullity 'PGfloat4)
458+-- expression = atan2_ pi 2
459+-- in renderExpression expression
460+-- :}
456-- "atan2(pi(), 2)"
461-- "atan2(pi(), 2)"
457atan2_
462atan2_
458 :: PGFloating float
463 :: PGFloating float
@@ -480,8 +485,12 @@ cast ty x = UnsafeExpression $ parenthesized $
480 renderExpression x <+> "::" <+> renderTypeExpression ty
485 renderExpression x <+> "::" <+> renderTypeExpression ty
481
486
482-- | integer division, truncates the result
487-- | integer division, truncates the result
483−--
488+-- :{
484−-- >>> renderExpression @_ @_ @_ @(_(_ 'PGint2)) $ 5 `quot_` 2
489+-- let
490+-- expression :: Expression tables grouping params (nullity 'PGint2)
491+-- expression = 5 `quot_` 2
492+-- in renderExpression expression
493+-- :}
485-- "(5 / 2)"
494-- "(5 / 2)"
486quot_
495quot_
487 :: PGIntegral int
496 :: PGIntegral int
@@ -494,7 +503,12 @@ quot_ = unsafeBinaryOp "/"
494
503
495-- | remainder upon integer division
504-- | remainder upon integer division
496--
505--
497−-- >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ 5 `rem_` 2
506+-- :{
507+-- let
508+-- expression :: Expression tables grouping params (nullity 'PGint2)
509+-- expression = 5 `rem_` 2
510+-- in renderExpression expression
511+-- :}
498-- "(5 % 2)"
512-- "(5 % 2)"
499rem_
513rem_
500 :: PGIntegral int
514 :: PGIntegral int
@@ -505,7 +519,12 @@ rem_
505 -> Expression tables grouping params (nullity int)
519 -> Expression tables grouping params (nullity int)
506rem_ = unsafeBinaryOp "%"
520rem_ = unsafeBinaryOp "%"
507
521
508−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ trunc pi
522+-- | :{
523+-- let
524+-- expression :: Expression tables grouping params (nullity 'PGfloat4)
525+-- expression = trunc pi
526+-- in renderExpression expression
527+-- :}
509-- "trunc(pi())"
528-- "trunc(pi())"
510trunc
529trunc
511 :: PGFloating frac
530 :: PGFloating frac
@@ -514,7 +533,12 @@ trunc
514 -> Expression tables grouping params (nullity frac)
533 -> Expression tables grouping params (nullity frac)
515trunc = unsafeFunction "trunc"
534trunc = unsafeFunction "trunc"
516
535
517−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ round_ pi
536+-- | :{
537+-- let
538+-- expression :: Expression tables grouping params (nullity 'PGfloat4)
539+-- expression = round_ pi
540+-- in renderExpression expression
541+-- :}
518-- "round(pi())"
542-- "round(pi())"
519round_
543round_
520 :: PGFloating frac
544 :: PGFloating frac
@@ -523,7 +547,12 @@ round_
523 -> Expression tables grouping params (nullity frac)
547 -> Expression tables grouping params (nullity frac)
524round_ = unsafeFunction "round"
548round_ = unsafeFunction "round"
525
549
526−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGfloat4)) $ ceiling_ pi
550+-- | :{
551+-- let
552+-- expression :: Expression tables grouping params (nullity 'PGfloat4)
553+-- expression = ceiling_ pi
554+-- in renderExpression expression
555+-- :}
527-- "ceiling(pi())"
556-- "ceiling(pi())"
528ceiling_
557ceiling_
529 :: PGFloating frac
558 :: PGFloating frac
@@ -572,7 +601,12 @@ not_ = unsafeUnaryOp "NOT"
572 -> Condition tables grouping params
601 -> Condition tables grouping params
573(.||) = unsafeBinaryOp "OR"
602(.||) = unsafeBinaryOp "OR"
574
603
604+-- | :{
605+-- let
606+-- expression :: Expression tables grouping params (nullity 'PGint2)
575−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ caseWhenThenElse [(true, 1), (false, 2)] 3
607+-- expression = caseWhenThenElse [(true, 1), (false, 2)] 3
608+-- in renderExpression expression
609+-- :}
576-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
610-- "CASE WHEN TRUE THEN 1 WHEN FALSE THEN 2 ELSE 3 END"
577caseWhenThenElse
611caseWhenThenElse
578 :: [ ( Condition tables grouping params
612 :: [ ( Condition tables grouping params
@@ -593,7 +627,12 @@ caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
593 , " END"
627 , " END"
594 ]
628 ]
595
629
630+-- | :{
631+-- let
632+-- expression :: Expression tables grouping params (nullity 'PGint2)
596−-- | >>> renderExpression @_ @_ @_ @(_ (_ 'PGint2)) $ ifThenElse true 1 0
633+-- expression = ifThenElse true 1 0
634+-- in renderExpression expression
635+-- :}
597-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
636-- "CASE WHEN TRUE THEN 1 ELSE 0 END"
598ifThenElse
637ifThenElse
599 :: Condition tables grouping params
638 :: Condition tables grouping params
@@ -775,7 +814,12 @@ unsafeAggregateDistinct
775unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
814unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
776 [fun, "(DISTINCT ", renderExpression x, ")"]
815 [fun, "(DISTINCT ", renderExpression x, ")"]
777
816
778−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sum_ #col
817+-- | :{
818+-- let
819+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGnumeric]] grouping params (nullity 'PGnumeric)
820+-- expression = sum_ #col
821+-- in renderExpression expression
822+-- :}
779-- "sum(col)"
823-- "sum(col)"
780sum_
824sum_
781 :: PGNum ty
825 :: PGNum ty
@@ -784,7 +828,12 @@ sum_
784 -> Expression tables ('Grouped bys) params (nullity ty)
828 -> Expression tables ('Grouped bys) params (nullity ty)
785sum_ = unsafeAggregate "sum"
829sum_ = unsafeAggregate "sum"
786
830
787−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGnumeric)]] $ sumDistinct #col
831+-- | :{
832+-- let
833+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGnumeric]] grouping params (nullity 'PGnumeric)
834+-- expression = sumDistinct #col
835+-- in renderExpression expression
836+-- :}
788-- "sum(DISTINCT col)"
837-- "sum(DISTINCT col)"
789sumDistinct
838sumDistinct
790 :: PGNum ty
839 :: PGNum ty
@@ -810,7 +859,12 @@ instance PGAvg 'PGfloat4 'PGfloat8
810instance PGAvg 'PGfloat8 'PGfloat8
859instance PGAvg 'PGfloat8 'PGfloat8
811instance PGAvg 'PGinterval 'PGinterval
860instance PGAvg 'PGinterval 'PGinterval
812
861
813−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAnd #col
862+-- | :{
863+-- let
864+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGint4]] grouping params (nullity 'PGint4)
865+-- expression = bitAnd #col
866+-- in renderExpression expression
867+-- :}
814-- "bit_and(col)"
868-- "bit_and(col)"
815bitAnd
869bitAnd
816 :: PGIntegral int
870 :: PGIntegral int
@@ -819,7 +873,12 @@ bitAnd
819 -> Expression tables ('Grouped bys) params (nullity int)
873 -> Expression tables ('Grouped bys) params (nullity int)
820bitAnd = unsafeAggregate "bit_and"
874bitAnd = unsafeAggregate "bit_and"
821
875
822−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOr #col
876+-- | :{
877+-- let
878+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGint4]] grouping params (nullity 'PGint4)
879+-- expression = bitOr #col
880+-- in renderExpression expression
881+-- :}
823-- "bit_or(col)"
882-- "bit_or(col)"
824bitOr
883bitOr
825 :: PGIntegral int
884 :: PGIntegral int
@@ -828,7 +887,12 @@ bitOr
828 -> Expression tables ('Grouped bys) params (nullity int)
887 -> Expression tables ('Grouped bys) params (nullity int)
829bitOr = unsafeAggregate "bit_or"
888bitOr = unsafeAggregate "bit_or"
830
889
890+-- | :{
891+-- let
892+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGint4]] grouping params (nullity 'PGint4)
831−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitAndDistinct #col
893+-- expression = bitAndDistinct #col
894+-- in renderExpression expression
895+-- :}
832-- "bit_and(DISTINCT col)"
896-- "bit_and(DISTINCT col)"
833bitAndDistinct
897bitAndDistinct
834 :: PGIntegral int
898 :: PGIntegral int
@@ -837,7 +901,12 @@ bitAndDistinct
837 -> Expression tables ('Grouped bys) params (nullity int)
901 -> Expression tables ('Grouped bys) params (nullity int)
838bitAndDistinct = unsafeAggregateDistinct "bit_and"
902bitAndDistinct = unsafeAggregateDistinct "bit_and"
839
903
904+-- | :{
905+-- let
906+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGint4]] grouping params (nullity 'PGint4)
840−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGint4)]] $ bitOrDistinct #col
907+-- expression = bitOrDistinct #col
908+-- in renderExpression expression
909+-- :}
841-- "bit_or(DISTINCT col)"
910-- "bit_or(DISTINCT col)"
842bitOrDistinct
911bitOrDistinct
843 :: PGIntegral int
912 :: PGIntegral int
@@ -846,7 +915,12 @@ bitOrDistinct
846 -> Expression tables ('Grouped bys) params (nullity int)
915 -> Expression tables ('Grouped bys) params (nullity int)
847bitOrDistinct = unsafeAggregateDistinct "bit_or"
916bitOrDistinct = unsafeAggregateDistinct "bit_or"
848
917
849−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAnd #col
918+-- | :{
919+-- let
920+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
921+-- expression = boolAnd #col
922+-- in renderExpression expression
923+-- :}
850-- "bool_and(col)"
924-- "bool_and(col)"
851boolAnd
925boolAnd
852 :: Expression tables 'Ungrouped params (nullity 'PGbool)
926 :: Expression tables 'Ungrouped params (nullity 'PGbool)
@@ -854,7 +928,12 @@ boolAnd
854 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
928 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
855boolAnd = unsafeAggregate "bool_and"
929boolAnd = unsafeAggregate "bool_and"
856
930
857−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOr #col
931+-- | :{
932+-- let
933+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
934+-- expression = boolOr #col
935+-- in renderExpression expression
936+-- :}
858-- "bool_or(col)"
937-- "bool_or(col)"
859boolOr
938boolOr
860 :: Expression tables 'Ungrouped params (nullity 'PGbool)
939 :: Expression tables 'Ungrouped params (nullity 'PGbool)
@@ -862,7 +941,12 @@ boolOr
862 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
941 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
863boolOr = unsafeAggregate "bool_or"
942boolOr = unsafeAggregate "bool_or"
864
943
944+-- | :{
945+-- let
946+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
865−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolAndDistinct #col
947+-- expression = boolAndDistinct #col
948+-- in renderExpression expression
949+-- :}
866-- "bool_and(DISTINCT col)"
950-- "bool_and(DISTINCT col)"
867boolAndDistinct
951boolAndDistinct
868 :: Expression tables 'Ungrouped params (nullity 'PGbool)
952 :: Expression tables 'Ungrouped params (nullity 'PGbool)
@@ -870,7 +954,12 @@ boolAndDistinct
870 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
954 -> Expression tables ('Grouped bys) params (nullity 'PGbool)
871boolAndDistinct = unsafeAggregateDistinct "bool_and"
955boolAndDistinct = unsafeAggregateDistinct "bool_and"
872
956
957+-- | :{
958+-- let
959+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
873−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ boolOrDistinct #col
960+-- expression = boolOrDistinct #col
961+-- in renderExpression expression
962+-- :}
874-- "bool_or(DISTINCT col)"
963-- "bool_or(DISTINCT col)"
875boolOrDistinct
964boolOrDistinct
876 :: Expression tables 'Ungrouped params (nullity 'PGbool)
965 :: Expression tables 'Ungrouped params (nullity 'PGbool)
@@ -886,25 +975,40 @@ countStar
886 :: Expression tables ('Grouped bys) params ('NotNull 'PGint8)
975 :: Expression tables ('Grouped bys) params ('NotNull 'PGint8)
887countStar = UnsafeExpression $ "count(*)"
976countStar = UnsafeExpression $ "count(*)"
888
977
889−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Optional _]] $ count #col
978+-- | :{
979+-- let
980+-- expression :: Expression [tab ::: ["col" ::: nullity ty]] grouping params (nullity 'PGbool)
981+-- expression = count #col
982+-- in renderExpression expression
983+-- :}
890-- "count(col)"
984-- "count(col)"
891count
985count
892− :: Expression tables 'Ungrouped params (ty)
986+ :: Expression tables 'Ungrouped params ty
893 -- ^ what to count
987 -- ^ what to count
894 -> Expression tables ('Grouped bys) params ('NotNull 'PGint8)
988 -> Expression tables ('Grouped bys) params ('NotNull 'PGint8)
895count = unsafeAggregate "count"
989count = unsafeAggregate "count"
896
990
991+-- | :{
992+-- let
993+-- expression :: Expression [tab ::: ["col" ::: nullity ty]] grouping params (nullity 'PGbool)
897−-- | >>> renderExpression @'[_ ::: '["col" ::: 'Required _]] $ countDistinct #col
994+-- expression = countDistinct #col
995+-- in renderExpression expression
996+-- :}
898-- "count(DISTINCT col)"
997-- "count(DISTINCT col)"
899countDistinct
998countDistinct
900− :: Expression tables 'Ungrouped params (ty)
999+ :: Expression tables 'Ungrouped params ty
901 -- ^ what to count
1000 -- ^ what to count
902 -> Expression tables ('Grouped bys) params ('NotNull 'PGint8)
1001 -> Expression tables ('Grouped bys) params ('NotNull 'PGint8)
903countDistinct = unsafeAggregateDistinct "count"
1002countDistinct = unsafeAggregateDistinct "count"
904
1003
905-- | synonym for `boolAnd`
1004-- | synonym for `boolAnd`
906--
1005--
907−-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ every #col
1006+-- :{
1007+-- let
1008+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
1009+-- expression = every #col
1010+-- in renderExpression expression
1011+-- :}
908-- "every(col)"
1012-- "every(col)"
909every
1013every
910 :: Expression tables 'Ungrouped params (nullity 'PGbool)
1014 :: Expression tables 'Ungrouped params (nullity 'PGbool)
@@ -914,7 +1018,12 @@ every = unsafeAggregate "every"
914
1018
915-- | synonym for `boolAndDistinct`
1019-- | synonym for `boolAndDistinct`
916--
1020--
1021+-- :{
1022+-- let
1023+-- expression :: Expression [tab ::: ["col" ::: nullity 'PGbool]] grouping params (nullity 'PGbool)
917−-- >>> renderExpression @'[_ ::: '["col" ::: 'Required (_ 'PGbool)]] $ everyDistinct #col
1024+-- expression = everyDistinct #col
1025+-- in renderExpression expression
1026+-- :}
918-- "every(DISTINCT col)"
1027-- "every(DISTINCT col)"
919everyDistinct
1028everyDistinct
920 :: Expression tables 'Ungrouped params (nullity 'PGbool)
1029 :: Expression tables 'Ungrouped params (nullity 'PGbool)
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+117−53
@@ -30,14 +30,20 @@ module Squeal.PostgreSQL.Manipulation
30 , queryStatement
30 , queryStatement
31 -- * Insert
31 -- * Insert
32 , insertRows
32 , insertRows
33+ , insertRow
34+ , insertRows_
35+ , insertRow_
33 , insertQuery
36 , insertQuery
37+ , insertQuery_
34 , ReturningClause (ReturningStar, Returning)
38 , ReturningClause (ReturningStar, Returning)
35 , renderReturningClause
39 , renderReturningClause
36 , ConflictClause (OnConflictDoRaise, OnConflictDoNothing, OnConflictDoUpdate)
40 , ConflictClause (OnConflictDoRaise, OnConflictDoNothing, OnConflictDoUpdate)
37 , renderConflictClause
41 , renderConflictClause
38 -- * Update
42 -- * Update
39 , update
43 , update
44+ , update_
40 , deleteFrom
45 , deleteFrom
46+ , deleteFrom_
41 , ColumnValue (..)
47 , ColumnValue (..)
42 ) where
48 ) where
43
49
@@ -86,30 +92,27 @@ simple insert:
86>>> :{
92>>> :{
87let
93let
88 manipulation :: Manipulation
94 manipulation :: Manipulation
89− '[ "tab" :::
95+ '[ "tab" ::: '[] :=>
90− '[ "col1" ::: 'Required ('NotNull 'PGint4)
96+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
91− , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[] '[]
97+ , "col2" ::: 'Def :=> 'NotNull 'PGint4 ]] '[] '[]
92 manipulation =
98 manipulation =
93− insertInto #tab (ColumnValues (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
99+ insertRow_ #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
94− OnConflictDoRaise (Returning Nil)
95in renderManipulation manipulation
100in renderManipulation manipulation
96:}
101:}
97−"INSERT INTO tab (col1, col2) VALUES (2, 4);"
102+"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT);"
98
103
99parameterized insert:
104parameterized insert:
100
105
101>>> :{
106>>> :{
102let
107let
103 manipulation :: Manipulation
108 manipulation :: Manipulation
104− '[ "tab" :::
109+ '[ "tab" ::: '[] :=>
105− '[ "col1" ::: 'Required ('NotNull 'PGint4)
110+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
106− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
111+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
107− '[ 'Required ('NotNull 'PGint4)
112+ '[ 'NotNull 'PGint4, 'NotNull 'PGint4 ] '[]
108− , 'Required ('NotNull 'PGint4) ] '[]
109 manipulation =
113 manipulation =
110− insertInto #tab
114+ insertRow_ #tab
111− (ColumnValues (param @1 `As` #col1 :* param @2 `As` #col2 :* Nil) [])
115+ (Set (param @1) `As` #col1 :* Set (param @2) `As` #col2 :* Nil)
112− OnConflictDoRaise (Returning Nil)
113in renderManipulation manipulation
116in renderManipulation manipulation
114:}
117:}
115"INSERT INTO tab (col1, col2) VALUES (($1 :: int4), ($2 :: int4));"
118"INSERT INTO tab (col1, col2) VALUES (($1 :: int4), ($2 :: int4));"
@@ -119,36 +122,34 @@ returning insert:
119>>> :{
122>>> :{
120let
123let
121 manipulation :: Manipulation
124 manipulation :: Manipulation
122− '[ "tab" :::
125+ '[ "tab" ::: '[] :=>
123− '[ "col1" ::: 'Required ('NotNull 'PGint4)
126+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
124− , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[]
127+ , "col2" ::: 'Def :=> 'NotNull 'PGint4 ]] '[]
125− '["fromOnly" ::: 'Required ('NotNull 'PGint4)]
128+ '["fromOnly" ::: 'NotNull 'PGint4]
126 manipulation =
129 manipulation =
127− insertInto #tab (ColumnValues (2 `As` #col1 :* 4 `As` #col2 :* Nil) [])
130+ insertRow #tab (Set 2 `As` #col1 :* Default `As` #col2 :* Nil)
128 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
131 OnConflictDoRaise (Returning (#col1 `As` #fromOnly :* Nil))
129in renderManipulation manipulation
132in renderManipulation manipulation
130:}
133:}
131−"INSERT INTO tab (col1, col2) VALUES (2, 4) RETURNING col1 AS fromOnly;"
134+"INSERT INTO tab (col1, col2) VALUES (2, DEFAULT) RETURNING col1 AS fromOnly;"
132
135
133query insert:
136query insert:
134
137
135>>> :{
138>>> :{
136let
139let
137 manipulation :: Manipulation
140 manipulation :: Manipulation
138− '[ "tab" :::
141+ '[ "tab" ::: '[] :=>
139− '[ "col1" ::: 'Required ('NotNull 'PGint4)
142+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
140− , "col2" ::: 'Required ('NotNull 'PGint4)
143+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4
141 ]
144 ]
142− , "other_tab" :::
145+ , "other_tab" ::: '[] :=>
143− '[ "col1" ::: 'Required ('NotNull 'PGint4)
146+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
144− , "col2" ::: 'Required ('NotNull 'PGint4)
147+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4
145 ]
148 ]
146 ] '[] '[]
149 ] '[] '[]
147 manipulation =
150 manipulation =
148− insertInto #tab
151+ insertQuery_ #tab
149− ( ColumnValuesQuery $
150− selectStar (from (Table (#other_tab `As` #t))) )
152+ (selectStar (from (Table (#other_tab `As` #t))))
151− OnConflictDoRaise (Returning Nil)
152in renderManipulation manipulation
153in renderManipulation manipulation
153:}
154:}
154"INSERT INTO tab SELECT * FROM other_tab AS t;"
155"INSERT INTO tab SELECT * FROM other_tab AS t;"
@@ -158,18 +159,17 @@ upsert:
158>>> :{
159>>> :{
159let
160let
160 manipulation :: Manipulation
161 manipulation :: Manipulation
161− '[ "tab" :::
162+ '[ "tab" ::: '[] :=>
162− '[ "col1" ::: 'Required ('NotNull 'PGint4)
163+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
163− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
164+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
164− '[] '[ "sum" ::: 'Required ('NotNull 'PGint4)]
165+ '[] '[ "sum" ::: 'NotNull 'PGint4]
165 manipulation =
166 manipulation =
166− insertInto #tab
167+ insertRows #tab
167− (ColumnValues
168− (2 `As` #col1 :* 4 `As` #col2 :* Nil)
168+ (Set 2 `As` #col1 :* Set 4 `As` #col2 :* Nil)
169− [6 `As` #col1 :* 8 `As` #col2 :* Nil])
169+ [Set 6 `As` #col1 :* Set 8 `As` #col2 :* Nil]
170 (OnConflictDoUpdate
170 (OnConflictDoUpdate
171 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
171 (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
172− (Just (#col1 .== #col2)))
172+ [#col1 .== #col2])
173 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
173 (Returning $ (#col1 + #col2) `As` #sum :* Nil)
174in renderManipulation manipulation
174in renderManipulation manipulation
175:}
175:}
@@ -204,6 +204,39 @@ insertRows tab row rows conflict returning = UnsafeManipulation $
204 Default -> "DEFAULT"
204 Default -> "DEFAULT"
205 Set expression -> renderExpression expression
205 Set expression -> renderExpression expression
206
206
207+insertRow
208+ :: ( SOP.SListI columns
209+ , SOP.SListI results
210+ , Has tab schema table
211+ , columns ~ TableToColumns table )
212+ => Alias tab -- ^ table to insert into
213+ -> NP (Aliased (ColumnValue '[] params)) columns
214+ -> ConflictClause columns params
215+ -- ^ what to do in case of constraint conflict
216+ -> ReturningClause columns params results -- ^ results to return
217+ -> Manipulation schema params results
218+insertRow tab row = insertRows tab row []
219+
220+insertRows_
221+ :: ( SOP.SListI columns
222+ , Has tab schema table
223+ , columns ~ TableToColumns table )
224+ => Alias tab -- ^ table to insert into
225+ -> NP (Aliased (ColumnValue '[] params)) columns
226+ -> [NP (Aliased (ColumnValue '[] params)) columns]
227+ -> Manipulation schema params '[]
228+insertRows_ tab row rows =
229+ insertRows tab row rows OnConflictDoRaise (Returning Nil)
230+
231+insertRow_
232+ :: ( SOP.SListI columns
233+ , Has tab schema table
234+ , columns ~ TableToColumns table )
235+ => Alias tab -- ^ table to insert into
236+ -> NP (Aliased (ColumnValue '[] params)) columns
237+ -> Manipulation schema params '[]
238+insertRow_ tab row = insertRow tab row OnConflictDoRaise (Returning Nil)
239+
207insertQuery
240insertQuery
208 :: ( SOP.SListI columns
241 :: ( SOP.SListI columns
209 , SOP.SListI results
242 , SOP.SListI results
@@ -221,6 +254,16 @@ insertQuery tab query conflict returning = UnsafeManipulation $
221 <> renderConflictClause conflict
254 <> renderConflictClause conflict
222 <> renderReturningClause returning
255 <> renderReturningClause returning
223
256
257+insertQuery_
258+ :: ( SOP.SListI columns
259+ , Has tab schema table
260+ , columns ~ TableToColumns table )
261+ => Alias tab -- ^ table to insert into
262+ -> Query schema params (ColumnsToRelation columns)
263+ -> Manipulation schema params '[]
264+insertQuery_ tab query =
265+ insertQuery tab query OnConflictDoRaise (Returning Nil)
266+
224data ColumnValue
267data ColumnValue
225 (columns :: RelationType)
268 (columns :: RelationType)
226 (params :: [NullityType])
269 (params :: [NullityType])
@@ -248,11 +291,11 @@ data ReturningClause
248 (results :: RelationType)
291 (results :: RelationType)
249 where
292 where
250 ReturningStar
293 ReturningStar
294+ :: results ~ ColumnsToRelation columns
251− :: ReturningClause columns params (ColumnsToRelation columns)
295+ => ReturningClause columns params results
252 Returning
296 Returning
253− :: NP
297+ :: rel ~ ColumnsToRelation columns
254− (Aliased (Expression '[table ::: (ColumnsToRelation columns)] 'Ungrouped params))
298+ => NP (Aliased (Expression '[table ::: rel] 'Ungrouped params)) results
255− results
256 -> ReturningClause columns params results
299 -> ReturningClause columns params results
257
300
258-- | Render a `ReturningClause`.
301-- | Render a `ReturningClause`.
@@ -314,12 +357,12 @@ UPDATE statements
314-- >>> :{
357-- >>> :{
315-- let
358-- let
316-- manipulation :: Manipulation
359-- manipulation :: Manipulation
317−-- '[ "tab" :::
360+-- '[ "tab" ::: '[] :=>
318−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
361+-- '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
319−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[] '[]
362+-- , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]] '[] '[]
320-- manipulation =
363-- manipulation =
321−-- update #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
364+-- update_ #tab (Set 2 `As` #col1 :* Same `As` #col2 :* Nil)
322−-- (#col1 ./= #col2) (Returning Nil)
365+-- (#col1 ./= #col2)
323-- in renderManipulation manipulation
366-- in renderManipulation manipulation
324-- :}
367-- :}
325-- "UPDATE tab SET col1 = 2 WHERE (col1 <> col2);"
368-- "UPDATE tab SET col1 = 2 WHERE (col1 <> col2);"
@@ -353,6 +396,18 @@ update table columns wh returning = UnsafeManipulation $
353 Set expression `As` column -> Just $
396 Set expression `As` column -> Just $
354 renderAlias column <+> "=" <+> renderExpression expression
397 renderAlias column <+> "=" <+> renderExpression expression
355
398
399+update_
400+ :: ( SOP.SListI columns
401+ , Has tab schema table
402+ , columns ~ TableToColumns table )
403+ => Alias tab -- ^ table to update
404+ -> NP (Aliased (ColumnValue (ColumnsToRelation columns) params)) columns
405+ -- ^ modified values to replace old values
406+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
407+ -- ^ condition under which to perform update on a row
408+ -> Manipulation schema params '[]
409+update_ table columns wh = update table columns wh (Returning Nil)
410+
356{-----------------------------------------
411{-----------------------------------------
357DELETE statements
412DELETE statements
358-----------------------------------------}
413-----------------------------------------}
@@ -362,11 +417,11 @@ DELETE statements
362-- >>> :{
417-- >>> :{
363-- let
418-- let
364-- manipulation :: Manipulation
419-- manipulation :: Manipulation
365−-- '[ "tab" :::
420+-- '[ "tab" ::: '[] :=>
366−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
421+-- '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
367−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]] '[]
422+-- , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]] '[]
368−-- '[ "col1" ::: 'Required ('NotNull 'PGint4)
423+-- '[ "col1" ::: 'NotNull 'PGint4
369−-- , "col2" ::: 'Required ('NotNull 'PGint4) ]
424+-- , "col2" ::: 'NotNull 'PGint4 ]
370-- manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
425-- manipulation = deleteFrom #tab (#col1 .== #col2) ReturningStar
371-- in renderManipulation manipulation
426-- in renderManipulation manipulation
372-- :}
427-- :}
@@ -384,3 +439,12 @@ deleteFrom table wh returning = UnsafeManipulation $
384 "DELETE FROM" <+> renderAlias table
439 "DELETE FROM" <+> renderAlias table
385 <+> "WHERE" <+> renderExpression wh
440 <+> "WHERE" <+> renderExpression wh
386 <> renderReturningClause returning
441 <> renderReturningClause returning
442+
443+deleteFrom_
444+ :: ( Has tab schema table
445+ , columns ~ TableToColumns table )
446+ => Alias tab -- ^ table to delete from
447+ -> Condition '[tab ::: ColumnsToRelation columns] 'Ungrouped params
448+ -- ^ condition under which to delete a row
449+ -> Manipulation schema params '[]
450+deleteFrom_ tab wh = deleteFrom tab wh (Returning Nil)
squeal-postgresql/src/Squeal/PostgreSQL/PQ.hs+2−1
@@ -119,8 +119,9 @@ backslashes within the value must be escaped with a backslash, i.e., ' and \.
119To specify the schema you wish to connect with, use type application.
119To specify the schema you wish to connect with, use type application.
120
120
121>>> :set -XDataKinds
121>>> :set -XDataKinds
122+>>> :set -XPolyKinds
122>>> :set -XTypeOperators
123>>> :set -XTypeOperators
123−>>> type Schema = '["tab" ::: Unconstraint :=> '["col" ::: 'NoDef :=> 'Null 'PGint2]]
124+>>> type Schema = '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint2]]
124>>> :set -XTypeApplications
125>>> :set -XTypeApplications
125>>> :set -XOverloadedStrings
126>>> :set -XOverloadedStrings
126>>> conn <- connectdb @Schema "host=localhost port=5432 dbname=exampledb"
127>>> conn <- connectdb @Schema "host=localhost port=5432 dbname=exampledb"
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+54−71
@@ -77,7 +77,6 @@ import Data.Word
77import Generics.SOP hiding (from)
77import Generics.SOP hiding (from)
78import GHC.TypeLits
78import GHC.TypeLits
79
79
80−import qualified Data.ByteString as ByteString
81import qualified GHC.Generics as GHC
80import qualified GHC.Generics as GHC
82
81
83import Squeal.PostgreSQL.Expression
82import Squeal.PostgreSQL.Expression
@@ -94,8 +93,10 @@ simple query:
94
93
95>>> :{
94>>> :{
96let
95let
96+ query :: Query
97− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
97+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
98+ '[]
98− '["col" ::: 'Required ('Null 'PGint4)]
99+ '["col" ::: 'Null 'PGint4]
99 query = selectStar (from (Table (#tab `As` #t)))
100 query = selectStar (from (Table (#tab `As` #t)))
100in renderQuery query
101in renderQuery query
101:}
102:}
@@ -106,12 +107,12 @@ restricted query:
106>>> :{
107>>> :{
107let
108let
108 query :: Query
109 query :: Query
109− '[ "tab" :::
110+ '[ "tab" ::: '[] :=>
110− '[ "col1" ::: 'Required ('NotNull 'PGint4)
111+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
111− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
112+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
112 '[]
113 '[]
113− '[ "sum" ::: 'Required ('NotNull 'PGint4)
114+ '[ "sum" ::: 'NotNull 'PGint4
114− , "col1" ::: 'Required ('NotNull 'PGint4) ]
115+ , "col1" ::: 'NotNull 'PGint4 ]
115 query =
116 query =
116 select
117 select
117 ((#col1 + #col2) `As` #sum :* #col1 `As` #col1 :* Nil)
118 ((#col1 + #col2) `As` #sum :* #col1 `As` #col1 :* Nil)
@@ -126,8 +127,10 @@ subquery:
126
127
127>>> :{
128>>> :{
128let
129let
130+ query :: Query
129− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
131+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
132+ '[]
130− '["col" ::: 'Required ('Null 'PGint4)]
133+ '["col" ::: 'Null 'PGint4]
131 query =
134 query =
132 selectStar
135 selectStar
133 (from (Subquery (selectStar (from (Table (#tab `As` #t))) `As` #sub)))
136 (from (Subquery (selectStar (from (Table (#tab `As` #t))) `As` #sub)))
@@ -139,8 +142,10 @@ limits and offsets:
139
142
140>>> :{
143>>> :{
141let
144let
145+ query :: Query
142− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
146+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
147+ '[]
143− '["col" ::: 'Required ('Null 'PGint4)]
148+ '["col" ::: 'Null 'PGint4]
144 query = selectStar
149 query = selectStar
145 (from (Table (#tab `As` #t)) & limit 100 & offset 2 & limit 50 & offset 2)
150 (from (Table (#tab `As` #t)) & limit 100 & offset 2 & limit 50 & offset 2)
146in renderQuery query
151in renderQuery query
@@ -151,9 +156,10 @@ parameterized query:
151
156
152>>> :{
157>>> :{
153let
158let
159+ query :: Query
154− query :: Query '["tab" ::: '["col" ::: 'Required ('NotNull 'PGfloat8)]]
160+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGfloat8]]
155− '[ 'Required ('NotNull 'PGfloat8)]
161+ '[ 'NotNull 'PGfloat8]
156− '["col" ::: 'Required ('NotNull 'PGfloat8)]
162+ '["col" ::: 'NotNull 'PGfloat8]
157 query = selectStar
163 query = selectStar
158 (from (Table (#tab `As` #t)) & where_ (#col .> param @1))
164 (from (Table (#tab `As` #t)) & where_ (#col .> param @1))
159in renderQuery query
165in renderQuery query
@@ -165,12 +171,12 @@ aggregation query:
165>>> :{
171>>> :{
166let
172let
167 query :: Query
173 query :: Query
168− '[ "tab" :::
174+ '[ "tab" ::: '[] :=>
169− '[ "col1" ::: 'Required ('NotNull 'PGint4)
175+ '[ "col1" ::: 'NoDef :=> 'NotNull 'PGint4
170− , "col2" ::: 'Required ('NotNull 'PGint4) ]]
176+ , "col2" ::: 'NoDef :=> 'NotNull 'PGint4 ]]
171 '[]
177 '[]
172− '[ "sum" ::: 'Required ('NotNull 'PGint4)
178+ '[ "sum" ::: 'NotNull 'PGint4
173− , "col1" ::: 'Required ('NotNull 'PGint4) ]
179+ , "col1" ::: 'NotNull 'PGint4 ]
174 query =
180 query =
175 select (sum_ #col2 `As` #sum :* #col1 `As` #col1 :* Nil)
181 select (sum_ #col2 `As` #sum :* #col1 `As` #col1 :* Nil)
176 ( from (Table (#tab `As` #table1))
182 ( from (Table (#tab `As` #table1))
@@ -184,8 +190,10 @@ sorted query:
184
190
185>>> :{
191>>> :{
186let
192let
193+ query :: Query
187− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
194+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
195+ '[]
188− '["col" ::: 'Required ('Null 'PGint4)]
196+ '["col" ::: 'Null 'PGint4]
189 query = selectStar
197 query = selectStar
190 (from (Table (#tab `As` #t)) & orderBy [#col & AscNullsFirst])
198 (from (Table (#tab `As` #t)) & orderBy [#col & AscNullsFirst])
191in renderQuery query
199in renderQuery query
@@ -199,24 +207,29 @@ joins:
199let
207let
200 query :: Query
208 query :: Query
201 '[ "orders" :::
209 '[ "orders" :::
210+ '["pk" ::: PrimaryKey '["id"]
211+ ,"fk_customer" ::: ForeignKey '["customer_id"] "customers" '["id"]
212+ ,"fk_shipper" ::: ForeignKey '["shipper_id"] "shippers" '["id"]] :=>
202− '[ "id" ::: 'Required ('NotNull 'PGint4)
213+ '[ "id" ::: 'NoDef :=> 'NotNull 'PGint4
203− , "price" ::: 'Required ('NotNull 'PGfloat4)
214+ , "price" ::: 'NoDef :=> 'NotNull 'PGfloat4
204− , "customer_id" ::: 'Required ('NotNull 'PGint4)
215+ , "customer_id" ::: 'NoDef :=> 'NotNull 'PGint4
205− , "shipper_id" ::: 'Required ('NotNull 'PGint4)
216+ , "shipper_id" ::: 'NoDef :=> 'NotNull 'PGint4
206 ]
217 ]
207 , "customers" :::
218 , "customers" :::
219+ '["pk" ::: PrimaryKey '["id"]] :=>
208− '[ "id" ::: 'Required ('NotNull 'PGint4)
220+ '[ "id" ::: 'NoDef :=> 'NotNull 'PGint4
209− , "name" ::: 'Required ('NotNull 'PGtext)
221+ , "name" ::: 'NoDef :=> 'NotNull 'PGtext
210 ]
222 ]
211 , "shippers" :::
223 , "shippers" :::
224+ '["pk" ::: PrimaryKey '["id"]] :=>
212− '[ "id" ::: 'Required ('NotNull 'PGint4)
225+ '[ "id" ::: 'NoDef :=> 'NotNull 'PGint4
213− , "name" ::: 'Required ('NotNull 'PGtext)
226+ , "name" ::: 'NoDef :=> 'NotNull 'PGtext
214 ]
227 ]
215 ]
228 ]
216 '[]
229 '[]
217− '[ "order_price" ::: 'Required ('NotNull 'PGfloat4)
230+ '[ "order_price" ::: 'NotNull 'PGfloat4
218− , "customer_name" ::: 'Required ('NotNull 'PGtext)
231+ , "customer_name" ::: 'NotNull 'PGtext
219− , "shipper_name" ::: 'Required ('NotNull 'PGtext)
232+ , "shipper_name" ::: 'NotNull 'PGtext
220 ]
233 ]
221 query = select
234 query = select
222 ( #o ! #price `As` #order_price :*
235 ( #o ! #price `As` #order_price :*
@@ -235,8 +248,10 @@ self-join:
235
248
236>>> :{
249>>> :{
237let
250let
251+ query :: Query
238− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
252+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
253+ '[]
239− '["col" ::: 'Required ('Null 'PGint4)]
254+ '["col" ::: 'Null 'PGint4]
240 query = selectDotStar #t1
255 query = selectDotStar #t1
241 (from (Table (#tab `As` #t1) & CrossJoin (Table (#tab `As` #t2))))
256 (from (Table (#tab `As` #t1) & CrossJoin (Table (#tab `As` #t2))))
242in renderQuery query
257in renderQuery query
@@ -247,8 +262,10 @@ set operations:
247
262
248>>> :{
263>>> :{
249let
264let
265+ query :: Query
250− query :: Query '["tab" ::: '["col" ::: 'Required ('Null 'PGint4)]] '[]
266+ '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4]]
267+ '[]
251− '["col" ::: 'Required ('Null 'PGint4)]
268+ '["col" ::: 'Null 'PGint4]
252 query =
269 query =
253 selectStar (from (Table (#tab `As` #t)))
270 selectStar (from (Table (#tab `As` #t)))
254 `unionAll`
271 `unionAll`
@@ -619,9 +636,6 @@ data FromClause schema params tables where
619 Subquery
636 Subquery
620 :: Aliased (Query schema params) table
637 :: Aliased (Query schema params) table
621 -> FromClause schema params '[table]
638 -> FromClause schema params '[table]
622− JoinRef
623− :: JoinRef schema tables
624− -> FromClause schema params tables
625 CrossJoin
639 CrossJoin
626 :: FromClause schema params right
640 :: FromClause schema params right
627 -> FromClause schema params left
641 -> FromClause schema params left
@@ -652,7 +666,6 @@ data FromClause schema params tables where
652renderFromClause :: FromClause schema params tables -> ByteString
666renderFromClause :: FromClause schema params tables -> ByteString
653renderFromClause = \case
667renderFromClause = \case
654 Table table -> renderAliasedAs renderTable table
668 Table table -> renderAliasedAs renderTable table
655− JoinRef joinRef -> renderJoinRef joinRef
656 Subquery selection -> renderAliasedAs (parenthesized . renderQuery) selection
669 Subquery selection -> renderAliasedAs (parenthesized . renderQuery) selection
657 CrossJoin right left ->
670 CrossJoin right left ->
658 renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
671 renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
@@ -665,36 +678,6 @@ renderFromClause = \case
665 renderFromClause left <+> op <+> renderFromClause right
678 renderFromClause left <+> op <+> renderFromClause right
666 <+> "ON" <+> renderExpression on
679 <+> "ON" <+> renderExpression on
667
680
668−newtype JoinRef (schema :: TablesType) (relations :: RelationsType)
669− = UnsafeJoinRef { renderJoinRef :: ByteString }
670− deriving (GHC.Generic,Show,Eq,Ord,NFData)
671−
672−utilizing
673− :: ( Has left schema (constraints :=> lcols)
674− , ColumnsToRelation lcols ~ lrel
675− , Has right (TablesToRelations schema) rrel
676− , Has ref constraints ('ForeignKey lnames right rnames)
677− , All KnownSymbol lnames
678− , All KnownSymbol rnames
679− )
680− => (Alias right, NP Alias rnames)
681− -> Alias ref
682− -> (Alias left, NP Alias lnames)
683− -> JoinRef schema '[left ::: lrel,right ::: rrel]
684−utilizing (rtab,rcols) _ref (ltab,lcols) = UnsafeJoinRef $
685− lname <+> "JOIN" <+> rname <+> "ON" <+> refEq
686− where
687− lname = renderAlias ltab
688− rname = renderAlias rtab
689− eq1 lcol rcol = lname <> "." <> lcol <> " = " <> rname <> "." <> rcol
690− renderAliases :: All KnownSymbol names => NP Alias names -> [ByteString]
691− renderAliases = \case
692− Nil -> []
693− alias :* aliases -> renderAlias alias : renderAliases aliases
694− refEq = ByteString.intercalate " AND " $ zipWith eq1
695− (renderAliases lcols)
696− (renderAliases rcols)
697−
698{-----------------------------------------
681{-----------------------------------------
699Grouping
682Grouping
700-----------------------------------------}
683-----------------------------------------}
squeal-postgresql/src/Squeal/PostgreSQL/Untitled-1new file+5
@@ -0,0 +1,5 @@
1+:{
2+type Schema0 = '["tab" ::: '[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
3+type Schema1 = '["tab" ::: '["positive" ::: 'Check ] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4]]
4+let x = 'x'
5+:}