Make TypedString preserve quote style (#1679)

This commit is contained in:
Paul Grau 2025-01-31 08:04:41 +02:00 committed by GitHub
parent aeaafbe6e4
commit 447142c6d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 132 additions and 55 deletions

View file

@ -898,6 +898,8 @@ pub enum Expr {
/// <https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html>
IntroducedString {
introducer: String,
/// The value of the constant.
/// Hint: you can unwrap the string value using `value.into_string()`.
value: Value,
},
/// A constant of form `<data_type> 'value'`.
@ -905,7 +907,9 @@ pub enum Expr {
/// as well as constants of other types (a non-standard PostgreSQL extension).
TypedString {
data_type: DataType,
value: String,
/// The value of the constant.
/// Hint: you can unwrap the string value using `value.into_string()`.
value: Value,
},
/// Scalar function call e.g. `LEFT(foo, 5)`
Function(Function),
@ -1622,7 +1626,7 @@ impl fmt::Display for Expr {
Expr::IntroducedString { introducer, value } => write!(f, "{introducer} {value}"),
Expr::TypedString { data_type, value } => {
write!(f, "{data_type}")?;
write!(f, " '{}'", &value::escape_single_quote_string(value))
write!(f, " {value}")
}
Expr::Function(fun) => write!(f, "{fun}"),
Expr::Method(method) => write!(f, "{method}"),

View file

@ -1266,7 +1266,7 @@ impl Spanned for AssignmentTarget {
/// f.e. `IS NULL <expr>` reports as `<expr>::span`.
///
/// Missing spans:
/// - [Expr::TypedString]
/// - [Expr::TypedString] # missing span for data_type
/// - [Expr::MatchAgainst] # MySQL specific
/// - [Expr::RLike] # MySQL specific
/// - [Expr::Struct] # BigQuery specific
@ -1362,7 +1362,7 @@ impl Spanned for Expr {
.union(&union_spans(collation.0.iter().map(|i| i.span()))),
Expr::Nested(expr) => expr.span(),
Expr::Value(value) => value.span(),
Expr::TypedString { .. } => Span::empty(),
Expr::TypedString { value, .. } => value.span(),
Expr::Function(function) => function.span(),
Expr::GroupingSets(vec) => {
union_spans(vec.iter().flat_map(|i| i.iter().map(|k| k.span())))

View file

@ -97,6 +97,32 @@ pub enum Value {
Placeholder(String),
}
impl Value {
/// If the underlying literal is a string, regardless of quote style, returns the associated string value
pub fn into_string(self) -> Option<String> {
match self {
Value::SingleQuotedString(s)
| Value::DoubleQuotedString(s)
| Value::TripleSingleQuotedString(s)
| Value::TripleDoubleQuotedString(s)
| Value::SingleQuotedByteStringLiteral(s)
| Value::DoubleQuotedByteStringLiteral(s)
| Value::TripleSingleQuotedByteStringLiteral(s)
| Value::TripleDoubleQuotedByteStringLiteral(s)
| Value::SingleQuotedRawStringLiteral(s)
| Value::DoubleQuotedRawStringLiteral(s)
| Value::TripleSingleQuotedRawStringLiteral(s)
| Value::TripleDoubleQuotedRawStringLiteral(s)
| Value::EscapedStringLiteral(s)
| Value::UnicodeStringLiteral(s)
| Value::NationalStringLiteral(s)
| Value::HexStringLiteral(s) => Some(s),
Value::DollarQuotedString(s) => Some(s.value),
_ => None,
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {

View file

@ -1316,7 +1316,7 @@ impl<'a> Parser<'a> {
DataType::Custom(..) => parser_err!("dummy", loc),
data_type => Ok(Expr::TypedString {
data_type,
value: parser.parse_literal_string()?,
value: parser.parse_value()?,
}),
}
})?;