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, #cohort_analysis_statement, #cost, #explain, #initialize, #reconnect, #supports_cohort_analysis?

Constructor Details

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

Instance Method Details

#parameter_bindingObject



40
41
42
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 40

def parameter_binding
  :positional
end

#preview_statementObject



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

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

#quotingObject



35
36
37
# File 'lib/blazer/adapters/cassandra_adapter.rb', line 35

def quoting
  :single_quote_escape
end

#run_statement(statement, comment, bind_params) ⇒ Object



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

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

  begin
    response = session.execute("#{statement} /*#{comment}*/", arguments: bind_params)
    rows = response.map { |r| r.values }
    columns = rows.any? ? response.first.keys : []
  rescue => e
    error = e.message
    error = Blazer::VARIABLE_MESSAGE if error.include?("no viable alternative at input '?'")
  end

  [columns, rows, error]
end

#schemaObject



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

def schema
  result = session.execute("SELECT keyspace_name, table_name, column_name, type, position FROM system_schema.columns WHERE keyspace_name = #{data_source.quote(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



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

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