{-# LANGUAGE
AllowAmbiguousTypes
, ConstraintKinds
, DeriveAnyClass
, DeriveGeneric
, DerivingStrategies
, FlexibleContexts
, FlexibleInstances
, GADTs
, LambdaCase
, MultiParamTypeClasses
, OverloadedLabels
, OverloadedStrings
, RankNTypes
, ScopedTypeVariables
, TypeApplications
, DataKinds
, PolyKinds
, TypeOperators
, UndecidableSuperClasses
#-}
module Squeal.PostgreSQL.Definition.View
(
createView
, createOrReplaceView
, dropView
, dropViewIfExists
, alterViewRename
, alterViewSetSchema
) where
import GHC.TypeLits
import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Definition
import Squeal.PostgreSQL.Query
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema
createView
:: (Has sch db schema, KnownSymbol vw)
=> QualifiedAlias sch vw
-> Query '[] '[] db '[] view
-> Definition db (Alter sch (Create vw ('View view) schema) db)
createView :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (vw :: Symbol) (view :: RowType).
(Has sch db schema, KnownSymbol vw) =>
QualifiedAlias sch vw
-> Query '[] '[] db '[] view
-> Definition db (Alter sch (Create vw ('View view) schema) db)
createView QualifiedAlias sch vw
alias Query '[] '[] db '[] view
query = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE" ByteString -> ByteString -> ByteString
<+> ByteString
"VIEW" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch vw
alias ByteString -> ByteString -> ByteString
<+> ByteString
"AS"
ByteString -> ByteString -> ByteString
<+> forall (lat :: FromType) (with :: FromType)
(db :: [(Symbol, SchemaType)]) (params :: [NullType])
(row :: RowType).
Query lat with db params row -> ByteString
renderQuery Query '[] '[] db '[] view
query forall a. Semigroup a => a -> a -> a
<> ByteString
";"
createOrReplaceView
:: (Has sch db schema, KnownSymbol vw)
=> QualifiedAlias sch vw
-> Query '[] '[] db '[] view
-> Definition db (Alter sch (CreateOrReplace vw ('View view) schema) db)
createOrReplaceView :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (vw :: Symbol) (view :: RowType).
(Has sch db schema, KnownSymbol vw) =>
QualifiedAlias sch vw
-> Query '[] '[] db '[] view
-> Definition
db (Alter sch (CreateOrReplace vw ('View view) schema) db)
createOrReplaceView QualifiedAlias sch vw
alias Query '[] '[] db '[] view
query = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"CREATE OR REPLACE VIEW" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch vw
alias ByteString -> ByteString -> ByteString
<+> ByteString
"AS"
ByteString -> ByteString -> ByteString
<+> forall (lat :: FromType) (with :: FromType)
(db :: [(Symbol, SchemaType)]) (params :: [NullType])
(row :: RowType).
Query lat with db params row -> ByteString
renderQuery Query '[] '[] db '[] view
query forall a. Semigroup a => a -> a -> a
<> ByteString
";"
dropView
:: (Has sch db schema, KnownSymbol vw)
=> QualifiedAlias sch vw
-> Definition db (Alter sch (DropSchemum vw 'View schema) db)
dropView :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (vw :: Symbol).
(Has sch db schema, KnownSymbol vw) =>
QualifiedAlias sch vw
-> Definition db (Alter sch (DropSchemum vw 'View schema) db)
dropView QualifiedAlias sch vw
vw = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$ ByteString
"DROP VIEW" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch vw
vw forall a. Semigroup a => a -> a -> a
<> ByteString
";"
dropViewIfExists
:: (Has sch db schema, KnownSymbol vw)
=> QualifiedAlias sch vw
-> Definition db (Alter sch (DropIfExists vw schema) db)
dropViewIfExists :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (vw :: Symbol).
(Has sch db schema, KnownSymbol vw) =>
QualifiedAlias sch vw
-> Definition db (Alter sch (DropIfExists vw schema) db)
dropViewIfExists QualifiedAlias sch vw
vw = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"DROP VIEW IF EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch vw
vw forall a. Semigroup a => a -> a -> a
<> ByteString
";"
alterViewRename
:: ( Has sch db schema
, KnownSymbol ty1
, Has ty0 schema ('View vw))
=> QualifiedAlias sch ty0
-> Alias ty1
-> Definition db (Alter sch (Rename ty0 ty1 schema) db )
alterViewRename :: forall (sch :: Symbol) (db :: [(Symbol, SchemaType)])
(schema :: SchemaType) (ty1 :: Symbol) (ty0 :: Symbol)
(vw :: RowType).
(Has sch db schema, KnownSymbol ty1, Has ty0 schema ('View vw)) =>
QualifiedAlias sch ty0
-> Alias ty1
-> Definition db (Alter sch (Rename ty0 ty1 schema) db)
alterViewRename QualifiedAlias sch ty0
vw0 Alias ty1
vw1 = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"ALTER VIEW" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch ty0
vw0
ByteString -> ByteString -> ByteString
<+> ByteString
"RENAME TO" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias ty1
vw1 forall a. Semigroup a => a -> a -> a
<> ByteString
";"
alterViewSetSchema
:: ( Has sch0 db schema0
, Has vw schema0 ('View view)
, Has sch1 db schema1 )
=> QualifiedAlias sch0 vw
-> Alias sch1
-> Definition db (SetSchema sch0 sch1 schema0 schema1 vw 'View view db)
alterViewSetSchema :: forall (sch0 :: Symbol) (db :: [(Symbol, SchemaType)])
(schema0 :: SchemaType) (vw :: Symbol) (view :: RowType)
(sch1 :: Symbol) (schema1 :: SchemaType).
(Has sch0 db schema0, Has vw schema0 ('View view),
Has sch1 db schema1) =>
QualifiedAlias sch0 vw
-> Alias sch1
-> Definition
db (SetSchema sch0 sch1 schema0 schema1 vw 'View view db)
alterViewSetSchema QualifiedAlias sch0 vw
ty Alias sch1
sch = forall (db0 :: [(Symbol, SchemaType)])
(db1 :: [(Symbol, SchemaType)]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
ByteString
"ALTER VIEW" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch0 vw
ty ByteString -> ByteString -> ByteString
<+> ByteString
"SET SCHEMA" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias sch1
sch forall a. Semigroup a => a -> a -> a
<> ByteString
";"