lib/jvertica.rb in jvertica-0.1.6 vs lib/jvertica.rb in jvertica-0.1.7
- old
+ new
@@ -14,11 +14,11 @@
port: 5433,
database: 'vdb',
password: '',
user: 'dbadmin',
AutoCommit: false,
- }
+ }
def self.connect options = {}
new options
end
@@ -39,12 +39,12 @@
end
@connection = begin
DriverManager.getConnection "jdbc:vertica://#{host}:#{port}/#{database}", prop
rescue => e
- raise ConnectionError.new("Connection Failed.\n" +
- "Error Message => #{e.message}\n" +
+ raise ConnectionError.new("Connection Failed.\n" +
+ "Error Message => #{e.message}\n" +
"see documentation => #{Constant::CONNECTION_PROPERTY_DOCUMENT_URL}\n")
end
@closed = false
@connection
end
@@ -56,11 +56,11 @@
def close
@connection.close && @closed = true
end
def commit
- @connection.commit
+ @connection.commit
end
def rollback
@connection.rollback
end
@@ -87,11 +87,11 @@
end
def query query, &blk
stmt = @connection.createStatement
case query
- when %r{\A\s*copy}miu then raise InvalidQuery.new('cannot use "copy".')
+ when %r{\A\s*copy}miu then return stmt.execute query
when %r{\A\s*insert}miu then return stmt.executeUpdate query
when %r{\A\s*update}miu then return stmt.executeUpdate query
when %r{\A\s*delete}miu then return stmt.executeUpdate query
when %r{\A\s*drop}miu then return stmt.execute query
when %r{\A\s*create}miu then return stmt.execute query
@@ -108,23 +108,26 @@
end
end
def copy query, source = nil, &blk
raise InvalidQuery.new('can use only "copy".') unless %r{\A\s*copy}miu === query
+ if !source.nil?
+ copy_stream(query, source, &blk)
+ else
+ [query(query), nil]
+ end
+ end
+
+ private
+ def copy_stream query, io = nil, &blk
stream = com.vertica.jdbc.VerticaCopyStream.new @connection, query
stream.start
thread = nil
begin
- if !source.nil?
- if source.is_a? IO
- stream.addStream org.jruby.util.IOInputStream.new(source)
- else
- raise InvalidObject.new("source must be a IO.")
- end
+ if block_given?
- elsif block_given?
i, o = IO.pipe
begin
thread = Thread.new do
yield(o)
o.close
@@ -132,12 +135,21 @@
stream.addStream org.jruby.util.IOInputStream.new(i)
rescue => e
raise e
ensure
end
+
+ else
+
+ if source.is_a? IO
+ stream.addStream org.jruby.util.IOInputStream.new(source)
+ else
+ raise InvalidObject.new("source must be a IO.")
+ end
+
end
-
+
rescue => e
r = stream.finish
raise e.class.new("[affected rows: #{r}] #{e.message}")
end
@@ -145,18 +157,17 @@
stream.execute
rejects = stream.getRejects
results = stream.finish
rescue => e
raise e
- ensure
- thread.join
+ ensure
+ thread.join unless thread.nil?
end
[results, rejects.to_ary]
end
- private
class ConnectionError < StandardError
end
class InvalidQuery < StandardError
end
@@ -355,10 +366,10 @@
end
module Constant
CONNECTION_PROPERTY_DOCUMENT_URL =
'http://my.vertica.com/docs/7.1.x/HTML/index.htm#Authoring/ProgrammersGuide/ClientJDBC/JDBCConnectionProperties.htm'
-
+
RUBY_SQL_TYPE_MAP = {
Fixnum => java.sql.Types::INTEGER,
Bignum => java.sql.Types::BIGINT,
String => java.sql.Types::VARCHAR,
Float => java.sql.Types::DOUBLE,