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
impl Dialect
Sourcepub fn get(dialect_type: DialectType) -> Self
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.
Sourcepub fn get_by_name(name: &str) -> Option<Self>
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.
Sourcepub fn dialect_type(&self) -> DialectType
pub fn dialect_type(&self) -> DialectType
Get the dialect type
Sourcepub fn generator_config(&self) -> &GeneratorConfig
pub fn generator_config(&self) -> &GeneratorConfig
Get the generator configuration
Sourcepub fn parse(&self, sql: &str) -> Result<Vec<Expression>>
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.
Sourcepub fn generate(&self, expr: &Expression) -> Result<String>
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.
Sourcepub fn generate_pretty(&self, expr: &Expression) -> Result<String>
pub fn generate_pretty(&self, expr: &Expression) -> Result<String>
Generate SQL from an expression with pretty printing enabled
Sourcepub fn generate_with_identify(&self, expr: &Expression) -> Result<String>
pub fn generate_with_identify(&self, expr: &Expression) -> Result<String>
Generate SQL from an expression with forced identifier quoting (identify=True)
Sourcepub fn generate_pretty_with_identify(&self, expr: &Expression) -> Result<String>
pub fn generate_pretty_with_identify(&self, expr: &Expression) -> Result<String>
Generate SQL from an expression with pretty printing and forced identifier quoting
Sourcepub fn transform(&self, expr: Expression) -> Result<Expression>
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:
- Preprocessing – whole-tree structural rewrites such as eliminating QUALIFY, ensuring boolean predicates, or converting DISTINCT ON to a window-function pattern.
- Recursive per-node transform – a bottom-up pass via
transform_recursivethat applies this dialect’sDialectImpl::transform_exprto 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).
Sourcepub fn transpile_to(
&self,
sql: &str,
target: DialectType,
) -> Result<Vec<String>>
pub fn transpile_to( &self, sql: &str, target: DialectType, ) -> Result<Vec<String>>
Transpile SQL from this dialect to another
Sourcepub fn transpile_to_pretty(
&self,
sql: &str,
target: DialectType,
) -> Result<Vec<String>>
pub fn transpile_to_pretty( &self, sql: &str, target: DialectType, ) -> Result<Vec<String>>
Transpile SQL from this dialect to another with pretty printing enabled