@@ -32,8 +32,8 @@ use crate::{
3232use super :: {
3333 display_comma_separated, helpers:: attached_token:: AttachedToken , query:: InputFormatClause ,
3434 Assignment , Expr , FromTable , Ident , InsertAliases , MysqlInsertPriority , ObjectName , OnInsert ,
35- OrderByExpr , Query , SelectInto , SelectItem , Setting , SqliteOnConflict , TableFactor ,
36- TableObject , TableWithJoins , UpdateTableFromKind , Values ,
35+ OptimizerHint , OrderByExpr , Query , SelectInto , SelectItem , Setting , SqliteOnConflict ,
36+ TableFactor , TableObject , TableWithJoins , UpdateTableFromKind , Values ,
3737} ;
3838
3939/// INSERT statement.
@@ -43,6 +43,11 @@ use super::{
4343pub struct Insert {
4444 /// Token for the `INSERT` keyword (or its substitutes)
4545 pub insert_token : AttachedToken ,
46+ /// A query optimizer hint
47+ ///
48+ /// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html)
49+ /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
50+ pub optimizer_hint : Option < OptimizerHint > ,
4651 /// Only for Sqlite
4752 pub or : Option < SqliteOnConflict > ,
4853 /// Only for mysql
@@ -102,7 +107,11 @@ impl Display for Insert {
102107 } ;
103108
104109 if let Some ( on_conflict) = self . or {
105- write ! ( f, "INSERT {on_conflict} INTO {table_name} " ) ?;
110+ f. write_str ( "INSERT" ) ?;
111+ if let Some ( hint) = self . optimizer_hint . as_ref ( ) {
112+ write ! ( f, " {hint}" ) ?;
113+ }
114+ write ! ( f, " {on_conflict} INTO {table_name} " ) ?;
106115 } else {
107116 write ! (
108117 f,
@@ -111,8 +120,11 @@ impl Display for Insert {
111120 "REPLACE"
112121 } else {
113122 "INSERT"
114- } ,
123+ }
115124 ) ?;
125+ if let Some ( hint) = self . optimizer_hint . as_ref ( ) {
126+ write ! ( f, " {hint}" ) ?;
127+ }
116128 if let Some ( priority) = self . priority {
117129 write ! ( f, " {priority}" , ) ?;
118130 }
@@ -188,6 +200,11 @@ impl Display for Insert {
188200pub struct Delete {
189201 /// Token for the `DELETE` keyword
190202 pub delete_token : AttachedToken ,
203+ /// A query optimizer hint
204+ ///
205+ /// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html)
206+ /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
207+ pub optimizer_hint : Option < OptimizerHint > ,
191208 /// Multi tables delete are supported in mysql
192209 pub tables : Vec < ObjectName > ,
193210 /// FROM
@@ -207,6 +224,10 @@ pub struct Delete {
207224impl Display for Delete {
208225 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
209226 f. write_str ( "DELETE" ) ?;
227+ if let Some ( hint) = self . optimizer_hint . as_ref ( ) {
228+ f. write_str ( " " ) ?;
229+ hint. fmt ( f) ?;
230+ }
210231 if !self . tables . is_empty ( ) {
211232 indented_list ( f, & self . tables ) ?;
212233 }
@@ -257,6 +278,11 @@ impl Display for Delete {
257278pub struct Update {
258279 /// Token for the `UPDATE` keyword
259280 pub update_token : AttachedToken ,
281+ /// A query optimizer hint
282+ ///
283+ /// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html)
284+ /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
285+ pub optimizer_hint : Option < OptimizerHint > ,
260286 /// TABLE
261287 pub table : TableWithJoins ,
262288 /// Column assignments
@@ -276,6 +302,10 @@ pub struct Update {
276302impl Display for Update {
277303 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
278304 f. write_str ( "UPDATE " ) ?;
305+ if let Some ( hint) = self . optimizer_hint . as_ref ( ) {
306+ hint. fmt ( f) ?;
307+ f. write_str ( " " ) ?;
308+ }
279309 if let Some ( or) = & self . or {
280310 or. fmt ( f) ?;
281311 f. write_str ( " " ) ?;
@@ -322,6 +352,10 @@ impl Display for Update {
322352pub struct Merge {
323353 /// The `MERGE` token that starts the statement.
324354 pub merge_token : AttachedToken ,
355+ /// A query optimizer hint
356+ ///
357+ /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
358+ pub optimizer_hint : Option < OptimizerHint > ,
325359 /// optional INTO keyword
326360 pub into : bool ,
327361 /// Specifies the table to merge
@@ -338,12 +372,18 @@ pub struct Merge {
338372
339373impl Display for Merge {
340374 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
375+ f. write_str ( "MERGE" ) ?;
376+ if let Some ( hint) = self . optimizer_hint . as_ref ( ) {
377+ write ! ( f, " {hint}" ) ?;
378+ }
379+ if self . into {
380+ write ! ( f, " INTO" ) ?;
381+ }
341382 write ! (
342383 f,
343- "MERGE{int} {table} USING {source} " ,
344- int = if self . into { " INTO" } else { "" } ,
384+ " {table} USING {source} " ,
345385 table = self . table,
346- source = self . source,
386+ source = self . source
347387 ) ?;
348388 write ! ( f, "ON {on} " , on = self . on) ?;
349389 write ! ( f, "{}" , display_separated( & self . clauses, " " ) ) ?;
0 commit comments