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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::*;
impl SelectStatement {
/// Construct a new [`SelectStatement`]
pub fn new() -> Self {
Self::default()
}
/// A select written as SQL text, with the values it binds in place, such
/// as an operand or a CTE body the caller wrote itself. It is rendered as
/// it is, so no other clause is set on it.
pub fn custom(sql: BoundSql) -> Self {
Self {
custom: Some(sql),
..Self::default()
}
}
/// Take the ownership of data in the current [`SelectStatement`]
pub fn take(&mut self) -> Self {
Self {
distinct: self.distinct.take(),
selects: std::mem::take(&mut self.selects),
into: std::mem::take(&mut self.into),
from: std::mem::take(&mut self.from),
join: std::mem::take(&mut self.join),
r#where: std::mem::replace(&mut self.r#where, ConditionHolder::new()),
groups: std::mem::take(&mut self.groups),
having: std::mem::replace(&mut self.having, ConditionHolder::new()),
unions: std::mem::take(&mut self.unions),
orders: std::mem::take(&mut self.orders),
limit: self.limit.take(),
offset: self.offset.take(),
lock: self.lock.take(),
window: self.window.take(),
with: self.with.take(),
custom: self.custom.take(),
}
}
/// A shorthand to express if ... else ... when constructing the select statement.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .column("character")
/// .from("character")
/// .conditions(
/// true,
/// |x| {
/// x.and_where(Expr::col("font_id").eq(5));
/// },
/// |x| {
/// x.and_where(Expr::col("font_id").eq(10));
/// },
/// )
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
/// );
/// ```
pub fn conditions<T, F>(&mut self, b: bool, if_true: T, if_false: F) -> &mut Self
where
T: FnOnce(&mut Self),
F: FnOnce(&mut Self),
{
if b {
if_true(self)
} else {
if_false(self)
}
self
}
/// A shorthand to express if ... else ... when constructing the select statement.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::select()
/// .column("character")
/// .from("character")
/// .apply_if(Some(5), |q, v| {
/// q.and_where(Expr::col("font_id").eq(v));
/// })
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
/// );
/// ```
pub fn apply_if<T, F>(&mut self, val: Option<T>, if_some: F) -> &mut Self
where
Self: Sized,
F: FnOnce(&mut Self, T),
{
if let Some(val) = val {
if_some(self, val);
}
self
}
/// Construct part of the select statement in another function.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let common_expr = |q: &mut SelectStatement| {
/// q.and_where(Expr::col("font_id").eq(5));
/// };
///
/// let query = Query::select()
/// .column("character")
/// .from("character")
/// .apply(common_expr)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
/// );
/// ```
pub fn apply<F>(&mut self, func: F) -> &mut Self
where
F: FnOnce(&mut Self),
{
func(self);
self
}
}