Class: Groonga::Schema::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/schema.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dumper

Returns a new instance of Dumper.



840
841
842
# File 'lib/groonga/schema.rb', line 840

def initialize(options={})
  @options = (options || {}).dup
end

Instance Method Details

#dumpObject



844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
# File 'lib/groonga/schema.rb', line 844

def dump
  context = @options[:context] || Groonga::Context.default
  database = context.database
  return nil if database.nil?

  reference_columns = []
  definitions = []
  database.each do |object|
    next unless object.is_a?(Groonga::Table)
    schema = "create_table(#{object.name.inspect}) do |table|\n"
    object.columns.sort_by {|column| column.local_name}.each do |column|
      if column.range.is_a?(Groonga::Table)
        reference_columns << column
      else
        type = column_method(column)
        name = column.local_name
        schema << "  table.#{type}(#{name.inspect})\n"
      end
    end
    schema << "end"
    definitions << schema
  end

  reference_columns.group_by do |column|
    column.table
  end.each do |table, columns|
    schema = "change_table(#{table.name.inspect}) do |table|\n"
    columns.each do |column|
      name = column.local_name
      reference = column.range
      schema << "  table.reference(#{name.inspect}, " +
                                  "#{reference.name.inspect})\n"
    end
    schema << "end"
    definitions << schema
  end

  if definitions.empty?
    ""
  else
    definitions.join("\n\n") + "\n"
  end
end