jvertica 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9139f44b6c6e4f55a0b8db36415fc9dd4c39a188
4
- data.tar.gz: f444a669fe6d9bd0587470b60972ce91a20eead6
3
+ metadata.gz: 4e58f3395ddff02d8858035b5f1c8cd83630ecff
4
+ data.tar.gz: 9cd5d1caacd940c2c69b133e939647e245ddd0de
5
5
  SHA512:
6
- metadata.gz: 63dd41ebebda1a5223a6e8f854461d380bb6a069a544913e128006a42e90df621b2e0e7cbc965efbb221614aa01e5ded408cc4ea72f5d4db24177e0fb1f59195
7
- data.tar.gz: 5a45ad60ce6956901f38dfc7c2ad2b5c6f8cc1074077fe02abed2cb5cc00020d522ad7d26548d1862e3b18683845e56c5aec6df0e1a9826d8a6ce207d35601f5
6
+ metadata.gz: 776711c902b8c08f906dbff6b412f7a516feb9009d236d91284526e8ad75bf521c877b0cc921f3f8216ff36b7c7c97b6504d128d55bc183b94ee3894b476d430
7
+ data.tar.gz: 8ee959130289e1b56635bce5dd2677a68d6a4131ba195dbfebffb3552ecc97ed9a2ba96bce6a6d1d71967f40b2747b7cf83e23e4900b838e063b3c1dbae4c80b
data/.gitignore CHANGED
@@ -11,5 +11,7 @@
11
11
  *.o
12
12
  *.a
13
13
  mkmf.log
14
- example
15
14
  vendor
15
+ .ruby-version
16
+ .bundle/
17
+ .env
@@ -1,3 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.4
3
+ - jruby-19mode
4
+ - jruby-head
5
+
6
+ matrix:
7
+ allow_failures:
8
+ - rvm: jruby-head
data/README.md CHANGED
@@ -23,7 +23,7 @@ Or install it yourself as:
23
23
  ```ruby
24
24
  require 'jvertica'
25
25
 
26
- conn_opts = {
26
+ params = {
27
27
  host: 'vertica.com',
28
28
  port: 5433,
29
29
  user: 'xxxxxx',
@@ -32,8 +32,8 @@ conn_opts = {
32
32
 
33
33
  sql = 'select * from sandbox.jdbc_tests;'
34
34
 
35
- c = Jvertica.connect conn_opts
36
- c.query sql do |row|
35
+ c = Jvertica.connect(params)
36
+ c.query(sql) do |row|
37
37
  p row
38
38
  end
39
39
  ```
@@ -0,0 +1,32 @@
1
+ require 'jvertica'
2
+ require 'dotenv'
3
+
4
+ # Create .env file like
5
+ # HOSTNAME=
6
+ # USERNAME=
7
+ # PASSWORD=
8
+ Dotenv.load
9
+
10
+ params = {
11
+ host: ENV['HOSTNAME'],
12
+ port: 5433,
13
+ user: ENV['USERNAME'],
14
+ password: ENV['PASSWORD'],
15
+ }
16
+ puts params
17
+
18
+ vertica = Jvertica.connect(params)
19
+ vertica.query("DROP TABLE sandbox.jvertica_test") rescue nil
20
+ vertica.query("CREATE TABLE sandbox.jvertica_test (id integer, str varchar(80))")
21
+ vertica.copy("COPY sandbox.jvertica_test FROM STDIN PARSER fjsonparser() NO COMMIT") do |io|
22
+ File.open(File.expand_path('../data.json', __FILE__), 'r') do |f|
23
+ begin
24
+ io << f.gets
25
+ end until f.eof?
26
+ end
27
+ end
28
+ vertica.commit
29
+ vertica.query("SELECT * from sandbox.jvertica_test") do |row|
30
+ p row
31
+ end
32
+ vertica.query("DROP TABLE sandbox.jvertica_test")
@@ -0,0 +1,2 @@
1
+ {"id":1,"str":"aaa"}
2
+ {"id":2,"str":"bbb"}
@@ -0,0 +1,23 @@
1
+ require 'jvertica'
2
+ require 'dotenv'
3
+
4
+ # Create .env file like
5
+ # HOSTNAME=
6
+ # USERNAME=
7
+ # PASSWORD=
8
+ Dotenv.load
9
+
10
+ params = {
11
+ host: ENV['HOSTNAME'],
12
+ port: 5433,
13
+ user: ENV['USERNAME'],
14
+ password: ENV['PASSWORD'],
15
+ }
16
+ puts params
17
+
18
+ sql = 'select * from dt;'
19
+
20
+ c = Jvertica.connect(params)
21
+ c.query(sql) do |row|
22
+ p row
23
+ end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "dotenv"
24
25
 
