mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #482 from kirs/multiple-insert
Support multiple inserts
This commit is contained in:
commit
6d105c7f89
7 changed files with 54 additions and 5 deletions
|
@ -37,8 +37,12 @@ module Arel
|
|||
self
|
||||
end
|
||||
|
||||
def create_values values, columns
|
||||
def create_values values, columns = nil
|
||||
Nodes::Values.new values, columns
|
||||
end
|
||||
|
||||
def create_tuple values
|
||||
Nodes::Tuple.new values
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,7 @@ require 'arel/nodes/select_core'
|
|||
require 'arel/nodes/insert_statement'
|
||||
require 'arel/nodes/update_statement'
|
||||
require 'arel/nodes/bind_param'
|
||||
require 'arel/nodes/tuple'
|
||||
|
||||
# terminal
|
||||
|
||||
|
|
13
lib/arel/nodes/tuple.rb
Normal file
13
lib/arel/nodes/tuple.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
# frozen_string_literal: true
|
||||
module Arel
|
||||
module Nodes
|
||||
class Tuple < Node
|
||||
attr_reader :values
|
||||
|
||||
def initialize(values)
|
||||
@values = values
|
||||
super()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -131,6 +131,7 @@ module Arel
|
|||
end
|
||||
|
||||
if o.values
|
||||
collector << " VALUES"
|
||||
maybe_visit o.values, collector
|
||||
elsif o.select
|
||||
maybe_visit o.select, collector
|
||||
|
@ -166,8 +167,19 @@ module Arel
|
|||
collector << "FALSE"
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Tuple o, collector
|
||||
len = o.values.length - 1
|
||||
o.values.each_with_index { |value, i|
|
||||
collector = visit value, collector
|
||||
unless i == len
|
||||
collector << COMMA
|
||||
end
|
||||
}
|
||||
collector
|
||||
end
|
||||
|
||||
def visit_Arel_Nodes_Values o, collector
|
||||
collector << "VALUES ("
|
||||
collector << "("
|
||||
|
||||
len = o.expressions.length - 1
|
||||
o.expressions.each_with_index { |value, i|
|
||||
|
|
|
@ -28,6 +28,25 @@ module Arel
|
|||
}
|
||||
end
|
||||
|
||||
it 'inserts multiple values' do
|
||||
table = Table.new(:users)
|
||||
manager = Arel::InsertManager.new
|
||||
manager.into table
|
||||
|
||||
manager.columns << table[:id]
|
||||
manager.columns << table[:name]
|
||||
|
||||
manager.values = manager.create_tuple([
|
||||
manager.create_values(%w{ 1 david }),
|
||||
manager.create_values(%w{ 2 kirs }),
|
||||
manager.create_values(["3", Arel.sql('DEFAULT')], []),
|
||||
])
|
||||
|
||||
manager.to_sql.must_be_like %{
|
||||
INSERT INTO \"users\" (\"id\", \"name\") VALUES ('1', 'david'), ('2', 'kirs'), ('3', DEFAULT)
|
||||
}
|
||||
end
|
||||
|
||||
it "inserts false" do
|
||||
table = Table.new(:users)
|
||||
manager = Arel::InsertManager.new
|
||||
|
|
|
@ -42,10 +42,10 @@ module Arel
|
|||
end
|
||||
|
||||
it 'should return an insert manager' do
|
||||
im = @relation.compile_insert 'VALUES(NULL)'
|
||||
im = @relation.compile_insert '(NULL)'
|
||||
assert_kind_of Arel::InsertManager, im
|
||||
im.into Table.new(:users)
|
||||
assert_equal "INSERT INTO \"users\" VALUES(NULL)", im.to_sql
|
||||
assert_equal "INSERT INTO \"users\" VALUES (NULL)", im.to_sql
|
||||
end
|
||||
|
||||
describe 'skip' do
|
||||
|
|
|
@ -26,7 +26,7 @@ module Arel
|
|||
bp = Nodes::BindParam.new
|
||||
values = Nodes::Values.new([bp])
|
||||
sql = compile values
|
||||
sql.must_be_like 'VALUES (?)'
|
||||
sql.must_be_like '(?)'
|
||||
end
|
||||
|
||||
it 'can define a dispatch method' do
|
||||
|
|
Loading…
Reference in a new issue