on dev help sign in
more operators and functions
22fb78fEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+14−2
@@ -49,6 +49,7 @@ module Squeal.PostgreSQL.Definition
49 , createTypeRange
49 , createTypeRange
50 , createIndex
50 , createIndex
51 , createFunction
51 , createFunction
52+ , createOrReplaceFunction
52 , TableConstraintExpression (..)
53 , TableConstraintExpression (..)
53 , check
54 , check
54 , unique
55 , unique
@@ -1120,13 +1121,24 @@ createFunction
1120 -> TypeExpression schemas arg
1121 -> TypeExpression schemas arg
1121 -> TypeExpression schemas ret
1122 -> TypeExpression schemas ret
1122 -> FunctionDefinition schemas arg ret
1123 -> 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)
1124createFunction fun arg ret fundef = UnsafeDefinition $
1125createFunction fun arg ret fundef = UnsafeDefinition $
1125 "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
1126 "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
1126 <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1127 <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1127
1128
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+
1128dropFunction
1140dropFunction
1129− :: (Has sch schemas schema, Has fun schema ('Function arg ret))
1141+ :: (Has sch schemas schema, Has fun schema ('Function funty))
1130 => QualifiedAlias sch fun
1142 => QualifiedAlias sch fun
1131 -- ^ name of the user defined function
1143 -- ^ name of the user defined function
1132 -> Definition schemas (Alter sch (Drop ix schema) schemas)
1144 -> 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
38 , unsafeFunction
38 , unsafeFunction
39 , function
39 , function
40 , unsafeUnaryOpL
40 , unsafeUnaryOpL
41+ , unaryOpL
41 , unsafeUnaryOpR
42 , unsafeUnaryOpR
43+ , unaryOpR
42 , Operator
44 , Operator
43 , unsafeBinaryOp
45 , unsafeBinaryOp
46+ , binaryOp
44 , FunctionVar
47 , FunctionVar
45 , unsafeFunctionVar
48 , unsafeFunctionVar
46 , FunctionN
49 , FunctionN
47 , unsafeFunctionN
50 , unsafeFunctionN
51+ , functionN
48 , PGSubset (..)
52 , PGSubset (..)
49 , PGIntersect (..)
53 , PGIntersect (..)
50 -- * Re-export
54 -- * Re-export
@@ -288,16 +292,31 @@ unsafeBinaryOp :: ByteString -> Operator ty0 ty1 ty2
288unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
292unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
289 renderSQL x <+> op <+> renderSQL y
293 renderSQL x <+> op <+> renderSQL y
290
294
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+
291-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
300-- | >>> printSQL $ unsafeUnaryOpL "NOT" true
292-- (NOT TRUE)
301-- (NOT TRUE)
293unsafeUnaryOpL :: ByteString -> x :--> y
302unsafeUnaryOpL :: ByteString -> x :--> y
294unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
303unsafeUnaryOpL op x = UnsafeExpression $ parenthesized $ op <+> renderSQL x
295
304
305+unaryOpL
306+ :: (Has sch schemas schema, Has op schema ('Operator ('UnaryOpL x y)))
307+ => Alias op -> x :--> y
308+unaryOpL = unsafeUnaryOpL . renderSQL
309+
296-- | >>> printSQL $ true & unsafeUnaryOpR "IS NOT TRUE"
310-- | >>> printSQL $ true & unsafeUnaryOpR "IS NOT TRUE"
297-- (TRUE IS NOT TRUE)
311-- (TRUE IS NOT TRUE)
298unsafeUnaryOpR :: ByteString -> x :--> y
312unsafeUnaryOpR :: ByteString -> x :--> y
299unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
313unsafeUnaryOpR op x = UnsafeExpression $ parenthesized $ renderSQL x <+> op
300
314
315+unaryOpR
316+ :: (Has sch schemas schema, Has op schema ('Operator ('UnaryOpR x y)))
317+ => Alias op -> x :--> y
318+unaryOpR = unsafeUnaryOpR . renderSQL
319+
301-- | >>> printSQL $ unsafeFunction "f" true
320-- | >>> printSQL $ unsafeFunction "f" true
302-- f(TRUE)
321-- f(TRUE)
303unsafeFunction :: ByteString -> x :--> y
322unsafeFunction :: ByteString -> x :--> y
@@ -305,7 +324,7 @@ unsafeFunction fun x = UnsafeExpression $
305 fun <> parenthesized (renderSQL x)
324 fun <> parenthesized (renderSQL x)
306
325
307function
326function
308− :: (Has sch schemas schema, Has fun schema ('Function x y))
327+ :: (Has sch schemas schema, Has fun schema ('Function ('UnaryFun x y)))
309 => QualifiedAlias sch fun
328 => QualifiedAlias sch fun
310 -> x :--> y
329 -> x :--> y
311function = unsafeFunction . renderSQL
330function = unsafeFunction . renderSQL
@@ -316,6 +335,12 @@ unsafeFunctionN :: SListI xs => ByteString -> FunctionN xs y
316unsafeFunctionN fun xs = UnsafeExpression $
335unsafeFunctionN fun xs = UnsafeExpression $
317 fun <> parenthesized (renderCommaSeparated renderSQL xs)
336 fun <> parenthesized (renderCommaSeparated renderSQL xs)
318
337
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+
319instance ty `In` PGNum
344instance ty `In` PGNum
320 => Num (Expression outer commons grp schemas params from (null ty)) where
345 => Num (Expression outer commons grp schemas params from (null ty)) where
321 (+) = unsafeBinaryOp "+"
346 (+) = unsafeBinaryOp "+"
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+15−1
@@ -44,6 +44,8 @@ module Squeal.PostgreSQL.Schema
44 , ColumnsType
44 , ColumnsType
45 , TableType
45 , TableType
46 , SchemumType (..)
46 , SchemumType (..)
47+ , OperatorType (..)
48+ , FunctionType (..)
47 , SchemaType
49 , SchemaType
48 , SchemasType
50 , SchemasType
49 , Public
51 , Public
@@ -367,9 +369,21 @@ data SchemumType
367 | View RowType
369 | View RowType
368 | Typedef PGType
370 | Typedef PGType
369 | Index
371 | Index
370− | Function NullityType NullityType
372+ | Function FunctionType
373+ | Operator OperatorType
371 | UnsafeSchemum Symbol
374 | UnsafeSchemum Symbol
372
375
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+
373{- | The schema of a database consists of a list of aliased,
387{- | The schema of a database consists of a list of aliased,
374user-defined `SchemumType`s.
388user-defined `SchemumType`s.
375
389