Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,14 +898,18 @@ 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.as_str()`.
value: Value,
},
/// A constant of form `<data_type> 'value'`.
/// This can represent ANSI SQL `DATE`, `TIME`, and `TIMESTAMP` literals (such as `DATE '2020-01-01'`),
/// 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.as_str()`.
value: Value,
},
/// Scalar function call e.g. `LEFT(foo, 5)`
Function(Function),
Expand Down Expand Up @@ -1620,7 +1624,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}"),
Expand Down
4 changes: 2 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())))
Expand Down
31 changes: 31 additions & 0 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ pub enum Value {
Placeholder(String),
}

impl Value {
/// Get the string value of a `Value` wrapping a string.
/// This includes all quotation styles
/// `{Single,Double,TripleSingle,TripleDouble}{QuotedString,RawString,ByteString}`
/// as well as `EscapedStringLiteral`, `UnicodeStringLiteral`, `NationalStringLiteral`,
/// `HexStringLiteral`, `DollarQuotedString`.
/// This will panic if called on a non-string variant like `Value::Number`` or `Value::Null`.
pub fn as_str(self) -> String {
match self {
Value::SingleQuotedString(s) => s,
Value::DoubleQuotedString(s) => s,
Value::TripleSingleQuotedString(s) => s,
Value::TripleDoubleQuotedString(s) => s,
Value::SingleQuotedByteStringLiteral(s) => s,
Value::DoubleQuotedByteStringLiteral(s) => s,
Value::TripleSingleQuotedByteStringLiteral(s) => s,
Value::TripleDoubleQuotedByteStringLiteral(s) => s,
Value::SingleQuotedRawStringLiteral(s) => s,
Value::DoubleQuotedRawStringLiteral(s) => s,
Value::TripleSingleQuotedRawStringLiteral(s) => s,
Value::TripleDoubleQuotedRawStringLiteral(s) => s,
Value::EscapedStringLiteral(s) => s,
Value::UnicodeStringLiteral(s) => s,
Value::NationalStringLiteral(s) => s,
Value::HexStringLiteral(s) => s,
Value::DollarQuotedString(s) => s.value,
_ => panic!("not a string value"),
}
}
}

impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?,
}),
}
})?;
Expand Down
Loading