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