jdbc-helper 0.4.3 → 0.4.4

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.
@@ -50,7 +50,7 @@ class TableWrapper < ObjectWrapper
50
50
  # @param [List of Hash/String] where Filter conditions
51
51
  # @return [Fixnum] Count of the records.
52
52
  def count *where
53
- @connection.query(JDBCHelper::SQL.count name, where.empty? ? @query_where : where)[0][0].to_i
53
+ @connection.query(JDBCHelper::SQL.count name, merge_where(self, where))[0][0].to_i
54
54
  end
55
55
 
56
56
  # Sees if the table is empty
@@ -90,7 +90,9 @@ class TableWrapper < ObjectWrapper
90
90
  # :where element of the given hash can (usually should) point to another Hash representing update filters.
91
91
  # @return [Fixnum] Number of affected records
92
92
  def update data_hash_with_where
93
- where = data_hash_with_where.delete(:where) || @query_where
93
+ where_ext = data_hash_with_where.delete(:where)
94
+ where_ext = [where_ext] unless where_ext.is_a? Array
95
+ where = merge_where(self, where_ext.compact)
94
96
  @connection.send @update_method, JDBCHelper::SQL.update(name, data_hash_with_where, where)
95
97
  end
96
98
 
@@ -98,7 +100,8 @@ class TableWrapper < ObjectWrapper
98
100
  # @param [List of Hash/String] where Delete filters
99
101
  # @return [Fixnum] Number of affected records
100
102
  def delete *where
101
- @connection.send @update_method, JDBCHelper::SQL.delete(name, where.empty? ? @query_where : where)
103
+ where = merge_where(self, where)
104
+ @connection.send @update_method, JDBCHelper::SQL.delete(name, where)
102
105
  end
103
106
 
104
107
  # Empties the table.
@@ -137,14 +140,14 @@ class TableWrapper < ObjectWrapper
137
140
  # Returns a new TableWrapper object which can be used to execute a select
138
141
  # statement for the table with the specified filter conditions.
139
142
  # If a block is given, executes the select statement and yields each row to the block.
140
- # @param [Hash/String] Filter conditions
143
+ # @param [List of Hash/String] Filter conditions
141
144
  # @return [JDBCHelper::TableWrapper]
142
145
  # @since 0.4.0
143
146
  def where *conditions, &block
144
147
  raise ArgumentError.new("Wrong number of arguments") if conditions.empty?
145
148
 
146
149
  obj = self.dup
147
- obj.instance_variable_set :@query_where, conditions
150
+ merge_where(obj, conditions, true)
148
151
  ret obj, &block
149
152
  end
150
153
 
@@ -215,6 +218,12 @@ private
215
218
  obj
216
219
  end
217
220
  end
221
+
222
+ def merge_where obj, where, apply = false
223
+ merged = (obj.instance_variable_get(:@query_where) || []) + (where || [])
224
+ obj.instance_variable_set :@query_where, merged if apply
225
+ merged
226
+ end
218
227
  end#TableWrapper
219
228
  end#JDBCHelper
220
229
 
@@ -174,10 +174,16 @@ class TestObjectWrapper < Test::Unit::TestCase
174
174
  # Count
175
175
  assert_equal 100, table.count
176
176
  assert_equal 100, table.count(:alpha => 100)
177
+ assert_equal 1, table.where(:alpha => 100).count(:id => 1) # scoped
178
+ assert_equal 0, table.where(:alpha => 200).count(:id => 1) # scoped
177
179
  assert_equal 0, table.count(:beta => nil)
178
180
 
179
181
  assert_equal 100, table.where(:alpha => 100).count
180
182
  assert_equal 0, table.where(:beta => nil).count
183
+ assert_equal 40, table.where('id >= 11', 'id <= 50').count
184
+ assert_equal 40, table.where('id >= 11').count('id <= 50')
185
+ assert_equal 40, table.where('id >= 11').where('id <= 50').count
186
+ assert_equal 40, table.where('id >= 11').where('id <= 50').where('1 = 1').count
181
187
  assert_equal 0, table.where(:alpha => 100).count(:beta => nil)
182
188
 
183
189
  assert_equal true, table.empty?(:beta => nil)
@@ -327,8 +333,9 @@ class TestObjectWrapper < Test::Unit::TestCase
327
333
  insert table
328
334
 
329
335
  assert_equal 10, table.update(:beta => 0, :where => { :id => (1..10) })
336
+ assert_equal 2, table.where(:id => (55..56)).update(:beta => 0, :where => { :id => (51..60) })
330
337
  assert_equal 10, table.where(:id => (11..20)).update(:beta => 0)
331
- assert_equal 20, table.count(:beta => 0)
338
+ assert_equal 22, table.count(:beta => 0)
332
339
  assert_equal 100, table.update(:beta => 1)
333
340
  end
334
341
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jdbc-helper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.3
5
+ version: 0.4.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Junegunn Choi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-06 00:00:00 +09:00
13
+ date: 2011-06-09 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency