Module: Polyglot

Defined in:
lib/polyglot.rb,
lib/polyglot/version.rb,
lib/polyglot/validation_result.rb

Defined Under Namespace

Classes: ValidationError, ValidationResult

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.format(sql, dialect: :generic) ⇒ String

Format (pretty-print) SQL.

Examples:

Polyglot.format("SELECT a, b FROM t WHERE x = 1", dialect: :postgres)

Parameters:

  • sql (String)

    the SQL to format

  • dialect (String, Symbol) (defaults to: :generic)

    the SQL dialect (default: :generic)

Returns:

  • (String)

    the formatted SQL



85
86
87
# File 'lib/polyglot.rb', line 85

def format(sql, dialect: :generic)
  _format(sql, dialect.to_s)
end

.generate(ast, dialect: :generic) ⇒ String

Generate SQL from an AST expression.

Examples:

ast = Polyglot.parse_one("SELECT 1", dialect: :postgres)
Polyglot.generate(ast, dialect: :mysql)

Parameters:

  • ast (Hash, String)

    the AST expression (Hash or JSON string)

  • dialect (String, Symbol) (defaults to: :generic)

    the target SQL dialect (default: :generic)

Returns:

  • (String)

    the generated SQL



71
72
73
74
# File 'lib/polyglot.rb', line 71

def generate(ast, dialect: :generic)
  json = ast.is_a?(String) ? ast : JSON.generate(ast)
  _generate(json, dialect.to_s)
end

.parse(sql, dialect: :generic) ⇒ Array<Hash>

Parse SQL into an abstract syntax tree.

Examples:

Polyglot.parse("SELECT 1", dialect: :postgres)
# => [{"select" => {"expressions" => [...]}}]

Parameters:

  • sql (String)

    the SQL to parse

  • dialect (String, Symbol) (defaults to: :generic)

    the SQL dialect (default: :generic)

Returns:

  • (Array<Hash>)

    the parsed AST expressions



43
44
45
# File 'lib/polyglot.rb', line 43

def parse(sql, dialect: :generic)
  JSON.parse(_parse(sql, dialect.to_s))
end

.parse_one(sql, dialect: :generic) ⇒ Hash

Parse a single SQL statement into an AST.

Examples:

ast = Polyglot.parse_one("SELECT 1", dialect: :postgres)

Parameters:

  • sql (String)

    a single SQL statement

  • dialect (String, Symbol) (defaults to: :generic)

    the SQL dialect (default: :generic)

Returns:

  • (Hash)

    the parsed AST expression

Raises:

  • (Polyglot::ParseError)

    if the SQL contains more or fewer than one statement



57
58
59
# File 'lib/polyglot.rb', line 57

def parse_one(sql, dialect: :generic)
  JSON.parse(_parse_one(sql, dialect.to_s))
end

.transpile(sql, from:, to:) ⇒ Array<String>

Transpile SQL from one dialect to another.

Examples:

Polyglot.transpile("SELECT NOW()", from: :postgres, to: :mysql)
# => ["SELECT CURRENT_TIMESTAMP()"]

Parameters:

  • sql (String)

    the SQL statement(s) to transpile

  • from (String, Symbol)

    the source dialect

  • to (String, Symbol)

    the target dialect

Returns:

  • (Array<String>)

    the transpiled SQL statements



29
30
31
# File 'lib/polyglot.rb', line 29

def transpile(sql, from:, to:)
  _transpile(sql, from.to_s, to.to_s)
end

.validate(sql, dialect: :generic) ⇒ Polyglot::ValidationResult

Validate SQL syntax.

Examples:

result = Polyglot.validate("SELECT 1", dialect: :postgres)
result.valid? # => true

Invalid SQL

result = Polyglot.validate("SELEC 1")
result.valid? # => false
result.errors.first.message # => "..."

Parameters:

  • sql (String)

    the SQL to validate

  • dialect (String, Symbol) (defaults to: :generic)

    the SQL dialect (default: :generic)

Returns:



104
105
106
107
# File 'lib/polyglot.rb', line 104

def validate(sql, dialect: :generic)
  data = JSON.parse(_validate(sql, dialect.to_s))
  ValidationResult.new(data)
end