Class: Jvertica

Inherits:
Object
  • Object
show all
Defined in:
lib/jvertica.rb,
lib/jvertica/version.rb

Defined Under Namespace

Modules: Constant Classes: ConnectionError, DataSource, DriverManager, InsufficientArgument, InvalidObject, InvalidQuery, Properties, ResultSet, Row

Constant Summary collapse

VERSION =
"0.1.5"
@@default_option_value =
{
  host: 'localhost',
  port: 5433,
  database: 'vdb',
  password: '',
  user: 'dbadmin',
  AutoCommit: false,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Jvertica

Returns a new instance of Jvertica.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jvertica.rb', line 27

def initialize options
  options = @@default_option_value.merge(options).to_sym
  @host = options[:host]
  @port = options[:port]
  @database = options[:database]
  %w(:host :port :database).map do |key|
    options.delete key
  end

  prop = Properties.new
  options.each do |key, value|
    prop.put key.to_s, value
  end

  @connection = begin
                  DriverManager.getConnection "jdbc:vertica://#{host}:#{port}/#{database}", prop
                rescue => e
                  raise ConnectionError.new("Connection Failed.\n" + 
                    "Error Message => #{e.message}\n" + 
                    "see documentation => #{Constant::CONNECTION_PROPERTY_DOCUMENT_URL}\n")
                end
  @closed = false
  @connection
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



25
26
27
# File 'lib/jvertica.rb', line 25

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/jvertica.rb', line 25

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/jvertica.rb', line 25

def port
  @port
end

Class Method Details

.connect(options = {}) ⇒ Object



21
22
23
# File 'lib/jvertica.rb', line 21

def self.connect options = {}
  new options
end

Instance Method Details

#closeObject



56
57
58
# File 'lib/jvertica.rb', line 56

def close
  @connection.close && @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/jvertica.rb', line 52

def closed?
  @closed
end

#commitObject



60
61
62
# File 'lib/jvertica.rb', line 60

def commit
  @connection.commit 
end

#copy(query, source = nil, &blk) ⇒ Object

Raises:



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/jvertica.rb', line 110

def copy query, source = nil, &blk
  raise InvalidQuery.new('can use only "copy".') unless %r{\A\s*copy}miu === query
  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

    elsif block_given?
      i, o = IO.pipe
      begin
        thread = Thread.new do
                   yield(o)
                   o.close
                 end
        stream.addStream org.jruby.util.IOInputStream.new(i)
      rescue => e
        raise e
      ensure
      end
    end
    
  rescue => e
    r = stream.finish
    raise e.class.new("[affected rows: #{r}] #{e.message}")
  end

  begin
    stream.execute
    rejects = stream.getRejects
    results = stream.finish
  rescue => e
    raise e
  ensure 
    thread.join
  end

  [results, rejects.to_ary]
end

#property(key, value = nil) ⇒ Object

def execute *args, &blk

TODO

end



80
81
82
83
84
85
86
87
# File 'lib/jvertica.rb', line 80

def property key, value = nil
  key = key.to_s
  if value.nil?
    @connection.getProperty key
  else
    @connection.setProperty key, value
  end
end

#query(query, &blk) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/jvertica.rb', line 89

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*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
  else rs = stmt.executeQuery query
  end

  if block_given?
    ResultSet.new(rs).each do |row|
      yield row
    end
  else
    ResultSet.new rs
  end
end

#rollbackObject



64
65
66
# File 'lib/jvertica.rb', line 64

def rollback
  @connection.rollback
end