pub struct Tokenizer<'a> { /* private fields */ }sql only.Expand description
SQL Tokenizer
Implementations§
Source§impl<'a> Tokenizer<'a>
impl<'a> Tokenizer<'a>
Sourcepub fn new(
dialect: &'a (dyn Dialect + 'static),
query: &'a str,
) -> Tokenizer<'a>
pub fn new( dialect: &'a (dyn Dialect + 'static), query: &'a str, ) -> Tokenizer<'a>
Create a new SQL tokenizer for the specified SQL statement
let query = r#"SELECT 'foo'"#;
// Parsing the query
let tokens = Tokenizer::new(&dialect, &query).tokenize().unwrap();
assert_eq!(tokens, vec![
Token::make_word("SELECT", None),
Token::Whitespace(Whitespace::Space),
Token::SingleQuotedString("foo".to_string()),
]);Sourcepub fn with_unescape(self, unescape: bool) -> Tokenizer<'a>
pub fn with_unescape(self, unescape: bool) -> Tokenizer<'a>
Set unescape mode
When true (default) the tokenizer unescapes literal values
(for example, "" in SQL is unescaped to the literal ").
When false, the tokenizer provides the raw strings as provided in the query. This can be helpful for programs that wish to recover the exact original query text without normalizing the escaping
§Example
let query = r#""Foo "" Bar""#;
let unescaped = Token::make_word(r#"Foo " Bar"#, Some('"'));
let original = Token::make_word(r#"Foo "" Bar"#, Some('"'));
// Parsing with unescaping (default)
let tokens = Tokenizer::new(&dialect, &query).tokenize().unwrap();
assert_eq!(tokens, vec![unescaped]);
// Parsing with unescape = false
let tokens = Tokenizer::new(&dialect, &query)
.with_unescape(false)
.tokenize().unwrap();
assert_eq!(tokens, vec![original]);Sourcepub fn tokenize(&mut self) -> Result<Vec<Token>, TokenizerError>
pub fn tokenize(&mut self) -> Result<Vec<Token>, TokenizerError>
Tokenize the statement and produce a vector of tokens
Sourcepub fn tokenize_with_location(
&mut self,
) -> Result<Vec<TokenWithSpan>, TokenizerError>
pub fn tokenize_with_location( &mut self, ) -> Result<Vec<TokenWithSpan>, TokenizerError>
Tokenize the statement and produce a vector of tokens with location information
Sourcepub fn tokenize_with_location_into_buf(
&mut self,
buf: &mut Vec<TokenWithSpan>,
) -> Result<(), TokenizerError>
pub fn tokenize_with_location_into_buf( &mut self, buf: &mut Vec<TokenWithSpan>, ) -> Result<(), TokenizerError>
Tokenize the statement and append tokens with location information into the provided buffer. If an error is thrown, the buffer will contain all tokens that were successfully parsed before the error.
Sourcepub fn tokenize_with_location_into_buf_with_mapper(
&mut self,
buf: &mut Vec<TokenWithSpan>,
mapper: impl FnMut(TokenWithSpan) -> TokenWithSpan,
) -> Result<(), TokenizerError>
pub fn tokenize_with_location_into_buf_with_mapper( &mut self, buf: &mut Vec<TokenWithSpan>, mapper: impl FnMut(TokenWithSpan) -> TokenWithSpan, ) -> Result<(), TokenizerError>
Tokenize the statement and produce a vector of tokens, mapping each token
with provided mapper
Auto Trait Implementations§
impl<'a> Freeze for Tokenizer<'a>
impl<'a> !RefUnwindSafe for Tokenizer<'a>
impl<'a> !Send for Tokenizer<'a>
impl<'a> !Sync for Tokenizer<'a>
impl<'a> Unpin for Tokenizer<'a>
impl<'a> UnsafeUnpin for Tokenizer<'a>
impl<'a> !UnwindSafe for Tokenizer<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more