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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use super::*;
impl InsertStatement {
/// ON CONFLICT expression
///
/// # Examples
///
/// - [`OnConflict::update_columns`]: Update column value of existing row with inserting value
pub fn on_conflict(&mut self, on_conflict: OnConflict) -> &mut Self {
self.on_conflict = Some(on_conflict);
self
}
/// Whether the statement says what to do on a conflict.
pub fn has_on_conflict(&self) -> bool {
self.on_conflict.is_some()
}
/// RETURNING expressions.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::insert()
/// .into_table("glyph")
/// .columns(["image"])
/// .values_panic(["12A".into()])
/// .returning(Query::returning().columns(["id"]))
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`"
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
/// );
/// ```
pub fn returning(&mut self, returning: ReturningClause) -> &mut Self {
self.returning = Some(returning);
self
}
/// RETURNING expressions for a column.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::insert()
/// .into_table("glyph")
/// .columns(["image"])
/// .values_panic(["12A".into()])
/// .returning_col("id")
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`"
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
/// );
/// ```
pub fn returning_col<C>(&mut self, col: C) -> &mut Self
where
C: IntoColumnRef,
{
self.returning(ReturningClause::Columns(vec![col.into_column_ref()]))
}
/// RETURNING expressions all columns.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// let query = Query::insert()
/// .into_table("glyph")
/// .columns(["image"])
/// .values_panic(["12A".into()])
/// .returning_all()
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING *"
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
/// );
/// ```
pub fn returning_all(&mut self) -> &mut Self {
self.returning(ReturningClause::All)
}
/// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
///
/// # Examples
///
/// ```
/// use sql_builder::{*, IntoCondition, IntoIden};
///
/// let select = SelectStatement::new()
/// .columns(["id", "image", "aspect"])
/// .from("glyph")
/// .take();
/// let cte = CommonTableExpression::new()
/// .query(select)
/// .column("id")
/// .column("image")
/// .column("aspect")
/// .table_name("cte")
/// .to_owned();
/// let with_clause = WithClause::new().cte(cte).to_owned();
/// let select = SelectStatement::new()
/// .columns(["id", "image", "aspect"])
/// .from("cte")
/// .take();
/// let mut insert = Query::insert();
/// insert
/// .into_table("glyph")
/// .columns(["id", "image", "aspect"])
/// .select_from(select)
/// .unwrap();
/// let query = insert.with(with_clause);
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"WITH `cte` (`id`, `image`, `aspect`) AS (SELECT `id`, `image`, `aspect` FROM `glyph`) INSERT INTO `glyph` (`id`, `image`, `aspect`) SELECT `id`, `image`, `aspect` FROM `cte`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
/// );
/// ```
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
}
/// Create a Common Table Expression by specifying a [CommonTableExpression][crate::CommonTableExpression]
/// or [WithClause] to execute this query with.
///
/// # Examples
///
/// ```
/// use sql_builder::{*, IntoCondition, IntoIden};
///
/// let select = SelectStatement::new()
/// .columns(["id", "image", "aspect"])
/// .from("glyph")
/// .take();
/// let cte = CommonTableExpression::new()
/// .query(select)
/// .column("id")
/// .column("image")
/// .column("aspect")
/// .table_name("cte")
/// .to_owned();
/// let with_clause = WithClause::new().cte(cte).to_owned();
/// let select = SelectStatement::new()
/// .columns(["id", "image", "aspect"])
/// .from("cte")
/// .take();
/// let mut query = Query::insert();
/// query
/// .with_cte(with_clause)
/// .into_table("glyph")
/// .columns(["id", "image", "aspect"])
/// .select_from(select)
/// .unwrap();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"WITH `cte` (`id`, `image`, `aspect`) AS (SELECT `id`, `image`, `aspect` FROM `glyph`) INSERT INTO `glyph` (`id`, `image`, `aspect`) SELECT `id`, `image`, `aspect` FROM `cte`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
/// );
/// ```
pub fn with_cte<C: Into<WithClause>>(&mut self, clause: C) -> &mut Self {
self.with = Some(clause.into());
self
}
/// Insert with default values if columns and values are not supplied.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// // Insert default
/// let query = Query::insert()
/// .into_table("glyph")
/// .or_default_values()
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"INSERT INTO `glyph` VALUES ()"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" VALUES (DEFAULT)"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" DEFAULT VALUES"#
/// );
///
/// // Ordinary insert as columns and values are supplied
/// let query = Query::insert()
/// .into_table("glyph")
/// .or_default_values()
/// .columns(["image"])
/// .values_panic(["ABC".into()])
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
/// );
/// ```
pub fn or_default_values(&mut self) -> &mut Self {
self.default_values = Some(1);
self
}
/// Insert multiple rows with default values if columns and values are not supplied.
///
/// # Examples
///
/// ```
/// use sql_builder::*;
///
/// // Insert default
/// let query = Query::insert()
/// .into_table("glyph")
/// .or_default_values_many(3)
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"INSERT INTO `glyph` VALUES (), (), ()"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" VALUES (DEFAULT), (DEFAULT), (DEFAULT)"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" (rowid) VALUES (NULL), (NULL), (NULL)"#
/// );
///
/// // Ordinary insert as columns and values are supplied
/// let query = Query::insert()
/// .into_table("glyph")
/// .or_default_values_many(3)
/// .columns(["image"])
/// .values_panic(["ABC".into()])
/// .take();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder).unwrap(),
/// r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder).unwrap(),
/// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
/// );
/// ```
pub fn or_default_values_many(&mut self, num_rows: usize) -> &mut Self {
self.default_values = Some(num_rows);
self
}
}