on dev help sign in
create and drop operators
3bd8b61Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+67−2
@@ -53,6 +53,9 @@ module Squeal.PostgreSQL.Definition
53 , createOrReplaceFunction
53 , createOrReplaceFunction
54 , createSetFunction
54 , createSetFunction
55 , createOrReplaceSetFunction
55 , createOrReplaceSetFunction
56+ , createBinaryOp
57+ , createUnaryOpL
58+ , createUnaryOpR
56 , TableConstraintExpression (..)
59 , TableConstraintExpression (..)
57 , check
60 , check
58 , unique
61 , unique
@@ -71,6 +74,7 @@ module Squeal.PostgreSQL.Definition
71 , dropType
74 , dropType
72 , dropIndex
75 , dropIndex
73 , dropFunction
76 , dropFunction
77+ , dropOperator
74 -- ** Alter
78 -- ** Alter
75 , alterTable
79 , alterTable
76 , alterTableRename
80 , alterTableRename
@@ -1215,13 +1219,74 @@ createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
1215 renderRet :: Aliased (TypeExpression s) r -> ByteString
1219 renderRet :: Aliased (TypeExpression s) r -> ByteString
1216 renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1220 renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
1217
1221
1222+createBinaryOp
1223+ :: ( Has sch schemas schema
1224+ , Has fun schema ('Function '[x,y] ('Returns z))
1225+ , KnownSymbol op )
1226+ => Alias op
1227+ -> Alias fun
1228+ -> TypeExpression schemas x
1229+ -> TypeExpression schemas y
1230+ -> Definition schemas
1231+ (Alter sch (Create op ('Operator ('BinaryOp x y z)) schema) schemas)
1232+createBinaryOp op fun x y = UnsafeDefinition $
1233+ "CREATE" <+> "OPERATOR" <+> renderSQL op
1234+ <+> parenthesized (commaSeparated opdef)
1235+ where
1236+ opdef =
1237+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1238+ , "LEFTARG" <+> "=" <+> renderSQL x
1239+ , "RIGHTARG" <+> "=" <+> renderSQL y ]
1240+
1241+createUnaryOpL
1242+ :: ( Has sch schemas schema
1243+ , Has fun schema ('Function '[x] ('Returns y))
1244+ , KnownSymbol op )
1245+ => Alias op
1246+ -> Alias fun
1247+ -> TypeExpression schemas x
1248+ -> Definition schemas
1249+ (Alter sch (Create op ('Operator ('UnaryOpL x y)) schema) schemas)
1250+createUnaryOpL op fun x = UnsafeDefinition $
1251+ "CREATE" <+> "OPERATOR" <+> renderSQL op
1252+ <+> parenthesized (commaSeparated opdef)
1253+ where
1254+ opdef =
1255+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1256+ , "RIGHTARG" <+> "=" <+> renderSQL x ]
1257+
1258+createUnaryOpR
1259+ :: ( Has sch schemas schema
1260+ , Has fun schema ('Function '[x] ('Returns y))
1261+ , KnownSymbol op )
1262+ => Alias op
1263+ -> Alias fun
1264+ -> TypeExpression schemas x
1265+ -> Definition schemas
1266+ (Alter sch (Create op ('Operator ('UnaryOpR x y)) schema) schemas)
1267+createUnaryOpR op fun x = UnsafeDefinition $
1268+ "CREATE" <+> "OPERATOR" <+> renderSQL op
1269+ <+> parenthesized (commaSeparated opdef)
1270+ where
1271+ opdef =
1272+ [ "FUNCTION" <+> "=" <+> renderSQL fun
1273+ , "LEFTARG" <+> "=" <+> renderSQL x ]
1274+
1218dropFunction
1275dropFunction
1219 :: (Has sch schemas schema, Has fun schema ('Function args ret))
1276 :: (Has sch schemas schema, Has fun schema ('Function args ret))
1220 => QualifiedAlias sch fun
1277 => QualifiedAlias sch fun
1221 -- ^ name of the user defined function
1278 -- ^ name of the user defined function
1222− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1279+ -> Definition schemas (Alter sch (Drop fun schema) schemas)
1223dropFunction fun = UnsafeDefinition $
1280dropFunction fun = UnsafeDefinition $
1224− "DROP" <+> "Function" <+> renderSQL fun <> ";"
1281+ "DROP" <+> "FUNCTION" <+> renderSQL fun <> ";"
1282+
1283+dropOperator
1284+ :: (Has sch schemas schema, Has op schema ('Operator oper))
1285+ => QualifiedAlias sch op
1286+ -- ^ name of the user defined operator
1287+ -> Definition schemas (Alter sch (Drop op schema) schemas)
1288+dropOperator op = UnsafeDefinition $
1289+ "DROP" <+> "OPERATOR" <+> renderSQL op <> ";"
1225
1290
1226newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
1291newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
1227 { renderFunctionDefinition :: ByteString }
1292 { renderFunctionDefinition :: ByteString }