Class: ActiveRecord::ConnectionAdapters::PostgreSQLSequenceDefinition

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

Overview

Class used to create or alter sequences. Generally you should be using PostgreSQLAdapter#create_sequence and its various sequence manipulation functions rather than using this class directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, action, name, options = {}) ⇒ PostgreSQLSequenceDefinition

:nodoc:



140
141
142
143
144
145
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 140

def initialize(base, action, name, options = {}) #:nodoc:
  assert_valid_owned_by(options)
  assert_valid_action(action)

  @base, @action, @name, @options = base, action, name, options
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



138
139
140
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 138

def action
  @action
end

#baseObject

Returns the value of attribute base.



138
139
140
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 138

def base
  @base
end

#nameObject

Returns the value of attribute name.



138
139
140
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 138

def name
  @name
end

#optionsObject

Returns the value of attribute options.



138
139
140
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 138

def options
  @options
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/active_record/postgresql_extensions/sequences.rb', line 147

def to_sql #:nodoc:
  sql = Array.new
  if action == :create
    sql << 'CREATE'
    sql << 'TEMPORARY' if options[:temporary]
  else
    sql << 'ALTER'
  end
  sql << "SEQUENCE #{base.quote_sequence(name)}"
  sql << "INCREMENT BY #{options[:increment].to_i}" if options[:increment]
  if options.has_key?(:min_value)
    sql << case options[:min_value]
      when NilClass, FalseClass
        'NO MINVALUE'
      else
        "MINVALUE #{options[:min_value].to_i}"
    end
  end

  if options.has_key?(:max_value)
    sql << case options[:max_value]
      when NilClass, FalseClass
        'NO MAXVALUE'
      else
        "MAXVALUE #{options[:max_value].to_i}"
    end
  end
  sql << "START WITH #{options[:start].to_i}" if options[:start]
  sql << "CACHE #{options[:cache].to_i}" if options[:cache]

  if options.has_key?(:cycle)
    sql << (options[:cycle] ? 'CYCLE' : 'NO CYCLE')
  end

  if options.has_key?(:owned_by)
    table_column = if options[:owned_by].is_a?(Hash)
      [ options[:owned_by][:table], options[:owned_by][:column] ]
    elsif options[:owned_by].is_a?(Array)
      options[:owned_by]
    end

    sql << 'OWNED BY ' + if options[:owned_by] == :none
      'NONE'
    else
      "#{base.quote_table_name(table_column.first)}.#{base.quote_column_name(table_column.last)}"
    end
  end

  if action != :create && options.has_key?(:restart_with)
    sql << "RESTART WITH #{options[:restart_with].to_i}"
  end
  "#{sql.join(' ')};"
end