Skip to content

Commit f80afd0

Browse files
committed
Guard optimizer hints parsing by an explicit dialect method
1 parent f261c89 commit f80afd0

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
@@ -271,4 +271,8 @@ impl Dialect for GenericDialect {
271271
fn supports_select_format(&self) -> bool {
272272
true
273273
}
274+
275+
fn supports_comment_optimizer_hint(&self) -> bool {
276+
true
277+
}
274278
}

src/dialect/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,16 @@ pub trait Dialect: Debug + Any {
13221322
false
13231323
}
13241324

1325+
/// Returns `true` if the dialect supports query optimizer hints in the
1326+
/// format of single and multi line comments immediately following a
1327+
/// `SELECT`, `INSERT`, `REPLACE`, `DELETE`, or `MERGE` keyword.
1328+
///
1329+
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html)
1330+
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Comments.html#SQLRF-GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
1331+
fn supports_comment_optimizer_hint(&self) -> bool {
1332+
false
1333+
}
1334+
13251335
/// Returns true if the dialect considers the `&&` operator as a boolean AND operator.
13261336
fn supports_double_ampersand_operator(&self) -> bool {
13271337
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
@@ -14061,39 +14061,32 @@ impl<'a> Parser<'a> {
1406114061
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html#optimizer-hints-overview)
1406214062
/// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Comments.html#GUID-D316D545-89E2-4D54-977F-FC97815CD62E)
1406314063
fn maybe_parse_optimizer_hint(&mut self) -> Result<Option<OptimizerHint>, ParserError> {
14064-
let supports_multiline = dialect_of!(self is MySqlDialect | OracleDialect | GenericDialect);
14065-
let supports_singleline = dialect_of!(self is OracleDialect | GenericDialect);
14066-
if !supports_multiline && !supports_singleline {
14064+
let supports_hints = self.dialect.supports_comment_optimizer_hint();
14065+
if !supports_hints {
1406714066
return Ok(None);
1406814067
}
1406914068
loop {
1407014069
let t = self.peek_nth_token_no_skip_ref(0);
1407114070
match &t.token {
1407214071
Token::Whitespace(ws) => {
1407314072
match ws {
14074-
Whitespace::SingleLineComment { comment, prefix } => {
14075-
return Ok(if supports_singleline && comment.starts_with("+") {
14076-
let text = comment.split_at(1).1.into();
14077-
let prefix = prefix.clone();
14078-
self.next_token_no_skip(); // Consume the comment token
14079-
Some(OptimizerHint {
14080-
text,
14081-
style: OptimizerHintStyle::SingleLine { prefix },
14082-
})
14083-
} else {
14084-
None
14085-
});
14086-
}
14087-
Whitespace::MultiLineComment(comment) => {
14088-
return Ok(if supports_multiline && comment.starts_with("+") {
14089-
let text = comment.split_at(1).1.into();
14090-
self.next_token_no_skip(); // Consume the comment token
14091-
Some(OptimizerHint {
14092-
text,
14093-
style: OptimizerHintStyle::MultiLine,
14094-
})
14095-
} else {
14096-
None
14073+
Whitespace::SingleLineComment { comment, .. }
14074+
| Whitespace::MultiLineComment(comment) => {
14075+
return Ok(match comment.strip_prefix("+") {
14076+
None => None,
14077+
Some(text) => {
14078+
let hint = OptimizerHint {
14079+
text: text.into(),
14080+
style: if let Whitespace::SingleLineComment { prefix, .. } = ws {
14081+
OptimizerHintStyle::SingleLine { prefix: prefix.clone() }
14082+
} else {
14083+
OptimizerHintStyle::MultiLine
14084+
}
14085+
};
14086+
// Consume the comment token
14087+
self.next_token_no_skip();
14088+
Some(hint)
14089+
}
1409714090
});
1409814091
}
1409914092
Whitespace::Space | Whitespace::Tab | Whitespace::Newline => {

0 commit comments

Comments
 (0)