Skip to content

Commit

Permalink
moving visitors around
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 23, 2010
1 parent 445a0ea commit 80ad95b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 52 deletions.
7 changes: 1 addition & 6 deletions lib/arel.rb
Expand Up @@ -12,12 +12,7 @@
require 'arel/expression'
####

require 'arel/visitors/to_sql'
require 'arel/visitors/postgresql'
require 'arel/visitors/update_sql'
require 'arel/visitors/join_sql'
require 'arel/visitors/order_clauses'
require 'arel/visitors/dot'
require 'arel/visitors'

require 'arel/tree_manager'
require 'arel/insert_manager'
Expand Down
5 changes: 3 additions & 2 deletions lib/arel/select_manager.rb
Expand Up @@ -2,10 +2,11 @@ module Arel
class SelectManager < Arel::TreeManager
include Arel::Crud

def initialize engine
super
def initialize engine, table = nil
super(engine)
@head = Nodes::SelectStatement.new
@ctx = @head.cores.last
from table
end

def taken
Expand Down
4 changes: 2 additions & 2 deletions lib/arel/table.rb
Expand Up @@ -37,11 +37,11 @@ def alias
end

def tm
SelectManager.new(@engine).from(self)
SelectManager.new(@engine, self)
end

def from table
SelectManager.new(@engine).from table
SelectManager.new(@engine, table)
end

def joins manager
Expand Down
22 changes: 15 additions & 7 deletions lib/arel/tree_manager.rb
Expand Up @@ -4,23 +4,31 @@ class TreeManager
include Arel::Relation

VISITORS = {
'postgresql' => Arel::Visitors::PostgreSQL
'postgresql' => Arel::Visitors::PostgreSQL,
'mysql' => Arel::Visitors::MySQL,
'mysql2' => Arel::Visitors::MySQL,
}

attr_accessor :visitor

def initialize engine
@engine = engine
@pool = engine.connection_pool
@adapter = @pool.spec.config[:adapter]
@visitor_klass = VISITORS[@adapter] || Visitors::ToSql
@engine = engine
@visitor = nil
end

def to_dot
Visitors::Dot.new.accept @head
end

def visitor
return @visitor if @visitor
pool = @engine.connection_pool
adapter = pool.spec.config[:adapter]
@visitor = (VISITORS[adapter] || Visitors::ToSql).new(@engine)
end

def to_sql
viz = @visitor_klass.new @engine
viz.accept @head
visitor.accept @head
end

def initialize_copy other
Expand Down
6 changes: 1 addition & 5 deletions lib/arel/update_manager.rb
Expand Up @@ -46,11 +46,7 @@ def set values
end

def to_sql
viz = @visitor_klass.new @engine
unless @engine.connection_pool.spec.config[:adapter] =~ /^mysql/
viz.extend(Visitors::UpdateSql)
end
viz.accept @head
visitor.accept @head
end
end
end
6 changes: 6 additions & 0 deletions lib/arel/visitors.rb
@@ -0,0 +1,6 @@
require 'arel/visitors/to_sql'
require 'arel/visitors/postgresql'
require 'arel/visitors/mysql'
require 'arel/visitors/join_sql'
require 'arel/visitors/order_clauses'
require 'arel/visitors/dot'
16 changes: 16 additions & 0 deletions lib/arel/visitors/mysql.rb
@@ -0,0 +1,16 @@
module Arel
module Visitors
class MySQL < Arel::Visitors::ToSql
def visit_Arel_Nodes_UpdateStatement o
[
"UPDATE #{visit o.relation}",
("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
("LIMIT #{o.limit}" if o.limit),
].compact.join ' '
end

end
end
end
18 changes: 14 additions & 4 deletions lib/arel/visitors/to_sql.rb
Expand Up @@ -22,15 +22,25 @@ def visit_Arel_Nodes_DeleteStatement o
end

def visit_Arel_Nodes_UpdateStatement o
if o.orders.empty? && o.limit.nil?
wheres = o.wheres
else
stmt = Nodes::SelectStatement.new
core = stmt.cores.first
core.froms = o.relation
core.projections = [o.relation.primary_key]
stmt.limit = o.limit
stmt.orders = o.orders

wheres = [Nodes::In.new(o.relation.primary_key, [stmt])]
end

[
"UPDATE #{visit o.relation}",
("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?),
("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
("LIMIT #{o.limit}" if o.limit),
("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?)
].compact.join ' '
end

def visit_Arel_Nodes_InsertStatement o
[
"INSERT INTO #{visit o.relation}",
Expand Down
26 changes: 0 additions & 26 deletions lib/arel/visitors/update_sql.rb

This file was deleted.

0 comments on commit 80ad95b

Please sign in to comment.