Skip to main content

Tokenizer

Struct Tokenizer 

Source
pub struct Tokenizer { /* private fields */ }
Expand description

Splits SQL into Tokens, the way one dialect writes it.

Dialects differ in the identifier quote (a backtick, or a double quote for PostgreSQL and SQLite), in MySQL’s # line comments and in PostgreSQL’s multi-character operators (@>, <@, <=>, <->, <#>, ?|, ?&).

PHP: Utopia\Query\Tokenizer\Tokenizer and its dialect subclasses.

Implementations§

Source§

impl Tokenizer

Source

pub const fn new() -> Self

The standard tokenizer: identifiers quoted with backticks (or double quotes).

PHP: new Tokenizer\Tokenizer().

Source

pub const fn mysql() -> Self

MySQL: also reads # line comments.

PHP: new Tokenizer\MySQL().

Source

pub const fn mariadb() -> Self

MariaDB: as MySQL.

PHP: new Tokenizer\MariaDB().

Source

pub const fn postgresql() -> Self

PostgreSQL: identifiers quoted with double quotes, and its multi-character operators as one token.

PHP: new Tokenizer\PostgreSQL().

Source

pub const fn sqlite() -> Self

SQLite: a backslash is a character like any other.

PHP: new Tokenizer\SQLite().

Source

pub const fn clickhouse() -> Self

ClickHouse: as the standard tokenizer.

PHP: new Tokenizer\ClickHouse().

Source

pub const fn with_literal(self, literal: StringLiteral) -> Self

The tokenizer reading string literals by literal’s rules.

PHP: new Tokenizer($literal).

Source

pub fn tokenize<'a, S: AsRef<[u8]> + ?Sized>( &self, sql: &'a S, ) -> Result<Vec<Token<'a>>>

The tokens of sql, whitespace and comments included, ending with an TokenType::Eof token. Fails with Error::Validation on an unterminated string literal or quoted identifier.

PHP: Tokenizer::tokenize().

use utopia_query::tokenizer::{TokenType, Tokenizer};

let tokens = Tokenizer::mysql().tokenize("SELECT 1 #note")?;
assert_eq!(tokens[4].kind, TokenType::LineComment);
assert_eq!(tokens[4].text(), "-- note");
assert!(Tokenizer::new().tokenize("SELECT 'open").is_err());
use Utopia\Query\Tokenizer\MySQL;

$tokens = (new MySQL())->tokenize('SELECT 1 #note');
echo $tokens[4]->type->name, ' ', $tokens[4]->value, "\n"; // LineComment -- note
Source

pub fn filter(tokens: Vec<Token<'_>>) -> Vec<Token<'_>>

tokens without whitespace and comments.

PHP: Tokenizer::filter().

Trait Implementations§

Source§

impl Clone for Tokenizer

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Tokenizer

Source§

impl Debug for Tokenizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Tokenizer

Source§

impl Eq for Tokenizer

Source§

impl PartialEq for Tokenizer

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Tokenizer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.