aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/arel.rb7
-rw-r--r--lib/arel/select_manager.rb5
-rw-r--r--lib/arel/table.rb4
-rw-r--r--lib/arel/tree_manager.rb22
-rw-r--r--lib/arel/update_manager.rb6
-rw-r--r--lib/arel/visitors.rb6
-rw-r--r--lib/arel/visitors/mysql.rb16
-rw-r--r--lib/arel/visitors/to_sql.rb18
-rw-r--r--lib/arel/visitors/update_sql.rb26
9 files changed, 58 insertions, 52 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index 85a866b0ce..afef2019d4 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -12,12 +12,7 @@ require 'arel/relation'
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'
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
index 4866ea66c9..21b9395120 100644
--- a/lib/arel/select_manager.rb
+++ b/lib/arel/select_manager.rb
@@ -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
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index a709a3d717..025ef30e55 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -37,11 +37,11 @@ module Arel
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
diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb
index 11d34ec138..4ee4a6f219 100644
--- a/lib/arel/tree_manager.rb
+++ b/lib/arel/tree_manager.rb
@@ -4,23 +4,31 @@ module Arel
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
diff --git a/lib/arel/update_manager.rb b/lib/arel/update_manager.rb
index 0cb5554954..b74e0f2a27 100644
--- a/lib/arel/update_manager.rb
+++ b/lib/arel/update_manager.rb
@@ -46,11 +46,7 @@ module Arel
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
diff --git a/lib/arel/visitors.rb b/lib/arel/visitors.rb
new file mode 100644
index 0000000000..22077d5943
--- /dev/null
+++ b/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'
diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb
new file mode 100644
index 0000000000..4551223591
--- /dev/null
+++ b/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
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 2ccc9d7d3f..d1cb115238 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -22,15 +22,25 @@ module Arel
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}",
diff --git a/lib/arel/visitors/update_sql.rb b/lib/arel/visitors/update_sql.rb
deleted file mode 100644
index 02196a0fb0..0000000000
--- a/lib/arel/visitors/update_sql.rb
+++ /dev/null
@@ -1,26 +0,0 @@
-module Arel
- module Visitors
- module UpdateSql
- 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 #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?)
- ].compact.join ' '
- end
- end
- end
-end