{-# LANGUAGE
AllowAmbiguousTypes
, ConstraintKinds
, DeriveAnyClass
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, DataKinds
, PolyKinds
, TypeOperators
, UndecidableSuperClasses
#-}
module Squeal.PostgreSQL.Definition.Index
(
createIndex
, createIndexIfNotExists
, dropIndex
, dropIndexIfExists
, IndexMethod (..)
, btree
, hash
, gist
, spgist
, gin
, brin
) where
import Data.ByteString
import GHC.TypeLits
import qualified GHC.Generics as GHC
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Definition
import Squeal.PostgreSQL.Expression.Sort
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
createIndex
:: (Has sch db schema, Has tab schema ('Table table), KnownSymbol ix)
=> Alias ix
-> QualifiedAlias sch tab
-> IndexMethod method
-> [SortExpression 'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
-> Definition db (Alter sch (Create ix ('Index method) schema) db)
createIndex :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
(schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
(table :: TableType) (ix :: Symbol) (method :: IndexType).
(Has sch db schema, Has tab schema ('Table table),
KnownSymbol ix) =>
Alias ix
-> QualifiedAlias sch tab
-> IndexMethod method
-> [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
-> Definition db (Alter sch (Create ix ('Index method) schema) db)
createIndex Alias ix
ix QualifiedAlias sch tab
tab IndexMethod method
method [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
cols = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
(db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE" ByteString -> ByteString -> ByteString
<+> ByteString
"INDEX" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias ix
ix ByteString -> ByteString -> ByteString
<+> ByteString
"ON" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab
ByteString -> ByteString -> ByteString
<+> ByteString
"USING" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL IndexMethod method
method
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ([ByteString] -> ByteString
commaSeparated (forall {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: [(Symbol, [(Symbol, SchemumType)])]} {params :: [NullType]}
{from :: FromType}.
SortExpression grp lat with db params from -> ByteString
renderIndex forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
cols))
forall a. Semigroup a => a -> a -> a
<> ByteString
";"
where
renderIndex :: SortExpression grp lat with db params from -> ByteString
renderIndex = \case
Asc Expression grp lat with db params from ('NotNull ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression) ByteString -> ByteString -> ByteString
<+> ByteString
"ASC"
Desc Expression grp lat with db params from ('NotNull ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression) ByteString -> ByteString -> ByteString
<+> ByteString
"DESC"
AscNullsFirst Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS FIRST"
DescNullsFirst Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS FIRST"
AscNullsLast Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS LAST"
DescNullsLast Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS LAST"
createIndexIfNotExists
:: (Has sch db schema, Has tab schema ('Table table), KnownSymbol ix)
=> Alias ix
-> QualifiedAlias sch tab
-> IndexMethod method
-> [SortExpression 'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
-> Definition db (Alter sch (CreateIfNotExists ix ('Index method) schema) db)
createIndexIfNotExists :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
(schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
(table :: TableType) (ix :: Symbol) (method :: IndexType).
(Has sch db schema, Has tab schema ('Table table),
KnownSymbol ix) =>
Alias ix
-> QualifiedAlias sch tab
-> IndexMethod method
-> [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
-> Definition
db (Alter sch (CreateIfNotExists ix ('Index method) schema) db)
createIndexIfNotExists Alias ix
ix QualifiedAlias sch tab
tab IndexMethod method
method [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
cols = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
(db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE INDEX IF NOT EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias ix
ix ByteString -> ByteString -> ByteString
<+> ByteString
"ON" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab
ByteString -> ByteString -> ByteString
<+> ByteString
"USING" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL IndexMethod method
method
ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized ([ByteString] -> ByteString
commaSeparated (forall {grp :: Grouping} {lat :: FromType} {with :: FromType}
{db :: [(Symbol, [(Symbol, SchemumType)])]} {params :: [NullType]}
{from :: FromType}.
SortExpression grp lat with db params from -> ByteString
renderIndex forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SortExpression
'Ungrouped '[] '[] db '[] '[tab ::: TableToRow table]]
cols))
forall a. Semigroup a => a -> a -> a
<> ByteString
";"
where
renderIndex :: SortExpression grp lat with db params from -> ByteString
renderIndex = \case
Asc Expression grp lat with db params from ('NotNull ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression) ByteString -> ByteString -> ByteString
<+> ByteString
"ASC"
Desc Expression grp lat with db params from ('NotNull ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('NotNull ty)
expression) ByteString -> ByteString -> ByteString
<+> ByteString
"DESC"
AscNullsFirst Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS FIRST"
DescNullsFirst Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS FIRST"
AscNullsLast Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"ASC NULLS LAST"
DescNullsLast Expression grp lat with db params from ('Null ty)
expression -> ByteString -> ByteString
parenthesized (forall sql. RenderSQL sql => sql -> ByteString
renderSQL Expression grp lat with db params from ('Null ty)
expression)
ByteString -> ByteString -> ByteString
<+> ByteString
"DESC NULLS LAST"
newtype IndexMethod ty = UnsafeIndexMethod {forall {k} (ty :: k). IndexMethod ty -> ByteString
renderIndexMethod :: ByteString}
deriving stock (IndexMethod ty -> IndexMethod ty -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
/= :: IndexMethod ty -> IndexMethod ty -> Bool
$c/= :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
== :: IndexMethod ty -> IndexMethod ty -> Bool
$c== :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
Eq, IndexMethod ty -> IndexMethod ty -> Bool
IndexMethod ty -> IndexMethod ty -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall k (ty :: k). Eq (IndexMethod ty)
forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Ordering
forall k (ty :: k).
IndexMethod ty -> IndexMethod ty -> IndexMethod ty
min :: IndexMethod ty -> IndexMethod ty -> IndexMethod ty
$cmin :: forall k (ty :: k).
IndexMethod ty -> IndexMethod ty -> IndexMethod ty
max :: IndexMethod ty -> IndexMethod ty -> IndexMethod ty
$cmax :: forall k (ty :: k).
IndexMethod ty -> IndexMethod ty -> IndexMethod ty
>= :: IndexMethod ty -> IndexMethod ty -> Bool
$c>= :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
> :: IndexMethod ty -> IndexMethod ty -> Bool
$c> :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
<= :: IndexMethod ty -> IndexMethod ty -> Bool
$c<= :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
< :: IndexMethod ty -> IndexMethod ty -> Bool
$c< :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Bool
compare :: IndexMethod ty -> IndexMethod ty -> Ordering
$ccompare :: forall k (ty :: k). IndexMethod ty -> IndexMethod ty -> Ordering
Ord, Int -> IndexMethod ty -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (ty :: k). Int -> IndexMethod ty -> ShowS
forall k (ty :: k). [IndexMethod ty] -> ShowS
forall k (ty :: k). IndexMethod ty -> String
showList :: [IndexMethod ty] -> ShowS
$cshowList :: forall k (ty :: k). [IndexMethod ty] -> ShowS
show :: IndexMethod ty -> String
$cshow :: forall k (ty :: k). IndexMethod ty -> String
showsPrec :: Int -> IndexMethod ty -> ShowS
$cshowsPrec :: forall k (ty :: k). Int -> IndexMethod ty -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (ty :: k) x. Rep (IndexMethod ty) x -> IndexMethod ty
forall k (ty :: k) x. IndexMethod ty -> Rep (IndexMethod ty) x
$cto :: forall k (ty :: k) x. Rep (IndexMethod ty) x -> IndexMethod ty
$cfrom :: forall k (ty :: k) x. IndexMethod ty -> Rep (IndexMethod ty) x
GHC.Generic)
instance RenderSQL (IndexMethod ty) where renderSQL :: IndexMethod ty -> ByteString
renderSQL = forall {k} (ty :: k). IndexMethod ty -> ByteString
renderIndexMethod
btree :: IndexMethod 'Btree
btree :: IndexMethod 'Btree
btree = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"btree"
hash :: IndexMethod 'Hash
hash :: IndexMethod 'Hash
hash = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"hash"
gist :: IndexMethod 'Gist
gist :: IndexMethod 'Gist
gist = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"gist"
spgist :: IndexMethod 'Spgist
spgist :: IndexMethod 'Spgist
spgist = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"spgist"
gin :: IndexMethod 'Gin
gin :: IndexMethod 'Gin
gin = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"gin"
brin :: IndexMethod 'Brin
brin :: IndexMethod 'Brin
brin = forall {k} (ty :: k). ByteString -> IndexMethod ty
UnsafeIndexMethod ByteString
"brin"
dropIndex
:: (Has sch db schema, KnownSymbol ix)
=> QualifiedAlias sch ix
-> Definition db (Alter sch (DropSchemum ix 'Index schema) db)
dropIndex :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
(schema :: [(Symbol, SchemumType)]) (ix :: Symbol).
(Has sch db schema, KnownSymbol ix) =>
QualifiedAlias sch ix
-> Definition db (Alter sch (DropSchemum ix 'Index schema) db)
dropIndex QualifiedAlias sch ix
ix = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
(db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$ ByteString
"DROP INDEX" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch ix
ix forall a. Semigroup a => a -> a -> a
<> ByteString
";"
dropIndexIfExists
:: (Has sch db schema, KnownSymbol ix)
=> QualifiedAlias sch ix
-> Definition db (Alter sch (DropSchemumIfExists ix 'Index schema) db)
dropIndexIfExists :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
(schema :: [(Symbol, SchemumType)]) (ix :: Symbol).
(Has sch db schema, KnownSymbol ix) =>
QualifiedAlias sch ix
-> Definition
db (Alter sch (DropSchemumIfExists ix 'Index schema) db)
dropIndexIfExists QualifiedAlias sch ix
ix = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
(db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"DROP INDEX IF EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch ix
ix forall a. Semigroup a => a -> a -> a
<> ByteString
";"