-
Notifications
You must be signed in to change notification settings - Fork 606
Replace type_id()
by trait method to allow wrapping dialects
#1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,15 @@ macro_rules! dialect_of { | |
/// | ||
/// [module level documentation]: crate | ||
pub trait Dialect: Debug + Any { | ||
/// Determine the [`TypeId`] of this dialect. | ||
/// | ||
/// By default, return the same [`TypeId`] as [`Any::type_id`]. Can be overriden | ||
/// by dialects that behave like other dialects | ||
/// (for example when wrapping a dialect). | ||
fn dialect(&self) -> TypeId { | ||
self.type_id() | ||
} | ||
|
||
/// Determine if a character starts a quoted identifier. The default | ||
/// implementation, accepting "double quoted" ids is both ANSI-compliant | ||
/// and appropriate for most dialects (with the notable exception of | ||
|
@@ -164,7 +173,7 @@ impl dyn Dialect { | |
#[inline] | ||
pub fn is<T: Dialect>(&self) -> bool { | ||
// borrowed from `Any` implementation | ||
TypeId::of::<T>() == self.type_id() | ||
TypeId::of::<T>() == self.dialect() | ||
} | ||
} | ||
|
||
|
@@ -248,4 +257,98 @@ mod tests { | |
fn parse_dialect(v: &str) -> Box<dyn Dialect> { | ||
dialect_from_str(v).unwrap() | ||
} | ||
|
||
#[test] | ||
fn parse_with_wrapped_dialect() { | ||
/// Wrapper for a dialect. In a real-world example, this wrapper | ||
/// would tweak the behavior of the dialect. For the test case, | ||
/// it wraps all methods unaltered. | ||
#[derive(Debug)] | ||
struct WrappedDialect(MySqlDialect); | ||
|
||
impl Dialect for WrappedDialect { | ||
fn dialect(&self) -> std::any::TypeId { | ||
self.0.dialect() | ||
} | ||
|
||
fn is_identifier_start(&self, ch: char) -> bool { | ||
self.0.is_identifier_start(ch) | ||
} | ||
|
||
fn is_delimited_identifier_start(&self, ch: char) -> bool { | ||
self.0.is_delimited_identifier_start(ch) | ||
} | ||
|
||
fn is_proper_identifier_inside_quotes( | ||
&self, | ||
chars: std::iter::Peekable<std::str::Chars<'_>>, | ||
) -> bool { | ||
self.0.is_proper_identifier_inside_quotes(chars) | ||
} | ||
|
||
fn supports_filter_during_aggregation(&self) -> bool { | ||
self.0.supports_filter_during_aggregation() | ||
} | ||
|
||
fn supports_within_after_array_aggregation(&self) -> bool { | ||
self.0.supports_within_after_array_aggregation() | ||
} | ||
|
||
fn supports_group_by_expr(&self) -> bool { | ||
self.0.supports_group_by_expr() | ||
} | ||
|
||
fn supports_substring_from_for_expr(&self) -> bool { | ||
self.0.supports_substring_from_for_expr() | ||
} | ||
|
||
fn supports_in_empty_list(&self) -> bool { | ||
self.0.supports_in_empty_list() | ||
} | ||
|
||
fn convert_type_before_value(&self) -> bool { | ||
self.0.convert_type_before_value() | ||
} | ||
|
||
fn parse_prefix( | ||
&self, | ||
parser: &mut sqlparser::parser::Parser, | ||
) -> Option<Result<Expr, sqlparser::parser::ParserError>> { | ||
self.0.parse_prefix(parser) | ||
} | ||
|
||
fn parse_infix( | ||
&self, | ||
parser: &mut sqlparser::parser::Parser, | ||
expr: &Expr, | ||
precedence: u8, | ||
) -> Option<Result<Expr, sqlparser::parser::ParserError>> { | ||
self.0.parse_infix(parser, expr, precedence) | ||
} | ||
|
||
fn get_next_precedence( | ||
&self, | ||
parser: &sqlparser::parser::Parser, | ||
) -> Option<Result<u8, sqlparser::parser::ParserError>> { | ||
self.0.get_next_precedence(parser) | ||
} | ||
|
||
fn parse_statement( | ||
&self, | ||
parser: &mut sqlparser::parser::Parser, | ||
) -> Option<Result<Statement, sqlparser::parser::ParserError>> { | ||
self.0.parse_statement(parser) | ||
} | ||
|
||
fn is_identifier_part(&self, ch: char) -> bool { | ||
self.0.is_identifier_part(ch) | ||
} | ||
} | ||
|
||
let statement = r#"SELECT 'Wayne\'s World'"#; | ||
let res1 = Parser::parse_sql(&MySqlDialect {}, statement); | ||
let res2 = Parser::parse_sql(&WrappedDialect(MySqlDialect {}), statement); | ||
assert!(res1.is_ok()); | ||
assert_eq!(res1, res2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without overriding
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👨🍳 👌 -- very nice example of creating a test illustrating the use case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!