sql_builder/query/insert/conflict.rs
1use super::*;
2
3impl InsertStatement {
4 /// ON CONFLICT expression
5 ///
6 /// # Examples
7 ///
8 /// - [`OnConflict::update_columns`]: Update column value of existing row with inserting value
9 pub fn on_conflict(&mut self, on_conflict: OnConflict) -> &mut Self {
10 self.on_conflict = Some(on_conflict);
11 self
12 }
13
14 /// Whether the statement says what to do on a conflict.
15 pub fn has_on_conflict(&self) -> bool {
16 self.on_conflict.is_some()
17 }
18
19 /// RETURNING expressions.
20 ///
21 /// # Examples
22 ///
23 /// ```
24 /// use sql_builder::*;
25 ///
26 /// let query = Query::insert()
27 /// .into_table("glyph")
28 /// .columns(["image"])
29 /// .values_panic(["12A".into()])
30 /// .returning(Query::returning().columns(["id"]))
31 /// .take();
32 ///
33 /// assert_eq!(
34 /// query.to_string(MariaDbDialect).unwrap(),
35 /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`"
36 /// );
37 /// assert_eq!(
38 /// query.to_string(PostgresQueryBuilder).unwrap(),
39 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
40 /// );
41 /// assert_eq!(
42 /// query.to_string(SqliteQueryBuilder).unwrap(),
43 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
44 /// );
45 /// ```
46 pub fn returning(&mut self, returning: ReturningClause) -> &mut Self {
47 self.returning = Some(returning);
48 self
49 }
50
51 /// RETURNING expressions for a column.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use sql_builder::*;
57 ///
58 /// let query = Query::insert()
59 /// .into_table("glyph")
60 /// .columns(["image"])
61 /// .values_panic(["12A".into()])
62 /// .returning_col("id")
63 /// .take();
64 ///
65 /// assert_eq!(
66 /// query.to_string(MariaDbDialect).unwrap(),
67 /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING `id`"
68 /// );
69 /// assert_eq!(
70 /// query.to_string(PostgresQueryBuilder).unwrap(),
71 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
72 /// );
73 /// assert_eq!(
74 /// query.to_string(SqliteQueryBuilder).unwrap(),
75 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
76 /// );
77 /// ```
78 pub fn returning_col<C>(&mut self, col: C) -> &mut Self
79 where
80 C: IntoColumnRef,
81 {
82 self.returning(ReturningClause::Columns(vec![col.into_column_ref()]))
83 }
84
85 /// RETURNING expressions all columns.
86 ///
87 /// # Examples
88 ///
89 /// ```
90 /// use sql_builder::*;
91 ///
92 /// let query = Query::insert()
93 /// .into_table("glyph")
94 /// .columns(["image"])
95 /// .values_panic(["12A".into()])
96 /// .returning_all()
97 /// .take();
98 ///
99 /// assert_eq!(
100 /// query.to_string(MariaDbDialect).unwrap(),
101 /// "INSERT INTO `glyph` (`image`) VALUES ('12A') RETURNING *"
102 /// );
103 /// assert_eq!(
104 /// query.to_string(PostgresQueryBuilder).unwrap(),
105 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
106 /// );
107 /// assert_eq!(
108 /// query.to_string(SqliteQueryBuilder).unwrap(),
109 /// r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
110 /// );
111 /// ```
112 pub fn returning_all(&mut self) -> &mut Self {
113 self.returning(ReturningClause::All)
114 }
115
116 /// Create a [WithQuery] by specifying a [WithClause] to execute this query with.
117 ///
118 /// # Examples
119 ///
120 /// ```
121 /// use sql_builder::{*, IntoCondition, IntoIden};
122 ///
123 /// let select = SelectStatement::new()
124 /// .columns(["id", "image", "aspect"])
125 /// .from("glyph")
126 /// .take();
127 /// let cte = CommonTableExpression::new()
128 /// .query(select)
129 /// .column("id")
130 /// .column("image")
131 /// .column("aspect")
132 /// .table_name("cte")
133 /// .to_owned();
134 /// let with_clause = WithClause::new().cte(cte).to_owned();
135 /// let select = SelectStatement::new()
136 /// .columns(["id", "image", "aspect"])
137 /// .from("cte")
138 /// .take();
139 /// let mut insert = Query::insert();
140 /// insert
141 /// .into_table("glyph")
142 /// .columns(["id", "image", "aspect"])
143 /// .select_from(select)
144 /// .unwrap();
145 /// let query = insert.with(with_clause);
146 ///
147 /// assert_eq!(
148 /// query.to_string(MysqlQueryBuilder).unwrap(),
149 /// 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`"#
150 /// );
151 /// assert_eq!(
152 /// query.to_string(PostgresQueryBuilder).unwrap(),
153 /// 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""#
154 /// );
155 /// assert_eq!(
156 /// query.to_string(SqliteQueryBuilder).unwrap(),
157 /// 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""#
158 /// );
159 /// ```
160 pub fn with(self, clause: WithClause) -> WithQuery {
161 clause.query(self)
162 }
163
164 /// Create a Common Table Expression by specifying a [CommonTableExpression][crate::CommonTableExpression]
165 /// or [WithClause] to execute this query with.
166 ///
167 /// # Examples
168 ///
169 /// ```
170 /// use sql_builder::{*, IntoCondition, IntoIden};
171 ///
172 /// let select = SelectStatement::new()
173 /// .columns(["id", "image", "aspect"])
174 /// .from("glyph")
175 /// .take();
176 /// let cte = CommonTableExpression::new()
177 /// .query(select)
178 /// .column("id")
179 /// .column("image")
180 /// .column("aspect")
181 /// .table_name("cte")
182 /// .to_owned();
183 /// let with_clause = WithClause::new().cte(cte).to_owned();
184 /// let select = SelectStatement::new()
185 /// .columns(["id", "image", "aspect"])
186 /// .from("cte")
187 /// .take();
188 /// let mut query = Query::insert();
189 /// query
190 /// .with_cte(with_clause)
191 /// .into_table("glyph")
192 /// .columns(["id", "image", "aspect"])
193 /// .select_from(select)
194 /// .unwrap();
195 ///
196 /// assert_eq!(
197 /// query.to_string(MysqlQueryBuilder).unwrap(),
198 /// 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`"#
199 /// );
200 /// assert_eq!(
201 /// query.to_string(PostgresQueryBuilder).unwrap(),
202 /// 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""#
203 /// );
204 /// assert_eq!(
205 /// query.to_string(SqliteQueryBuilder).unwrap(),
206 /// 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""#
207 /// );
208 /// ```
209 pub fn with_cte<C: Into<WithClause>>(&mut self, clause: C) -> &mut Self {
210 self.with = Some(clause.into());
211 self
212 }
213
214 /// Insert with default values if columns and values are not supplied.
215 ///
216 /// # Examples
217 ///
218 /// ```
219 /// use sql_builder::*;
220 ///
221 /// // Insert default
222 /// let query = Query::insert()
223 /// .into_table("glyph")
224 /// .or_default_values()
225 /// .take();
226 ///
227 /// assert_eq!(
228 /// query.to_string(MysqlQueryBuilder).unwrap(),
229 /// r#"INSERT INTO `glyph` VALUES ()"#
230 /// );
231 /// assert_eq!(
232 /// query.to_string(PostgresQueryBuilder).unwrap(),
233 /// r#"INSERT INTO "glyph" VALUES (DEFAULT)"#
234 /// );
235 /// assert_eq!(
236 /// query.to_string(SqliteQueryBuilder).unwrap(),
237 /// r#"INSERT INTO "glyph" DEFAULT VALUES"#
238 /// );
239 ///
240 /// // Ordinary insert as columns and values are supplied
241 /// let query = Query::insert()
242 /// .into_table("glyph")
243 /// .or_default_values()
244 /// .columns(["image"])
245 /// .values_panic(["ABC".into()])
246 /// .take();
247 ///
248 /// assert_eq!(
249 /// query.to_string(MysqlQueryBuilder).unwrap(),
250 /// r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
251 /// );
252 /// assert_eq!(
253 /// query.to_string(PostgresQueryBuilder).unwrap(),
254 /// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
255 /// );
256 /// assert_eq!(
257 /// query.to_string(SqliteQueryBuilder).unwrap(),
258 /// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
259 /// );
260 /// ```
261 pub fn or_default_values(&mut self) -> &mut Self {
262 self.default_values = Some(1);
263 self
264 }
265
266 /// Insert multiple rows with default values if columns and values are not supplied.
267 ///
268 /// # Examples
269 ///
270 /// ```
271 /// use sql_builder::*;
272 ///
273 /// // Insert default
274 /// let query = Query::insert()
275 /// .into_table("glyph")
276 /// .or_default_values_many(3)
277 /// .take();
278 ///
279 /// assert_eq!(
280 /// query.to_string(MysqlQueryBuilder).unwrap(),
281 /// r#"INSERT INTO `glyph` VALUES (), (), ()"#
282 /// );
283 /// assert_eq!(
284 /// query.to_string(PostgresQueryBuilder).unwrap(),
285 /// r#"INSERT INTO "glyph" VALUES (DEFAULT), (DEFAULT), (DEFAULT)"#
286 /// );
287 /// assert_eq!(
288 /// query.to_string(SqliteQueryBuilder).unwrap(),
289 /// r#"INSERT INTO "glyph" (rowid) VALUES (NULL), (NULL), (NULL)"#
290 /// );
291 ///
292 /// // Ordinary insert as columns and values are supplied
293 /// let query = Query::insert()
294 /// .into_table("glyph")
295 /// .or_default_values_many(3)
296 /// .columns(["image"])
297 /// .values_panic(["ABC".into()])
298 /// .take();
299 ///
300 /// assert_eq!(
301 /// query.to_string(MysqlQueryBuilder).unwrap(),
302 /// r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
303 /// );
304 /// assert_eq!(
305 /// query.to_string(PostgresQueryBuilder).unwrap(),
306 /// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
307 /// );
308 /// assert_eq!(
309 /// query.to_string(SqliteQueryBuilder).unwrap(),
310 /// r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
311 /// );
312 /// ```
313 pub fn or_default_values_many(&mut self, num_rows: usize) -> &mut Self {
314 self.default_values = Some(num_rows);
315 self
316 }
317}