1use super::*;
2
3macro_rules! mysql_family_query_builder {
6 ($builder:ty { $($own:tt)* }) => {
7 impl QueryBuilder for $builder {
8 $($own)*
9
10 fn lexer_dialect(&self) -> crate::lexer::Dialect {
11 crate::lexer::Dialect::MySql
12 }
13
14 fn prepare_select_distinct(
15 &self,
16 select_distinct: &SelectDistinct,
17 sql: &mut impl SqlWriter,
18 ) -> Result<(), RenderError> {
19 match select_distinct {
20 SelectDistinct::All => sql.write_str("ALL").unwrap(),
21 SelectDistinct::Distinct => sql.write_str("DISTINCT").unwrap(),
22 SelectDistinct::DistinctRow => sql.write_str("DISTINCTROW").unwrap(),
23 SelectDistinct::DistinctOn(_) => {
24 return Err(RenderError::Unsupported("MySQL has no DISTINCT ON".into()));
25 }
26 };
27 Ok(())
28 }
29
30 fn prepare_function_filter(
31 &self,
32 _: &Condition,
33 _: &mut impl SqlWriter,
34 ) -> Result<(), RenderError> {
35 Err(RenderError::Unsupported(
36 "MySQL and MariaDB have no FILTER on an aggregate; put a CASE WHEN in its argument".into(),
37 ))
38 }
39
40 fn prepare_select_into(
41 &self,
42 _: &SelectInto,
43 _: &mut impl SqlWriter,
44 ) -> Result<(), RenderError> {
45 Err(RenderError::Unsupported(
46 "MySQL has no SELECT INTO a table".into(),
47 ))
48 }
49
50 fn prepare_insert_with(
52 &self,
53 _: &WithClause,
54 _: &mut impl SqlWriter,
55 ) -> Result<(), RenderError> {
56 Err(RenderError::Unsupported(
57 "MySQL and MariaDB have no WITH before INSERT; give the INSERT's SELECT the WITH clause".into(),
58 ))
59 }
60
61 fn prepare_with_query_clause_materialization(
62 &self,
63 _: &CommonTableExpression,
64 _: &mut impl SqlWriter,
65 ) -> Result<(), RenderError> {
66 Ok(())
68 }
69
70 fn prepare_update_join(
71 &self,
72 from: &[TableRef],
73 condition: &ConditionHolder,
74 sql: &mut impl SqlWriter,
75 ) -> Result<(), RenderError> {
76 if from.is_empty() {
77 return Ok(());
78 }
79
80 for table in from {
81 sql.write_str(" JOIN ").unwrap();
82 expressions::prepare_table_ref(self, table, sql)?;
83 }
84
85 conflict::prepare_condition(self, condition, "ON", sql)?;
86 Ok(())
87 }
88
89 fn prepare_update_from(
90 &self,
91 _: &[TableRef],
92 _: &mut impl SqlWriter,
93 ) -> Result<(), RenderError> {
94 Ok(())
95 }
96
97 fn prepare_update_column(
98 &self,
99 table: &Option<Box<TableRef>>,
100 from: &[TableRef],
101 column: &DynIden,
102 sql: &mut impl SqlWriter,
103 ) -> Result<(), RenderError> {
104 use std::ops::Deref;
105
106 if !from.is_empty()
109 && let Some(table) = table
110 && let TableRef::Table(name, alias) = table.deref()
111 {
112 let target = match alias {
113 Some(alias) => TableName(None, alias.clone()),
114 None => name.clone(),
115 };
116 let column_name = ColumnName(Some(target), column.clone());
117 expressions::prepare_column_ref(self, &ColumnRef::Column(column_name), sql);
118 return Ok(());
119 }
120 self.prepare_iden(column, sql);
121 Ok(())
122 }
123
124 fn prepare_update_condition(
125 &self,
126 from: &[TableRef],
127 condition: &ConditionHolder,
128 sql: &mut impl SqlWriter,
129 ) -> Result<(), RenderError> {
130 if !from.is_empty() {
131 return Ok(());
132 }
133 conflict::prepare_condition(self, condition, "WHERE", sql)?;
134 Ok(())
135 }
136
137 fn prepare_join_type(
138 &self,
139 join_type: &JoinType,
140 sql: &mut impl SqlWriter,
141 ) -> Result<(), RenderError> {
142 match join_type {
143 JoinType::FullOuterJoin => {
144 return Err(RenderError::Unsupported(
145 "Mysql does not support FULL OUTER JOIN".into(),
146 ));
147 }
148 JoinType::StraightJoin => sql.write_str("STRAIGHT_JOIN").unwrap(),
149 _ => functions::prepare_join_type_common(self, join_type, sql)?,
150 }
151 Ok(())
152 }
153
154 fn prepare_order_expr(
155 &self,
156 order_expr: &OrderExpr,
157 sql: &mut impl SqlWriter,
158 ) -> Result<(), RenderError> {
159 match order_expr.nulls {
160 None => (),
161 Some(NullOrdering::Last) => {
162 self.prepare_expr(&order_expr.expr, sql)?;
163 sql.write_str(" IS NULL ASC, ").unwrap()
164 }
165 Some(NullOrdering::First) => {
166 self.prepare_expr(&order_expr.expr, sql)?;
167 sql.write_str(" IS NULL DESC, ").unwrap()
168 }
169 }
170 if !matches!(order_expr.order, Order::Field(_)) {
171 self.prepare_expr(&order_expr.expr, sql)?;
172 }
173 functions::prepare_order(self, order_expr, sql)?;
174 Ok(())
175 }
176
177 fn prepare_select_limit_offset(
178 &self,
179 select: &SelectStatement,
180 sql: &mut impl SqlWriter,
181 ) -> Result<(), RenderError> {
182 prepare_limit_offset(self, select, SqlDialect::open_limit(self), sql)
183 }
184
185 fn prepare_on_conflict_target(
186 &self,
187 _: &OnConflictTarget,
188 _: &mut impl SqlWriter,
189 ) -> Result<(), RenderError> {
190 Ok(())
192 }
193
194 fn prepare_on_conflict_action(
195 &self,
196 on_conflict_action: &Option<OnConflictAction>,
197 sql: &mut impl SqlWriter,
198 ) -> Result<(), RenderError> {
199 match on_conflict_action {
200 Some(OnConflictAction::DoNothing(pk_cols)) => {
201 if !pk_cols.is_empty() {
202 self.prepare_on_conflict_do_update_keywords(sql)?;
203 let mut pk_cols_iter = pk_cols.iter();
204 join_io!(
205 pk_cols_iter,
206 pk_col,
207 join {
208 sql.write_str(", ").unwrap();
209 },
210 do {
211 self.prepare_iden(pk_col, sql);
212 sql.write_str(" = ").unwrap();
213 self.prepare_iden(pk_col, sql);
214 }
215 );
216 } else {
217 return Err(RenderError::Unsupported(
218 "MySQL has no ON DUPLICATE KEY DO NOTHING; name the key columns with do_nothing_on".into(),
219 ));
220 }
221 }
222 _ => conflict::prepare_on_conflict_action_common(self, on_conflict_action, sql)?,
223 }
224 Ok(())
225 }
226
227 fn prepare_on_conflict_keywords(&self, sql: &mut impl SqlWriter) -> Result<(), RenderError> {
228 sql.write_str(" ON DUPLICATE KEY").unwrap();
229 Ok(())
230 }
231
232 fn prepare_on_conflict_do_update_keywords(
233 &self,
234 sql: &mut impl SqlWriter,
235 ) -> Result<(), RenderError> {
236 sql.write_str(" UPDATE ").unwrap();
237 Ok(())
238 }
239
240 fn prepare_on_conflict_excluded_table(
241 &self,
242 col: &DynIden,
243 sql: &mut impl SqlWriter,
244 ) -> Result<(), RenderError> {
245 sql.write_str("VALUES(").unwrap();
246 self.prepare_iden(col, sql);
247 sql.write_str(")").unwrap();
248 Ok(())
249 }
250
251 fn prepare_on_conflict_condition(
252 &self,
253 _: &ConditionHolder,
254 _: &mut impl SqlWriter,
255 ) -> Result<(), RenderError> {
256 Ok(())
257 }
258
259 fn prepare_on_conflict_action_condition(
260 &self,
261 condition: &ConditionHolder,
262 _: &mut impl SqlWriter,
263 ) -> Result<(), RenderError> {
264 if condition.is_empty() {
265 return Ok(());
266 }
267 Err(RenderError::Unsupported(
268 "MySQL has no WHERE on ON DUPLICATE KEY UPDATE".into(),
269 ))
270 }
271
272 fn random_function(&self) -> &str {
273 "RAND"
274 }
275
276 fn insert_default_keyword(&self) -> &str {
277 "()"
278 }
279 }
280 };
281}
282
283mysql_family_query_builder!(MysqlQueryBuilder {
284 fn values_list_tuple_prefix(&self) -> &str {
286 "ROW"
287 }
288
289 fn prepare_returning(
290 &self,
291 returning: &Option<ReturningClause>,
292 _: &mut impl SqlWriter,
293 ) -> Result<(), RenderError> {
294 match returning {
295 None => Ok(()),
296 Some(_) => Err(RenderError::Unsupported("MySQL has no RETURNING".into())),
297 }
298 }
299
300 fn prepare_update_order_limit(
302 &self,
303 from: &[TableRef],
304 orders: &[OrderExpr],
305 limit: &Option<Value>,
306 sql: &mut impl SqlWriter,
307 ) -> Result<(), RenderError> {
308 if !from.is_empty() && (!orders.is_empty() || limit.is_some()) {
309 return Err(RenderError::Unsupported(
310 "MySQL has no ORDER BY or LIMIT in an UPDATE that joins other tables".into(),
311 ));
312 }
313 self.prepare_mutation_order_limit(orders, limit, sql)
314 }
315
316 fn prepare_select_lock(
319 &self,
320 lock: &LockClause,
321 sql: &mut impl SqlWriter,
322 ) -> Result<(), RenderError> {
323 if matches!(lock.r#type, LockType::NoKeyUpdate | LockType::KeyShare) {
324 return Err(RenderError::Unsupported(
325 "MySQL has no FOR NO KEY UPDATE or FOR KEY SHARE lock".into(),
326 ));
327 }
328 expressions::prepare_select_lock(self, lock, sql)
329 }
330});
331
332mysql_family_query_builder!(MariaDbDialect {
333 fn prepare_select_lock(
336 &self,
337 lock: &LockClause,
338 sql: &mut impl SqlWriter,
339 ) -> Result<(), RenderError> {
340 if matches!(lock.r#type, LockType::NoKeyUpdate | LockType::KeyShare) {
341 return Err(RenderError::Unsupported(
342 "MariaDB has no FOR NO KEY UPDATE or FOR KEY SHARE lock".into(),
343 ));
344 }
345 if !lock.tables.is_empty() {
346 return Err(RenderError::Unsupported(
347 "MariaDB has no OF in a row lock; it locks every table the SELECT reads".into(),
348 ));
349 }
350 expressions::prepare_select_lock(self, lock, sql)
351 }
352
353 fn prepare_update_returning(
355 &self,
356 from: &[TableRef],
357 returning: &Option<ReturningClause>,
358 sql: &mut impl SqlWriter,
359 ) -> Result<(), RenderError> {
360 if !from.is_empty() && returning.is_some() {
361 return Err(RenderError::Unsupported(
362 "MariaDB has no RETURNING in an UPDATE that joins other tables".into(),
363 ));
364 }
365 self.prepare_returning(returning, sql)
366 }
367
368 fn lock_phrase(&self, lock_type: LockType) -> &'static str {
369 match lock_type {
370 LockType::Update => "FOR UPDATE",
371 LockType::NoKeyUpdate => "FOR NO KEY UPDATE",
372 LockType::Share => "LOCK IN SHARE MODE",
373 LockType::KeyShare => "FOR KEY SHARE",
374 }
375 }
376});