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.7"
@@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:



111
112
113
114
115
116
117
118
# File 'lib/jvertica.rb', line 111

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

#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
109
# File 'lib/jvertica.rb', line 89

def query query, &blk
  stmt = @connection.createStatement
  case query
  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
  when %r{\A\s*set}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