Skip to content

Commit 20b7074

Browse files
committed
Guard optimizer hints parsing by an explicit dialect method
1 parent 4986503 commit 20b7074

5 files changed

Lines changed: 41 additions & 26 deletions

File tree

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,8 @@ impl Dialect for GenericDialect {
207207
fn supports_lambda_functions(&self) -> bool {
208208
true
209209
}
210+
211+
fn supports_comment_optimizer_hint(&self) -> bool {
212+
true
213+
}
210214
}

src/dialect/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,16 @@ pub trait Dialect: Debug + Any {
12411241
false
12421242
}
12431243

1244+
/// Returns `true` if the dialect supports query optimizer hints in the
1245+
/// format of single and multi line comments immediately following a
1246+
/// `SELECT`, `INSERT`, `REPLACE`, `DELETE`, or `MERGE` keyword.
1247+
///
1248+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html)
1249+
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Comments.html#SQLRF-GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
1250+
fn supports_comment_optimizer_hint(&self) -> bool {
1251+
false
1252+
}
1253+
12441254
/// Returns true if the dialect considers the `&&` operator as a boolean AND operator.
12451255
fn supports_double_ampersand_operator(&self) -> bool {
12461256
false

src/dialect/mysql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ impl Dialect for MySqlDialect {
182182
fn supports_binary_kw_as_cast(&self) -> bool {
183183
true
184184
}
185+
186+
fn supports_comment_optimizer_hint(&self) -> bool {
187+
true
188+
}
185189
}
186190

187191
/// `LOCK TABLES`

src/dialect/oracle.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,8 @@ impl Dialect for OracleDialect {
9999
fn supports_quote_delimited_string(&self) -> bool {
100100
true
101101
}
102+
103+
fn supports_comment_optimizer_hint(&self) -> bool {
104+
true
105+
}
102106
}

src/parser/mod.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14020,39 +14020,32 @@ impl<'a> Parser<'a> {
1402014020
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html#optimizer-hints-overview)
1402114021
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
1402214022
fn maybe_parse_optimizer_hint(&mut self) -> Result<Option<OptimizerHint>, ParserError> {
14023-
let supports_multiline = dialect_of!(self is MySqlDialect | OracleDialect | GenericDialect);
14024-
let supports_singleline = dialect_of!(self is OracleDialect | GenericDialect);
14025-
if !supports_multiline && !supports_singleline {
14023+
let supports_hints = self.dialect.supports_comment_optimizer_hint();
14024+
if !supports_hints {
1402614025
return Ok(None);
1402714026
}
1402814027
loop {
1402914028
let t = self.peek_nth_token_no_skip_ref(0);
1403014029
match &t.token {
1403114030
Token::Whitespace(ws) => {
1403214031
match ws {
14033-
Whitespace::SingleLineComment { comment, prefix } => {
14034-
return Ok(if supports_singleline && comment.starts_with("+") {
14035-
let text = comment.split_at(1).1.into();
14036-
let prefix = prefix.clone();
14037-
self.next_token_no_skip(); // Consume the comment token
14038-
Some(OptimizerHint {
14039-
text,
14040-
style: OptimizerHintStyle::SingleLine { prefix },
14041-
})
14042-
} else {
14043-
None
14044-
});
14045-
}
14046-
Whitespace::MultiLineComment(comment) => {
14047-
return Ok(if supports_multiline && comment.starts_with("+") {
14048-
let text = comment.split_at(1).1.into();
14049-
self.next_token_no_skip(); // Consume the comment token
14050-
Some(OptimizerHint {
14051-
text,
14052-
style: OptimizerHintStyle::MultiLine,
14053-
})
14054-
} else {
14055-
None
14032+
Whitespace::SingleLineComment { comment, .. }
14033+
| Whitespace::MultiLineComment(comment) => {
14034+
return Ok(match comment.strip_prefix("+") {
14035+
None => None,
14036+
Some(text) => {
14037+
let hint = OptimizerHint {
14038+
text: text.into(),
14039+
style: if let Whitespace::SingleLineComment { prefix, .. } = ws {
14040+
OptimizerHintStyle::SingleLine { prefix: prefix.clone() }
14041+
} else {
14042+
OptimizerHintStyle::MultiLine
14043+
}
14044+
};
14045+
// Consume the comment token
14046+
self.next_token_no_skip();
14047+
Some(hint)
14048+
}
1405614049
});
1405714050
}
1405814051
Whitespace::Space | Whitespace::Tab | Whitespace::Newline => {

0 commit comments

Comments
 (0)