Class: XMigra::DeclarativeSupport::Table::ForeignKey

Inherits:
ColumnListConstraint show all
Defined in:
lib/xmigra/declarative_support/table.rb

Constant Summary collapse

IDENTIFIER =
"foreign key"
IMPLICIT_PREFIX =
"FK_"

Constants inherited from Constraint

Constraint::SUBTYPES

Instance Attribute Summary collapse

Attributes inherited from ColumnListConstraint

#columns

Attributes inherited from Constraint

#name

Instance Method Summary collapse

Methods inherited from Constraint

bad_spec, #constraint_type, deserialize, each_type, implicit_type, inherited, #only_on_column_at_creation?, type_by_identifier

Constructor Details

#initialize(name, constr_spec) ⇒ ForeignKey

Returns a new instance of ForeignKey.



221
222
223
224
225
226
227
228
# File 'lib/xmigra/declarative_support/table.rb', line 221

def initialize(name, constr_spec)
  super(name, constr_spec)
  @referent = constr_spec['link to'] || Constraint.bad_spec(
    %Q{Foreign key constraint #{@name} does not specify "link to" (referent)}
  )
  @update_rule = constr_spec['on update'].tap {|v| break v.upcase if v}
  @delete_rule = constr_spec['on delete'].tap {|v| break v.upcase if v}
end

Instance Attribute Details

#delete_ruleObject

Returns the value of attribute delete_rule.



230
231
232
# File 'lib/xmigra/declarative_support/table.rb', line 230

def delete_rule
  @delete_rule
end

#referentObject

Returns the value of attribute referent.



230
231
232
# File 'lib/xmigra/declarative_support/table.rb', line 230

def referent
  @referent
end

#update_ruleObject

Returns the value of attribute update_rule.



230
231
232
# File 'lib/xmigra/declarative_support/table.rb', line 230

def update_rule
  @update_rule
end

Instance Method Details

#constrained_colnamesObject



232
233
234
# File 'lib/xmigra/declarative_support/table.rb', line 232

def constrained_colnames
  columns.keys
end

#creation_sqlObject



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/xmigra/declarative_support/table.rb', line 240

def creation_sql
  "".tap do |result|
    result << creation_name_sql
    result << "FOREIGN KEY (#{constrained_colnames.join(', ')})"
    result << "\n    REFERENCES #{referent} (#{referenced_colnames.join(', ')})"
    if update_rule
      result << "\n    ON UPDATE #{update_rule}"
    end
    if delete_rule
      result << "\n    ON DELETE #{delete_rule}"
    end
  end
end

#referenced_colnamesObject



236
237
238
# File 'lib/xmigra/declarative_support/table.rb', line 236

def referenced_colnames
  columns.values
end