Class: Jvertica

Inherits:
Object
  • Object
show all
Defined in:
lib/jvertica.rb,
lib/jvertica/row.rb,
lib/jvertica/error.rb,
lib/jvertica/version.rb,
lib/jvertica/constant.rb,
lib/jvertica/result_set.rb

Defined Under Namespace

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

Constant Summary collapse

DEFAULT_OPTION_VALUES =
{
  host: 'localhost',
  port: 5433,
  database: 'vdb',
  password: '',
  user: 'dbadmin',
  AutoCommit: false,
}
VERSION =
"0.1.11"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Jvertica

Returns a new instance of Jvertica.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jvertica.rb', line 31

def initialize(options)
  options   = options.inject({}) {|h, (k, v)| h[k.to_sym] = v; h } # symbolize_keys
  options   = DEFAULT_OPTION_VALUES.merge(options)
  @host     = options.delete(:host)
  @port     = options.delete(:port)
  @database = options.delete(:database)

  prop = Properties.new
  options.each do |key, value|
    prop.put(key.to_s, value) unless value.nil?
  end

  @connection =
    begin
      com.vertica.jdbc.Driver.new.connect("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.



29
30
31
# File 'lib/jvertica.rb', line 29

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



29
30
31
# File 'lib/jvertica.rb', line 29

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



29
30
31
# File 'lib/jvertica.rb', line 29

def port
  @port
end

Class Method Details

.connect(options = {}) ⇒ Object



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

def self.connect(options = {})
  new(options)
end

Instance Method Details

#closeObject



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

def close
  @connection.close
ensure
  @connection = nil
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/jvertica.rb', line 58

def closed?
  @closed
end

#commitObject



69
70
71
# File 'lib/jvertica.rb', line 69

def commit
  @connection.commit
end

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

Raises:



120
121
122
123
124
125
126
127
# File 'lib/jvertica.rb', line 120

def copy(query, source = nil, &blk)
  raise InvalidQuery.new('can use only "copy".') unless %r{\A\s*copy}miu === query
  if source or block_given?
    copy_stream(query, source, &blk)
  else
    [query(query), nil]
  end
end

#property(key, value = nil) ⇒ Object

def execute *args, &blk

TODO

end



89
90
91
92
93
94
95
96
# File 'lib/jvertica.rb', line 89

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jvertica.rb', line 98

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



73
74
75
# File 'lib/jvertica.rb', line 73

def rollback
  @connection.rollback
end