@@ -11,11 +11,12 @@ Squeal data definition language.
1111{-# LANGUAGE
1212 AllowAmbiguousTypes
1313 , ConstraintKinds
14+ , DeriveAnyClass
1415 , DeriveGeneric
16+ , DerivingStrategies
1517 , FlexibleContexts
1618 , FlexibleInstances
1719 , GADTs
18− , GeneralizedNewtypeDeriving
1920 , LambdaCase
2021 , MultiParamTypeClasses
2122 , OverloadedStrings
@@ -1061,15 +1062,54 @@ createTypeRange range ty = UnsafeDefinition $
10611062 "CREATE" <+> "TYPE" <+> renderSQL range <+> "AS" <+> "RANGE" <+>
10621063 parenthesized ("subtype" <+> "=" <+> renderTypeExpression ty) <> ";"
10631064
1065+{- |
1066+>>> :{
1067+type Table = '[] :=>
1068+ '[ "a" ::: 'NoDef :=> 'Null 'PGint4
1069+ , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
1070+:}
1071+
1072+>>> :{
1073+let
1074+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1075+ setup =
1076+ createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1077+ createIndex #ix #tab Gin [#a & AscNullsFirst, #b & AscNullsLast]
1078+in printSQL setup
1079+:}
1080+CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
1081+CREATE INDEX "ix" ON "tab" USING gin (("a" ASC NULLS FIRST), ("b" ASC NULLS LAST));
1082+-}
10641083createIndex
10651084 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
10661085 => Alias ix
10671086 -> QualifiedAlias sch tab
1087+ -> IndexMethod
10681088 -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
10691089 -> Definition schemas (Alter sch (Create ix 'Index schema) schemas)
1070−createIndex ix tab cols = UnsafeDefinition $
1090+createIndex ix tab method cols = UnsafeDefinition $
10711091 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1092+ <+> "USING" <+> renderSQL method
10721093 <+> parenthesized (commaSeparated (parenthesized . renderSQL <$> cols))
1094+ <> ";"
1095+
1096+data IndexMethod
1097+ = Btree
1098+ | Hash
1099+ | Gist
1100+ | Spgist
1101+ | Gin
1102+ | Brin
1103+ deriving stock (Eq, Ord, Show, GHC.Generic)
1104+ deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1105+instance RenderSQL IndexMethod where
1106+ renderSQL = \case
1107+ Btree -> "btree"
1108+ Hash -> "hash"
1109+ Gist -> "gist"
1110+ Spgist -> "spgist"
1111+ Gin -> "gin"
1112+ Brin -> "brin"
10731113
10741114-- | Lift `PGTyped` to a field
10751115class FieldTyped schemas ty where