Class: Blazer::Adapters::CassandraAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/blazer/adapters/cassandra_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#data_source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#cachable?, #cancel, #cost, #explain, #initialize, #reconnect

Constructor Details

This class inherits a constructor from Blazer::Adapters::BaseAdapter

Instance Method Details

#preview_statementObject



29
30
31
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 29

def preview_statement
  "SELECT * FROM {table} LIMIT 10"
end

#run_statement(statement, comment) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 4

def run_statement(statement, comment)
  columns = []
  rows = []
  error = nil

  begin
    response = session.execute("#{statement} /*#{comment}*/")
    rows = response.map { |r| r.values }
    columns = rows.any? ? response.first.keys : []
  rescue => e
    error = e.message
  end

  [columns, rows, error]
end

#schemaObject



24
25
26
27
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 24

def schema
  result = session.execute("SELECT keyspace_name, table_name, column_name, type, position FROM system_schema.columns WHERE keyspace_name = '#{keyspace}'")
  result.map(&:values).group_by { |r| [r[0], r[1]] }.map { |k, vs| {schema: k[0], table: k[1], columns: vs.sort_by { |v| v[2] }.map { |v| {name: v[2], data_type: v[3]} }} }
end

#tablesObject



20
21
22
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 20

def tables
  session.execute("SELECT table_name FROM system_schema.tables WHERE keyspace_name = '#{keyspace}'").map { |r| r["table_name"] }
end