on dev help sign in
create or replace
ee96a86Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+13−2
@@ -41,6 +41,7 @@ module Squeal.PostgreSQL.Definition
41 , createTable
41 , createTable
42 , createTableIfNotExists
42 , createTableIfNotExists
43 , createView
43 , createView
44+ , createOrReplaceView
44 , createTypeEnum
45 , createTypeEnum
45 , createTypeEnumFrom
46 , createTypeEnumFrom
46 , createTypeComposite
47 , createTypeComposite
@@ -916,6 +917,16 @@ createView alias query = UnsafeDefinition $
916 "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
917 "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
917 <+> renderQuery query <> ";"
918 <+> renderQuery query <> ";"
918
919
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+
919-- | Drop a view.
930-- | Drop a view.
920--
931--
921-- >>> :{
932-- >>> :{
@@ -1163,7 +1174,7 @@ createOrReplaceFunction
1163 -> NP (TypeExpression schemas) args
1174 -> NP (TypeExpression schemas) args
1164 -> TypeExpression schemas ret
1175 -> TypeExpression schemas ret
1165 -> FunctionDefinition schemas args ('Returns ret)
1176 -> 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)
1167createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1178createOrReplaceFunction fun args ret fundef = UnsafeDefinition $
1168 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1179 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1169 <+> parenthesized (renderCommaSeparated renderSQL args)
1180 <+> parenthesized (renderCommaSeparated renderSQL args)
@@ -1211,7 +1222,7 @@ createOrReplaceSetFunction
1211 -> NP (TypeExpression schemas) args
1222 -> NP (TypeExpression schemas) args
1212 -> NP (Aliased (TypeExpression schemas)) rets
1223 -> NP (Aliased (TypeExpression schemas)) rets
1213 -> FunctionDefinition schemas args ('ReturnsTable rets)
1224 -> 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)
1215createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1226createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1216 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1227 "CREATE" <+> "OR" <+> "REPLACE" <+> "FUNCTION" <+> renderSQL fun
1217 <+> parenthesized (renderCommaSeparated renderSQL args)
1228 <+> parenthesized (renderCommaSeparated renderSQL args)
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−2
@@ -61,6 +61,7 @@ module Squeal.PostgreSQL.Schema
61 -- * Data Definitions
61 -- * Data Definitions
62 , Create
62 , Create
63 , CreateIfNotExists
63 , CreateIfNotExists
64+ , CreateOrReplace
64 , Drop
65 , Drop
65 , Alter
66 , Alter
66 , Rename
67 , Rename
@@ -328,8 +329,20 @@ type family Create alias x xs where
328
329
329type family CreateIfNotExists alias x xs where
330type family CreateIfNotExists alias x xs where
330 CreateIfNotExists alias x '[] = '[alias ::: x]
331 CreateIfNotExists alias x '[] = '[alias ::: x]
331− CreateIfNotExists alias x (alias ::: y ': xs) = (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)
332− CreateIfNotExists alias y (x ': xs) = x ': Create alias y xs
345+ CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
333
346
334-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
347-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
335-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
348-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements