class Arel::Visitors::ToSql

def column_cache

def column_cache
  @column_cache ||= Hash.new do |hash, key|
    hash[key] = Hash[
      @engine.connection.columns(key, "#{key} Columns").map do |c|
        [c.name, c]
      end
    ]
  end
end

def column_for attr

def column_for attr
  name    = attr.name.to_s
  table   = attr.relation.table_name
  column_cache[table][name]
end

def visit_Arel_Nodes_And o

def visit_Arel_Nodes_And o
  o.children.map { |x| visit x }.join ' AND '
end

def visit_Arel_Nodes_InfixOperation o

def visit_Arel_Nodes_InfixOperation o
  "#{visit o.left} #{o.operator} #{visit o.right}"
end

def visit_Arel_Nodes_NamedFunction o

def visit_Arel_Nodes_NamedFunction o
  "#{o.name}(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
    visit x
  }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
end

def visit_Arel_Nodes_Not o

def visit_Arel_Nodes_Not o
  "NOT (#{visit o.expr})"
end

def visit_Arel_Nodes_Values o

def visit_Arel_Nodes_Values o
  "VALUES (#{o.expressions.zip(o.columns).map { |value, attr|
    if Nodes::SqlLiteral === value
      visit_Arel_Nodes_SqlLiteral value
    else
      quote(value, attr && column_for(attr))
    end
  }.join ', '})"
end