Skip to main content

Serializer

Struct Serializer 

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

Writes a syntax tree as SQL, quoting identifiers the way one dialect does: backticks (MySQL, MariaDB, ClickHouse, and the base serializer) or double quotes (PostgreSQL, SQLite), with the quote doubled inside.

PHP: Utopia\Query\AST\Serializer and its dialect subclasses.

use utopia_query::ast::{Parser, Serializer};

let select = Parser::new().parse_sql("select id, name from users where id = 1")?;
assert_eq!(Serializer::new().serialize(&select)?, "SELECT `id`, `name` FROM `users` WHERE `id` = 1");
assert_eq!(Serializer::postgresql().serialize(&select)?, r#"SELECT "id", "name" FROM "users" WHERE "id" = 1"#);
use Utopia\Query\AST\Parser;
use Utopia\Query\AST\Serializer;
use Utopia\Query\Tokenizer\Tokenizer;

$select = (new Parser())->parse(Tokenizer::filter((new Tokenizer())->tokenize('select id, name from users where id = 1')));
echo (new Serializer())->serialize($select), "\n"; // SELECT `id`, `name` FROM `users` WHERE `id` = 1

Implementations§

Source§

impl Serializer

Source

pub const fn new() -> Self

The base serializer: identifiers in backticks.

PHP: new Serializer().

Source

pub const fn mysql() -> Self

MySQL: identifiers in backticks.

PHP: new Serializer\MySQL().

Source

pub const fn mariadb() -> Self

MariaDB: identifiers in backticks.

PHP: new Serializer\MariaDB().

Source

pub const fn clickhouse() -> Self

ClickHouse: identifiers in backticks.

PHP: new Serializer\ClickHouse().

Source

pub const fn postgresql() -> Self

PostgreSQL: identifiers in double quotes.

PHP: new Serializer\PostgreSQL().

Source

pub const fn sqlite() -> Self

SQLite: identifiers in double quotes.

PHP: new Serializer\SQLite().

Source

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

The serializer writing string literals by literal’s rules.

PHP: new Serializer($literal).

Source

pub const fn with_quote(quote: char) -> Self

A serializer quoting identifiers with quote (doubled inside them).

PHP: a subclass overriding quoteIdentifier().

Source

pub fn serialize(&self, statement: &Select) -> Result<String>

statement as SQL. Fails with Error::Query on a node no SQL position takes (a CaseWhen outside a CASE).

PHP: Serializer::serialize().

Source

pub fn serialize_expression(&self, expression: &Expression) -> Result<String>

expression as SQL.

PHP: Serializer::serializeExpression().

Source

pub fn quote_identifier(&self, name: &str) -> String

name quoted as an identifier.

PHP: Serializer::quoteIdentifier() (protected).

Trait Implementations§

Source§

impl AstWriter for Serializer

Source§

fn serialize(&self, statement: &Select) -> Result<String>

A statement as SQL.
Source§

fn serialize_expression(&self, expression: &Expression) -> Result<String>

An expression as SQL.
Source§

impl Clone for Serializer

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 Serializer

Source§

impl Debug for Serializer

Source§

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

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

impl Default for Serializer

Source§

impl Eq for Serializer

Source§

impl PartialEq for Serializer

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 Serializer

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.