on dev help sign in
indexes
d641873Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+22−1
@@ -46,6 +46,7 @@ module Squeal.PostgreSQL.Definition
46 , FieldTyped (..)
46 , FieldTyped (..)
47 , createDomain
47 , createDomain
48 , createTypeRange
48 , createTypeRange
49+ , createIndex
49 , TableConstraintExpression (..)
50 , TableConstraintExpression (..)
50 , check
51 , check
51 , unique
52 , unique
@@ -101,6 +102,7 @@ import qualified GHC.Generics as GHC
101import Squeal.PostgreSQL.Alias
102import Squeal.PostgreSQL.Alias
102import Squeal.PostgreSQL.Expression
103import Squeal.PostgreSQL.Expression
103import Squeal.PostgreSQL.Expression.Logic
104import Squeal.PostgreSQL.Expression.Logic
105+import Squeal.PostgreSQL.Expression.Sort
104import Squeal.PostgreSQL.Expression.Type
106import Squeal.PostgreSQL.Expression.Type
105import Squeal.PostgreSQL.List
107import Squeal.PostgreSQL.List
106import Squeal.PostgreSQL.PG
108import Squeal.PostgreSQL.PG
@@ -1041,6 +1043,15 @@ createDomain dom ty condition =
1041 <+> "AS" <+> renderTypeExpression ty
1043 <+> "AS" <+> renderTypeExpression ty
1042 <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
1044 <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
1043
1045
1046+{- |
1047+>>> :{
1048+let
1049+ createSmallIntRange :: Definition (Public '[]) (Public '["int2range" ::: 'Typedef ('PGrange 'PGint2)])
1050+ createSmallIntRange = createTypeRange #int2range int2
1051+in printSQL createSmallIntRange
1052+:}
1053+CREATE TYPE "int2range" AS RANGE (subtype = int2);
1054+-}
1044createTypeRange
1055createTypeRange
1045 :: (Has sch schemas schema, KnownSymbol range)
1056 :: (Has sch schemas schema, KnownSymbol range)
1046 => QualifiedAlias sch range
1057 => QualifiedAlias sch range
@@ -1048,7 +1059,17 @@ createTypeRange
1048 -> Definition schemas (Alter sch (Create range ('Typedef ('PGrange ty)) schema) schemas)
1059 -> Definition schemas (Alter sch (Create range ('Typedef ('PGrange ty)) schema) schemas)
1049createTypeRange range ty = UnsafeDefinition $
1060createTypeRange range ty = UnsafeDefinition $
1050 "CREATE" <+> "TYPE" <+> renderSQL range <+> "AS" <+> "RANGE" <+>
1061 "CREATE" <+> "TYPE" <+> renderSQL range <+> "AS" <+> "RANGE" <+>
1051− parenthesized ("subtype" <+> "=" <+> renderTypeExpression ty)
1062+ parenthesized ("subtype" <+> "=" <+> renderTypeExpression ty) <> ";"
1063+
1064+createIndex
1065+ :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1066+ => Alias ix
1067+ -> QualifiedAlias sch tab
1068+ -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1069+ -> Definition schemas (Alter sch (Create ix 'Index schema) schemas)
1070+createIndex ix tab cols = UnsafeDefinition $
1071+ "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1072+ <+> parenthesized (commaSeparated (parenthesized . renderSQL <$> cols))
1052
1073
1053-- | Lift `PGTyped` to a field
1074-- | Lift `PGTyped` to a field
1054class FieldTyped schemas ty where
1075class FieldTyped schemas ty where
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+2
@@ -365,6 +365,8 @@ data SchemumType
365 = Table TableType
365 = Table TableType
366 | View RowType
366 | View RowType
367 | Typedef PGType
367 | Typedef PGType
368+ | Index
369+ | UnsafeSchemum Symbol
368
370
369{- | The schema of a database consists of a list of aliased,
371{- | The schema of a database consists of a list of aliased,
370user-defined `SchemumType`s.
372user-defined `SchemumType`s.