Class: Blazer::Adapters::Neo4jAdapter

Inherits:
BaseAdapter show all
Defined in:
lib/blazer/adapters/neo4j_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, #schema, #supports_cohort_analysis?

Constructor Details

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

Instance Method Details

#parameter_bindingObject



53
54
55
56
57
58
59
60
# File 'lib/blazer/adapters/neo4j_adapter.rb', line 53

def parameter_binding
  proc do |statement, variables|
    variables.each_key do |k|
      statement = statement.gsub("{#{k}}") { "$#{k} " }
    end
    [statement, variables]
  end
end

#preview_statementObject



44
45
46
# File 'lib/blazer/adapters/neo4j_adapter.rb', line 44

def preview_statement
  "MATCH (n:{table}) RETURN n LIMIT 10"
end

#quotingObject



49
50
51
# File 'lib/blazer/adapters/neo4j_adapter.rb', line 49

def quoting
  :backslash_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
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/blazer/adapters/neo4j_adapter.rb', line 4

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

  begin
    if bolt?
      result = session.run("#{statement} /*#{comment}*/", bind_params).to_a
      columns = result.any? ? result.first.keys.map(&:to_s) : []
      rows = result.map(&:values)
    else
      result = session.query("#{statement} /*#{comment}*/", bind_params)
      columns = result.columns.map(&:to_s)
      rows = []
      result.each do |row|
        rows << columns.map do |c|
          v = row.send(c)
          v = v.properties if v.respond_to?(:properties)
          v
        end
      end
    end
  rescue => e
    error = e.message
    error = Blazer::VARIABLE_MESSAGE if error.include?("Invalid input '$'")
  end

  [columns, rows, error]
end

#tablesObject



34
35
36
37
38
39
40
41
42
# File 'lib/blazer/adapters/neo4j_adapter.rb', line 34

def tables
  if bolt?
    result = session.run("CALL db.labels()").to_a
    result.map { |r| r.values.first }
  else
    result = session.query("CALL db.labels()")
    result.rows.map(&:first)
  end
end