sql_builder/query/select/construction.rs
1use super::*;
2
3impl SelectStatement {
4 /// Construct a new [`SelectStatement`]
5 pub fn new() -> Self {
6 Self::default()
7 }
8
9 /// A select written as SQL text, with the values it binds in place, such
10 /// as an operand or a CTE body the caller wrote itself. It is rendered as
11 /// it is, so no other clause is set on it.
12 pub fn custom(sql: BoundSql) -> Self {
13 Self {
14 custom: Some(sql),
15 ..Self::default()
16 }
17 }
18
19 /// Take the ownership of data in the current [`SelectStatement`]
20 pub fn take(&mut self) -> Self {
21 Self {
22 distinct: self.distinct.take(),
23 selects: std::mem::take(&mut self.selects),
24 into: std::mem::take(&mut self.into),
25 from: std::mem::take(&mut self.from),
26 join: std::mem::take(&mut self.join),
27 r#where: std::mem::replace(&mut self.r#where, ConditionHolder::new()),
28 groups: std::mem::take(&mut self.groups),
29 having: std::mem::replace(&mut self.having, ConditionHolder::new()),
30 unions: std::mem::take(&mut self.unions),
31 orders: std::mem::take(&mut self.orders),
32 limit: self.limit.take(),
33 offset: self.offset.take(),
34 lock: self.lock.take(),
35 window: self.window.take(),
36 with: self.with.take(),
37 custom: self.custom.take(),
38 }
39 }
40
41 /// A shorthand to express if ... else ... when constructing the select statement.
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// use sql_builder::*;
47 ///
48 /// let query = Query::select()
49 /// .column("character")
50 /// .from("character")
51 /// .conditions(
52 /// true,
53 /// |x| {
54 /// x.and_where(Expr::col("font_id").eq(5));
55 /// },
56 /// |x| {
57 /// x.and_where(Expr::col("font_id").eq(10));
58 /// },
59 /// )
60 /// .to_owned();
61 ///
62 /// assert_eq!(
63 /// query.to_string(MysqlQueryBuilder).unwrap(),
64 /// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
65 /// );
66 /// assert_eq!(
67 /// query.to_string(PostgresQueryBuilder).unwrap(),
68 /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
69 /// );
70 /// assert_eq!(
71 /// query.to_string(SqliteQueryBuilder).unwrap(),
72 /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
73 /// );
74 /// ```
75 pub fn conditions<T, F>(&mut self, b: bool, if_true: T, if_false: F) -> &mut Self
76 where
77 T: FnOnce(&mut Self),
78 F: FnOnce(&mut Self),
79 {
80 if b {
81 if_true(self)
82 } else {
83 if_false(self)
84 }
85 self
86 }
87
88 /// A shorthand to express if ... else ... when constructing the select statement.
89 ///
90 /// # Examples
91 ///
92 /// ```
93 /// use sql_builder::*;
94 ///
95 /// let query = Query::select()
96 /// .column("character")
97 /// .from("character")
98 /// .apply_if(Some(5), |q, v| {
99 /// q.and_where(Expr::col("font_id").eq(v));
100 /// })
101 /// .to_owned();
102 ///
103 /// assert_eq!(
104 /// query.to_string(MysqlQueryBuilder).unwrap(),
105 /// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
106 /// );
107 /// ```
108 pub fn apply_if<T, F>(&mut self, val: Option<T>, if_some: F) -> &mut Self
109 where
110 Self: Sized,
111 F: FnOnce(&mut Self, T),
112 {
113 if let Some(val) = val {
114 if_some(self, val);
115 }
116 self
117 }
118
119 /// Construct part of the select statement in another function.
120 ///
121 /// # Examples
122 ///
123 /// ```
124 /// use sql_builder::*;
125 ///
126 /// let common_expr = |q: &mut SelectStatement| {
127 /// q.and_where(Expr::col("font_id").eq(5));
128 /// };
129 ///
130 /// let query = Query::select()
131 /// .column("character")
132 /// .from("character")
133 /// .apply(common_expr)
134 /// .to_owned();
135 ///
136 /// assert_eq!(
137 /// query.to_string(MysqlQueryBuilder).unwrap(),
138 /// r#"SELECT `character` FROM `character` WHERE `font_id` = 5"#
139 /// );
140 /// assert_eq!(
141 /// query.to_string(PostgresQueryBuilder).unwrap(),
142 /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
143 /// );
144 /// assert_eq!(
145 /// query.to_string(SqliteQueryBuilder).unwrap(),
146 /// r#"SELECT "character" FROM "character" WHERE "font_id" = 5"#
147 /// );
148 /// ```
149 pub fn apply<F>(&mut self, func: F) -> &mut Self
150 where
151 F: FnOnce(&mut Self),
152 {
153 func(self);
154 self
155 }
156}