Class: Mkxms::Mssql::ForeignKey

Inherits:
Object
  • Object
show all
Includes:
ExtendedProperties, Property::Hosting
Defined in:
lib/mkxms/mssql/foreign_key_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExtendedProperties

#extended_properties

Methods included from Property::Hosting

#extended_properties_sql

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_reconciliationObject

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

#enabledObject

Returns the value of attribute enabled.



25
26
27
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 25

def enabled
  @enabled
end

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

#nameObject

Returns the value of attribute name.



24
25
26
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24

def name
  @name
end

#referencesObject

Returns the value of attribute references.



26
27
28
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 26

def references
  @references
end

#schemaObject

Returns the value of attribute schema.



24
25
26
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24

def schema
  @schema
end

#tableObject

Returns the value of attribute table.



24
25
26
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 24

def table
  @table
end

#update_reconciliationObject

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_nameObject



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_identifiersObject



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_nameObject



44
45
46
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 44

def qualified_name
  "#@schema.#@name" if @name
end

#qualified_tableObject



40
41
42
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 40

def qualified_table
  "#@schema.#@table"
end

#to_sqlObject



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/mkxms/mssql/foreign_key_handler.rb', line 52

def unnamed?
  @is_unnamed
end