Class: Mkxms::Mssql::Trigger

Inherits:
Object
  • Object
show all
Extended by:
Utils::InitializedAttributes
Includes:
Dependencies, ExtendedProperties, Property::Hosting, Property::SchemaScoped, Utils::SchemaQualifiedName, XMigra::MSSQLSpecifics
Defined in:
lib/mkxms/mssql/dml_trigger_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::InitializedAttributes

attr_init

Methods included from Utils::SchemaQualifiedName

#qualified_name

Methods included from Dependencies

#dependencies

Methods included from ExtendedProperties

#extended_properties

Methods included from Property::Hosting

#extended_properties_sql

Methods included from Property::SchemaScoped

#property_subject_identifiers, #subitem_extended_properties_sql

Constructor Details

#initialize(schema, name, timing, execute_as: nil, disabled: false, not_replicable: false) ⇒ Trigger

Returns a new instance of Trigger.



15
16
17
18
19
20
21
22
# File 'lib/mkxms/mssql/dml_trigger_handler.rb', line 15

def initialize(schema, name, timing, execute_as: nil, disabled: false, not_replicable: false)
  @schema = schema
  @name = name
  @timing = timing
  @execute_as = execute_as
  @disabled = disabled
  @not_replicable = not_replicable
end

Instance Attribute Details

#clr_implObject

Returns the value of attribute clr_impl.



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

def clr_impl
  @clr_impl
end

#disabledObject

Returns the value of attribute disabled.



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

def disabled
  @disabled
end

#execute_asObject

Returns the value of attribute execute_as.



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

def execute_as
  @execute_as
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#not_replicableObject

Returns the value of attribute not_replicable.



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

def not_replicable
  @not_replicable
end

#schemaObject

Returns the value of attribute schema.



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

def schema
  @schema
end

#tableObject

Returns the value of attribute table.



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

def table
  @table
end

#timingObject

Returns the value of attribute timing.



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

def timing
  @timing
end

Instance Method Details

#clr_definitionObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mkxms/mssql/dml_trigger_handler.rb', line 40

def clr_definition
  [].tap do |lines|
    lines << "CREATE TRIGGER #{schema}.#{name}"
    lines << "ON #{table.qualified_name}"
    case execute_as
    when 'OWNER'
      lines << "WITH EXECUTE AS OWNER"
    when String
      lines << "WITH EXECUTE AS #{execute_as.sql_quoted}"
    end
    lines << "#{timing} #{events.join(', ')}"
    lines << "NOT FOR REPLICATION" if not_replicable
    lines << "AS EXTERNAL NAME #{clr_impl.full_specifier};"
  end.join("\n")
end

#to_sqlObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mkxms/mssql/dml_trigger_handler.rb', line 28

def to_sql
  def_sql = clr_impl ? clr_definition : definition
  [def_sql.expand_tabs.gsub(/ +\n/, "\n")].tap do |result|
    unless (ep_sql = extended_properties_sql).empty?
      result << ep_sql.joined_on_new_lines
    end
    if disabled
      result << "DISABLE TRIGGER #{qualified_name} ON #{table.qualified_name};"
    end
  end.join(ddl_block_separator)
end