Class: ActiveRecord::ConnectionAdapters::Vertica

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/vertica_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
'Vertica'.freeze

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 57

def active?
  @connection.opened?
end

#adapter_nameObject

:nodoc:



53
54
55
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 53

def adapter_name #:nodoc:
  ADAPTER_NAME
end

#columns(table_name, name = nil) ⇒ Object

:nodoc:



100
101
102
103
104
105
106
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 100

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.



68
69
70
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 68

def disconnect!
  @connection.close rescue nil
end

#execute(sql, name = nil) ⇒ Object

return raw object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 73

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

#quote_column_name(name) ⇒ Object

QUOTING



117
118
119
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 117

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

#quote_table_name(name) ⇒ Object

:nodoc:



121
122
123
124
125
126
127
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 121

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.



63
64
65
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 63

def reconnect!
  @connection.reset
end

#schema_nameObject



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

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

#select(sql, name = nil) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 108

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

#tables(name = nil) ⇒ Object

:nodoc:



92
93
94
95
96
97
98
# File 'lib/active_record/connection_adapters/vertica_adapter.rb', line 92

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