aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/arel.rb2
-rw-r--r--lib/arel/insert_manager.rb13
-rw-r--r--lib/arel/nodes.rb1
-rw-r--r--lib/arel/nodes/insert_statement.rb13
-rw-r--r--lib/arel/select_manager.rb29
-rw-r--r--lib/arel/table.rb2
-rw-r--r--lib/arel/tree_manager.rb30
-rw-r--r--lib/arel/visitors/to_sql.rb4
8 files changed, 65 insertions, 29 deletions
diff --git a/lib/arel.rb b/lib/arel.rb
index 1cca58f31b..8465d8e658 100644
--- a/lib/arel.rb
+++ b/lib/arel.rb
@@ -8,6 +8,8 @@ require 'arel/relation'
####
require 'arel/tree_manager'
+require 'arel/insert_manager'
+require 'arel/select_manager'
require 'arel/nodes'
#### these are deprecated
diff --git a/lib/arel/insert_manager.rb b/lib/arel/insert_manager.rb
new file mode 100644
index 0000000000..bfd3034df4
--- /dev/null
+++ b/lib/arel/insert_manager.rb
@@ -0,0 +1,13 @@
+module Arel
+ class InsertManager < Arel::TreeManager
+ def initialize engine
+ super
+ @head = Nodes::InsertStatement.new
+ end
+
+ def into table
+ @head.relation = table
+ self
+ end
+ end
+end
diff --git a/lib/arel/nodes.rb b/lib/arel/nodes.rb
index 5db177d53d..eed9641bbc 100644
--- a/lib/arel/nodes.rb
+++ b/lib/arel/nodes.rb
@@ -2,3 +2,4 @@ require 'arel/nodes/equality'
require 'arel/nodes/sql_literal'
require 'arel/nodes/select_core'
require 'arel/nodes/select_statement'
+require 'arel/nodes/insert_statement'
diff --git a/lib/arel/nodes/insert_statement.rb b/lib/arel/nodes/insert_statement.rb
new file mode 100644
index 0000000000..ed601aa841
--- /dev/null
+++ b/lib/arel/nodes/insert_statement.rb
@@ -0,0 +1,13 @@
+module Arel
+ module Nodes
+ class InsertStatement
+ attr_accessor :relation, :columns, :values
+
+ def initialize
+ @relation = nil
+ @columns = []
+ @values = []
+ end
+ end
+ end
+end
diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb
new file mode 100644
index 0000000000..6420142fa0
--- /dev/null
+++ b/lib/arel/select_manager.rb
@@ -0,0 +1,29 @@
+module Arel
+ class SelectManager < Arel::TreeManager
+ def initialize engine
+ super
+ @head = Nodes::SelectStatement.new
+ @ctx = @head.cores.last
+ end
+
+ def from table
+ @ctx.froms << table
+ self
+ end
+
+ def project projection
+ @ctx.projections << projection
+ self
+ end
+
+ def where expr
+ @ctx.wheres << expr
+ self
+ end
+
+ def take limit
+ @head.limit = limit
+ self
+ end
+ end
+end
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index a189838c07..4269dfc62f 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -13,7 +13,7 @@ module Arel
end
def tm
- TreeManager.new(@engine).from(self)
+ SelectManager.new(@engine).from(self)
end
def where condition
diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb
index 9de86e2add..9dd9f27858 100644
--- a/lib/arel/tree_manager.rb
+++ b/lib/arel/tree_manager.rb
@@ -4,38 +4,12 @@ module Arel
include Arel::Relation
def initialize engine
- @engine = engine
- @selects = []
-
- # default to Select
- @stmt = Nodes::SelectStatement.new
- @core = @stmt.cores.last
- @selects << @stmt
- end
-
- def from table
- @core.froms << table
- self
- end
-
- def project projection
- @core.projections << projection
- self
- end
-
- def where expr
- @core.wheres << expr
- self
- end
-
- def take limit
- @stmt.limit = limit
- self
+ @engine = engine
end
def to_sql
viz = Visitors::ToSql.new @engine
- viz.accept @stmt
+ viz.accept @head
end
end
end
diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb
index 2b3216abd3..7a97b0d445 100644
--- a/lib/arel/visitors/to_sql.rb
+++ b/lib/arel/visitors/to_sql.rb
@@ -12,6 +12,10 @@ module Arel
end
private
+ def visit_Arel_Nodes_InsertStatement o
+ "INSERT INTO #{visit o.relation}"
+ end
+
def visit_Arel_Nodes_SelectStatement o
[
o.cores.map { |x| visit x }.join,