An abstract SQL compiler for Alf expressions
- http://github.com/alf-tool/alf
- http://github.com/alf-tool/alf-sql
- http://github.com/alf-tool/alf-sequel
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.
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