Skip to main content

Dialect

Struct Dialect 

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

Main entry point for dialect-specific SQL operations.

A Dialect bundles together a tokenizer, generator configuration, and expression transformer for a specific SQL database engine. It is the high-level API through which callers parse, generate, transform, and transpile SQL.

§Usage

use polyglot_sql::dialects::{Dialect, DialectType};

// Parse PostgreSQL SQL into an AST
let pg = Dialect::get(DialectType::PostgreSQL);
let exprs = pg.parse("SELECT id, name FROM users WHERE active")?;

// Transpile from PostgreSQL to BigQuery
let results = pg.transpile_to("SELECT NOW()", DialectType::BigQuery)?;
assert_eq!(results[0], "SELECT CURRENT_TIMESTAMP()");

Obtain an instance via Dialect::get or Dialect::get_by_name. The struct is Send + Sync safe so it can be shared across threads.

Implementations§

Source§

impl Dialect

Source

pub fn get(dialect_type: DialectType) -> Self

Creates a fully configured Dialect instance for the given DialectType.

This is the primary constructor. It initializes the tokenizer, generator config, and expression transformer based on the dialect’s DialectImpl implementation. For hybrid dialects like Athena, it also sets up expression-specific generator config routing.

Source

pub fn get_by_name(name: &str) -> Option<Self>

Look up a dialect by string name.

Checks built-in dialect names first (via DialectType::from_str), then falls back to the custom dialect registry. Returns None if no dialect with the given name exists.

Source

pub fn dialect_type(&self) -> DialectType

Get the dialect type

Source

pub fn generator_config(&self) -> &GeneratorConfig

Get the generator configuration

Source

pub fn parse(&self, sql: &str) -> Result<Vec<Expression>>

Parses a SQL string into a list of Expression AST nodes.

The input may contain multiple semicolon-separated statements; each one produces a separate element in the returned vector. Tokenization uses this dialect’s configured tokenizer, and parsing uses the dialect-aware parser.

Source

pub fn generate(&self, expr: &Expression) -> Result<String>

Generates a SQL string from an Expression AST node.

The output uses this dialect’s generator configuration for identifier quoting, keyword casing, function name normalization, and syntax style. The result is a single-line (non-pretty) SQL string.

Source

pub fn generate_pretty(&self, expr: &Expression) -> Result<String>

Generate SQL from an expression with pretty printing enabled

Source

pub fn generate_with_identify(&self, expr: &Expression) -> Result<String>

Generate SQL from an expression with forced identifier quoting (identify=True)

Source

pub fn generate_pretty_with_identify(&self, expr: &Expression) -> Result<String>

Generate SQL from an expression with pretty printing and forced identifier quoting

Source

pub fn transform(&self, expr: Expression) -> Result<Expression>

Transforms an expression tree to conform to this dialect’s syntax and semantics.

The transformation proceeds in two phases:

  1. Preprocessing – whole-tree structural rewrites such as eliminating QUALIFY, ensuring boolean predicates, or converting DISTINCT ON to a window-function pattern.
  2. Recursive per-node transform – a bottom-up pass via transform_recursive that applies this dialect’s DialectImpl::transform_expr to every node.

This method is used both during transpilation (to rewrite an AST for a target dialect) and for identity transforms (normalizing SQL within the same dialect).

Source

pub fn transpile_to( &self, sql: &str, target: DialectType, ) -> Result<Vec<String>>

Transpile SQL from this dialect to another

Source

pub fn transpile_to_pretty( &self, sql: &str, target: DialectType, ) -> Result<Vec<String>>

Transpile SQL from this dialect to another with pretty printing enabled

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.