Class: Mkxms::Mssql::ForeignKey
- Inherits:
-
Object
- Object
- Mkxms::Mssql::ForeignKey
- Includes:
- ExtendedProperties, Property::Hosting
- Defined in:
- lib/mkxms/mssql/foreign_key_handler.rb
Instance Attribute Summary collapse
-
#delete_reconciliation ⇒ Object
Returns the value of attribute delete_reconciliation.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#links ⇒ Object
readonly
Array of elements like [column_in_referrer, column_in_referent].
-
#name ⇒ Object
Returns the value of attribute name.
-
#references ⇒ Object
Returns the value of attribute references.
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#table ⇒ Object
Returns the value of attribute table.
-
#update_reconciliation ⇒ Object
Returns the value of attribute update_reconciliation.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(schema, table, name, on_delete: 'NO ACTION', on_update: 'NO ACTION', enabled: true) ⇒ ForeignKey
constructor
A new instance of ForeignKey.
- #property_subject_identifiers ⇒ Object
- #qualified_name ⇒ Object
- #qualified_table ⇒ Object
- #to_sql ⇒ Object
- #unnamed? ⇒ Boolean
Methods included from ExtendedProperties
Methods included from Property::Hosting
Constructor Details
#initialize(schema, table, name, on_delete: 'NO ACTION', on_update: 'NO ACTION', enabled: true) ⇒ ForeignKey
Returns a new instance of ForeignKey.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 13 def initialize(schema, table, name, on_delete: 'NO ACTION', on_update: 'NO ACTION', enabled: true) @schema = schema @table = table @delete_reconciliation = on_delete @update_reconciliation = on_update @enabled = enabled @name = name || self.class.generated_name @is_unnamed = !name @links = [] end |
Instance Attribute Details
#delete_reconciliation ⇒ Object
Returns the value of attribute delete_reconciliation.
25 26 27 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 25 def delete_reconciliation @delete_reconciliation end |
#enabled ⇒ Object
Returns the value of attribute enabled.
25 26 27 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 25 def enabled @enabled end |
#links ⇒ Object (readonly)
Array of elements like [column_in_referrer, column_in_referent]
27 28 29 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 27 def links @links end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24 def name @name end |
#references ⇒ Object
Returns the value of attribute references.
26 27 28 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 26 def references @references end |
#schema ⇒ Object
Returns the value of attribute schema.
24 25 26 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24 def schema @schema end |
#table ⇒ Object
Returns the value of attribute table.
24 25 26 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24 def table @table end |
#update_reconciliation ⇒ Object
Returns the value of attribute update_reconciliation.
25 26 27 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 25 def update_reconciliation @update_reconciliation end |
Class Method Details
.generated_name ⇒ Object
9 10 11 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 9 def self.generated_name "XMigra_unnamed_foreign_key_constraint_#{@anon_counter = (@anon_counter || 0) + 1}" end |
Instance Method Details
#property_subject_identifiers ⇒ Object
48 49 50 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 48 def property_subject_identifiers ['SCHEMA', schema, 'TABLE', table, 'CONSTRAINT', name].map {|n| Utils.unquoted_name(n)} end |
#qualified_name ⇒ Object
44 45 46 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 44 def qualified_name "#@schema.#@name" if @name end |
#qualified_table ⇒ Object
40 41 42 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 40 def qualified_table "#@schema.#@table" end |
#to_sql ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 29 def to_sql "ALTER TABLE #{qualified_table} ADD CONSTRAINT #@name FOREIGN KEY (%s) REFERENCES #{@references[0]}.#{@references[1]} (%s)%s%s;" % [ @links.map {|e| e[0]}.join(', '), @links.map {|e| e[1]}.join(', '), (" ON DELETE #@delete_reconciliation" if @delete_reconciliation), (" ON UPDATE #@update_reconciliation" if @update_reconciliation), ] + ( @enabled ? '' : "\nALTER TABLE #{qualified_table} NOCHECK CONSTRAINT #@name;" ) + extended_properties_sql.joined_on_new_lines end |
#unnamed? ⇒ Boolean
52 53 54 |
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 52 def unnamed? @is_unnamed end |