diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-07-01 20:29:33 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-07-01 20:29:33 -0300 |
commit | 808b9e90a38c6c19e109da8eb5f2a264fd780d9a (patch) | |
tree | de89ed495a0462f5e2c74be9fa3dbacae7c18124 | |
parent | 97811698ab0e68b33fbf3067c3764e385dd75d53 (diff) | |
download | rails-808b9e90a38c6c19e109da8eb5f2a264fd780d9a.tar.gz rails-808b9e90a38c6c19e109da8eb5f2a264fd780d9a.tar.bz2 rails-808b9e90a38c6c19e109da8eb5f2a264fd780d9a.zip |
Allow SQL strings to insert query. Insert should better return engine's result.
-rw-r--r-- | lib/arel/engines/sql/relations/writes.rb | 11 | ||||
-rw-r--r-- | lib/arel/session.rb | 1 | ||||
-rw-r--r-- | spec/arel/engines/memory/integration/joins/cross_engine_spec.rb | 5 | ||||
-rw-r--r-- | spec/arel/engines/memory/unit/relations/insert_spec.rb | 14 |
4 files changed, 17 insertions, 14 deletions
diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb index b90d380c64..d648a54d91 100644 --- a/lib/arel/engines/sql/relations/writes.rb +++ b/lib/arel/engines/sql/relations/writes.rb @@ -11,11 +11,17 @@ module Arel class Insert < Compound def to_sql(formatter = nil) + insertion_attributes_values_sql = if record.is_a?(Value) + record.value + else + build_query "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", + "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + end + build_query \ "INSERT", "INTO #{table_sql}", - "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", - "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + insertion_attributes_values_sql end end @@ -28,7 +34,6 @@ module Arel end protected - def assignment_sql if assignments.respond_to?(:collect) assignments.collect do |attribute, value| diff --git a/lib/arel/session.rb b/lib/arel/session.rb index abef5c543b..cf04e8a93a 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -24,7 +24,6 @@ module Arel module CRUD def create(insert) insert.call - insert end def read(select) diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb index bffecc9182..07228bb0f7 100644 --- a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -10,9 +10,8 @@ module Arel ], [:id, :name]) @photos = Table.new(:photos) @photos.delete - @photos \ - .insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \ - .insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) + @photos.insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) + @photos.insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) end describe 'when the in memory relation is on the left' do diff --git a/spec/arel/engines/memory/unit/relations/insert_spec.rb b/spec/arel/engines/memory/unit/relations/insert_spec.rb index 59e43328a3..873737e3b5 100644 --- a/spec/arel/engines/memory/unit/relations/insert_spec.rb +++ b/spec/arel/engines/memory/unit/relations/insert_spec.rb @@ -14,13 +14,13 @@ module Arel it "manufactures an array of hashes of attributes to values" do @relation \ .insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [1, 'duck']), - Row.new(relation, [2, 'duck']), - Row.new(relation, [3, 'goose']), - Row.new(relation, [4, 'guinea fowl']) - ] + do |relation| + relation.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + Row.new(relation, [3, 'goose']), + Row.new(relation, [4, 'guinea fowl']) + ] end end end |