25
26
  spec.add_dependency "jdbc-vertica"
26
27
  end
@@ -1,15 +1,19 @@
1
1
  require 'java'
2
2
  require 'thread'
3
- require 'jvertica/version'
4
3
  require 'jdbc-vertica'
5
4
  require Jdbc::Vertica.driver_jar
5
+ require 'jvertica/version'
6
+ require 'jvertica/result_set'
7
+ require 'jvertica/row'
8
+ require 'jvertica/error'
9
+ require 'jvertica/constant'
6
10
 
7
11
  class Jvertica
8
12
  unless %r{java} === RUBY_PLATFORM
9
13
  warn "only for use with JRuby"
10
14
  end
11
15
 
12
- @@default_option_value = {
16
+ DEFAULT_OPTION_VALUES = {
13
17
  host: 'localhost',
14
18
  port: 5433,
15
19
  database: 'vdb',
@@ -18,33 +22,35 @@ class Jvertica
18
22
  AutoCommit: false,
19
23
  }
20
24
 
21
- def self.connect options = {}
22
- new options
25
+ def self.connect(options = {})
26
+ new(options)
23
27
  end
24
28
 
25
29
  attr_reader :host, :port, :database
26
30
 
27
- def initialize options
28
- options = @@default_option_value.merge(options).to_sym
29
- @host = options[:host]
30
- @port = options[:port]
31
- @database = options[:database]
32
- %w(:host :port :database).map do |key|
33
- options.delete key
34
- end
31
+ def initialize(options)
32
+ options = options.inject({}) {|h, (k, v)| h[k.to_sym] = v; h } # symbolize_keys
33
+ options = DEFAULT_OPTION_VALUES.merge(options)
34
+ @host = options.delete(:host)
35
+ @port = options.delete(:port)
36
+ @database = options.delete(:database)
35
37
 
36
38
  prop = Properties.new
37
39
  options.each do |key, value|
38
- prop.put key.to_s, value
40
+ prop.put(key.to_s, value) unless value.nil?
39
41
  end
40
42
 
41
- @connection = begin
42
- DriverManager.getConnection "jdbc:vertica://#{host}:#{port}/#{database}", prop
43
- rescue => e
44
- raise ConnectionError.new("Connection Failed.\n" +
45
- "Error Message => #{e.message}\n" +
46
- "see documentation => #{Constant::CONNECTION_PROPERTY_DOCUMENT_URL}\n")
47
- end
43
+ @connection =
44
+ begin
45
+ DriverManager.getConnection("jdbc:vertica://#{host}:#{port}/#{database}", prop)
46
+ rescue => e
47
+ raise ConnectionError.new(
48
+ "Connection Failed.\n" <<
49
+ "Error Message => #{e.message}\n" <<
50
+ "see documentation => #{Constant::CONNECTION_PROPERTY_DOCUMENT_URL}\n"
51
+ )
52
+ end
53
+
48
54
  @closed = false
49
55
  @connection
50
56
  end
@@ -54,7 +60,10 @@ class Jvertica
54
60
  end
55
61
 
56
62
  def close
57
- @connection.close && @closed = true
63
+ @connection.close
64
+ ensure
65
+ @connection = nil
66
+ @closed = true
58
67
  end
59
68
 
60
69
  def commit
@@ -77,26 +86,26 @@ class Jvertica
77
86
  # TODO
78
87
  #end
79
88
 
80
- def property key, value = nil
89
+ def property(key, value = nil)
81
90
  key = key.to_s
82
91
  if value.nil?
83
- @connection.getProperty key
92
+ @connection.getProperty(key)
84
93
  else
85
- @connection.setProperty key, value
94
+ @connection.setProperty(key, value)
86
95
  end
87
96
  end
88
97
 
89
- def query query, &blk
98
+ def query(query, &blk)
90
99
  stmt = @connection.createStatement
91
100
  case query
92
- when %r{\A\s*copy}miu then return stmt.execute query
93
- when %r{\A\s*insert}miu then return stmt.executeUpdate query
94
- when %r{\A\s*update}miu then return stmt.executeUpdate query
95
- when %r{\A\s*delete}miu then return stmt.executeUpdate query
96
- when %r{\A\s*drop}miu then return stmt.execute query
97
- when %r{\A\s*create}miu then return stmt.execute query
98
- when %r{\A\s*set}miu then return stmt.execute query
99
- else rs = stmt.executeQuery query
101
+ when %r{\A\s*copy}miu then return stmt.execute(query)
102
+ when %r{\A\s*insert}miu then return stmt.executeUpdate(query)
103
+ when %r{\A\s*update}miu then return stmt.executeUpdate(query)
104
+ when %r{\A\s*delete}miu then return stmt.executeUpdate(query)
105
+ when %r{\A\s*drop}miu then return stmt.execute(query)
106
+ when %r{\A\s*create}miu then return stmt.execute(query)
107
+ when %r{\A\s*set}miu then return stmt.execute(query)
108
+ else rs = stmt.executeQuery(query)
100
109
  end
101
110
 
102
111
  if block_given?
@@ -104,13 +113,13 @@ class Jvertica
104
113
  yield row
105
114
  end
106
115
  else
107
- ResultSet.new rs
116
+ ResultSet.new(rs)
108
117
  end
109
118
  end
110
119
 
111
- def copy query, source = nil, &blk
120
+ def copy(query, source = nil, &blk)
112
121
  raise InvalidQuery.new('can use only "copy".') unless %r{\A\s*copy}miu === query
113
- if !source.nil? or block_given?
122
+ if source or block_given?
114
123
  copy_stream(query, source, &blk)
115
124
  else
116
125
  [query(query), nil]
@@ -118,34 +127,36 @@ class Jvertica
118
127
  end
119
128
 
120
129
  private
121
- def copy_stream query, io = nil, &blk
122
- stream = com.vertica.jdbc.VerticaCopyStream.new @connection, query
130
+
131
+ class DriverManager < java.sql.DriverManager
132
+ end
133
+
134
+ class Properties < java.util.Properties
135
+ end
136
+
137
+ class DataSource < com.vertica.jdbc.DataSource
138
+ end
139
+
140
+ def copy_stream(query, io = nil, &blk)
141
+ unless ((io and io.is_a?(IO)) or block_given?)
142
+ raise InvalidObject.new("block or IO object is required.")
143
+ end
144
+
145
+ stream = com.vertica.jdbc.VerticaCopyStream.new(@connection, query)
123
146
  stream.start
124
147
  thread = nil
148
+
125
149
  begin
126
150
 
127
151
  if block_given?
128
-
129
152
  i, o = IO.pipe
130
- begin
131
- thread = Thread.new do
132
- yield(o)
133
- o.close
134
- end
135
- stream.addStream org.jruby.util.IOInputStream.new(i)
136
- rescue => e
137
- raise e
138
- ensure
153
+ thread = Thread.new do
154
+ yield(o)
155
+ o.close
139
156
  end
140
-
157
+ stream.addStream(org.jruby.util.IOInputStream.new(i))
141
158
  else
142
-
143
- if io.is_a? IO
144
- stream.addStream org.jruby.util.IOInputStream.new(io)
145
- else
146
- raise InvalidObject.new("source must be a IO.")
147
- end
148
-
159
+ stream.addStream(org.jruby.util.IOInputStream.new(io))
149
160
  end
150
161
 
151
162
  rescue => e
@@ -157,262 +168,10 @@ class Jvertica
157
168
  stream.execute
158
169
  rejects = stream.getRejects
159
170
  results = stream.finish
160
- rescue => e
161
- raise e
162
171
  ensure
163
172
  thread.join unless thread.nil?
164
173
  end
165
174
 
166
175
  [results, rejects.to_ary]
167
176
  end
168
-
169
- class ConnectionError < StandardError
170
- end
171
-
172
- class InvalidQuery < StandardError
173
- end
174
-
175
- class InvalidObject < StandardError
176
- end
177
-
178
- class InsufficientArgument < StandardError
179
- end
180
-
181
- class ResultSet
182
- include Enumerable
183
-
184
- def each
185
- return enum_for(:each) unless block_given?
186
- return if closed?
187
-
188
- while @nrow
189
- idx = 0
190
- row = Jvertica::Row.new(
191
- @col_labels,
192
- @col_labels_d,
193
- @getters.map{|gt|
194
- case gt
195
- when :getBigNum
196
- v = @rset.getBigDecimal idx+=1
197
- @rset.was_null ? nil : v.toPlainString.to_i
198
- when :getBigDecimal
199
- v = @rset.getBigDecimal idx+=1
200
- @rset.was_null ? nil : BigDecimal.new(v.toPlainString)
201
- else
202
- v = @rset.send gt, idx+=1
203
- @rset.was_null ? nil : v
204
- end
205
- },
206
- @rownum += 1
207
- )
208
- close unless @nrow = @rset.next
209
- yield row
210
- end
211
- close
212
- end
213
-
214
- def close
215
- return if closed?
216
-
217
- @rset.close
218
- @close_callback.call if @close_callback
219
- @closed = true
220
- end
221
-
222
- def closed?
223
- @closed
224
- end
225
-
226
- def initialize rset, &close_callback
227
- unless rset.respond_to? :get_meta_data
228
- rset.close if rset
229
- @closed = true
230
- return
231
- end
232
-
233
- @close_callback = close_callback
234
- @rset = rset
235
- @rsmd = @rset.get_meta_data
236
- @num_col = @rsmd.get_column_count
237
- @getters = []
238
- @col_labels = []
239
- @col_labels_d = []
240
- (1..@num_col).each do |i|
241
- type = @rsmd.get_column_type i
242
-
243
- @getters <<
244
- case type
245
- when java.sql.Types::NUMERIC, java.sql.Types::DECIMAL
246
- precision = @rsmd.get_precision i
247
- scale = @rsmd.get_scale i
248
-
249
- if precision > 0 and scale >= 0
250
- if scale > 0 then :getBigDecimal
251
- else
252
-
253
- if precision <= 9 then :getInt
254
- elsif precision <= 18 then :getLong
255
- else :getBigNum
256
- end
257
-
258
- end
259
- else :getBigDecimal
260
- end
261
-
262
- else Jvertica::Constant::GETTER_MAP.fetch type, :get_string
263
- end
264
-
265
- label = @rsmd.get_column_label i
266
- @col_labels << label
267
- @col_labels_d << label.downcase
268
- end
269
-
270
- @rownum = -1
271
- @nrow = @rset.next
272
- @closed = false
273
- end
274
- end
275
-
276
- class Row
277
- attr_reader :labels, :values, :rownum
278
- alias_method :keys, :labels
279
-
280
- include Enumerable
281
-
282
- def [] *idx
283
- return @values[*idx] if idx.length > 1
284
-
285
- idx = idx.first
286
- case idx
287
- when Fixnum
288
- raise RangeError.new("Index out of bound") if idx >= @values.length
289
- @values[idx]
290
- when String, Symbol
291
- vidx = @labels_d.index(idx.to_s.downcase) or
292
- raise NameError.new("Unknown column label: #{idx}")
293
- @values[vidx]
294
- else
295
- @values[idx]
296
- end
297
- end
298
-
299
- def each(&blk)
300
- @values.each do |v|
301
- yield v
302
- end
303
- end
304
-
305
- def inspect
306
- strs = []
307
- @labels.each do |col|
308
- strs << "#{col}: #{self[col] || '(null)'}"
309
- end
310
- '[' + strs.join(', ') + ']'
311
- end
312
-
313
- def to_a
314
- @values
315
- end
316
-
317
- def join sep = $OUTPUT_FIELD_SEPARATOR
318
- to_a.join sep
319
- end
320
-
321
- def eql? other
322
- self.hash == other.hash
323
- end
324
-
325
- def hash
326
- @labels.zip(@values).sort.hash
327
- end
328
-
329
- def to_h
330
- Hash[@labels.zip @values]
331
- end
332
-
333
- alias :== :eql?
334
-
335
- def initialize col_labels, col_labels_d, values, rownum
336
- @labels = col_labels
337
- @labels_d = col_labels_d
338
- @values = values
339
- @rownum = rownum
340
- end
341
-
342
- def method_missing symb, *args
343
- if vidx = @labels_d.index(symb.to_s.downcase)
344
- @values[vidx]
345
- elsif @values.respond_to? symb
346
- @values.send symb, *args
347
- else
348
- raise NoMethodError.new("undefined method or attribute `#{symb}'")
349
- end
350
- end
351
-
352
- [:id, :tap, :gem, :display, :class, :method, :methods, :trust].select do |s|
353
- method_defined? s
354
- end.each do |m|
355
- undef_method m
356
- end
357
- end
358
-
359
- class DriverManager < java.sql.DriverManager
360
- end
361
-
362
- class Properties < java.util.Properties
363
- end
364
-
365
- class DataSource < com.vertica.jdbc.DataSource
366
- end
367
-
368
- module Constant
369
- CONNECTION_PROPERTY_DOCUMENT_URL =
370
- 'http://my.vertica.com/docs/7.1.x/HTML/index.htm#Authoring/ProgrammersGuide/ClientJDBC/JDBCConnectionProperties.htm'
371
-
372
- RUBY_SQL_TYPE_MAP = {
373
- Fixnum => java.sql.Types::INTEGER,
374
- Bignum => java.sql.Types::BIGINT,
375
- String => java.sql.Types::VARCHAR,
376
- Float => java.sql.Types::DOUBLE,
377
- Time => java.sql.Types::TIMESTAMP
378
- }
379
- GETTER_MAP = {
380
- java.sql.Types::TINYINT => :getInt,
381
- java.sql.Types::SMALLINT => :getInt,
382
- java.sql.Types::INTEGER => :getInt,
383
- java.sql.Types::BIGINT => :getLong,
384
- java.sql.Types::CHAR => :getString,
385
- java.sql.Types::VARCHAR => :getString,
386
- java.sql.Types::LONGVARCHAR => :getString,
387
- (java.sql.Types::NCHAR rescue nil) => :getString,
388
- (java.sql.Types::NVARCHAR rescue nil) => :getString,
389
- (java.sql.Types::LONGNVARCHAR rescue nil) => :getString,
390
- java.sql.Types::BINARY => :getBinaryStream,
391
- java.sql.Types::VARBINARY => :getBinaryStream,
392
- java.sql.Types::LONGVARBINARY => :getBinaryStream,
393
- java.sql.Types::REAL => :getDouble,
394
- java.sql.Types::FLOAT => :getFloat,
395
- java.sql.Types::DOUBLE => :getDouble,
396
- java.sql.Types::DATE => :getDate,
397
- java.sql.Types::TIME => :getTime,
398
- java.sql.Types::TIMESTAMP => :getTimestamp,
399
- java.sql.Types::BLOB => :getBlob,
400
- java.sql.Types::CLOB => :getString,
401
- (java.sql.Types::NCLOB rescue nil) => :getString,
402
- java.sql.Types::BOOLEAN => :getBoolean
403
- }
404
- end
405
- end
406
-
407
- class Hash
408
- def to_sym
409
- self.inject self.class.new do |h, (k, v)|
410
- h[k.to_sym] = if v.is_a? self.class
411
- v.to_sym
412
- else
413
- v
414
- end
415
- h
416
- end
417
- end
418
177
  end
@@ -0,0 +1,39 @@
1
+ class Jvertica
2
+ module Constant
3
+ CONNECTION_PROPERTY_DOCUMENT_URL =
4
+ 'http://my.vertica.com/docs/7.1.x/HTML/index.htm#Authoring/ProgrammersGuide/ClientJDBC/JDBCConnectionProperties.htm'
5
+
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
+ GETTER_MAP = {
14
+ java.sql.Types::TINYINT => :getInt,
15
+ java.sql.Types::SMALLINT => :getInt,
16
+ java.sql.Types::INTEGER => :getInt,
17
+ java.sql.Types::BIGINT => :getLong,
18
+ java.sql.Types::CHAR => :getString,
19
+ java.sql.Types::VARCHAR => :getString,
20
+ java.sql.Types::LONGVARCHAR => :getString,
21
+ (java.sql.Types::NCHAR rescue nil) => :getString,
22
+ (java.sql.Types::NVARCHAR rescue nil) => :getString,
23
+ (java.sql.Types::LONGNVARCHAR rescue nil) => :getString,
24
+ java.sql.Types::BINARY => :getBinaryStream,
25
+ java.sql.Types::VARBINARY => :getBinaryStream,
26
+ java.sql.Types::LONGVARBINARY => :getBinaryStream,
27
+ java.sql.Types::REAL => :getDouble,
28
+ java.sql.Types::FLOAT => :getFloat,
29
+ java.sql.Types::DOUBLE => :getDouble,
30
+ java.sql.Types::DATE => :getDate,
31
+ java.sql.Types::TIME => :getTime,
32
+ java.sql.Types::TIMESTAMP => :getTimestamp,
33
+ java.sql.Types::BLOB => :getBlob,
34
+ java.sql.Types::CLOB => :getString,
35
+ (java.sql.Types::NCLOB rescue nil) => :getString,
36
+ java.sql.Types::BOOLEAN => :getBoolean
37
+ }
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ class Jvertica
2
+ class Error < StandardError; end
3
+ class ConnectionError < Error; end
4
+ class InvalidQuery < Error; end
5
+ class InvalidObject < Error; end
6
+ class InsufficientArgument < Error; end
7
+ end
@@ -0,0 +1,101 @@
1
+ require 'jvertica/row'
2
+
3
+ class Jvertica
4
+ class ResultSet
5
+ include Enumerable
6
+
7
+ def initialize(rset, &close_callback)
8
+ unless rset.respond_to?(:get_meta_data)
9
+ rset.close if rset
10
+ @closed = true
11
+ return
12
+ end
13
+
14
+ @close_callback = close_callback
15
+ @rset = rset
16
+ @rsmd = @rset.get_meta_data
17
+ @num_col = @rsmd.get_column_count
18
+ @getters = []
19
+ @col_labels = []
20
+ @col_labels_d = []
21
+
22
+ (1..@num_col).each do |i|
23
+ type = @rsmd.get_column_type(i)
24
+
25
+ @getters <<
26
+ case type
27
+ when java.sql.Types::NUMERIC, java.sql.Types::DECIMAL
28
+ precision = @rsmd.get_precision(i)
29
+ scale = @rsmd.get_scale(i)
30
+
31
+ if precision > 0 and scale >= 0
32
+ if scale > 0
33
+ :getBigDecimal
34
+ elsif precision <= 9
35
+ :getInt
36
+ elsif precision <= 18
37
+ :getLong
38
+ else
39
+ :getBigNum
40
+ end
41
+ else
42
+ :getBigDecimal
43
+ end
44
+
45
+ else Jvertica::Constant::GETTER_MAP.fetch(type)
46
+ :get_string
47
+ end
48
+
49
+ label = @rsmd.get_column_label(i)
50
+ @col_labels << label
51
+ @col_labels_d << label.downcase
52
+ end
53
+
54
+ @rownum = -1
55
+ @nrow = @rset.next
56
+ @closed = false
57
+ end
58
+
59
+ def each
60
+ return enum_for(:each) unless block_given?
61
+ return if closed?
62
+
63
+ while @nrow
64
+ idx = 0
65
+ row = Jvertica::Row.new(
66
+ @col_labels,
67
+ @col_labels_d,
68
+ @getters.map {|gt|
69
+ case gt
70
+ when :getBigNum
71
+ v = @rset.getBigDecimal(idx+=1)
72
+ @rset.was_null ? nil : v.toPlainString.to_i
73
+ when :getBigDecimal
74
+ v = @rset.getBigDecimal(idx+=1)
75
+ @rset.was_null ? nil : BigDecimal.new(v.toPlainString)
76
+ else
77
+ v = @rset.send(gt, idx+=1)
78
+ @rset.was_null ? nil : v
79
+ end
80
+ },
81
+ @rownum += 1
82
+ )
83
+ close unless @nrow = @rset.next
84
+ yield row
85
+ end
86
+ close
87
+ end
88
+
89
+ def close
90
+ return if closed?
91
+ @rset.close rescue nil
92
+ @close_callback.call if @close_callback
93
+ ensure
94
+ @closed = true
95
+ end
96
+
97
+ def closed?
98
+ @closed
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,84 @@
1
+ class Jvertica
2
+ class Row
3
+ attr_reader :labels, :values, :rownum
4
+ alias_method :keys, :labels
5
+
6
+ include Enumerable
7
+
8
+ def initialize(col_labels, col_labels_d, values, rownum)
9
+ @labels = col_labels
10
+ @labels_d = col_labels_d
11
+ @values = values
12
+ @rownum = rownum
13
+ end
14
+
15
+ def [](*idx)
16
+ return @values[*idx] if idx.length > 1
17
+
18
+ idx = idx.first
19
+ case idx
20
+ when Fixnum
21
+ raise RangeError.new("Index out of bound") if idx >= @values.length
22
+ @values[idx]
23
+ when String, Symbol
24
+ vidx = @labels_d.index(idx.to_s.downcase) or
25
+ raise NameError.new("Unknown column label: #{idx}")
26
+ @values[vidx]
27
+ else
28
+ @values[idx]
29
+ end
30
+ end
31
+
32
+ def each(&blk)
33
+ @values.each do |v|
34
+ yield v
35
+ end
36
+ end
37
+
38
+ def inspect
39
+ strs = []
40
+ @labels.each do |col|
41
+ strs << "#{col}: #{self[col] || '(null)'}"
42
+ end
43
+ '[' + strs.join(', ') + ']'
44
+ end
45
+
46
+ def to_a
47
+ @values
48
+ end
49
+
50
+ def join(sep = $OUTPUT_FIELD_SEPARATOR)
51
+ to_a.join(sep)
52
+ end
53
+
54
+ def eql?(other)
55
+ self.hash == other.hash
56
+ end
57
+
58
+ def hash
59
+ @labels.zip(@values).sort.hash
60
+ end
61
+
62
+ def to_h
63
+ Hash[@labels.zip(@values)]
64
+ end
65
+
66
+ alias :== :eql?
67
+
68
+ def method_missing(symb, *args)
69
+ if vidx = @labels_d.index(symb.to_s.downcase)
70
+ @values[vidx]
71
+ elsif @values.respond_to?(symb)
72
+ @values.send(symb, *args)
73
+ else
74
+ raise NoMethodError.new("undefined method or attribute `#{symb}'")
75
+ end
76
+ end
77
+
78
+ [:id, :tap, :gem, :display, :class, :method, :methods, :trust].select do |s|
79
+ method_defined?(s)
80
+ end.each do |m|
81
+ undef_method(m)
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  class Jvertica
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
@@ -4,8 +4,4 @@ describe Jvertica do
4
4
  it 'has a version number' do
5
5
  expect(Jvertica::VERSION).not_to be nil
6
6
  end
7
-
8
- it 'does something useful' do
9
- expect(false).to eq(true)
10
- end
11
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jvertica
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - takahiro.nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-14 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: dotenv
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  requirement: !ruby/object:Gem::Requirement
57
71
  requirements:
@@ -75,14 +89,20 @@ extra_rdoc_files: []
75
89
  files:
76
90
  - .gitignore
77
91
  - .rspec
78
- - .ruby-version
79
92
  - .travis.yml
80
93
  - Gemfile
81
94
  - LICENSE.txt
82
95
  - README.md
83
96
  - Rakefile
97
+ - example/copy.rb
98
+ - example/data.json
99
+ - example/select.rb
84
100
  - jvertica.gemspec
85
101
  - lib/jvertica.rb
102
+ - lib/jvertica/constant.rb
103
+ - lib/jvertica/error.rb
104
+ - lib/jvertica/result_set.rb
105
+ - lib/jvertica/row.rb
86
106
  - lib/jvertica/version.rb
87
107
  - spec/jvertica_spec.rb
88
108
  - spec/spec_helper.rb
@@ -106,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
126
  version: '0'
107
127
  requirements: []
108
128
  rubyforge_project:
109
- rubygems_version: 2.1.9
129
+ rubygems_version: 2.4.5
110
130
  signing_key:
111
131
  specification_version: 4
112
132
  summary: jvertica
@@ -1 +0,0 @@
1
- jruby-1.7.16