Alf::Sql

Build Status Dependency Status Code Climate

An abstract SQL compiler for Alf expressions

Links

Synopsis

This sub-module provides an abstract SQL compiler for Alf expressions. It is NOT aimed at being used by end-users (the API illustrated below is considered private and may change at any time). Instead, it provides a basis for concrete translators converting the resulting SQL abstract syntax tree to a concrete SQL dialect through third-party libraries.

See alf-sequel for a translator built on top of Sequel.

Example

require 'alf-sql'

Alf.connect("sap.db") do |conn|
  # Let parse some relational expression
  expr = conn.parse{
    restrict(suppliers, city: 'London')
  }

  # Translate to SQL
  # (non-portable SQL output unless you require alf-sequel as well)
  puts expr.to_sql
  # => SELECT t1.sid, t1.name, t1.status, t1.city
  #      FROM suppliers AS t1
  #     WHERE t1.city = 'London'

  # Alternatively (for translator implementers),
  # compile to an abstract cog (cannot be iterated)
  cog = Alf::Sql::Compiler.new.call(expr)

  # Let see the SQL AST
  puts cog.to_sexpr.inspect
  # => [ :select_exp, [:select_list, ...] ]
end