sea_query/extension/postgres/
select.rs

1use crate::SelectStatement;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct TableSample {
5    pub method: SampleMethod,
6    pub percentage: f64,
7    pub repeatable: Option<f64>,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum SampleMethod {
13    BERNOULLI,
14    SYSTEM,
15}
16
17pub trait PostgresSelectStatementExt {
18    fn table_sample(
19        &mut self,
20        method: SampleMethod,
21        percentage: f64,
22        repeatable: Option<f64>,
23    ) -> &mut Self;
24}
25
26impl PostgresSelectStatementExt for SelectStatement {
27    /// TABLESAMPLE
28    ///
29    /// # Examples
30    ///
31    /// ```
32    /// use sea_query::{extension::postgres::*, tests_cfg::*, *};
33    ///
34    /// let query = Query::select()
35    ///     .columns([Glyph::Image])
36    ///     .from(Glyph::Table)
37    ///     .table_sample(SampleMethod::SYSTEM, 50.0, None)
38    ///     .to_owned();
39    ///
40    /// assert_eq!(
41    ///     query.to_string(PostgresQueryBuilder),
42    ///     r#"SELECT "image" FROM "glyph" TABLESAMPLE SYSTEM (50)"#
43    /// );
44    ///
45    /// let query = Query::select()
46    ///     .columns([Glyph::Image])
47    ///     .from(Glyph::Table)
48    ///     .table_sample(SampleMethod::SYSTEM, 50.0, Some(3.14))
49    ///     .to_owned();
50    ///
51    /// assert_eq!(
52    ///     query.to_string(PostgresQueryBuilder),
53    ///     r#"SELECT "image" FROM "glyph" TABLESAMPLE SYSTEM (50) REPEATABLE (3.14)"#
54    /// );
55    /// ```
56    fn table_sample(
57        &mut self,
58        method: SampleMethod,
59        percentage: f64,
60        repeatable: Option<f64>,
61    ) -> &mut Self {
62        self.table_sample = Some(TableSample {
63            method,
64            percentage,
65            repeatable,
66        });
67        self
68    }
69}