on dev help sign in
create or replace
commitee96a869231aa7a4ae7eb423550c88d4ad0e3f0e authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 23 Sep 2019, 19:53 (7 years ago) parentefdac7e
2 files changed+28−4
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+13−2 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−2
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+13−2
@@ -41,6 +41,7 @@ module Squeal.PostgreSQL.Definition
4141 , createTable
4242 , createTableIfNotExists
4343 , createView
44+ , createOrReplaceView
4445 , createTypeEnum
4546 , createTypeEnumFrom
4647 , createTypeComposite
@@ -916,6 +917,16 @@ createView alias query = UnsafeDefinition $
916917 "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
917918 <+> renderQuery query <> ";"
918919
920+
921+createOrReplaceView
922+ :: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema)
923+ => QualifiedAlias sch vw -- ^ the name of the view to add
924+ -> Query '[] '[] schemas '[] view -- ^ query
925+ -> Definition schemas (Alter sch (CreateOrReplace vw ('View view) schema) schemas)
926+createOrReplaceView alias query = UnsafeDefinition $
927+ "CREATE OR REPLACE VIEW" <+> renderSQL alias <+> "AS"
928+ <+> renderQuery query <> ";"
929+
919930-- | Drop a view.
920931--
921932-- >>> :{
@@ -1163,7 +1174,7 @@ createOrReplaceFunction
11631174 -> NP (TypeExpression schemas) args
11641175 -> TypeExpression schemas ret
11651176 -> FunctionDefinition schemas args ('Returns ret)
1166− -> Definition schemas (Alter sch (Alter fun ('Function args ('Returns ret)) schema) schemas)
1177+ -> Definition schemas (Alter sch (CreateOrReplace fun ('Function args ('Returns ret)) schema) schemas)
11671178createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
11681179 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
11691180 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1211,7 +1222,7 @@ createOrReplaceSetFunction
12111222 -> NP (TypeExpression schemas) args
12121223 -> NP (Aliased (TypeExpression schemas)) rets
12131224 -> FunctionDefinition schemas args ('ReturnsTable rets)
1214− -> Definition schemas (Alter sch (Alter fun ('Function args ('ReturnsTable rets)) schema) schemas)
1225+ -> Definition schemas (Alter sch (CreateOrReplace fun ('Function args ('ReturnsTable rets)) schema) schemas)
12151226createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
12161227 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
12171228 <+> parenthesized (renderCommaSeparated renderSQL args)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−2
@@ -61,6 +61,7 @@ module Squeal.PostgreSQL.Schema
6161 -- * Data Definitions
6262 , Create
6363 , CreateIfNotExists
64+ , CreateOrReplace
6465 , Drop
6566 , Alter
6667 , Rename
@@ -328,8 +329,20 @@ type family Create alias x xs where
328329
329330type family CreateIfNotExists alias x xs where
330331 CreateIfNotExists alias x '[] = '[alias ::: x]
331− CreateIfNotExists alias x (alias ::: y ': xs) = (alias ::: y ': xs)
332− CreateIfNotExists alias y (x ': xs) = x ': Create alias y xs
332+ CreateIfNotExists alias x (alias ::: y ': xs) = alias ::: y ': xs
333+ CreateIfNotExists alias y (x ': xs) = x ': CreateIfNotExists alias y xs
334+
335+type family CreateOrReplace alias x xs where
336+ CreateOrReplace alias x '[] = '[alias ::: x]
337+ CreateOrReplace alias x (alias ::: x ': xs) = alias ::: x ': xs
338+ CreateOrReplace alias x (alias ::: y ': xs) = TypeError
339+ ('Text "CreateOrReplace: alias "
340+ ':<>: 'ShowType alias
341+ ':<>: 'Text " of type "
342+ ':<>: 'ShowType x
343+ ':<>: 'Text " cannot be replaced by unequal type "
344+ ':<>: 'ShowType y)
345+ CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
333346
334347-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
335348-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements