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.



227
228
229
230
231
232
233
234
# File 'lib/xmigra/declarative_support/table.rb', line 227

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.



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

def delete_rule
  @delete_rule
end

#referentObject

Returns the value of attribute referent.



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

def referent
  @referent
end

#update_ruleObject

Returns the value of attribute update_rule.



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

def update_rule
  @update_rule
end

Instance Method Details

#constrained_colnamesObject



238
239
240
# File 'lib/xmigra/declarative_support/table.rb', line 238

def constrained_colnames
  columns.keys
end

#creation_sqlObject



246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/xmigra/declarative_support/table.rb', line 246

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



242
243
244
# File 'lib/xmigra/declarative_support/table.rb', line 242

def referenced_colnames
  columns.values
end