activerecord_save_many 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.7.0
@@ -4,10 +4,10 @@ module ActiveRecord
4
4
  module SaveMany
5
5
  MAX_QUERY_SIZE = 1024 * 1024
6
6
  # options for the create_many method
7
- OPTIONS_KEYS = [:columns, :max_rows, :async, :ignore, :update, :updates].to_set
7
+ OPTIONS_KEYS = [:columns, :max_rows, :replace, :async, :ignore, :update, :updates].to_set
8
8
 
9
- # options for the create_many_sql method
10
- SQL_OPTIONS_KEYS = [:async, :ignore, :update, :updates].to_set
9
+ # options for the save_many_sql method
10
+ SQL_OPTIONS_KEYS = [:replace, :async, :ignore, :update, :updates].to_set
11
11
 
12
12
  class << self
13
13
  attr_accessor :default_max_rows
@@ -29,6 +29,7 @@ module ActiveRecord
29
29
  def check_options(permitted, options)
30
30
  unknown_keys = options.keys.to_set - permitted
31
31
  raise "unknown options: #{unknown_keys.to_a.join(", ")}" if !unknown_keys.empty?
32
+ raise "can't :replace and :ignore||:update||:async" if options[:replace] && (options[:ignore] || options[:update] || options[:updates] || options[:async])
32
33
  end
33
34
  module_function :check_options
34
35
 
@@ -91,7 +92,8 @@ module ActiveRecord
91
92
  table_name,
92
93
  columns,
93
94
  rows,
94
- { :ignore=>options[:ignore],
95
+ { :replace=>options[:replace],
96
+ :ignore=>options[:ignore],
95
97
  :async=>options[:async] && !Functions::disable_async?(),
96
98
  :update=>options[:update] || options[:updates],
97
99
  :updates=>options[:updates] || {} })
@@ -103,9 +105,10 @@ module ActiveRecord
103
105
 
104
106
  module MySQL
105
107
  def save_many_sql(klass, table_name, columns, rows, options)
106
- Functions::check_options(SQL_OPTIONS_KEYS, options)
107
108
 
108
- sql = ["insert", ("delayed" if options[:async]), ("ignore" if options[:ignore])].compact.join(' ') +
109
+ sql = [(if options[:replace] then "replace" else "insert" end),
110
+ ("delayed" if options[:async]),
111
+ ("ignore" if options[:ignore])].compact.join(' ') +
109
112
  " into #{table_name} (#{columns.join(',')}) values " +
110
113
  rows.map{|vals| "(" + vals.map{|v| klass.quote_value(v)}.join(",") +")"}.join(",") +
111
114
  (" on duplicate key update "+columns.map{|c| options[:updates][c] || "#{c}=values(#{c})"}.join(",") if options[:update]).to_s
@@ -40,6 +40,21 @@ module ActiveRecord
40
40
  SaveMany::Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS, :foo=>100)
41
41
  end.should raise_error(RuntimeError)
42
42
  end
43
+
44
+ it "should refuse :replace with :ignore, :async, :update or :updates" do
45
+ lambda do
46
+ SaveMany::Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS, :replace=>true, :ignore=>true)
47
+ end.should raise_error(RuntimeError)
48
+ lambda do
49
+ SaveMany::Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS, :replace=>true, :async=>true)
50
+ end.should raise_error(RuntimeError)
51
+ lambda do
52
+ SaveMany::Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS, :replace=>true, :update=>true)
53
+ end.should raise_error(RuntimeError)
54
+ lambda do
55
+ SaveMany::Functions::check_options(ActiveRecord::SaveMany::OPTIONS_KEYS, :replace=>true, :updates=>true)
56
+ end.should raise_error(RuntimeError)
57
+ end
43
58
  end
44
59
 
45
60
  describe "slice_array" do
@@ -176,6 +191,12 @@ module ActiveRecord
176
191
  k.save_many([kinst], :columns=>["foo"], :ignore=>true)
177
192
  end
178
193
 
194
+ it "should generate replace sql if :replace param given" do
195
+ k=new_ar_stub("Foo", [:foo], "foos", "replace into foos (foo) values ('foofoo')")
196
+ kinst = new_ar_inst(k, nil, true, {"foo"=>"foofoo"})
197
+ k.save_many([kinst], :columns=>["foo"], :replace=>true)
198
+ end
199
+
179
200
  it "should generate update sql with all columns if :update given" do
180
201
  k=new_ar_stub("Foo", [:foo], "foos",
181
202
  "insert into foos (foo) values ('foofoo') on duplicate key update foo=values(foo)")
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 6
8
- - 2
9
- version: 0.6.2
7
+ - 7
8
+ - 0
9
+ version: 0.7.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - mccraigmccraig
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-11 00:00:00 +00:00
17
+ date: 2010-03-15 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency