jdbc-helper 0.4.0 → 0.4.1

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.
@@ -39,7 +39,7 @@ class SQL
39
39
 
40
40
  # Generates SQL where cluase with the given conditions.
41
41
  # Parameter can be either Hash of String.
42
- def self.where conds
42
+ def self.where *conds
43
43
  where_clause = where_internal conds
44
44
  where_clause.empty? ? where_clause : check(where_clause)
45
45
  end
@@ -97,6 +97,7 @@ class SQL
97
97
  end
98
98
 
99
99
  # FIXME: Naive protection for SQL Injection
100
+ # TODO: check caching?
100
101
  def self.check expr, is_name = false
101
102
  return nil if expr.nil?
102
103
 
@@ -126,43 +127,46 @@ private
126
127
 
127
128
  # No check
128
129
  def self.where_internal conds
129
- return '' if conds.nil?
130
-
131
- where =
132
- case conds
133
- when String
134
- conds.strip
135
- when Hash
136
- conds.map { |k, v|
137
- "#{k} " +
138
- case v
139
- when NilClass
140
- "is null"
141
- when NotNilClass
142
- "is not null"
143
- when Fixnum, Bignum, Float, JDBCHelper::SQL
144
- "= #{v}"
145
- when Range
146
- ">= #{v.first} and #{k} <#{'=' unless v.exclude_end?} #{v.last}"
147
- when Array
148
- "in (" +
149
- v.map { |e|
150
- case e
151
- when String
152
- "'#{esc e}'"
153
- else
154
- e
155
- end }.join(', ') + ")"
156
- when String
157
- "= '#{esc v}'"
158
- else
159
- raise NotImplementedError.new("Unsupported class: #{v.class}")
160
- end
161
- }.join(' and ')
162
- else
163
- raise NotImplementedError.new("Parameter to where must be either Hash or String")
164
- end
165
- where.empty? ? '' : 'where ' + where
130
+ conds = [conds] unless conds.is_a? Array
131
+ where_clause = conds.compact.map { |cond| where_unit cond }.reject(&:empty?).join(' and ')
132
+ where_clause.empty? ? '' : 'where ' + where_clause
133
+ end
134
+
135
+ def self.where_unit conds
136
+ case conds
137
+ when String
138
+ conds = conds.strip
139
+ conds.empty? ? '' : "(#{conds})"
140
+ when Hash
141
+ conds.map { |k, v|
142
+ "#{k} " +
143
+ case v
144
+ when NilClass
145
+ "is null"
146
+ when NotNilClass
147
+ "is not null"
148
+ when Fixnum, Bignum, Float, JDBCHelper::SQL
149
+ "= #{v}"
150
+ when Range
151
+ ">= #{v.first} and #{k} <#{'=' unless v.exclude_end?} #{v.last}"
152
+ when Array
153
+ "in (" +
154
+ v.map { |e|
155
+ case e
156
+ when String
157
+ "'#{esc e}'"
158
+ else
159
+ e
160
+ end }.join(', ') + ")"
161
+ when String
162
+ "= '#{esc v}'"
163
+ else
164
+ raise NotImplementedError.new("Unsupported class: #{v.class}")
165
+ end
166
+ }.join(' and ')
167
+ else
168
+ raise NotImplementedError.new("Parameter to where must be either Hash or String")
169
+ end
166
170
  end
167
171
 
168
172
  def self.insert_internal cmd, table, data_hash
@@ -47,17 +47,17 @@ class TableWrapper < ObjectWrapper
47
47
  alias to_s name
48
48
 
49
49
  # Retrieves the count of the table
50
- # @param [Hash/String] Filter conditions
50
+ # @param [List of Hash/String] where Filter conditions
51
51
  # @return [Fixnum] Count of the records.
52
- def count where = nil
53
- @connection.query(JDBCHelper::SQL.count name, where || @query_where)[0][0].to_i
52
+ def count *where
53
+ @connection.query(JDBCHelper::SQL.count name, where.empty? ? @query_where : where)[0][0].to_i
54
54
  end
55
55
 
56
56
  # Sees if the table is empty
57
57
  # @param [Hash/String] Filter conditions
58
58
  # @return [boolean]
59
- def empty? where = nil
60
- count(where) == 0
59
+ def empty? *where
60
+ count(*where) == 0
61
61
  end
62
62
 
63
63
  # Inserts a record into the table with the given hash
@@ -95,10 +95,10 @@ class TableWrapper < ObjectWrapper
95
95
  end
96
96
 
97
97
  # Deletes records matching given condtion
98
- # @param [Hash] where Delete filters
98
+ # @param [List of Hash/String] where Delete filters
99
99
  # @return [Fixnum] Number of affected records
100
- def delete where = nil
101
- @connection.send @update_method, JDBCHelper::SQL.delete(name, where || @query_where)
100
+ def delete *where
101
+ @connection.send @update_method, JDBCHelper::SQL.delete(name, where.empty? ? @query_where : where)
102
102
  end
