on dev help sign in
operators
1cb61ccEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+69−12
@@ -34,6 +34,9 @@ Squeal expressions are the atoms used to build statements.
34module Squeal.PostgreSQL.Expression
34module Squeal.PostgreSQL.Expression
35 ( -- * Expression
35 ( -- * Expression
36 Expression (..)
36 Expression (..)
37+ , ClosedExpression
38+ , Operator
39+ , ComparisonOperator
37 , HasParameter (parameter)
40 , HasParameter (parameter)
38 , param
41 , param
39 -- ** Null
42 -- ** Null
@@ -151,6 +154,12 @@ module Squeal.PostgreSQL.Expression
151 , jsonbSet
154 , jsonbSet
152 , jsonbInsert
155 , jsonbInsert
153 , jsonbPretty
156 , jsonbPretty
157+ -- ** Text Search
158+ , (@@)
159+ , (.&)
160+ , (.|)
161+ , (.!)
162+ , (<->)
154 -- ** Aggregation
163 -- ** Aggregation
155 , Aggregate (..)
164 , Aggregate (..)
156 , Distinction (..)
165 , Distinction (..)
@@ -267,10 +276,22 @@ newtype Expression
267 (ty :: NullityType)
276 (ty :: NullityType)
268 = UnsafeExpression { renderExpression :: ByteString }
277 = UnsafeExpression { renderExpression :: ByteString }
269 deriving (GHC.Generic,Show,Eq,Ord,NFData)
278 deriving (GHC.Generic,Show,Eq,Ord,NFData)
270−
271instance RenderSQL (Expression outer grp commons schemas params from ty) where
279instance RenderSQL (Expression outer grp commons schemas params from ty) where
272 renderSQL = renderExpression
280 renderSQL = renderExpression
273
281
282+type ClosedExpression ty
283+ = forall outer grp commons schemas params from
284+ . Expression outer grp commons schemas params from ty
285+
286+type Operator ty1 ty2 ty3
287+ = forall outer grp commons schemas params from
288+ . Expression outer grp commons schemas params from ty1
289+ -> Expression outer grp commons schemas params from ty2
290+ -> Expression outer grp commons schemas params from ty3
291+
292+type ComparisonOperator ty1 ty2
293+ = Operator ty1 ty2 ('Null 'PGbool)
294+
274{- | A `HasParameter` constraint is used to indicate a value that is
295{- | A `HasParameter` constraint is used to indicate a value that is
275supplied externally to a SQL statement.
296supplied externally to a SQL statement.
276`Squeal.PostgreSQL.PQ.manipulateParams`,
297`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -1090,30 +1111,46 @@ localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
1090text
1111text
1091-----------------------------------------}
1112-----------------------------------------}
1092
1113
1114+escape :: Char -> String
1115+escape = \case
1116+ '\NUL' -> "\\0"
1117+ '\'' -> "''"
1118+ '"' -> "\\\""
1119+ '\b' -> "\\b"
1120+ '\n' -> "\\n"
1121+ '\r' -> "\\r"
1122+ '\t' -> "\\t"
1123+ '\\' -> "\\\\"
1124+ c -> [c]
1125+
1093instance IsString
1126instance IsString
1094 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1127 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1095 fromString str = UnsafeExpression $
1128 fromString str = UnsafeExpression $
1096 "E\'" <> fromString (escape =<< str) <> "\'"
1129 "E\'" <> fromString (escape =<< str) <> "\'"
1097− where
1130+instance IsString
1098− escape = \case
1131+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1099− '\NUL' -> "\\0"
1132+ fromString str = cast tsvector . UnsafeExpression $
1100− '\'' -> "''"
1133+ "E\'" <> fromString (escape =<< str) <> "\'"
1101− '"' -> "\\\""
1134+instance IsString
1102− '\b' -> "\\b"
1135+ (Expression outer grp commons schemas params from (nullity 'PGtsquery)) where
1103− '\n' -> "\\n"
1136+ fromString str = cast tsquery . UnsafeExpression $
1104− '\r' -> "\\r"
1137+ "E\'" <> fromString (escape =<< str) <> "\'"
1105− '\t' -> "\\t"
1106− '\\' -> "\\\\"
1107− c -> [c]
1108
1138
1109instance Semigroup
1139instance Semigroup
1110 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1140 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1111 (<>) = unsafeBinaryOp "||"
1141 (<>) = unsafeBinaryOp "||"
1142+instance Semigroup
1143+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1144+ (<>) = unsafeBinaryOp "||"
1112
1145
1113instance Monoid
1146instance Monoid
1114 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1147 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
1115 mempty = fromString ""
1148 mempty = fromString ""
1116 mappend = (<>)
1149 mappend = (<>)
1150+instance Monoid
1151+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1152+ mempty = fromString ""
1153+ mappend = (<>)
1117
1154
1118-- | >>> printSQL $ lower "ARRRGGG"
1155-- | >>> printSQL $ lower "ARRRGGG"
1119-- lower(E'ARRRGGG')
1156-- lower(E'ARRRGGG')
@@ -1571,6 +1608,23 @@ jsonbPretty
1571 -> Expression outer grp commons schemas params from (nullity 'PGtext)
1608 -> Expression outer grp commons schemas params from (nullity 'PGtext)
1572jsonbPretty = unsafeFunction "jsonb_pretty"
1609jsonbPretty = unsafeFunction "jsonb_pretty"
1573
1610
1611+(@@) :: ComparisonOperator (nullity 'PGtsvector) (nullity 'PGtsquery)
1612+(@@) = unsafeBinaryOp "@@"
1613+
1614+(.&) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1615+(.&) = unsafeBinaryOp "&&"
1616+
1617+(.|) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1618+(.|) = unsafeBinaryOp "||"
1619+
1620+(.!)
1621+ :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1622+ -> Expression outer grp commons schemas params from (nullity 'PGtsquery)
1623+(.!) = unsafeUnaryOp "!!"
1624+
1625+(<->) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1626+(<->) = unsafeBinaryOp "<->"
1627+
1574{-----------------------------------------
1628{-----------------------------------------
1575aggregation
1629aggregation
1576-----------------------------------------}
1630-----------------------------------------}
@@ -2165,6 +2219,7 @@ instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
2165instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
2219instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
2166instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
2220instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
2167instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2221instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2222+instance PGTyped schemas (nullity 'PGmoney) where pgtype = money
2168instance PGTyped schemas (nullity 'PGtext) where pgtype = text
2223instance PGTyped schemas (nullity 'PGtext) where pgtype = text
2169instance (KnownNat n, 1 <= n)
2224instance (KnownNat n, 1 <= n)
2170 => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
2225 => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
@@ -2186,6 +2241,8 @@ instance PGTyped schemas ty
2186instance (SOP.All KnownNat dims, PGTyped schemas ty)
2241instance (SOP.All KnownNat dims, PGTyped schemas ty)
2187 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2242 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
2188 pgtype = fixarray @dims (pgtype @schemas @ty)
2243 pgtype = fixarray @dims (pgtype @schemas @ty)
2244+instance PGTyped schemas (nullity 'PGtsvector) where pgtype = tsvector
2245+instance PGTyped schemas (nullity 'PGtsquery) where pgtype = tsquery
2189
2246
2190{-----------------------------------------
2247{-----------------------------------------
2191Sorting
2248Sorting
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs−6
@@ -1101,12 +1101,6 @@ exists
1101 -> Condition outer grp commons schemas params from
1101 -> Condition outer grp commons schemas params from
1102exists query = UnsafeExpression $ "EXISTS" <+> parenthesized (renderSQL query)
1102exists query = UnsafeExpression $ "EXISTS" <+> parenthesized (renderSQL query)
1103
1103
1104−type Operator ty1 ty2 ty3
1105− = forall outer grp commons schemas params from
1106− . Expression outer grp commons schemas params from ty1
1107− -> Expression outer grp commons schemas params from ty2
1108− -> Expression outer grp commons schemas params from ty3
1109−
1110{- |
1104{- |
1111The right-hand side is a parenthesized subquery, which must return
1105The right-hand side is a parenthesized subquery, which must return
1112exactly one column. The left-hand expression is evaluated and compared to each
1106exactly one column. The left-hand expression is evaluated and compared to each