on dev help sign in
create and drop operators
commit3bd8b61c20d9a3a008837f1cf6ec9b574bf776ba authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 2 Sep 2019, 15:20 (7 years ago) parent5dd61ab
1 file changed+67−2
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+67−2
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+67−2
@@ -53,6 +53,9 @@ module Squeal.PostgreSQL.Definition
5353 , createOrReplaceFunction
5454 , createSetFunction
5555 , createOrReplaceSetFunction
56+ , createBinaryOp
57+ , createUnaryOpL
58+ , createUnaryOpR
5659 , TableConstraintExpression (..)
5760 , check
5861 , unique
@@ -71,6 +74,7 @@ module Squeal.PostgreSQL.Definition
7174 , dropType
7275 , dropIndex
7376 , dropFunction
77+ , dropOperator
7478 -- ** Alter
7579 , alterTable
7680 , alterTableRename
@@ -1215,13 +1219,74 @@ createOrReplaceSetFunction fun args rets fundef = UnsafeDefinition $
12151219 renderRet :: Aliased (TypeExpression s) r -> ByteString
12161220 renderRet (ty `As` col) = renderSQL col <+> renderSQL ty
12171221
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+
12181275dropFunction
12191276 :: (Has sch schemas schema, Has fun schema ('Function args ret))
12201277 => QualifiedAlias sch fun
12211278 -- ^ name of the user defined function
1222− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1279+ -> Definition schemas (Alter sch (Drop fun schema) schemas)
12231280dropFunction 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 <> ";"
12251290
12261291newtype FunctionDefinition schemas args ret = UnsafeFunctionDefinition
12271292 { renderFunctionDefinition :: ByteString }