This commit is contained in:
etgarperets 2025-08-21 18:21:12 +03:00 committed by GitHub
commit 14df49f7e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 61 additions and 2 deletions

View file

@ -476,6 +476,12 @@ pub trait Dialect: Debug + Any {
false
}
// Does the Dialect support concatenating of string literal
// Example: SELECT 'Hello ' "world" => SELECT 'Hello world'
fn supports_concat_quoted_identifiers(&self) -> bool {
false
}
/// Does the dialect support trailing commas in the projection list?
fn supports_projection_trailing_commas(&self) -> bool {
self.supports_trailing_commas()

View file

@ -71,6 +71,11 @@ impl Dialect for MySqlDialect {
true
}
// see <https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#:~:text=mysql%3E%20SELECT%20%27My%27%20%27S%27%20%27QL%27%3B%0A%20%20%20%20%20%20%20%20%2D%3E%20%27MySQL%27>
fn supports_concat_quoted_identifiers(&self) -> bool {
true
}
fn ignores_wildcard_escapes(&self) -> bool {
true
}

View file

@ -9765,8 +9765,18 @@ impl<'a> Parser<'a> {
// bigdecimal feature is enabled, and is otherwise a no-op
// (i.e., it returns the input string).
Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
Token::SingleQuotedString(ref s) => {
if self.dialect.supports_concat_quoted_identifiers() {
return ok_value(Value::SingleQuotedString(self.combine_quoted(next_token)));
}
ok_value(Value::SingleQuotedString(s.to_string()))
}
Token::DoubleQuotedString(ref s) => {
if self.dialect.supports_concat_quoted_identifiers() {
return ok_value(Value::DoubleQuotedString(self.combine_quoted(next_token)));
}
ok_value(Value::DoubleQuotedString(s.to_string()))
}
Token::TripleSingleQuotedString(ref s) => {
ok_value(Value::TripleSingleQuotedString(s.to_string()))
}
@ -9836,6 +9846,35 @@ impl<'a> Parser<'a> {
}
}
fn is_quoted_string(&self, token: &Token) -> bool {
matches!(
token,
Token::SingleQuotedString(_) | Token::DoubleQuotedString(_)
)
}
fn get_quoted_string(&self, token: &Token) -> String {
match token {
Token::SingleQuotedString(s) => s.clone(),
Token::DoubleQuotedString(s) => s.clone(),
_ => String::new(),
}
}
fn combine_quoted(&mut self, token: TokenWithSpan) -> String {
let mut combined_string = self.get_quoted_string(&token.token);
loop {
let next_token = self.next_token();
if !self.is_quoted_string(&next_token.token) {
self.prev_token();
break;
}
let s = self.get_quoted_string(&next_token.token);
combined_string.push_str(&s);
}
combined_string
}
/// Parse an unsigned numeric literal
pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
let value_wrapper = self.parse_value()?;

View file

@ -4247,3 +4247,12 @@ fn test_create_index_options() {
"CREATE INDEX idx_name ON t(c1, c2) USING BTREE LOCK = EXCLUSIVE ALGORITHM = DEFAULT",
);
}
#[test]
fn parse_adjacent_string_literal_concatenation() {
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
mysql().one_statement_parses_to(sql, r"SELECT 'MySql'");
let sql = "SELECT * FROM t WHERE col = 'Hello' \n ' ' \t 'World!'";
mysql().one_statement_parses_to(sql, r"SELECT * FROM t WHERE col = 'Hello World!'");
}