@@ -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 , HasParameter (parameter)
40 , HasParameter (parameter)
39 -- ** Null
42 -- ** Null
@@ -151,6 +154,12 @@ module Squeal.PostgreSQL.Expression
151 , jsonbSet
154 , jsonbSet
152 , jsonbInsert
155 , jsonbInsert
153 , jsonbPretty
156 , jsonbPretty
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)
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
282+type ClosedExpression ty
283+ = forall outer grp commons schemas params from
284+ . Expression outer grp commons schemas params from ty
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
292+type ComparisonOperator ty1 ty2
293+ = Operator ty1 ty2 ('Null 'PGbool)
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"
1091-----------------------------------------}
1112-----------------------------------------}
1114+escape :: Char -> String
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) <> "\'"
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 "||"
1143+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1144+ (<>) = unsafeBinaryOp "||"
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 = (<>)
1151+ (Expression outer grp commons schemas params from (nullity 'PGtsvector)) where
1152+ mempty = fromString ""
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"
1611+(@@) :: ComparisonOperator (nullity 'PGtsvector) (nullity 'PGtsquery)
1612+(@@) = unsafeBinaryOp "@@"
1614+(.&) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1615+(.&) = unsafeBinaryOp "&&"
1617+(.|) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1618+(.|) = unsafeBinaryOp "||"
1621+ :: Expression outer grp commons schemas params from (nullity 'PGtsquery)
1622+ -> Expression outer grp commons schemas params from (nullity 'PGtsquery)
1623+(.!) = unsafeUnaryOp "!!"
1625+(<->) :: Operator (nullity 'PGtsquery) (nullity 'PGtsquery) (nullity 'PGtsquery)
1626+(<->) = unsafeBinaryOp "<->"
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
2190{-----------------------------------------
2247{-----------------------------------------