drop functions and operators

some fixes
This commit is contained in:
Eitan Chatav 2019-09-24 09:06:07 -07:00
parent 50bf2addf1
commit 10c4aa6a3f
2 changed files with 30 additions and 13 deletions

View File

@ -46,9 +46,9 @@ module Squeal.PostgreSQL.Definition
, createTypeEnumFrom
, createTypeComposite
, createTypeCompositeFrom
, FieldTyped (..)
, createDomain
, createTypeRange
, createDomain
, FieldTyped (..)
, createIndex
, createIndexIfNotExists
, IndexMethod (..)
@ -83,7 +83,9 @@ module Squeal.PostgreSQL.Definition
, dropIndex
, dropIndexIfExists
, dropFunction
, dropFunctionIfExists
, dropOperator
, dropOperatorIfExists
-- ** Alter
, alterTable
, alterTableRename
@ -931,7 +933,7 @@ in printSQL definition
CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
-}
createView
:: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema)
:: (Has sch schemas schema, KnownSymbol vw)
=> QualifiedAlias sch vw -- ^ the name of the view to add
-> Query '[] '[] schemas '[] view -- ^ query
-> Definition schemas (Alter sch (Create vw ('View view) schema) schemas)
@ -939,9 +941,8 @@ createView alias query = UnsafeDefinition $
"CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
<+> renderQuery query <> ";"
createOrReplaceView
:: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema)
:: (Has sch schemas schema, KnownSymbol vw)
=> QualifiedAlias sch vw -- ^ the name of the view to add
-> Query '[] '[] schemas '[] view -- ^ query
-> Definition schemas (Alter sch (CreateOrReplace vw ('View view) schema) schemas)
@ -1338,21 +1339,37 @@ createRightOp fun x = UnsafeDefinition $
, "LEFTARG" <+> "=" <+> renderSQL x ]
dropFunction
:: (Has sch schemas schema, Has fun schema ('Function (args '::--> ret)))
:: (Has sch schemas schema, KnownSymbol fun)
=> QualifiedAlias sch fun
-- ^ name of the user defined function
-> Definition schemas (Alter sch (Drop fun schema) schemas)
-> Definition schemas (Alter sch (DropSchemum fun 'Function schema) schemas)
dropFunction fun = UnsafeDefinition $
"DROP" <+> "FUNCTION" <+> renderSQL fun <> ";"
dropFunctionIfExists
:: (Has sch schemas schema, KnownSymbol fun)
=> QualifiedAlias sch fun
-- ^ name of the user defined function
-> Definition schemas (Alter sch (DropSchemumIfExists fun 'Function schema) schemas)
dropFunctionIfExists fun = UnsafeDefinition $
"DROP FUNCTION IF EXISTS" <+> renderSQL fun <> ";"
dropOperator
:: (Has sch schemas schema, Has op schema ('Operator oper))
:: (Has sch schemas schema, KnownSymbol op)
=> QualifiedAlias sch op
-- ^ name of the user defined operator
-> Definition schemas (Alter sch (Drop op schema) schemas)
-> Definition schemas (Alter sch (DropSchemum op 'Operator schema) schemas)
dropOperator op = UnsafeDefinition $
"DROP" <+> "OPERATOR" <+> renderSQL op <> ";"
dropOperatorIfExists
:: (Has sch schemas schema, KnownSymbol op)
=> QualifiedAlias sch op
-- ^ name of the user defined operator
-> Definition schemas (Alter sch (DropSchemumIfExists op 'Operator schema) schemas)
dropOperatorIfExists op = UnsafeDefinition $
"DROP OPERATOR IF EXISTS" <+> renderSQL op <> ";"
newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
{ renderFunctionDefinition :: ByteString }
deriving (Eq,Show,GHC.Generic,NFData)

View File

@ -342,11 +342,11 @@ type family CreateOrReplace alias x xs where
CreateOrReplace alias x (alias ::: x ': xs) = alias ::: x ': xs
CreateOrReplace alias x (alias ::: y ': xs) = TypeError
('Text "CreateOrReplace: expected type "
':<>: 'ShowType y
':<>: 'ShowType x
':<>: 'Text " but alias "
':<>: 'ShowType alias
':<>: 'Text " has type "
':<>: 'ShowType x)
':<>: 'ShowType y)
CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
@ -368,11 +368,11 @@ type family DropSchemum alias sch xs where
DropSchemum alias sch (alias ::: sch x ': xs) = xs
DropSchemum alias sch0 (alias ::: sch1 x ': xs) = TypeError
('Text "DropSchemum: expected schemum "
':<>: 'ShowType sch1
':<>: 'ShowType sch0
':<>: 'Text " but alias "
':<>: 'ShowType alias
':<>: 'Text " has schemum "
':<>: 'ShowType sch0)
':<>: 'ShowType sch1)
DropSchemum alias sch (x ': xs) = x ': DropSchemum alias sch xs
type family DropIfExists alias xs where