Skip to main content

Generator

Struct Generator 

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

SQL code generator that converts an AST (Expression) back into a SQL string.

The generator walks the expression tree and emits dialect-specific SQL text. It supports pretty-printing with configurable indentation, identifier quoting, keyword casing, function name normalization, and 30+ SQL dialect variants.

§Usage

use polyglot_sql::generator::Generator;
use polyglot_sql::parser::Parser;

let ast = Parser::parse_sql("SELECT 1")?;
// Quick one-shot generation (default config):
let sql = Generator::sql(&ast[0])?;

// Pretty-printed output:
let pretty = Generator::pretty_sql(&ast[0])?;

// Custom config (e.g. for a specific dialect):
let config = GeneratorConfig { pretty: true, ..GeneratorConfig::default() };
let mut gen = Generator::with_config(config);
let sql = gen.generate(&ast[0])?;

Implementations§

Source§

impl Generator

Source

pub fn new() -> Self

Create a new generator with the default configuration.

Equivalent to Generator::with_config(GeneratorConfig::default()). Uses uppercase keywords, double-quote identifier quoting, no pretty-printing, and no dialect-specific transformations.

Source

pub fn with_config(config: GeneratorConfig) -> Self

Create a generator with a custom GeneratorConfig.

Use this when you need dialect-specific output, pretty-printing, or other non-default settings.

Source

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

Generate a SQL string from an AST expression.

This is the primary generation method. It clears any previous internal state, walks the expression tree, and returns the resulting SQL text. The output respects the GeneratorConfig that was supplied at construction time.

The generator can be reused across multiple calls; each call to generate resets the internal buffer.

Source

pub fn sql(expr: &Expression) -> Result<String>

Convenience: generate SQL with the default configuration (no dialect, compact output).

This is a static helper that creates a throwaway Generator internally. For repeated generation, prefer constructing a Generator once and calling generate on it.

Source

pub fn pretty_sql(expr: &Expression) -> Result<String>

Convenience: generate SQL with pretty-printing enabled (indented, multi-line).

Produces human-readable output with newlines and indentation. A trailing semicolon is appended automatically if not already present.

Trait Implementations§

Source§

impl Default for Generator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.