on db148ff help sign in
rel8/src/Rel8/Expr/Text.hs
276 lines 7.2 kB history raw
1{-# language DataKinds #-}
3module Rel8.Expr.Text
4 (
5 -- * String concatenation
6 (++.)
8 -- * Regular expression operators
9 , (~.), (~*), (!~), (!~*)
11 -- * Standard SQL functions
12 , bitLength, charLength, lower, octetLength, upper
14 -- * PostgreSQL functions
15 , ascii, btrim, chr, convert, convertFrom, convertTo, decode, encode
16 , initcap, left, length, lengthEncoding, lpad, ltrim, md5
17 , pgClientEncoding, quoteIdent, quoteLiteral, quoteNullable, regexpReplace
18 , regexpSplitToArray, repeat, replace, reverse, right, rpad, rtrim
19 , splitPart, strpos, substr, translate
20 )
21where
23-- base
24import Data.Bool ( Bool )
25import Data.Int ( Int32 )
26import Data.Maybe ( Maybe( Nothing, Just ) )
27import Prelude ()
29-- bytestring
30import Data.ByteString ( ByteString )
32-- rel8
33import Rel8.Expr ( Expr )
34import Rel8.Expr.Function ( binaryOperator, function, nullaryFunction )
35import Rel8.Type.Array1D ( Array1D )
37-- text
38import Data.Text (Text)
41-- | The PostgreSQL string concatenation operator.
42(++.) :: Expr Text -> Expr Text -> Expr Text
43(++.) = binaryOperator "||"
44infixr 6 ++.
47-- * Regular expression operators
49-- See https://www.postgresql.org/docs/9.5/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP
52-- | Matches regular expression, case sensitive
53--
54-- Corresponds to the @~.@ operator.
55(~.) :: Expr Text -> Expr Text -> Expr Bool
56(~.) = binaryOperator "~."
57infix 2 ~.
60-- | Matches regular expression, case insensitive
61--
62-- Corresponds to the @~*@ operator.
63(~*) :: Expr Text -> Expr Text -> Expr Bool
64(~*) = binaryOperator "~*"
65infix 2 ~*
68-- | Does not match regular expression, case sensitive
69--
70-- Corresponds to the @!~@ operator.
71(!~) :: Expr Text -> Expr Text -> Expr Bool
72(!~) = binaryOperator "!~"
73infix 2 !~
76-- | Does not match regular expression, case insensitive
77--
78-- Corresponds to the @!~*@ operator.
79(!~*) :: Expr Text -> Expr Text -> Expr Bool
80(!~*) = binaryOperator "!~*"
81infix 2 !~*
84-- See https://www.postgresql.org/docs/9.5/static/functions-Expr.'PGHtml
86-- * Standard SQL functions
89-- | Corresponds to the @bit_length@ function.
90bitLength :: Expr Text -> Expr Int32
91bitLength = function "bit_length"
94-- | Corresponds to the @char_length@ function.
95charLength :: Expr Text -> Expr Int32
96charLength = function "char_length"
99-- | Corresponds to the @lower@ function.
100lower :: Expr Text -> Expr Text
101lower = function "lower"
104-- | Corresponds to the @octet_length@ function.
105octetLength :: Expr Text -> Expr Int32
106octetLength = function "octet_length"
109-- | Corresponds to the @upper@ function.
110upper :: Expr Text -> Expr Text
111upper = function "upper"
114-- | Corresponds to the @ascii@ function.
115ascii :: Expr Text -> Expr Int32
116ascii = function "ascii"
119-- | Corresponds to the @btrim@ function.
120btrim :: Expr Text -> Maybe (Expr Text) -> Expr Text
121btrim a (Just b) = function "btrim" a b
122btrim a Nothing = function "btrim" a
125-- | Corresponds to the @chr@ function.
126chr :: Expr Int32 -> Expr Text
127chr = function "chr"
130-- | Corresponds to the @convert@ function.
131convert :: Expr ByteString -> Expr Text -> Expr Text -> Expr ByteString
132convert = function "convert"
135-- | Corresponds to the @convert_from@ function.
136convertFrom :: Expr ByteString -> Expr Text -> Expr Text
137convertFrom = function "convert_from"
140-- | Corresponds to the @convert_to@ function.
141convertTo :: Expr Text -> Expr Text -> Expr ByteString
142convertTo = function "convert_to"
145-- | Corresponds to the @decode@ function.
146decode :: Expr Text -> Expr Text -> Expr ByteString
147decode = function "decode"
150-- | Corresponds to the @encode@ function.
151encode :: Expr ByteString -> Expr Text -> Expr Text
152encode = function "encode"
155-- | Corresponds to the @initcap@ function.
156initcap :: Expr Text -> Expr Text
157initcap = function "initcap"
160-- | Corresponds to the @left@ function.
161left :: Expr Text -> Expr Int32 -> Expr Text
162left = function "left"
165-- | Corresponds to the @length@ function.
166length :: Expr Text -> Expr Int32
167length = function "length"
170-- | Corresponds to the @length@ function.
171lengthEncoding :: Expr ByteString -> Expr Text -> Expr Int32
172lengthEncoding = function "length"
175-- | Corresponds to the @lpad@ function.
176lpad :: Expr Text -> Expr Int32 -> Maybe (Expr Text) -> Expr Text
177lpad a b (Just c) = function "lpad" a b c
178lpad a b Nothing = function "lpad" a b
181-- | Corresponds to the @ltrim@ function.
182ltrim :: Expr Text -> Maybe (Expr Text) -> Expr Text
183ltrim a (Just b) = function "ltrim" a b
184ltrim a Nothing = function "ltrim" a
187-- | Corresponds to the @md5@ function.
188md5 :: Expr Text -> Expr Text
189md5 = function "md5"
192-- | Corresponds to the @pg_client_encoding()@ expression.
193pgClientEncoding :: Expr Text
194pgClientEncoding = nullaryFunction "pg_client_encoding"
197-- | Corresponds to the @quote_ident@ function.
198quoteIdent :: Expr Text -> Expr Text
199quoteIdent = function "quote_ident"
202-- | Corresponds to the @quote_literal@ function.
203quoteLiteral :: Expr Text -> Expr Text
204quoteLiteral = function "quote_literal"
207-- | Corresponds to the @quote_nullable@ function.
208quoteNullable :: Expr Text -> Expr Text
209quoteNullable = function "quote_nullable"
212-- | Corresponds to the @regexp_replace@ function.
213regexpReplace :: ()
214 => Expr Text -> Expr Text -> Expr Text -> Maybe (Expr Text) -> Expr Text
215regexpReplace a b c (Just d) = function "regexp_replace" a b c d
216regexpReplace a b c Nothing = function "regexp_replace" a b c
219-- | Corresponds to the @regexp_split_to_array@ function.
220regexpSplitToArray :: ()
221 => Expr Text -> Expr Text -> Maybe (Expr Text) -> Expr (Array1D Text)
222regexpSplitToArray a b (Just c) = function "regexp_split_to_array" a b c
223regexpSplitToArray a b Nothing = function "regexp_split_to_array" a b
226-- | Corresponds to the @repeat@ function.
227repeat :: Expr Text -> Expr Int32 -> Expr Text
228repeat = function "repeat"
231-- | Corresponds to the @replace@ function.
232replace :: Expr Text -> Expr Text -> Expr Text -> Expr Text
233replace = function "replace"
236-- | Corresponds to the @reverse@ function.
237reverse :: Expr Text -> Expr Text
238reverse = function "reverse"
241-- | Corresponds to the @right@ function.
242right :: Expr Text -> Expr Int32 -> Expr Text
243right = function "right"
246-- | Corresponds to the @rpad@ function.
247rpad :: Expr Text -> Expr Int32 -> Maybe (Expr Text) -> Expr Text
248rpad a b (Just c) = function "rpad" a b c
249rpad a b Nothing = function "rpad" a b
252-- | Corresponds to the @rtrim@ function.
253rtrim :: Expr Text -> Maybe (Expr Text) -> Expr Text
254rtrim a (Just b) = function "rtrim" a b
255rtrim a Nothing = function "rtrim" a
258-- | Corresponds to the @split_part@ function.
259splitPart :: Expr Text -> Expr Text -> Expr Int32 -> Expr Text
260splitPart = function "split_part"
263-- | Corresponds to the @strpos@ function.
264strpos :: Expr Text -> Expr Text -> Expr Int32
265strpos = function "strpos"
268-- | Corresponds to the @substr@ function.
269substr :: Expr Text -> Expr Int32 -> Maybe (Expr Int32) -> Expr Text
270substr a b (Just c) = function "substr" a b c
271substr a b Nothing = function "substr" a b
274-- | Corresponds to the @translate@ function.
275translate :: Expr Text -> Expr Text -> Expr Text -> Expr Text
276translate = function "translate"