Class: ActiveRecord::ConnectionAdapters::PostgreSQLIndexDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/postgresql_extensions/indexes.rb

Overview

Creates a PostgreSQL index definition. This class isn’t really meant to be used directly. Instead, see PostgreSQLAdapter#create_index for usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, object, columns, options = {}) ⇒ PostgreSQLIndexDefinition

:nodoc:



172
173
174
175
176
177
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 172

def initialize(base, name, object, columns, options = {}) #:nodoc:
  assert_valid_columns(columns)
  assert_valid_fill_factor(options[:fill_factor])

  @base, @name, @object, @columns, @options = base, name, object, columns, options
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



170
171
172
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 170

def base
  @base
end

#columnsObject

Returns the value of attribute columns.



170
171
172
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 170

def columns
  @columns
end

#nameObject

Returns the value of attribute name.



170
171
172
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 170

def name
  @name
end

#objectObject

Returns the value of attribute object.



170
171
172
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 170

def object
  @object
end

#optionsObject

Returns the value of attribute options.



170
171
172
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 170

def options
  @options
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/active_record/postgresql_extensions/indexes.rb', line 179

def to_sql #:nodoc:
  sql = 'CREATE '
  sql << 'UNIQUE ' if options[:unique]
  sql << 'INDEX '
  sql << 'CONCURRENTLY ' if options[:concurrently]
  sql << "#{base.quote_generic(name)} ON #{base.quote_table_name(object)}"
  sql << " USING #{base.quote_generic(options[:using])}" if options[:using]
  sql << '('
  sql << [ columns ].flatten.collect do |column|
    column_def = String.new
    if column.is_a?(Hash)
      column_def << if column[:column]
        "#{base.quote_column_name(column[:column])}"
      else
        "(#{column[:expression]})"
      end

      column_def << " #{base.quote_generic(column[:opclass])}" if column[:opclass]
      column_def << " #{column[:order].to_s.upcase}" if column[:order]
      column_def << " NULLS #{column[:nulls].to_s.upcase}" if column[:nulls]
      column_def
    else
      base.quote_column_name(column.to_s)
    end
  end.join(', ')
  sql << ')'
  sql << " WITH (FILLFACTOR = #{options[:fill_factor].to_i})" if options[:fill_factor]
  sql << " WITH (#{ActiveRecord::PostgreSQLExtensions::Utils.options_from_hash_or_string(options[:index_parameters], base)})" if options[:index_parameters].present?
  sql << " TABLESPACE #{base.quote_tablespace(options[:tablespace])}" if options[:tablespace]
  sql << " WHERE (#{options[:conditions] || options[:where]})" if options[:conditions] || options[:where]
  "#{sql};"
end