@@ -34,6 +34,9 @@ Squeal expressions are the atoms used to build statements.
3434module Squeal.PostgreSQL.Expression
3535 ( -- * Expression
3636 Expression (..)
37+ , ClosedExpression
38+ , Operator
39+ , ComparisonOperator
3740 , HasParameter (parameter)
3841 , param
3942 -- ** Null
@@ -151,6 +154,12 @@ module Squeal.PostgreSQL.Expression
151154 , jsonbSet
152155 , jsonbInsert
153156 , jsonbPretty
157+ -- ** Text Search
158+ , (@@)
159+ , (.&)
160+ , (.|)
161+ , (.!)
162+ , (<->)
154163 -- ** Aggregation
155164 , Aggregate (..)
156165 , Distinction (..)
@@ -267,10 +276,22 @@ newtype Expression
267276 (ty :: NullityType)
268277 = UnsafeExpression { renderExpression :: ByteString }
269278 deriving (GHC.Generic,Show,Eq,Ord,NFData)
270−
271279instance RenderSQL (Expression outer grp commons schemas params from ty) where
272280 renderSQL = renderExpression
273281
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+
274295{- | A `HasParameter` constraint is used to indicate a value that is
275296supplied externally to a SQL statement.
276297`Squeal.PostgreSQL.PQ.manipulateParams`,
@@ -1090,30 +1111,46 @@ localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
10901111text
10911112-----------------------------------------}
10921113
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+
10931126instance IsString
10941127 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
10951128 fromString str = UnsafeExpression $
10961129 "E\'" <> fromString (escape =<< str) <> "\'"
1097− where
1098− escape = \case
1099− '\NUL' -> "\\0"
1100− '\'' -> "''"
1101− '"' -> "\\\""
1102− '\b' -> "\\b"
1103− '\n' -> "\\n"
1104− '\r' -> "\\r"
1105− '\t' -> "\\t"
1106− '\\' -> "\\\\"
1107− c -> [c]
1130+instance IsString
1131+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1132+ fromString str = cast tsvector . UnsafeExpression $
1133+ "E\'" <> fromString (escape =<< str) <> "\'"
1134+instance IsString
1135+ (Expression outer grp commons schemas params from (nullity 'PGtsquery)) where
1136+ fromString str = cast tsquery . UnsafeExpression $
1137+ "E\'" <> fromString (escape =<< str) <> "\'"
11081138
11091139instance Semigroup
11101140 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
11111141 (<>) = unsafeBinaryOp "||"
1142+instance Semigroup
1143+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1144+ (<>) = unsafeBinaryOp "||"
11121145
11131146instance Monoid
11141147 (Expression outer grp commons schemas params from (nullity 'PGtext)) where
11151148 mempty = fromString ""
11161149 mappend = (<>)
1150+instance Monoid
1151+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1152+ mempty = fromString ""
1153+ mappend = (<>)
11171154
11181155-- | >>> printSQL $ lower "ARRRGGG"
11191156-- lower(E'ARRRGGG')
@@ -1571,6 +1608,23 @@ jsonbPretty
15711608 -> Expression outer grp commons schemas params from (nullity 'PGtext)
15721609jsonbPretty = unsafeFunction "jsonb_pretty"
15731610
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+
15741628{-----------------------------------------
15751629aggregation
15761630-----------------------------------------}
@@ -2165,6 +2219,7 @@ instance PGTyped schemas (nullity 'PGint8) where pgtype = int8
21652219instance PGTyped schemas (nullity 'PGnumeric) where pgtype = numeric
21662220instance PGTyped schemas (nullity 'PGfloat4) where pgtype = float4
21672221instance PGTyped schemas (nullity 'PGfloat8) where pgtype = float8
2222+instance PGTyped schemas (nullity 'PGmoney) where pgtype = money
21682223instance PGTyped schemas (nullity 'PGtext) where pgtype = text
21692224instance (KnownNat n, 1 <= n)
21702225 => PGTyped schemas (nullity ('PGchar n)) where pgtype = char @n
@@ -2186,6 +2241,8 @@ instance PGTyped schemas ty
21862241instance (SOP.All KnownNat dims, PGTyped schemas ty)
21872242 => PGTyped schemas (nullity ('PGfixarray dims ty)) where
21882243 pgtype = fixarray @dims (pgtype @schemas @ty)
2244+instance PGTyped schemas (nullity 'PGtsvector) where pgtype = tsvector
2245+instance PGTyped schemas (nullity 'PGtsquery) where pgtype = tsquery
21892246
21902247{-----------------------------------------
21912248Sorting