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.
@@ -24,123 +24,114 @@ class Connection
24
24
  # puts row.rownum
25
25
  # end
26
26
  class Row
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 [String]
82
- def to_s
83
- @values.to_s
84
- end
85
-
86
- # @return [Array]
87
- def to_a
88
- @values
89
- end
90
-
91
- # @return [String]
92
- def join(sep = $,)
93
- to_a.join(sep)
94
- end
95
-
96
- # @return [Boolean]
97
- def eql?(other)
98
- self.hash == other.hash
99
- end
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
- def initialize(col_labels, col_labels_d, values, rownum) # :nodoc:
109
- @labels = col_labels
110
- @labels_d = col_labels_d
111
- @values = values
112
- @rownum = rownum
113
-
114
- # @labels_d.each do | l |
115
- # (class << self; self; end).instance_eval do
116
- # define_method l do
117
- # self[l]
118
- # end
119
- # end
120
- # end
121
- end
122
-
123
- # Performs better than defining methods
124
- def method_missing(symb, *args)
125
- if vidx = @labels_d.index(symb.to_s.downcase)
126
- begin
127
- @values[vidx]
128
- rescue NameError
129
- raise NoMethodError.new("undefined method or attribute `#{symb}'")
130
- end
131
- elsif @values.respond_to?(symb)
132
- @values.send(symb, *args)
133
- else
134
- raise NoMethodError.new("undefined method or attribute `#{symb}'")
135
- end
136
- end
137
-
138
- # Remove dangerous default methods
139
- # excluding :object_id, :hash
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
- def initialize(conn, max_size = 20)
11
- @conn = conn
12
- @max_size = max_size # TODO
13
- @free = []
14
- @occupied = []
15
- end
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
- def with
18
- begin
19
- yield stmt = take
20
- ensure
21
- give stmt
22
- end
23
- end
17
+ def with
18
+ begin
19
+ yield stmt = take
20
+ ensure
21
+ give stmt
22
+ end
23
+ end
24
24
 
25
- def take
26
- if @free.empty?
27
- raise Exception.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
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
- def give(stmt)
39
- raise Exception.new("Not my statement") unless
40
- @occupied.include? stmt
38
+ def give(stmt)
39
+ return if stmt.nil?
40
+ raise Exception.new("Not my statement") unless @occupied.include? stmt
41
41
 
42
- @occupied.delete stmt
43
- @free << stmt
44
- end
42
+ @occupied.delete stmt
43
+ @free << stmt
44
+ end
45
45
 
46
- def close
47
- (@free + @occupied).each do | stmt |
48
- stmt.close
49
- end
50
- @conn = nil
51
- @free = []
52
- @occupied = []
53
- end
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
- def each
56
- (@free + @occupied).each do | stmt |
57
- yield stmt
58
- end
59
- end
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
- 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
- SETTER_MAP =
15
- {
16
- Fixnum => :setInt,
17
- String => :setString,
18
- NilClass => :setNull,
19
- Float => :setDouble,
20
-
21
- # See there's a caveat. Check out ParameterizedStatement#set_param
22
- Time => :setTimestamp,
23
-
24
- Java::JavaSql::Date => :setDate,
25
- Java::JavaSql::Time => :setTime,
26
- Java::JavaSql::Timestamp => :setTimestamp,
27
- Java::JavaSql::Blob => :setBinaryStream,
28
-
29
- #########
30
- # MySQL #
31
- #########
32
- # Only available when MySQL JDBC driver is loaded.
33
- # So we use the string representation of the class.
34
- 'Java::ComMysqlJdbc::Blob' => :setBinaryStream
35
-
36
- # FIXME-MORE
37
- } # :nodoc:
38
-
39
- GETTER_MAP =
40
- {
41
- java.sql.Types::TINYINT => :getInt,
42
- java.sql.Types::SMALLINT => :getInt,
43
- java.sql.Types::INTEGER => :getInt,
44
- java.sql.Types::BIGINT => :getLong,
45
-
46
- java.sql.Types::CHAR => :getString,
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
- 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
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'