class PgSqlLexer::Formatter

Overview

This class converts an array of Token objects produced by a Lexer into a formatted string. Currently the only method is to produce a 'minified' format, but at some point this may be expanded to other formats. Typical usage:

raw_sql = {slurp from a file maybe}
minified = PgSqlLexer::Formatter.new(PgSqlLexer::Lexer.new(raw_sql).tokens).format_minified
:

Defined in:

pg_sql_lexer/formatter.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(tokens : Array(Token)) #

[View source]

Instance Method Detail

def format_minified(include_comments = false) : String #

This method takes a collection of tokens and outputs a 'minified' version of the SQL.

The 'reconstituted' SQL is formatted on a single line (i.e. no newlines) and with no more than one space between tokens. Sometimes there is no space between tokens (e.g. if the previous token is a ().

For example (from test suite):

it "correctly minifies a statement excluding multi-line comments" do
  PgSqlLexer::Formatter.new(
    PgSqlLexer::Lexer
      .new("SELECT 1\n/*\n Some comment\n*/\nFROM\t\tsome_table\n;")
      .tokens)
    .format_minified.should eq("select 1 from some_table;")
end

[View source]