class G2R::RDBMS::Database

The target RDBMS database that will have the schema and data generated for. It is the target of the conversion.

Public Class Methods

new(connection_options, app_options = {}) click to toggle source

INITIALIZATION

# File lib/graph2relational/rdbms-database.rb, line 10
def initialize(connection_options, app_options = {})
  # services
  @options = app_options
  @conn = Connection.new(connection_options)

  # data
  reset_data()
end

Public Instance Methods

base_tables() click to toggle source

TABLES

Get all the base tables that will be generated. They are based on the Neo4J labels. Columns are based on the Neo4J node attributes.

# File lib/graph2relational/rdbms-database.rb, line 69
def base_tables
  # lazy initialization
  if @base_tables.nil?
    @base_tables = @source.labels.map do |label|

      # if excluding label, do not transform into base table
      next if exclude_label? label

      # generate table
      table = Table.new(label)

      # generate primary key
      table.add_columns(Column.new('id').primary_key)

      # generate schema columns
      columns = @source.label_attributes(label).map {|attribute| Column.new(attribute)}
      table.add_columns(columns)

      # add forced columns
      if @options.has_key? :additional_base_columns
        forced_columns = @options[:additional_base_columns].map {|name| Column.new(name)}
        table.add_columns(forced_columns)
      end

      # generate data
      label_data = @source.label_data(label)
      table.add_data(label_data)

      # generated table
      table
    end.compact
  end

  @base_tables.compact
end
convert() click to toggle source

Export the schema creation and data import scripts to the specified location

# File lib/graph2relational/rdbms-database.rb, line 31
def convert
  # prepare
  reset_data()

  # create tables
  puts "Creating base tables schema"
  base_tables.each do |table|
    @conn.create_table(table)
  end

  puts "Creating relationship tables schema"
  relationship_tables.each do |table|
    @conn.create_table(table)
  end

  # insert data
  puts "Inserting base tables data"
  base_tables.each do |table|
    @conn.insert_data(table)
  end

  puts "Inserting relationship tables data"
  relationship_tables.each do |table|
    @conn.insert_data(table)
  end
end
exclude_label?(label) click to toggle source

Check if a label should be excluded and not be transformed into a table

# File lib/graph2relational/rdbms-database.rb, line 153
def exclude_label?(label)
  @options.has_key? :exclude_labels and @options[:exclude_labels].include? label
end
exclude_relationship?(relationship) click to toggle source

Check if a relationship should be excluded and not be transformed into a relationship table

# File lib/graph2relational/rdbms-database.rb, line 158
def exclude_relationship?(relationship)
  @options.has_key? :exclude_relationships and @options[:exclude_relationships].include? relationship
end
relationship_tables() click to toggle source

Get all the relationships tables that will be generated. They are based in the found Neo4J relationships. Columss are based on the Neo4J relationship attributes.

# File lib/graph2relational/rdbms-database.rb, line 107
def relationship_tables
  # lazy initialization
  if @relationship_tables.nil?
    @relationship_tables = @source.labels.flat_map do |label|

      # if excluding label, do not transform into relationship table
      next if exclude_label? label

      @source.label_relationships(label).map do |relationship|
        relationship, target_label = relationship

        # if excluding label, do not transform into relationship table
        next if exclude_relationship? relationship
        next if exclude_label? target_label

        # gerarate table
        table = JoinTable.new(label, relationship, target_label)

        # generate columns
        columns = @source.relationship_attributes(label, relationship, target_label).map {|attribute| Column.new(attribute)}
        table.add_columns(columns)

        # add forced columns
        if @options.has_key? :additional_relationship_columns
          forced_columns = @options[:additional_relationship_columns].map {|name| Column.new(name)}
          table.add_columns(forced_columns)
        end

        # generate data
        relationship_data = @source.relationship_data(label, relationship, target_label)
        table.add_data(relationship_data)

        # generated table
        table
      end
    end.compact
  end

  @relationship_tables
end
reset_data() click to toggle source

Reset the saved data to perform a new conversion

# File lib/graph2relational/rdbms-database.rb, line 59
def reset_data
  @base_tables = nil
  @relationship_tables = nil
end
source() click to toggle source
# File lib/graph2relational/rdbms-database.rb, line 26
def source
  @source
end
source=(source) click to toggle source

CONVERSION

# File lib/graph2relational/rdbms-database.rb, line 22
def source=(source)
  @source = source
end