Class: Groonga::Schema::IndexColumnDefinition

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Path

#columns_directory_path, #rmdir_if_available, #tables_directory_path

Constructor Details

#initialize(name, options = {}) ⇒ IndexColumnDefinition

Returns a new instance of IndexColumnDefinition.



1839
1840
1841
1842
1843
1844
1845
# File 'lib/groonga/schema.rb', line 1839

def initialize(name, options={})
  @name = name
  @name = @name.to_s if @name.is_a?(Symbol)
  @options = (options || {}).dup
  @target_table = nil
  @target_columns = nil
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



1836
1837
1838
# File 'lib/groonga/schema.rb', line 1836

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



1837
1838
1839
# File 'lib/groonga/schema.rb', line 1837

def options
  @options
end

#target_columnsObject

Returns the value of attribute target_columns.



1836
1837
1838
# File 'lib/groonga/schema.rb', line 1836

def target_columns
  @target_columns
end

#target_tableObject

Returns the value of attribute target_table.



1836
1837
1838
# File 'lib/groonga/schema.rb', line 1836

def target_table
  @target_table
end

Class Method Details

.column_name(context, target_table, target_columns) ⇒ Object



1821
1822
1823
1824
# File 'lib/groonga/schema.rb', line 1821

def column_name(context, target_table, target_columns)
  target_table = resolve(context, target_table)
  "#{target_table.name}_#{target_columns.join('_')}"
end

.resolve(context, object) ⇒ Object



1826
1827
1828
1829
1830
1831
1832
1833
# File 'lib/groonga/schema.rb', line 1826

def resolve(context, object)
  return object if object.is_a?(Groonga::Object)
  if object.respond_to?(:call)
    object = object.call(context)
  end
  return nil if object.nil?
  context[object]
end

Instance Method Details

#define(table_definition, table) ⇒ Object



1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
# File 'lib/groonga/schema.rb', line 1847

def define(table_definition, table)
  context = table_definition.context
  target_table = resolve_target_table(context)
  if target_table.nil?
    raise UnknownIndexTargetTable.new(@target_table)
  end
  nonexistent_columns = nonexistent_columns(target_table)
  unless nonexistent_columns.empty?
    raise UnknownIndexTarget.new(target_table, nonexistent_columns)
  end
  name = @name || self.class.column_name(context,
                                         target_table,
                                         @target_columns)
  index = table.column(name)
  if index
    return index if same_index?(context, index, target_table)
    if @options[:force]
      index.remove
    else
      options = @options.merge(:type => :index,
                               :target_table => target_table,
                               :target_columns => @target_columns)
      raise ColumnCreationWithDifferentOptions.new(index, options)
    end
  end
  index = table.define_index_column(name,
                                    target_table,
                                    define_options(context, table, name))
  index.sources = @target_columns.collect do |column|
    target_table.column(column)
  end
  index
end