Class: G2R::RDBMS::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/graph2relational/rdbms-database.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(connection_options, app_options = {}) ⇒ Database

INITIALIZATION



10
11
12
13
14
15
16
17
# 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

Instance Method Details

#base_tablesObject

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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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

#convertObject

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


153
154
155
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


158
159
160
# 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_tablesObject

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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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_dataObject

Reset the saved data to perform a new conversion



59
60
61
62
# File 'lib/graph2relational/rdbms-database.rb', line 59

def reset_data
  @base_tables = nil
  @relationship_tables = nil
end

#sourceObject



26
27
28
# File 'lib/graph2relational/rdbms-database.rb', line 26

def source
  @source
end

#source=(source) ⇒ Object

CONVERSION



22
23
24
# File 'lib/graph2relational/rdbms-database.rb', line 22

def source=(source)
  @source = source
end