103
103
 
104
104
  # Empties the table.
@@ -136,7 +136,9 @@ class TableWrapper < ObjectWrapper
136
136
  # @param [Hash/String] Filter conditions
137
137
  # @return [JDBCHelper::TableWrapper]
138
138
  # @since 0.4.0
139
- def where conditions, &block
139
+ def where *conditions, &block
140
+ raise ArgumentError.new("Wrong number of arguments") if conditions.empty?
141
+
140
142
  obj = self.dup
141
143
  obj.instance_variable_set :@query_where, conditions
142
144
  ret obj, &block
@@ -249,6 +249,12 @@ class TestObjectWrapper < Test::Unit::TestCase
249
249
  select(:a, :b, 'c cc').
250
250
  order('id desc', 'name asc').sql
251
251
 
252
+ assert_equal "select a, b, c cc from tmp_jdbc_helper " +
253
+ "where (id != 15) and id >= 11 and id <= 20 order by id desc, name asc",
254
+ table.where("id != 15", :id => 11..20).
255
+ select(:a, :b, 'c cc').
256
+ order('id desc', 'name asc').sql
257
+
252
258
  assert_raise(ArgumentError) { table.order }
253
259
  assert_raise(ArgumentError) { table.order.where }
254
260
  assert_raise(ArgumentError) { table.where.order }
@@ -54,13 +54,14 @@ class TestPerformance < Test::Unit::TestCase
54
54
  end
55
55
  }.real}"
56
56
 
57
- puts "Inserts with hash (chunk-transactional): #{Benchmark.measure {
57
+ puts "Inserts with hash (batch & chunk-transactional): #{Benchmark.measure {
58
58
  table = conn.table(@table)
59
59
  (0...@count).each_slice(50) do |slice|
60
60
  conn.transaction do
61
61
  slice.each do |i|
62
- table.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
62
+ table.batch.insert @range.inject({}) { |hash, key| hash[key] = rand; hash }
63
63
  end
64
+ conn.execute_batch
64
65
  end
65
66
  end
66
67
  }.real}"
data/test/test_sql.rb CHANGED
@@ -47,7 +47,11 @@ class TestSQL < Test::Unit::TestCase
47
47
  assert_equal "where sysdate = sysdate", SQL.where(JDBCHelper::SQL('sysdate') => JDBCHelper::SQL('sysdate'))
48
48
  assert_equal "where a in ('aa', 'bb', 'cc')", SQL.where(:a => %w[aa bb cc])
49
49
  assert_equal "where a = 1 and b = 'A''s'", SQL.where(:a => 1, :b => "A's")
50
- assert_equal "where a = 1 or b = 1", SQL.where("a = 1 or b = 1")
50
+ assert_equal "where (a = 1 or b = 1)", SQL.where("a = 1 or b = 1")
51
+ assert_equal "where (a = 1 or b = 1) and c = 2", SQL.where("a = 1 or b = 1", :c => 2)
52
+ assert_equal "where c = 2 and (a = 1 or b = 1)", SQL.where({:c => 2}, "a = 1 or b = 1")
53
+ assert_equal "where c = 2 and (a = 1 or b = 1) and (e = 2) and f = 3",
54
+ SQL.where({:c => 2}, "a = 1 or b = 1", nil, "", "e = 2", nil, {:f => 3}, {})
51
55
  assert_equal '', SQL.where(nil)
52
56
  assert_equal '', SQL.where(" ")
53
57
 
@@ -55,6 +59,11 @@ class TestSQL < Test::Unit::TestCase
55
59
  assert_raise(NotImplementedError) { SQL.where(:a => Time.now) }
56
60
 
57
61
  # Invalid SQL detection
62
+ assert_raise(ArgumentError) { SQL.where(" 'a--b' -- cde") }
63
+ assert_raise(ArgumentError) { SQL.where(" 'a--b' ;") }
64
+ assert_raise(ArgumentError) { SQL.where(" 'a--b' -- cde", :a => 1) }
65
+ assert_raise(ArgumentError) { SQL.where(" 'a--b' -- cde", :a => 1) }
66
+ assert_raise(ArgumentError) { SQL.where({:a => 1}, "/* a") }
58
67
  assert_raise(ArgumentError) { SQL.where(:a => JDBCHelper::SQL(" 'a--b' -- cde")) }
59
68
  assert_raise(ArgumentError) { SQL.where(:a => JDBCHelper::SQL(" 'aabbb''dd")) }
60
69
  assert_raise(ArgumentError) { SQL.where(:a => JDBCHelper::SQL(" 'aabbb''dd' /* aaa */")) }
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.0
5
+ version: 0.4.1
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-05-31 00:00:00 +09:00
13
+ date: 2011-06-01 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency