sql_builder/extension/postgres/func/aggregate.rs
1use super::*;
2
3impl PgFunc {
4 /// Call `STARTS_WITH` function. Postgres only.
5 ///
6 /// # Examples
7 ///
8 /// ```
9 /// use sql_builder::*;
10 ///
11 /// let query = Query::select()
12 /// .expr(PgFunc::starts_with("123", "1"))
13 /// .to_owned();
14 ///
15 /// assert_eq!(
16 /// query.to_string(PostgresQueryBuilder).unwrap(),
17 /// r#"SELECT STARTS_WITH('123', '1')"#
18 /// );
19 /// ```
20 pub fn starts_with<T, P>(text: T, prefix: P) -> FunctionCall
21 where
22 T: Into<Expr>,
23 P: Into<Expr>,
24 {
25 FunctionCall::new(PgFunc::StartsWith).args([text.into(), prefix.into()])
26 }
27
28 /// Call `GEN_RANDOM_UUID` function. Postgres only.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// use sql_builder::*;
34 ///
35 /// let query = Query::select().expr(PgFunc::gen_random_uuid()).to_owned();
36 ///
37 /// assert_eq!(
38 /// query.to_string(PostgresQueryBuilder).unwrap(),
39 /// r#"SELECT GEN_RANDOM_UUID()"#
40 /// );
41 /// ```
42 pub fn gen_random_uuid() -> FunctionCall {
43 FunctionCall::new(PgFunc::GenRandomUUID)
44 }
45
46 /// Call the `JSON_BUILD_OBJECT` function. Postgres only.
47 ///
48 /// # Examples
49 ///
50 /// ```
51 /// use sql_builder::*;
52 ///
53 /// let query = Query::select()
54 /// .expr(PgFunc::json_build_object(vec![
55 /// (Expr::val("a"), Expr::val(1)),
56 /// (Expr::val("b"), Expr::val("2")),
57 /// ]))
58 /// .to_owned();
59 ///
60 /// assert_eq!(
61 /// query.to_string(PostgresQueryBuilder).unwrap(),
62 /// r#"SELECT JSON_BUILD_OBJECT('a', 1, 'b', '2')"#
63 /// );
64 /// ```
65 pub fn json_build_object<T>(pairs: Vec<(T, T)>) -> FunctionCall
66 where
67 T: Into<Expr>,
68 {
69 let mut args = vec![];
70 for (key, value) in pairs {
71 args.push(key.into());
72 args.push(value.into());
73 }
74 FunctionCall::new(PgFunc::JsonBuildObject).args(args)
75 }
76
77 /// Call the `DATE_TRUNC` function. Postgres only.
78 ///
79 /// # Examples
80 ///
81 /// ```
82 /// use sql_builder::*;
83 ///
84 /// let query = Query::select()
85 /// .expr(PgFunc::date_trunc(
86 /// PgDateTruncUnit::Day,
87 /// Expr::val("2020-01-01"),
88 /// ))
89 /// .to_owned();
90 ///
91 /// assert_eq!(
92 /// query.to_string(PostgresQueryBuilder).unwrap(),
93 /// r#"SELECT DATE_TRUNC('day', '2020-01-01')"#
94 /// );
95 ///
96 /// let query = Query::select()
97 /// .expr(PgFunc::date_trunc(
98 /// PgDateTruncUnit::Microseconds,
99 /// Expr::val("2020-01-01"),
100 /// ))
101 /// .to_owned();
102 ///
103 /// assert_eq!(
104 /// query.to_string(PostgresQueryBuilder).unwrap(),
105 /// r#"SELECT DATE_TRUNC('microseconds', '2020-01-01')"#
106 /// );
107 /// ```
108 pub fn date_trunc<T>(unit: PgDateTruncUnit, expr: T) -> FunctionCall
109 where
110 T: Into<Expr>,
111 {
112 FunctionCall::new(PgFunc::DateTrunc).args([Expr::val(unit.to_string()), expr.into()])
113 }
114
115 /// Call the `JSON_AGG` function. Postgres only.
116 ///
117 /// # Examples
118 ///
119 /// ```
120 /// use sql_builder::*;
121 ///
122 /// let query = Query::select()
123 /// .from("character")
124 /// .expr(PgFunc::json_agg(Expr::col("size_w")))
125 /// .to_owned();
126 ///
127 /// assert_eq!(
128 /// query.to_string(PostgresQueryBuilder).unwrap(),
129 /// r#"SELECT JSON_AGG("size_w") FROM "character""#
130 /// );
131 /// ```
132 pub fn json_agg<T>(expr: T) -> FunctionCall
133 where
134 T: Into<Expr>,
135 {
136 FunctionCall::new(PgFunc::JsonAgg).arg(expr)
137 }
138
139 /// Call the `ARRAY_AGG` function. Postgres only.
140 ///
141 /// # Examples
142 ///
143 /// ```
144 /// use sql_builder::*;
145 ///
146 /// let query = Query::select()
147 /// .from("character")
148 /// .expr(PgFunc::array_agg(Expr::col("id")))
149 /// .group_by_col("character")
150 /// .to_owned();
151 ///
152 /// assert_eq!(
153 /// query.to_string(PostgresQueryBuilder).unwrap(),
154 /// r#"SELECT ARRAY_AGG("id") FROM "character" GROUP BY "character""#
155 /// );
156 /// ```
157 pub fn array_agg<T>(expr: T) -> FunctionCall
158 where
159 T: Into<Expr>,
160 {
161 FunctionCall::new(PgFunc::ArrayAgg).arg(expr)
162 }
163
164 /// Call the `ARRAY_AGG` function with the `DISTINCT` modifier. Postgres only.
165 ///
166 /// # Examples
167 ///
168 /// ```
169 /// use sql_builder::*;
170 ///
171 /// let query = Query::select()
172 /// .from("character")
173 /// .expr(PgFunc::array_agg_distinct(Expr::col("id")))
174 /// .group_by_col("character")
175 /// .to_owned();
176 ///
177 /// assert_eq!(
178 /// query.to_string(PostgresQueryBuilder).unwrap(),
179 /// r#"SELECT ARRAY_AGG(DISTINCT "id") FROM "character" GROUP BY "character""#
180 /// );
181 /// ```
182 pub fn array_agg_distinct<T>(expr: T) -> FunctionCall
183 where
184 T: Into<Expr>,
185 {
186 FunctionCall::new(PgFunc::ArrayAgg).arg_with(expr, FuncArgMod { distinct: true })
187 }
188
189 /// Call `PG_ADVISORY_LOCK` function. Postgres only.
190 ///
191 /// # Examples
192 ///
193 /// ```
194 /// use sql_builder::*;
195 ///
196 /// let query = Query::select()
197 /// .expr(PgFunc::advisory_lock(Expr::val(12345_i64)))
198 /// .to_owned();
199 ///
200 /// assert_eq!(
201 /// query.to_string(PostgresQueryBuilder).unwrap(),
202 /// r#"SELECT PG_ADVISORY_LOCK(12345)"#
203 /// );
204 /// ```
205 pub fn advisory_lock<T>(key: T) -> FunctionCall
206 where
207 T: Into<Expr>,
208 {
209 FunctionCall::new(PgFunc::AdvisoryLock).arg(key)
210 }
211
212 /// Call `PG_ADVISORY_LOCK_SHARED` function. Postgres only.
213 ///
214 /// # Examples
215 ///
216 /// ```
217 /// use sql_builder::*;
218 ///
219 /// let query = Query::select()
220 /// .expr(PgFunc::advisory_lock_shared(Expr::val(12345_i64)))
221 /// .to_owned();
222 ///
223 /// assert_eq!(
224 /// query.to_string(PostgresQueryBuilder).unwrap(),
225 /// r#"SELECT PG_ADVISORY_LOCK_SHARED(12345)"#
226 /// );
227 /// ```
228 pub fn advisory_lock_shared<T>(key: T) -> FunctionCall
229 where
230 T: Into<Expr>,
231 {
232 FunctionCall::new(PgFunc::AdvisoryLockShared).arg(key)
233 }
234
235 /// Call `PG_TRY_ADVISORY_LOCK` function. Postgres only.
236 ///
237 /// # Examples
238 ///
239 /// ```
240 /// use sql_builder::*;
241 ///
242 /// let query = Query::select()
243 /// .expr(PgFunc::try_advisory_lock(Expr::val(12345_i64)))
244 /// .to_owned();
245 ///
246 /// assert_eq!(
247 /// query.to_string(PostgresQueryBuilder).unwrap(),
248 /// r#"SELECT PG_TRY_ADVISORY_LOCK(12345)"#
249 /// );
250 /// ```
251 pub fn try_advisory_lock<T>(key: T) -> FunctionCall
252 where
253 T: Into<Expr>,
254 {
255 FunctionCall::new(PgFunc::TryAdvisoryLock).arg(key)
256 }
257
258 /// Call `PG_TRY_ADVISORY_LOCK_SHARED` function. Postgres only.
259 ///
260 /// # Examples
261 ///
262 /// ```
263 /// use sql_builder::*;
264 ///
265 /// let query = Query::select()
266 /// .expr(PgFunc::try_advisory_lock_shared(Expr::val(12345_i64)))
267 /// .to_owned();
268 ///
269 /// assert_eq!(
270 /// query.to_string(PostgresQueryBuilder).unwrap(),
271 /// r#"SELECT PG_TRY_ADVISORY_LOCK_SHARED(12345)"#
272 /// );
273 /// ```
274 pub fn try_advisory_lock_shared<T>(key: T) -> FunctionCall
275 where
276 T: Into<Expr>,
277 {
278 FunctionCall::new(PgFunc::TryAdvisoryLockShared).arg(key)
279 }
280
281 /// Call `PG_ADVISORY_UNLOCK` function. Postgres only.
282 ///
283 /// # Examples
284 ///
285 /// ```
286 /// use sql_builder::*;
287 ///
288 /// let query = Query::select()
289 /// .expr(PgFunc::advisory_unlock(Expr::val(12345_i64)))
290 /// .to_owned();
291 ///
292 /// assert_eq!(
293 /// query.to_string(PostgresQueryBuilder).unwrap(),
294 /// r#"SELECT PG_ADVISORY_UNLOCK(12345)"#
295 /// );
296 /// ```
297 pub fn advisory_unlock<T>(key: T) -> FunctionCall
298 where
299 T: Into<Expr>,
300 {
301 FunctionCall::new(PgFunc::AdvisoryUnlock).arg(key)
302 }
303
304 /// Call `PG_ADVISORY_UNLOCK_SHARED` function. Postgres only.
305 ///
306 /// # Examples
307 ///
308 /// ```
309 /// use sql_builder::*;
310 ///
311 /// let query = Query::select()
312 /// .expr(PgFunc::advisory_unlock_shared(Expr::val(12345_i64)))
313 /// .to_owned();
314 ///
315 /// assert_eq!(
316 /// query.to_string(PostgresQueryBuilder).unwrap(),
317 /// r#"SELECT PG_ADVISORY_UNLOCK_SHARED(12345)"#
318 /// );
319 /// ```
320 pub fn advisory_unlock_shared<T>(key: T) -> FunctionCall
321 where
322 T: Into<Expr>,
323 {
324 FunctionCall::new(PgFunc::AdvisoryUnlockShared).arg(key)
325 }
326
327 /// Call `PG_ADVISORY_UNLOCK_ALL` function. Postgres only.
328 ///
329 /// # Examples
330 ///
331 /// ```
332 /// use sql_builder::*;
333 ///
334 /// let query = Query::select()
335 /// .expr(PgFunc::advisory_unlock_all())
336 /// .to_owned();
337 ///
338 /// assert_eq!(
339 /// query.to_string(PostgresQueryBuilder).unwrap(),
340 /// r#"SELECT PG_ADVISORY_UNLOCK_ALL()"#
341 /// );
342 /// ```
343 pub fn advisory_unlock_all() -> FunctionCall {
344 FunctionCall::new(PgFunc::AdvisoryUnlockAll)
345 }
346
347 /// Call `PG_ADVISORY_XACT_LOCK` function. Postgres only.
348 ///
349 /// # Examples
350 ///
351 /// ```
352 /// use sql_builder::*;
353 ///
354 /// let query = Query::select()
355 /// .expr(PgFunc::advisory_xact_lock(Expr::val(12345_i64)))
356 /// .to_owned();
357 ///
358 /// assert_eq!(
359 /// query.to_string(PostgresQueryBuilder).unwrap(),
360 /// r#"SELECT PG_ADVISORY_XACT_LOCK(12345)"#
361 /// );
362 /// ```
363 pub fn advisory_xact_lock<T>(key: T) -> FunctionCall
364 where
365 T: Into<Expr>,
366 {
367 FunctionCall::new(PgFunc::AdvisoryXactLock).arg(key)
368 }
369
370 /// Call `PG_ADVISORY_XACT_LOCK_SHARED` function. Postgres only.
371 ///
372 /// # Examples
373 ///
374 /// ```
375 /// use sql_builder::*;
376 ///
377 /// let query = Query::select()
378 /// .expr(PgFunc::advisory_xact_lock_shared(Expr::val(12345_i64)))
379 /// .to_owned();
380 ///
381 /// assert_eq!(
382 /// query.to_string(PostgresQueryBuilder).unwrap(),
383 /// r#"SELECT PG_ADVISORY_XACT_LOCK_SHARED(12345)"#
384 /// );
385 /// ```
386 pub fn advisory_xact_lock_shared<T>(key: T) -> FunctionCall
387 where
388 T: Into<Expr>,
389 {
390 FunctionCall::new(PgFunc::AdvisoryXactLockShared).arg(key)
391 }
392
393 /// Call `PG_TRY_ADVISORY_XACT_LOCK` function. Postgres only.
394 ///
395 /// # Examples
396 ///
397 /// ```
398 /// use sql_builder::*;
399 ///
400 /// let query = Query::select()
401 /// .expr(PgFunc::try_advisory_xact_lock(Expr::val(12345_i64)))
402 /// .to_owned();
403 ///
404 /// assert_eq!(
405 /// query.to_string(PostgresQueryBuilder).unwrap(),
406 /// r#"SELECT PG_TRY_ADVISORY_XACT_LOCK(12345)"#
407 /// );
408 /// ```
409 pub fn try_advisory_xact_lock<T>(key: T) -> FunctionCall
410 where
411 T: Into<Expr>,
412 {
413 FunctionCall::new(PgFunc::TryAdvisoryXactLock).arg(key)
414 }
415
416 /// Call `PG_TRY_ADVISORY_XACT_LOCK_SHARED` function. Postgres only.
417 ///
418 /// # Examples
419 ///
420 /// ```
421 /// use sql_builder::*;
422 ///
423 /// let query = Query::select()
424 /// .expr(PgFunc::try_advisory_xact_lock_shared(Expr::val(12345_i64)))
425 /// .to_owned();
426 ///
427 /// assert_eq!(
428 /// query.to_string(PostgresQueryBuilder).unwrap(),
429 /// r#"SELECT PG_TRY_ADVISORY_XACT_LOCK_SHARED(12345)"#
430 /// );
431 /// ```
432 pub fn try_advisory_xact_lock_shared<T>(key: T) -> FunctionCall
433 where
434 T: Into<Expr>,
435 {
436 FunctionCall::new(PgFunc::TryAdvisoryXactLockShared).arg(key)
437 }
438}