Class: ActiveRecord::ConnectionAdapters::VerticaAdapter

Inherits:
AbstractAdapter
  • Object
show all
Includes:
Import::VerticaAdapter::InstanceMethods
Defined in:
lib/active_record/connection_adapters/vertica_adapter.rb,
lib/activerecord-import/active_record/adapters/vertica_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'Vertica'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(connection, connection_options, config) ⇒ VerticaAdapter

Returns a new instance of VerticaAdapter.



86
87
88
89
90
91
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 86

def initialize(connection, connection_options, config)
  super(connection)
  @connection_options, @config = connection_options, config
  @quoted_column_names, @quoted_table_names = {}, {}
  # connect
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 93

def active?
  @connection.opened?
end

#adapter_nameObject

:nodoc:



82
83
84
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 82

def adapter_name #:nodoc:
  ADAPTER_NAME
end

#columns(table_name, name = nil) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 140

def columns(table_name, name = nil)#:nodoc:
  sql = "SELECT * FROM columns WHERE table_name = #{quote_column_name(table_name)}"

  columns = []
  execute(sql, name){ |field| columns << VerticaColumn.new(field[:column_name],field[:column_default],field[:data_type],field[:is_nullable])}
  columns
end

#disconnect!Object

Close the connection.



108
109
110
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 108

def disconnect!
  @connection.close rescue nil
end

#execute(sql, name = nil) ⇒ Object

return raw object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 113

def execute(sql, name=nil)
  log(sql,name) do
    if block_given?
      @connection = ::Vertica.connect(@connection.options)
      @connection.query(sql) {|row| yield row }
      @connection.close
    else
      @connection = ::Vertica.connect(@connection.options)
      results = @connection.query(sql)
      @connection.close
      results
    end
  end
end

#primary_key(table) ⇒ Object



156
157
158
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 156

def primary_key(table)
  ''
end

#quote_column_name(name) ⇒ Object

QUOTING



161
162
163
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 161

def quote_column_name(name) #:nodoc:
  "'#{name}'"
end

#quote_table_name(name) ⇒ Object

:nodoc:



165
166
167
168
169
170
171
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 165

def quote_table_name(name) #:nodoc:
  if schema_name.blank?
    name
  else
    "#{schema_name}.#{name}"
  end
end

#reconnect!Object

Disconnects from the database if already connected, and establishes a new connection with the database.



99
100
101
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 99

def reconnect!
  @connection.reset_connection
end

#resetObject



103
104
105
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 103

def reset
  reconnect!
end

#schema_nameObject



128
129
130
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 128

def schema_name
  @schema ||= @connection.options[:schema]
end

#select(sql, name = nil, binds = []) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 148

def select(sql, name = nil, binds = [])
  rows = []
  @connection = ::Vertica.connect(@connection.options)
  @connection.query(sql) {|row| rows << row }
  @connection.close
  rows
end

#tables(name = nil) ⇒ Object

:nodoc:



132
133
134
135
136
137
138
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 132

def tables(name = nil) #:nodoc:
  sql = "SELECT * FROM tables WHERE table_schema = #{quote_column_name(schema_name)}"

  tables = []
  execute(sql, name) { |field| tables << field[:table_name] }
  tables
end