class G2R::Neo4J::Database

The Neo4J database that will have its data converted. It is the source of the conversion.

Public Class Methods

new(connection_options) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 7
def initialize(connection_options)
  # services
  @conn = Connection.new(connection_options)

  # data
  @labels = nil
  @label_attributes = {}
  @label_relationships = {}
  @relationships = nil
  @relationship_attributes = {}
end

Public Instance Methods

label_attributes(label) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 32
def label_attributes(label)
  if not @label_attributes.key? label
    @label_attributes[label] = @conn.query_columns("MATCH (n:#{label}) RETURN n LIMIT 1000")
  end

  @label_attributes[label]
end
label_data(label) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 48
def label_data(label)
  return_clause = label_attributes(label).map {|column| "n.#{column} as #{column}"}.join(", ")
  @conn.query_hash("MATCH (n:#{label}) RETURN id(n) as id, #{return_clause}")
end
label_relationships(label) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 40
def label_relationships(label)
  if not @label_relationships.key? label
    @label_relationships[label] = @conn.query_data("MATCH (n:#{label})-[r]->(m) UNWIND labels(m) AS label WITH label, type(r) as relationship RETURN DISTINCT relationship, label")
  end

  @label_relationships[label]
end
labels() click to toggle source

NODES

# File lib/graph2relational/neo4j-database.rb, line 22
def labels
  # lazy load labels
  if @labels.nil?
    rows = @conn.query_data("MATCH (n) UNWIND labels(n) AS label RETURN DISTINCT label ORDER BY label")
    @labels = rows.map {|row| row[0]}
  end

  @labels
end
relationship_attributes(source, type, target) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 66
def relationship_attributes(source, type, target)
  key = "#{source}_#{type}_#{target}"
  if not @relationship_attributes.has_key? key
    @relationship_attributes[key] = @conn.query_columns("MATCH (n:#{source})-[r:#{type}]->(m:#{target}) RETURN r LIMIT 1000")
  end

  @relationship_attributes[key]
end
relationship_data(source, type, target) click to toggle source
# File lib/graph2relational/neo4j-database.rb, line 75
def relationship_data(source, type, target)
  return_clause = relationship_attributes(source, type, target).map {|column| "r.#{column}"}.join(", ")
  @conn.query_data("MATCH (n:#{source})-[r:#{type}]->(m:#{target}) RETURN id(n), id(m), #{return_clause}")
end
relationships() click to toggle source

RELATIONSIIPS

# File lib/graph2relational/neo4j-database.rb, line 56
def relationships
  # lazy load relationships
  if @relationships.nil?
    rows = @conn.query_data("MATCH (n)-[r]-(m) RETURN DISTINCT type(r) AS relationship ORDER BY relationship")
    @relationships = rows.map{|row| row[0]}
  end

  @relationships
end