on dev help sign in
more operators and functions

and create or replace functions

commit22fb78f7536a961e63ddacc4808e87410cad3db6 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 2 Sep 2019, 09:21 (7 years ago) parent68393ec
3 files changed+55−4
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+14−2 squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+26−1 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−1
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+14−2
@@ -49,6 +49,7 @@ module Squeal.PostgreSQL.Definition
4949 , createTypeRange
5050 , createIndex
5151 , createFunction
52+ , createOrReplaceFunction
5253 , TableConstraintExpression (..)
5354 , check
5455 , unique
@@ -1120,13 +1121,24 @@ createFunction
11201121 -> TypeExpression schemas arg
11211122 -> TypeExpression schemas ret
11221123 -> FunctionDefinition schemas arg ret
1123− -> Definition schemas (Alter sch (Create fun ('Function arg ret) schema) schemas)
1124+ -> Definition schemas (Alter sch (Create fun ('Function ('UnaryFun arg ret)) schema) schemas)
11241125createFunction fun arg ret fundef = UnsafeDefinition $
11251126 "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
11261127 <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
11271128
1129+createOrReplaceFunction
1130+ :: (Has sch schemas schema, Has fun schema ('Function ('UnaryFun arg ret)))
1131+ => QualifiedAlias sch fun
1132+ -> TypeExpression schemas arg
1133+ -> TypeExpression schemas ret
1134+ -> FunctionDefinition schemas arg ret
1135+ -> Definition schemas (Alter sch (Alter fun ('Function ('UnaryFun arg ret)) schema) schemas)
1136+createOrReplaceFunction fun arg ret fundef = UnsafeDefinition $
1137+ "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
1138+ <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1139+
11281140dropFunction
1129− :: (Has sch schemas schema, Has fun schema ('Function arg ret))
1141+ :: (Has sch schemas schema, Has fun schema ('Function funty))
11301142 => QualifiedAlias sch fun
11311143 -- ^ name of the user defined function
11321144 -> Definition schemas (Alter sch (Drop ix schema) schemas)
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+26−1
@@ -38,13 +38,17 @@ module Squeal.PostgreSQL.Expression
3838 , unsafeFunction
3939 , function
4040 , unsafeUnaryOpL
41+ , unaryOpL
4142 , unsafeUnaryOpR
43+ , unaryOpR
4244 , Operator
4345 , unsafeBinaryOp
46+ , binaryOp
4447 , FunctionVar
4548 , unsafeFunctionVar
4649 , FunctionN
4750 , unsafeFunctionN
51+ , functionN
4852 , PGSubset (..)
4953 , PGIntersect (..)
5054 -- * Re-export
@@ -288,16 +292,31 @@ unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
288292unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
289293 renderSQL x <+> op <+> renderSQL y
290294
295+binaryOp
296+ :: (Has sch schemas schema, Has op schema ('Operator ('BinaryOp x1 x2 y)))
297+ => Alias op -> Operator x1 x2 y
298+binaryOp = unsafeBinaryOp . renderSQL
299+
291300-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
292301-- (NOT TRUE)
293302unsafeUnaryOpL :: ByteString -> x :--> y
294303unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
295304
305+unaryOpL
306+ :: (Has sch schemas schema, Has op schema ('Operator ('UnaryOpL x y)))
307+ => Alias op -> x :--> y
308+unaryOpL = unsafeUnaryOpL . renderSQL
309+
296310-- | >>> printSQL $ true & unsafeUnaryOpR "IS NOT TRUE"
297311-- (TRUE IS NOT TRUE)
298312unsafeUnaryOpR :: ByteString -> x :--> y
299313unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
300314
315+unaryOpR
316+ :: (Has sch schemas schema, Has op schema ('Operator ('UnaryOpR x y)))
317+ => Alias op -> x :--> y
318+unaryOpR = unsafeUnaryOpR . renderSQL
319+
301320-- | >>> printSQL $ unsafeFunction "f" true
302321-- f(TRUE)
303322unsafeFunction :: ByteString -> x :--> y
@@ -305,7 +324,7 @@ unsafeFunction fun x = UnsafeExpression $
305324 fun <> parenthesized (renderSQL x)
306325
307326function
308− :: (Has sch schemas schema, Has fun schema ('Function x y))
327+ :: (Has sch schemas schema, Has fun schema ('Function ('UnaryFun x y)))
309328 => QualifiedAlias sch fun
310329 -> x :--> y
311330function = unsafeFunction . renderSQL
@@ -316,6 +335,12 @@ unsafeFunctionN :: SListI xs => ByteString -> FunctionN xs y
316335unsafeFunctionN fun xs = UnsafeExpression $
317336 fun <> parenthesized (renderCommaSeparated renderSQL xs)
318337
338+functionN
339+ :: (Has sch schemas schema, Has fun schema ('Function ('NaryFun xs y)), SListI xs)
340+ => QualifiedAlias sch fun
341+ -> FunctionN xs y
342+functionN = unsafeFunctionN . renderSQL
343+
319344instance ty `In` PGNum
320345 => Num (Expression outer commons grp schemas params from (null ty)) where
321346 (+) = unsafeBinaryOp "+"
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−1
@@ -44,6 +44,8 @@ module Squeal.PostgreSQL.Schema
4444 , ColumnsType
4545 , TableType
4646 , SchemumType (..)
47+ , OperatorType (..)
48+ , FunctionType (..)
4749 , SchemaType
4850 , SchemasType
4951 , Public
@@ -367,9 +369,21 @@ data SchemumType
367369 | View RowType
368370 | Typedef PGType
369371 | Index
370− | Function NullityType NullityType
372+ | Function FunctionType
373+ | Operator OperatorType
371374 | UnsafeSchemum Symbol
372375
376+data FunctionType
377+ = UnaryFun NullityType NullityType
378+ | NaryFun [NullityType] NullityType
379+ | UnarySetFun NullityType [NullityType]
380+ | NarySetFun [NullityType] [NullityType]
381+
382+data OperatorType
383+ = BinaryOp NullityType NullityType NullityType
384+ | UnaryOpL NullityType NullityType
385+ | UnaryOpR NullityType NullityType
386+
373387{- | The schema of a database consists of a list of aliased,
374388user-defined `SchemumType`s.
375389