jdbc-helper 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +251 -0
- data/lib/jdbc-helper.rb +1 -10
- data/lib/jdbc-helper/connection.rb +347 -351
- data/lib/jdbc-helper/connection/callable_statement.rb +56 -56
- data/lib/jdbc-helper/connection/parameterized_statement.rb +57 -46
- data/lib/jdbc-helper/connection/prepared_statement.rb +88 -88
- data/lib/jdbc-helper/connection/result_set_enumerator.rb +102 -91
- data/lib/jdbc-helper/connection/row.rb +106 -115
- data/lib/jdbc-helper/connection/statement_pool.rb +44 -44
- data/lib/jdbc-helper/connection/type_map.rb +41 -66
- data/lib/jdbc-helper/connector.rb +10 -10
- data/lib/jdbc-helper/connector/mysql_connector.rb +24 -24
- data/lib/jdbc-helper/connector/oracle_connector.rb +33 -32
- data/lib/jdbc-helper/constants.rb +20 -17
- data/lib/jdbc-helper/sql.rb +168 -175
- data/lib/jdbc-helper/wrapper/function_wrapper.rb +12 -12
- data/lib/jdbc-helper/wrapper/object_wrapper.rb +17 -17
- data/lib/jdbc-helper/wrapper/procedure_wrapper.rb +120 -123
- data/lib/jdbc-helper/wrapper/sequence_wrapper.rb +43 -43
- data/lib/jdbc-helper/wrapper/table_wrapper.rb +172 -169
- data/test/helper.rb +2 -10
- data/test/performance.rb +141 -0
- data/test/test_connection.rb +443 -389
- data/test/test_connectors.rb +47 -47
- data/test/test_object_wrapper.rb +541 -432
- data/test/test_sql.rb +143 -135
- metadata +119 -123
- data/README.rdoc +0 -223
- data/test/test_performance.rb +0 -138
@@ -24,123 +24,114 @@ class Connection
|
|
24
24
|
# puts row.rownum
|
25
25
|
# end
|
26
26
|
class Row
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
def hash # :nodoc:
|
102
|
-
@labels.zip(@values).sort.hash
|
103
|
-
end
|
104
|
-
|
105
|
-
alias :== :eql?
|
27
|
+
# @return [Array] Labels of the columns
|
28
|
+
attr_reader :labels
|
29
|
+
# @return [Array] Values in Array
|
30
|
+
attr_reader :values
|
31
|
+
# @return [Fixnum] Sequential row number assigned within the scope of the query
|
32
|
+
attr_reader :rownum
|
33
|
+
alias_method :keys, :labels
|
34
|
+
|
35
|
+
include Enumerable
|
36
|
+
|
37
|
+
# @overload [](idx)
|
38
|
+
# @param [Fixnum/String/Symbol/Range] idx Access index
|
39
|
+
# @return [Object]
|
40
|
+
# @overload [](offset, len)
|
41
|
+
# @param [Fixnum] offset Start offset
|
42
|
+
# @param [Fixnum] len Length of Array
|
43
|
+
# @return [Array]
|
44
|
+
def [](*idx)
|
45
|
+
return @values[*idx] if idx.length > 1
|
46
|
+
|
47
|
+
idx = idx.first
|
48
|
+
case idx
|
49
|
+
when Fixnum
|
50
|
+
raise RangeError.new("Index out of bound") if idx >= @values.length
|
51
|
+
@values[idx]
|
52
|
+
when String, Symbol
|
53
|
+
# case-insensitive, assuming no duplicate labels
|
54
|
+
vidx = @labels_d.index(idx.to_s.downcase) or
|
55
|
+
raise NameError.new("Unknown column label: #{idx}")
|
56
|
+
@values[vidx]
|
57
|
+
else
|
58
|
+
# See how it goes...
|
59
|
+
@values[idx]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# @yield [Object]
|
64
|
+
def each(&blk)
|
65
|
+
@values.each { | v | yield v }
|
66
|
+
|
67
|
+
# @labels.each_with_index do | label, idx |
|
68
|
+
# yield label, @values[idx]
|
69
|
+
# end
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [String]
|
73
|
+
def inspect
|
74
|
+
strs = []
|
75
|
+
@labels.each do | col |
|
76
|
+
strs << "#{col}: #{self[col] || '(null)'}"
|
77
|
+
end
|
78
|
+
'[' + strs.join(', ') + ']'
|
79
|
+
end
|
80
|
+
|
81
|
+
# @return [Array]
|
82
|
+
def to_a
|
83
|
+
@values
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [String]
|
87
|
+
def join(sep = $,)
|
88
|
+
to_a.join(sep)
|
89
|
+
end
|
90
|
+
|
91
|
+
# @return [Boolean]
|
92
|
+
def eql?(other)
|
93
|
+
self.hash == other.hash
|
94
|
+
end
|
95
|
+
|
96
|
+
def hash # :nodoc:
|
97
|
+
@labels.zip(@values).sort.hash
|
98
|
+
end
|
99
|
+
|
100
|
+
alias :== :eql?
|
106
101
|
|
107
102
|
private
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
[:id, :tap, :gem, :display, :class, :method, :methods, :trust].
|
141
|
-
select { | s | method_defined? s }.each do | m |
|
142
|
-
undef_method m
|
143
|
-
end
|
103
|
+
def initialize(col_labels, col_labels_d, values, rownum) # :nodoc:
|
104
|
+
@labels = col_labels
|
105
|
+
@labels_d = col_labels_d
|
106
|
+
@values = values
|
107
|
+
@rownum = rownum
|
108
|
+
|
109
|
+
# @labels_d.each do | l |
|
110
|
+
# (class << self; self; end).instance_eval do
|
111
|
+
# define_method l do
|
112
|
+
# self[l]
|
113
|
+
# end
|
114
|
+
# end
|
115
|
+
# end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Performs better than defining methods
|
119
|
+
def method_missing(symb, *args)
|
120
|
+
if vidx = @labels_d.index(symb.to_s.downcase)
|
121
|
+
@values[vidx]
|
122
|
+
elsif @values.respond_to?(symb)
|
123
|
+
@values.send(symb, *args)
|
124
|
+
else
|
125
|
+
raise NoMethodError.new("undefined method or attribute `#{symb}'")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Remove dangerous default methods
|
130
|
+
# excluding :object_id, :hash
|
131
|
+
[:id, :tap, :gem, :display, :class, :method, :methods, :trust].
|
132
|
+
select { | s | method_defined? s }.each do | m |
|
133
|
+
undef_method m
|
134
|
+
end
|
144
135
|
end#Row
|
145
136
|
end#Connection
|
146
137
|
end#JDBCHelper
|
@@ -7,56 +7,56 @@ class Connection
|
|
7
7
|
# Not thread-safe. (Sharing a JDBC connection between threads is not the best idea anyway.)
|
8
8
|
# @private
|
9
9
|
class StatementPool
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
def initialize(conn, max_size = JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL)
|
11
|
+
@conn = conn
|
12
|
+
@max_size = max_size # TODO
|
13
|
+
@free = []
|
14
|
+
@occupied = []
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
def with
|
18
|
+
begin
|
19
|
+
yield stmt = take
|
20
|
+
ensure
|
21
|
+
give stmt
|
22
|
+
end
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
25
|
+
def take
|
26
|
+
if @free.empty?
|
27
|
+
raise RuntimeError.new("Statement nesting level is too deep (likely a bug)") if
|
28
|
+
@occupied.length >= @max_size
|
29
|
+
@occupied << nstmt = @conn.send(:create_statement)
|
30
|
+
nstmt
|
31
|
+
else
|
32
|
+
stmt = @free.pop
|
33
|
+
@occupied << stmt
|
34
|
+
stmt
|
35
|
+
end
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
def give(stmt)
|
39
|
+
return if stmt.nil?
|
40
|
+
raise Exception.new("Not my statement") unless @occupied.include? stmt
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
@occupied.delete stmt
|
43
|
+
@free << stmt
|
44
|
+
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
46
|
+
def close
|
47
|
+
(@free + @occupied).each do | stmt |
|
48
|
+
stmt.close
|
49
|
+
end
|
50
|
+
@conn = nil
|
51
|
+
@free = []
|
52
|
+
@occupied = []
|
53
|
+
end
|
54
54
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def each
|
56
|
+
(@free + @occupied).each do | stmt |
|
57
|
+
yield stmt
|
58
|
+
end
|
59
|
+
end
|
60
60
|
end#StatementPool
|
61
61
|
end#Connection
|
62
62
|
end#JDBCHelper
|
@@ -3,72 +3,47 @@
|
|
3
3
|
|
4
4
|
module JDBCHelper
|
5
5
|
class Connection
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
java.sql.Types::VARCHAR => :getString,
|
48
|
-
java.sql.Types::LONGVARCHAR => :getString,
|
49
|
-
(java.sql.Types::NCHAR rescue nil) => :getString,
|
50
|
-
(java.sql.Types::NVARCHAR rescue nil) => :getString,
|
51
|
-
(java.sql.Types::LONGNVARCHAR rescue nil) => :getBlob, # FIXME: MySQL
|
52
|
-
java.sql.Types::BINARY => :getString,
|
53
|
-
java.sql.Types::VARBINARY => :getString,
|
54
|
-
java.sql.Types::LONGVARBINARY => :getBlob, # FIXME: MySQL
|
55
|
-
|
56
|
-
java.sql.Types::REAL => :getDouble,
|
57
|
-
java.sql.Types::FLOAT => :getFloat,
|
58
|
-
java.sql.Types::DOUBLE => :getDouble,
|
59
|
-
java.sql.Types::NUMERIC => :getString, # FIXME: get_big_decimal=no inherent jruby support
|
60
|
-
java.sql.Types::DECIMAL => :getString, # FIXME: get_big_decimal
|
61
|
-
|
62
|
-
java.sql.Types::DATE => :getDate,
|
63
|
-
java.sql.Types::TIME => :getTime,
|
64
|
-
java.sql.Types::TIMESTAMP => :getTimestamp,
|
65
|
-
|
66
|
-
java.sql.Types::BLOB => :getBlob,
|
67
|
-
java.sql.Types::CLOB => :getString,
|
68
|
-
(java.sql.Types::NCLOB rescue nil) => :getString,
|
69
|
-
|
70
|
-
java.sql.Types::BOOLEAN => :getBoolean
|
71
|
-
} # :nodoc:
|
6
|
+
RUBY_SQL_TYPE_MAP = {
|
7
|
+
Fixnum => java.sql.Types::INTEGER,
|
8
|
+
Bignum => java.sql.Types::BIGINT,
|
9
|
+
String => java.sql.Types::VARCHAR,
|
10
|
+
Float => java.sql.Types::DOUBLE,
|
11
|
+
Time => java.sql.Types::TIMESTAMP
|
12
|
+
}
|
13
|
+
|
14
|
+
GETTER_MAP =
|
15
|
+
{
|
16
|
+
java.sql.Types::TINYINT => :getInt,
|
17
|
+
java.sql.Types::SMALLINT => :getInt,
|
18
|
+
java.sql.Types::INTEGER => :getInt,
|
19
|
+
java.sql.Types::BIGINT => :getLong,
|
20
|
+
|
21
|
+
java.sql.Types::CHAR => :getString,
|
22
|
+
java.sql.Types::VARCHAR => :getString,
|
23
|
+
java.sql.Types::LONGVARCHAR => :getString,
|
24
|
+
(java.sql.Types::NCHAR rescue nil) => :getString,
|
25
|
+
(java.sql.Types::NVARCHAR rescue nil) => :getString,
|
26
|
+
(java.sql.Types::LONGNVARCHAR rescue nil) => :getBlob, # FIXME: MySQL
|
27
|
+
java.sql.Types::BINARY => :getString,
|
28
|
+
java.sql.Types::VARBINARY => :getString,
|
29
|
+
java.sql.Types::LONGVARBINARY => :getBlob, # FIXME: MySQL
|
30
|
+
|
31
|
+
java.sql.Types::REAL => :getDouble,
|
32
|
+
java.sql.Types::FLOAT => :getFloat,
|
33
|
+
java.sql.Types::DOUBLE => :getDouble,
|
34
|
+
#java.sql.Types::NUMERIC => :getBigDecimal,
|
35
|
+
#java.sql.Types::DECIMAL => :getBigDecimal,
|
36
|
+
|
37
|
+
java.sql.Types::DATE => :getDate,
|
38
|
+
java.sql.Types::TIME => :getTime,
|
39
|
+
java.sql.Types::TIMESTAMP => :getTimestamp,
|
40
|
+
|
41
|
+
java.sql.Types::BLOB => :getBlob,
|
42
|
+
java.sql.Types::CLOB => :getString,
|
43
|
+
(java.sql.Types::NCLOB rescue nil) => :getString,
|
44
|
+
|
45
|
+
java.sql.Types::BOOLEAN => :getBoolean
|
46
|
+
} # :nodoc:
|
72
47
|
end#Connection
|
73
48
|
end#JDBCHelper
|
74
49
|
|
@@ -2,16 +2,16 @@
|
|
2
2
|
# Junegunn Choi (junegunn.c@gmail.com)
|
3
3
|
|
4
4
|
module JDBCHelper
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
5
|
+
class Connector
|
6
|
+
private
|
7
|
+
def self.ensure_close conn
|
8
|
+
begin
|
9
|
+
yield conn
|
10
|
+
ensure
|
11
|
+
conn.close rescue nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end#Connector
|
15
15
|
end#JDBCHelper
|
16
16
|
|
17
17
|
require 'jdbc-helper/connector/oracle_connector'
|