jdbc-helper 0.6.2 → 0.6.3
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/README.markdown +24 -0
- data/lib/jdbc-helper/connection.rb +4 -7
- data/lib/jdbc-helper/constants.rb +2 -1
- data/lib/jdbc-helper/sql.rb +16 -4
- data/lib/jdbc-helper/sql_prepared.rb +7 -0
- data/test/database.yml +1 -0
- data/test/database.yml.local +1 -0
- data/test/test_sql.rb +8 -2
- metadata +21 -10
data/README.markdown
CHANGED
@@ -204,6 +204,30 @@ table.truncate!
|
|
204
204
|
table.drop!
|
205
205
|
```
|
206
206
|
|
207
|
+
#### Invalid use of dynamic conditions
|
208
|
+
|
209
|
+
TableWrapper object internally creates JDBC PreparedStatements.
|
210
|
+
If you dynamically build many condition-strings as the following example,
|
211
|
+
it would soon fail because there will be too many open PreparedStatements.
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
10000.times do |idx|
|
215
|
+
table.count("id = #{idx}")
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Correct ways of doing the same would be as follows.
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
10000.times do |idx|
|
223
|
+
# 1. with Hash
|
224
|
+
table.count('id' => idx)
|
225
|
+
|
226
|
+
# 2. with Array
|
227
|
+
table.count(["id = ?", idx])
|
228
|
+
end
|
229
|
+
```
|
230
|
+
|
207
231
|
### Using function wrappers (since 0.2.2)
|
208
232
|
```ruby
|
209
233
|
conn.function(:mod).call 5, 3
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
# Junegunn Choi (junegunn.c@gmail.com)
|
3
3
|
|
4
|
+
require 'insensitive_hash'
|
5
|
+
|
4
6
|
require 'jdbc-helper/connection/type_map'
|
5
7
|
require 'jdbc-helper/connection/parameterized_statement'
|
6
8
|
require 'jdbc-helper/connection/prepared_statement'
|
@@ -145,12 +147,7 @@ class Connection
|
|
145
147
|
def initialize(args = {})
|
146
148
|
# Subsequent deletes should not affect the input
|
147
149
|
@args = Marshal.load(Marshal.dump(args))
|
148
|
-
args = Marshal.load(Marshal.dump(args))
|
149
|
-
|
150
|
-
# String/Symbol
|
151
|
-
%w[driver url user password timeout].each do | strk |
|
152
|
-
args[strk.to_sym] = args.delete strk if args.has_key? strk
|
153
|
-
end
|
150
|
+
args = Marshal.load(Marshal.dump(args)).insensitive
|
154
151
|
|
155
152
|
raise ArgumentError.new("driver not given") unless args.has_key? :driver
|
156
153
|
raise ArgumentError.new("url not given") unless args.has_key? :url
|
@@ -170,7 +167,7 @@ class Connection
|
|
170
167
|
end
|
171
168
|
|
172
169
|
props = java.util.Properties.new
|
173
|
-
|
170
|
+
args.keys.each do |key|
|
174
171
|
props.setProperty(key.to_s, args[key].to_s) if args[key]
|
175
172
|
end
|
176
173
|
|
@@ -19,7 +19,8 @@ module Constants
|
|
19
19
|
DEFAULT_PARAMETERS = {
|
20
20
|
:mysql => {
|
21
21
|
'zeroDateTimeBehavior' => 'convertToNull',
|
22
|
-
|
22
|
+
# Removed from 0.6.3: This can have a performance hit when batch size is large
|
23
|
+
# 'rewriteBatchedStatements' => 'true',
|
23
24
|
'useServerPrepStmts' => 'true',
|
24
25
|
'useCursorFetch' => 'true'
|
25
26
|
}
|
data/lib/jdbc-helper/sql.rb
CHANGED
@@ -29,10 +29,10 @@ class SQL
|
|
29
29
|
case data
|
30
30
|
when NilClass
|
31
31
|
'null'
|
32
|
-
when Fixnum, Bignum, Float
|
33
|
-
data
|
34
32
|
when BigDecimal
|
35
33
|
data.to_s("F")
|
34
|
+
when Numeric
|
35
|
+
data
|
36
36
|
when JDBCHelper::SQL
|
37
37
|
data.to_s
|
38
38
|
when String
|
@@ -138,7 +138,7 @@ class SQL
|
|
138
138
|
|
139
139
|
protected
|
140
140
|
def self.esc str
|
141
|
-
str.gsub("'", "''")
|
141
|
+
str.to_s.gsub("'", "''")
|
142
142
|
end
|
143
143
|
|
144
144
|
# No check
|
@@ -161,7 +161,7 @@ protected
|
|
161
161
|
"is null"
|
162
162
|
when NotNilClass
|
163
163
|
"is not null"
|
164
|
-
when
|
164
|
+
when Numeric, JDBCHelper::SQL, String
|
165
165
|
"= #{value v}"
|
166
166
|
when Range
|
167
167
|
">= #{v.first} and #{k} <#{'=' unless v.exclude_end?} #{v.last}"
|
@@ -171,6 +171,18 @@ protected
|
|
171
171
|
raise NotImplementedError.new("Unsupported class: #{v.class}")
|
172
172
|
end
|
173
173
|
}.join(' and ')
|
174
|
+
when Array
|
175
|
+
if conds.empty?
|
176
|
+
''
|
177
|
+
else
|
178
|
+
base = conds.first.to_s
|
179
|
+
params = conds[1..-1] || []
|
180
|
+
'(' +
|
181
|
+
base.gsub('?') {
|
182
|
+
param = params.shift
|
183
|
+
param ? value(param) : '?'
|
184
|
+
} + ')'
|
185
|
+
end
|
174
186
|
else
|
175
187
|
raise NotImplementedError.new("Parameter to where must be either Hash or String")
|
176
188
|
end
|
@@ -134,6 +134,13 @@ class SQLPrepared < JDBCHelper::SQL
|
|
134
134
|
"= ?"
|
135
135
|
end
|
136
136
|
}.join(' and ')
|
137
|
+
when Array
|
138
|
+
if conds.empty?
|
139
|
+
''
|
140
|
+
else
|
141
|
+
binds += conds[1..-1] if conds.length > 1
|
142
|
+
"(#{conds.first})"
|
143
|
+
end
|
137
144
|
else
|
138
145
|
raise NotImplementedError.new("Parameter to where must be either Hash or String")
|
139
146
|
end
|
data/test/database.yml
CHANGED
data/test/database.yml.local
CHANGED
data/test/test_sql.rb
CHANGED
@@ -38,6 +38,8 @@ class TestSQL < Test::Unit::TestCase
|
|
38
38
|
|
39
39
|
def test_where
|
40
40
|
assert_equal "where a = 1", SQL.where(:a => 1)
|
41
|
+
assert_equal "where a = 12345678901234567890.1234567890123456789",
|
42
|
+
SQL.where(:a => BigDecimal.new("12345678901234567890.1234567890123456789"))
|
41
43
|
assert_equal "where a = 1.2", SQL.where(:a => 1.2)
|
42
44
|
assert_equal "where a = 9999999999999999999", SQL.where(:a => 9999999999999999999)
|
43
45
|
assert_equal "where a >= 1 and a <= 2", SQL.where(:a => 1..2)
|
@@ -55,8 +57,9 @@ class TestSQL < Test::Unit::TestCase
|
|
55
57
|
assert_equal "where (a = 1 or b = 1)", SQL.where(JDBCHelper::SQL("a = 1 or b = 1"))
|
56
58
|
assert_equal "where (a = 1 or b = 1) and c = 2", SQL.where("a = 1 or b = 1", :c => 2)
|
57
59
|
assert_equal "where c = 2 and (a = 1 or b = 1)", SQL.where({:c => 2}, "a = 1 or b = 1")
|
58
|
-
assert_equal "where c = 2 and (a = 1 or b = 1) and (e = 2) and f = 3",
|
59
|
-
SQL.where({:c => 2}, "a = 1 or b = 1", nil, "", "e = 2", nil, {:f => 3}, {}
|
60
|
+
assert_equal "where c = 2 and (a = 1 or b = 1) and (e = 2) and f = 3 and (abc not like % || 'abc???' || % or def != 100 and ghi = '??') and (1 = 1)",
|
61
|
+
SQL.where({:c => 2}, "a = 1 or b = 1", nil, "", "e = 2", nil, {:f => 3}, {},
|
62
|
+
["abc not like % || ? || % or def != ? and ghi = '??'", 'abc???', 100], [], ['1 = 1'])
|
60
63
|
assert_equal '', SQL.where(nil)
|
61
64
|
assert_equal '', SQL.where(" ")
|
62
65
|
|
@@ -98,6 +101,9 @@ class TestSQL < Test::Unit::TestCase
|
|
98
101
|
assert_equal ["where c = ? and (a = 1 or b = 1)", [2]], SQLPrepared.where({:c => 2}, "a = 1 or b = 1")
|
99
102
|
assert_equal ["where c = ? and (a = 1 or b = 1) and (e = 2) and f = ?", [2, 3]],
|
100
103
|
SQLPrepared.where({:c => 2}, "a = 1 or b = 1", nil, "", "e = 2", nil, {:f => 3}, {})
|
104
|
+
assert_equal ["where c = ? and (a = 1 or b = 1) and (e = 2) and f = ? and (abc not like % || ? || % or def != ?) and (1 = 1)", [2, 3, 'abc', 100]],
|
105
|
+
SQLPrepared.where({:c => 2}, "a = 1 or b = 1", nil, "", "e = 2", nil, {:f => 3}, {},
|
106
|
+
["abc not like % || ? || % or def != ?", 'abc', 100], [], ['1 = 1'])
|
101
107
|
assert_equal [nil, []], SQLPrepared.where(nil)
|
102
108
|
assert_equal [nil, []], SQLPrepared.where(" ")
|
103
109
|
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.6.
|
5
|
+
version: 0.6.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Junegunn Choi
|
@@ -10,50 +10,61 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-11-09 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: insensitive_hash
|
17
17
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.3
|
23
|
+
requirement: *id001
|
24
|
+
prerelease: false
|
25
|
+
type: :runtime
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
18
29
|
none: false
|
19
30
|
requirements:
|
20
31
|
- - ~>
|
21
32
|
- !ruby/object:Gem::Version
|
22
33
|
version: 1.0.0
|
23
|
-
requirement: *
|
34
|
+
requirement: *id002
|
24
35
|
prerelease: false
|
25
36
|
type: :development
|
26
37
|
- !ruby/object:Gem::Dependency
|
27
38
|
name: jeweler
|
28
|
-
version_requirements: &
|
39
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
29
40
|
none: false
|
30
41
|
requirements:
|
31
42
|
- - ~>
|
32
43
|
- !ruby/object:Gem::Version
|
33
44
|
version: 1.5.2
|
34
|
-
requirement: *
|
45
|
+
requirement: *id003
|
35
46
|
prerelease: false
|
36
47
|
type: :development
|
37
48
|
- !ruby/object:Gem::Dependency
|
38
49
|
name: rcov
|
39
|
-
version_requirements: &
|
50
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
40
51
|
none: false
|
41
52
|
requirements:
|
42
53
|
- - ">="
|
43
54
|
- !ruby/object:Gem::Version
|
44
55
|
version: "0"
|
45
|
-
requirement: *
|
56
|
+
requirement: *id004
|
46
57
|
prerelease: false
|
47
58
|
type: :development
|
48
59
|
- !ruby/object:Gem::Dependency
|
49
60
|
name: test-unit
|
50
|
-
version_requirements: &
|
61
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
51
62
|
none: false
|
52
63
|
requirements:
|
53
64
|
- - ">="
|
54
65
|
- !ruby/object:Gem::Version
|
55
66
|
version: 2.3.0
|
56
|
-
requirement: *
|
67
|
+
requirement: *id005
|
57
68
|
prerelease: false
|
58
69
|
type: :development
|
59
70
|
description: A JDBC helper for JRuby/Database developers.
|