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