on dev help sign in
SQL function of 1 argument type
commit8df5d8d2a950f6514ab115e8a7b650e453c1f730 authorEitan Chatav <eitan@Eitans-MacBook-Pro.local> dateFri 15 Mar 2019, 16:45 (8 years ago) parent9d2c665
1 file changed+44−94
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+44−94
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+44−94
@@ -287,11 +287,16 @@ newtype Expression
287287instance RenderSQL (Expression outer grp commons schemas params from ty) where
288288 renderSQL = renderExpression
289289
290−type Operator ty1 ty2 ty3
290+type Operator x1 x2 y
291291 = forall outer grp commons schemas params from
292− . Expression outer grp commons schemas params from ty1
293− -> Expression outer grp commons schemas params from ty2
294− -> Expression outer grp commons schemas params from ty3
292+ . Expression outer grp commons schemas params from x1
293+ -> Expression outer grp commons schemas params from x2
294+ -> Expression outer grp commons schemas params from y
295+
296+type (:-->) x y
297+ = forall outer grp commons schemas params from
298+ . Expression outer grp commons schemas params from x
299+ -> Expression outer grp commons schemas params from y
295300
296301{- | A `HasParameter` constraint is used to indicate a value that is
297302supplied externally to a SQL statement.
@@ -529,17 +534,15 @@ array xs = UnsafeExpression $
529534
530535-- | >>> printSQL $ cardinality (array [null_, false, true])
531536-- cardinality(ARRAY[NULL, FALSE, TRUE])
532−cardinality
533− :: Expression outer grp commons schemas params from (nullity ('PGvararray ty))
534− -> Expression outer grp commons schemas params from (nullity 'PGint8)
537+cardinality :: nullity ('PGvararray ty) :--> nullity 'PGint8
535538cardinality = unsafeFunction "cardinality"
536539
537540-- | >>> printSQL $ array [null_, false, true] & index 2
538541-- (ARRAY[NULL, FALSE, TRUE])[2]
539542index
540− :: Word64 -- ^ index
541− -> Expression outer grp commons schemas params from (nullity ('PGvararray ty)) -- ^ array
542− -> Expression outer grp commons schemas params from (NullifyType ty)
543+ :: Word64 -- ^ index
544+ -> nullity ('PGvararray ty) -- ^ array
545+ :--> NullifyType ty
543546index n expr = UnsafeExpression $
544547 parenthesized (renderSQL expr) <> "[" <> fromString (show n) <> "]"
545548
@@ -632,21 +635,13 @@ unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
632635
633636-- | >>> printSQL $ unsafeUnaryOp "NOT" true
634637-- (NOT TRUE)
635−unsafeUnaryOp
636− :: ByteString
637− -- ^ operator
638− -> Expression outer grp commons schemas params from (ty0)
639− -> Expression outer grp commons schemas params from (ty1)
638+unsafeUnaryOp :: ByteString {- ^ operator -} -> x :--> y
640639unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
641640 op <+> renderSQL x
642641
643642-- | >>> printSQL $ unsafeFunction "f" true
644643-- f(TRUE)
645−unsafeFunction
646− :: ByteString
647− -- ^ function
648− -> Expression outer grp commons schemas params from xty
649− -> Expression outer grp commons schemas params from yty
644+unsafeFunction :: ByteString {- ^ function -} -> x :--> y
650645unsafeFunction fun x = UnsafeExpression $
651646 fun <> parenthesized (renderSQL x)
652647
@@ -780,11 +775,7 @@ rem_ = unsafeBinaryOp "%"
780775-- in printSQL expression
781776-- :}
782777-- trunc(pi())
783−trunc
784− :: frac `In` PGFloating
785− => Expression outer grp commons schemas params from (nullity frac)
786− -- ^ fractional number
787− -> Expression outer grp commons schemas params from (nullity frac)
778+trunc :: frac `In` PGFloating => nullity frac :--> nullity frac
788779trunc = unsafeFunction "trunc"
789780
790781-- | >>> :{
@@ -794,11 +785,7 @@ trunc = unsafeFunction "trunc"
794785-- in printSQL expression
795786-- :}
796787-- round(pi())
797−round_
798− :: frac `In` PGFloating
799− => Expression outer grp commons schemas params from (nullity frac)
800− -- ^ fractional number
801− -> Expression outer grp commons schemas params from (nullity frac)
788+round_ :: frac `In` PGFloating => nullity frac :--> nullity frac
802789round_ = unsafeFunction "round"
803790
804791-- | >>> :{
@@ -808,11 +795,7 @@ round_ = unsafeFunction "round"
808795-- in printSQL expression
809796-- :}
810797-- ceiling(pi())
811−ceiling_
812− :: frac `In` PGFloating
813− => Expression outer grp commons schemas params from (nullity frac)
814− -- ^ fractional number
815− -> Expression outer grp commons schemas params from (nullity frac)
798+ceiling_ :: frac `In` PGFloating => nullity frac :--> nullity frac
816799ceiling_ = unsafeFunction "ceiling"
817800
818801-- | A `Condition` is an `Expression`, which can evaluate
@@ -833,9 +816,7 @@ false = UnsafeExpression "FALSE"
833816
834817-- | >>> printSQL $ not_ true
835818-- (NOT TRUE)
836−not_
837− :: Expression outer grp commons schemas params from (nullity 'PGbool)
838− -> Expression outer grp commons schemas params from (nullity 'PGbool)
819+not_ :: nullity 'PGbool :--> nullity 'PGbool
839820not_ = unsafeUnaryOp "NOT"
840821
841822-- | >>> printSQL $ true .&& false
@@ -1125,25 +1106,25 @@ instance Monoid
11251106-- | >>> printSQL $ lower "ARRRGGG"
11261107-- lower(E'ARRRGGG')
11271108lower
1128− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1109+ :: nullity 'PGtext
11291110 -- ^ string to lower case
1130− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1111+ :--> nullity 'PGtext
11311112lower = unsafeFunction "lower"
11321113
11331114-- | >>> printSQL $ upper "eeee"
11341115-- upper(E'eeee')
11351116upper
1136− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1117+ :: nullity 'PGtext
11371118 -- ^ string to upper case
1138− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1119+ :--> nullity 'PGtext
11391120upper = unsafeFunction "upper"
11401121
11411122-- | >>> printSQL $ charLength "four"
11421123-- char_length(E'four')
11431124charLength
1144− :: Expression outer grp commons schemas params from (nullity 'PGtext)
1125+ :: nullity 'PGtext
11451126 -- ^ string to measure
1146− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1127+ :--> nullity 'PGint4
11471128charLength = unsafeFunction "char_length"
11481129
11491130-- | The `like` expression returns true if the @string@ matches
@@ -1294,9 +1275,7 @@ jsonLit = cast json . UnsafeExpression
12941275-- otherwise, a scalar value is produced. For any scalar type other than a
12951276-- number, a Boolean, or a null value, the text representation will be used, in
12961277-- such a fashion that it is a valid json value.
1297−toJson
1298− :: Expression outer grp commons schemas params from (nullity ty)
1299− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1278+toJson :: nullity ty :--> nullity 'PGjson
13001279toJson = unsafeFunction "to_json"
13011280
13021281-- | Returns the value as jsonb. Arrays and composites are converted
@@ -1305,23 +1284,18 @@ toJson = unsafeFunction "to_json"
13051284-- otherwise, a scalar value is produced. For any scalar type other than a
13061285-- number, a Boolean, or a null value, the text representation will be used, in
13071286-- such a fashion that it is a valid jsonb value.
1308−toJsonb
1309− :: Expression outer grp commons schemas params from (nullity ty)
1310− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1287+toJsonb :: nullity ty :--> nullity 'PGjsonb
13111288toJsonb = unsafeFunction "to_jsonb"
13121289
13131290-- | Returns the array as a JSON array. A PostgreSQL multidimensional array
13141291-- becomes a JSON array of arrays.
13151292arrayToJson
13161293 :: PGArray "arrayToJson" arr
1317− => Expression outer grp commons schemas params from (nullity arr)
1318− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1294+ => nullity arr :--> nullity 'PGjson
13191295arrayToJson = unsafeFunction "array_to_json"
13201296
13211297-- | Returns the row as a JSON object.
1322−rowToJson
1323− :: Expression outer grp commons schemas params from (nullity ('PGcomposite ty))
1324− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1298+rowToJson :: nullity ('PGcomposite ty) :--> nullity 'PGjson
13251299rowToJson = unsafeFunction "row_to_json"
13261300
13271301-- | Builds a possibly-heterogeneously-typed JSON array out of a variadic
@@ -1384,8 +1358,7 @@ jsonbBuildObject
13841358-- array has exactly two elements, which are taken as a key/value pair.
13851359jsonObject
13861360 :: PGArrayOf "jsonObject" arr ('NotNull 'PGtext)
1387− => Expression outer grp commons schemas params from (nullity arr)
1388− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1361+ => nullity arr :--> nullity 'PGjson
13891362jsonObject = unsafeFunction "json_object"
13901363
13911364-- | Builds a binary JSON object out of a text array. The array must have either
@@ -1394,8 +1367,7 @@ jsonObject = unsafeFunction "json_object"
13941367-- array has exactly two elements, which are taken as a key/value pair.
13951368jsonbObject
13961369 :: PGArrayOf "jsonbObject" arr ('NotNull 'PGtext)
1397− => Expression outer grp commons schemas params from (nullity arr)
1398− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1370+ => nullity arr :--> nullity 'PGjsonb
13991371jsonbObject = unsafeFunction "jsonb_object"
14001372
14011373-- | This is an alternate form of 'jsonObject' that takes two arrays; one for
@@ -1426,15 +1398,11 @@ Table 9.46: JSON processing functions
14261398-----------------------------------------}
14271399
14281400-- | Returns the number of elements in the outermost JSON array.
1429−jsonArrayLength
1430− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1431− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1401+jsonArrayLength :: nullity 'PGjson :--> nullity 'PGint4
14321402jsonArrayLength = unsafeFunction "json_array_length"
14331403
14341404-- | Returns the number of elements in the outermost binary JSON array.
1435−jsonbArrayLength
1436− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1437− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1405+jsonbArrayLength :: nullity 'PGjsonb :--> nullity 'PGint4
14381406jsonbArrayLength = unsafeFunction "jsonb_array_length"
14391407
14401408-- | Returns JSON value pointed to by the given path (equivalent to #>
@@ -1479,30 +1447,22 @@ jsonbExtractPathAsText x xs =
14791447
14801448-- | Returns the type of the outermost JSON value as a text string. Possible
14811449-- types are object, array, string, number, boolean, and null.
1482−jsonTypeof
1483− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1484− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1450+jsonTypeof :: nullity 'PGjson :--> nullity 'PGtext
14851451jsonTypeof = unsafeFunction "json_typeof"
14861452
14871453-- | Returns the type of the outermost binary JSON value as a text string.
14881454-- Possible types are object, array, string, number, boolean, and null.
1489−jsonbTypeof
1490− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1491− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1455+jsonbTypeof :: nullity 'PGjsonb :--> nullity 'PGtext
14921456jsonbTypeof = unsafeFunction "jsonb_typeof"
14931457
14941458-- | Returns its argument with all object fields that have null values omitted.
14951459-- Other null values are untouched.
1496−jsonStripNulls
1497− :: Expression outer grp commons schemas params from (nullity 'PGjson)
1498− -> Expression outer grp commons schemas params from (nullity 'PGjson)
1460+jsonStripNulls :: nullity 'PGjson :--> nullity 'PGjson
14991461jsonStripNulls = unsafeFunction "json_strip_nulls"
15001462
15011463-- | Returns its argument with all object fields that have null values omitted.
15021464-- Other null values are untouched.
1503−jsonbStripNulls
1504− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1505− -> Expression outer grp commons schemas params from (nullity 'PGjsonb)
1465+jsonbStripNulls :: nullity 'PGjsonb :--> nullity 'PGjsonb
15061466jsonbStripNulls = unsafeFunction "jsonb_strip_nulls"
15071467
15081468-- | @ jsonbSet target path new_value create_missing @
@@ -1543,9 +1503,7 @@ jsonbInsert tgt path val insertAfter = case insertAfter of
15431503 Nothing -> unsafeVariadicFunction "jsonb_insert" (tgt :* path :* val :* Nil)
15441504
15451505-- | Returns its argument as indented JSON text.
1546−jsonbPretty
1547− :: Expression outer grp commons schemas params from (nullity 'PGjsonb)
1548− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1506+jsonbPretty :: nullity 'PGjsonb :--> nullity 'PGtext
15491507jsonbPretty = unsafeFunction "jsonb_pretty"
15501508
15511509(@@) :: Operator (nullity 'PGtsvector) (nullity 'PGtsquery) ('Null 'PGbool)
@@ -1557,27 +1515,21 @@ jsonbPretty = unsafeFunction "jsonb_pretty"
15571515(.|) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
15581516(.|) = unsafeBinaryOp "||"
15591517
1560−(.!)
1561− :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1562− -> Expression outer grp commons schemas params from (nullity 'PGtsquery)
1518+(.!) :: nullity 'PGtsquery :--> nullity 'PGtsquery
15631519(.!) = unsafeUnaryOp "!!"
15641520
15651521(<->) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
15661522(<->) = unsafeBinaryOp "<->"
15671523
15681524arrayToTSvector
1569− :: Expression outer grp commons schemas params from (nullity ('PGvararray ('NotNull 'PGtext)))
1570− -> Expression outer grp commons schemas params from (nullity 'PGtsvector)
1525+ :: nullity ('PGvararray ('NotNull 'PGtext))
1526+ :--> nullity 'PGtsvector
15711527arrayToTSvector = unsafeFunction "array_to_tsvector"
15721528
1573−tsvectorLength
1574− :: Expression outer grp commons schemas params from (nullity 'PGtsvector)
1575− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1529+tsvectorLength :: nullity 'PGtsvector :--> nullity 'PGint4
15761530tsvectorLength = unsafeFunction "length"
15771531
1578−numnode
1579− :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1580− -> Expression outer grp commons schemas params from (nullity 'PGint4)
1532+numnode :: nullity 'PGtsquery :--> nullity 'PGint4
15811533numnode = unsafeFunction "numnode"
15821534
15831535plainToTSquery
@@ -1598,9 +1550,7 @@ websearchToTSquery
15981550 -> Expression outer grp commons schemas params from (nullity 'PGtsquery)
15991551websearchToTSquery = unsafeFunction2 "websearch_to_tsquery"
16001552
1601−queryTree
1602− :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1603− -> Expression outer grp commons schemas params from (nullity 'PGtext)
1553+queryTree :: nullity 'PGtsquery :--> nullity 'PGtext
16041554queryTree = unsafeFunction "query_tree"
16051555
16061556toTSquery