on dev help sign in
indexes

and range doctest

commitd641873800fe31c110c359c9d60bbecfdeb67270 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateFri 30 Aug 2019, 13:43 (7 years ago) parent7996804
2 files changed+24−1
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+22−1 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+2
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+22−1
@@ -46,6 +46,7 @@ module Squeal.PostgreSQL.Definition
4646 , FieldTyped (..)
4747 , createDomain
4848 , createTypeRange
49+ , createIndex
4950 , TableConstraintExpression (..)
5051 , check
5152 , unique
@@ -101,6 +102,7 @@ import qualified GHC.Generics as GHC
101102import Squeal.PostgreSQL.Alias
102103import Squeal.PostgreSQL.Expression
103104import Squeal.PostgreSQL.Expression.Logic
105+import Squeal.PostgreSQL.Expression.Sort
104106import Squeal.PostgreSQL.Expression.Type
105107import Squeal.PostgreSQL.List
106108import Squeal.PostgreSQL.PG
@@ -1041,6 +1043,15 @@ createDomain dom ty condition =
10411043 <+> "AS" <+> renderTypeExpression ty
10421044 <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
10431045
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+-}
10441055createTypeRange
10451056 :: (Has sch schemas schema, KnownSymbol range)
10461057 => QualifiedAlias sch range
@@ -1048,7 +1059,17 @@ createTypeRange
10481059 -> Definition schemas (Alter sch (Create range ('Typedef ('PGrange ty)) schema) schemas)
10491060createTypeRange range ty = UnsafeDefinition $
10501061 "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))
10521073
10531074-- | Lift `PGTyped` to a field
10541075class FieldTyped schemas ty where
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+2
@@ -365,6 +365,8 @@ data SchemumType
365365 = Table TableType
366366 | View RowType
367367 | Typedef PGType
368+ | Index
369+ | UnsafeSchemum Symbol
368370
369371{- | The schema of a database consists of a list of aliased,
370372user-defined `SchemumType`s.