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
impl Generator
Sourcepub fn new() -> Self
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.
Sourcepub fn with_config(config: GeneratorConfig) -> Self
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.
Sourcepub fn generate(&mut self, expr: &Expression) -> Result<String>
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.
Sourcepub fn sql(expr: &Expression) -> Result<String>
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.
Sourcepub fn pretty_sql(expr: &Expression) -> Result<String>
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.