Skip to main content

sql_builder/extension/postgres/func/
search.rs

1use super::*;
2
3impl PgFunc {
4    /// Call `TO_TSQUERY` function. Postgres only.
5    ///
6    /// The parameter `regconfig` represents the OID of the text search configuration.
7    /// If the value is `None` the argument is omitted from the query, and hence the database default used.
8    ///
9    /// # Examples
10    ///
11    /// ```
12    /// use sql_builder::*;
13    ///
14    /// let query = Query::select()
15    ///     .expr(PgFunc::to_tsquery("a & b", None))
16    ///     .to_owned();
17    ///
18    /// assert_eq!(
19    ///     query.to_string(PostgresQueryBuilder).unwrap(),
20    ///     r#"SELECT TO_TSQUERY('a & b')"#
21    /// );
22    /// ```
23    pub fn to_tsquery<T>(expr: T, regconfig: Option<u32>) -> FunctionCall
24    where
25        T: Into<Expr>,
26    {
27        text_search(PgFunc::ToTsquery, expr.into(), regconfig)
28    }
29
30    /// Call `TO_TSVECTOR` function. Postgres only.
31    ///
32    /// The parameter `regconfig` represents the OID of the text search configuration.
33    /// If the value is `None` the argument is omitted from the query, and hence the database default used.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use sql_builder::*;
39    ///
40    /// let query = Query::select()
41    ///     .expr(PgFunc::to_tsvector("a b", None))
42    ///     .to_owned();
43    ///
44    /// assert_eq!(
45    ///     query.to_string(PostgresQueryBuilder).unwrap(),
46    ///     r#"SELECT TO_TSVECTOR('a b')"#
47    /// );
48    /// ```
49    pub fn to_tsvector<T>(expr: T, regconfig: Option<u32>) -> FunctionCall
50    where
51        T: Into<Expr>,
52    {
53        text_search(PgFunc::ToTsvector, expr.into(), regconfig)
54    }
55
56    /// Call `PHRASE_TO_TSQUERY` function. Postgres only.
57    ///
58    /// The parameter `regconfig` represents the OID of the text search configuration.
59    /// If the value is `None` the argument is omitted from the query, and hence the database default used.
60    ///
61    /// # Examples
62    ///
63    /// ```
64    /// use sql_builder::*;
65    ///
66    /// let query = Query::select()
67    ///     .expr(PgFunc::phraseto_tsquery("a b", None))
68    ///     .to_owned();
69    ///
70    /// assert_eq!(
71    ///     query.to_string(PostgresQueryBuilder).unwrap(),
72    ///     r#"SELECT PHRASETO_TSQUERY('a b')"#
73    /// );
74    /// ```
75    pub fn phraseto_tsquery<T>(expr: T, regconfig: Option<u32>) -> FunctionCall
76    where
77        T: Into<Expr>,
78    {
79        text_search(PgFunc::PhrasetoTsquery, expr.into(), regconfig)
80    }
81
82    /// Call `PLAIN_TO_TSQUERY` function. Postgres only.
83    ///
84    /// The parameter `regconfig` represents the OID of the text search configuration.
85    /// If the value is `None` the argument is omitted from the query, and hence the database default used.
86    ///
87    /// # Examples
88    ///
89    /// ```
90    /// use sql_builder::*;
91    ///
92    /// let query = Query::select()
93    ///     .expr(PgFunc::plainto_tsquery("a b", None))
94    ///     .to_owned();
95    ///
96    /// assert_eq!(
97    ///     query.to_string(PostgresQueryBuilder).unwrap(),
98    ///     r#"SELECT PLAINTO_TSQUERY('a b')"#
99    /// );
100    /// ```
101    pub fn plainto_tsquery<T>(expr: T, regconfig: Option<u32>) -> FunctionCall
102    where
103        T: Into<Expr>,
104    {
105        text_search(PgFunc::PlaintoTsquery, expr.into(), regconfig)
106    }
107
108    /// Call `WEBSEARCH_TO_TSQUERY` function. Postgres only.
109    ///
110    /// The parameter `regconfig` represents the OID of the text search configuration.
111    /// If the value is `None` the argument is omitted from the query, and hence the database default used.
112    ///
113    /// # Examples
114    ///
115    /// ```
116    /// use sql_builder::*;
117    ///
118    /// let query = Query::select()
119    ///     .expr(PgFunc::websearch_to_tsquery("a b", None))
120    ///     .to_owned();
121    ///
122    /// assert_eq!(
123    ///     query.to_string(PostgresQueryBuilder).unwrap(),
124    ///     r#"SELECT WEBSEARCH_TO_TSQUERY('a b')"#
125    /// );
126    /// ```
127    pub fn websearch_to_tsquery<T>(expr: T, regconfig: Option<u32>) -> FunctionCall
128    where
129        T: Into<Expr>,
130    {
131        text_search(PgFunc::WebsearchToTsquery, expr.into(), regconfig)
132    }
133
134    /// Call `TS_RANK` function. Postgres only.
135    ///
136    /// # Examples
137    ///
138    /// ```
139    /// use sql_builder::*;
140    ///
141    /// let query = Query::select()
142    ///     .expr(PgFunc::ts_rank("a b", "a&b"))
143    ///     .to_owned();
144    ///
145    /// assert_eq!(
146    ///     query.to_string(PostgresQueryBuilder).unwrap(),
147    ///     r#"SELECT TS_RANK('a b', 'a&b')"#
148    /// );
149    /// ```
150    pub fn ts_rank<T>(vector: T, query: T) -> FunctionCall
151    where
152        T: Into<Expr>,
153    {
154        FunctionCall::new(PgFunc::TsRank).args([vector.into(), query.into()])
155    }
156
157    /// Call `TS_RANK_CD` function. Postgres only.
158    ///
159    /// # Examples
160    ///
161    /// ```
162    /// use sql_builder::*;
163    ///
164    /// let query = Query::select()
165    ///     .expr(PgFunc::ts_rank_cd("a b", "a&b"))
166    ///     .to_owned();
167    ///
168    /// assert_eq!(
169    ///     query.to_string(PostgresQueryBuilder).unwrap(),
170    ///     r#"SELECT TS_RANK_CD('a b', 'a&b')"#
171    /// );
172    /// ```
173    pub fn ts_rank_cd<T>(vector: T, query: T) -> FunctionCall
174    where
175        T: Into<Expr>,
176    {
177        FunctionCall::new(PgFunc::TsRankCd).args([vector.into(), query.into()])
178    }
179
180    /// Call `ANY` function. Postgres only.
181    ///
182    /// # Examples
183    ///
184    /// ```
185    /// use sql_builder::*;
186    ///
187    /// let query = Query::select().expr(PgFunc::any(vec![0, 1])).to_owned();
188    ///
189    /// assert_eq!(
190    ///     query.to_string(PostgresQueryBuilder).unwrap(),
191    ///     r#"SELECT ANY(ARRAY [0,1])"#
192    /// );
193    /// ```
194    /// ```
195    /// use sql_builder::*;
196    ///
197    /// let query = Query::select()
198    ///     .column("id")
199    ///     .from("post")
200    ///     .and_where(Expr::col("tag").eq(PgFunc::any(Expr::cust("string_to_array('a,b,c', ',')"))))
201    ///     .to_owned();
202    ///
203    /// assert_eq!(
204    ///     query.to_string(PostgresQueryBuilder).unwrap(),
205    ///     r#"SELECT "id" FROM "post" WHERE "tag" = ANY(string_to_array('a,b,c', ','))"#
206    /// );
207    /// ```
208    pub fn any<T>(expr: T) -> FunctionCall
209    where
210        T: Into<Expr>,
211    {
212        FunctionCall::new(PgFunc::Any).arg(expr)
213    }
214
215    /// Call `SOME` function. Postgres only.
216    ///
217    /// # Examples
218    ///
219    /// ```
220    /// use sql_builder::*;
221    ///
222    /// let query = Query::select().expr(PgFunc::some(vec![0, 1])).to_owned();
223    ///
224    /// assert_eq!(
225    ///     query.to_string(PostgresQueryBuilder).unwrap(),
226    ///     r#"SELECT SOME(ARRAY [0,1])"#
227    /// );
228    /// ```
229    pub fn some<T>(expr: T) -> FunctionCall
230    where
231        T: Into<Expr>,
232    {
233        FunctionCall::new(PgFunc::Some).arg(expr)
234    }
235
236    /// Call `ALL` function. Postgres only.
237    ///
238    /// # Examples
239    ///
240    /// ```
241    /// use sql_builder::*;
242    ///
243    /// let query = Query::select().expr(PgFunc::all(vec![0, 1])).to_owned();
244    ///
245    /// assert_eq!(
246    ///     query.to_string(PostgresQueryBuilder).unwrap(),
247    ///     r#"SELECT ALL(ARRAY [0,1])"#
248    /// );
249    /// ```
250    pub fn all<T>(expr: T) -> FunctionCall
251    where
252        T: Into<Expr>,
253    {
254        FunctionCall::new(PgFunc::All).arg(expr)
255    }
256}
257
258/// Calls `func` on `expr`, with the text search configuration `regconfig`
259/// first when one is given.
260fn text_search(func: PgFunc, expr: Expr, regconfig: Option<u32>) -> FunctionCall {
261    match regconfig {
262        Some(config) => FunctionCall::new(func).args([Expr::Value(config.into()), expr]),
263        None => FunctionCall::new(func).arg(expr),
264    }
265}