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.2.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Jvertica

Returns a new instance of Jvertica.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jvertica.rb', line 65

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.



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

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



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

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

.quote(value) ⇒ String

Properly quotes a value for safe usage in SQL queries.

This method has quoting rules for common types. Any other object will be converted to a string using :to_s and then quoted as a string.

Parameters:

  • value (Object)

    The value to quote.

Returns:

  • (String)

    The quoted value that can be safely included in SQL queries.



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

def self.quote(value)
  case value
  when nil        then 'NULL'
  when false      then 'FALSE'
  when true       then 'TRUE'
  when DateTime   then value.strftime("'%Y-%m-%d %H:%M:%S'::timestamp")
  when Time       then value.strftime("'%Y-%m-%d %H:%M:%S'::timestamp")
  when Date       then value.strftime("'%Y-%m-%d'::date")
  when String     then "'#{value.gsub(/'/, "''")}'"
  when Numeric    then value.to_s
  when Array      then value.map { |v| self.quote(v) }.join(', ')
  else
    if defined?(BigDecimal) and BigDecimal === value
      value.to_s('F')
    else
      self.quote(value.to_s)
    end
  end
end

.quote_identifier(identifier) ⇒ String

Quotes an identifier for safe use within SQL queries, using double quotes.

Parameters:

  • identifier (:to_s)

    The identifier to quote.

Returns:

  • (String)

    The quoted identifier that can be safely included in SQL queries.



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

def self.quote_identifier(identifier)
  "\"#{identifier.to_s.gsub(/"/, '""')}\""
end

Instance Method Details

#closeObject



96
97
98
99
100
101
# File 'lib/jvertica.rb', line 96

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

#closed?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/jvertica.rb', line 92

def closed?
  @closed
end

#commitObject



103
104
105
# File 'lib/jvertica.rb', line 103

def commit
  @connection.commit
end

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

Raises:



156
157
158
159
160
161
162
163
# File 'lib/jvertica.rb', line 156

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



123
124
125
126
127
128
129
130
# File 'lib/jvertica.rb', line 123

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



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 132

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*alter}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)
  when %r{\A\s*grant}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



107
108
109
# File 'lib/jvertica.rb', line 107

def rollback
  @connection.rollback
end