class Mkxms::Mssql::Utils::RaiserrorWriter

Create one instance of this class to write a sequence of similar RAISERROR messages. The state of each message will be unique within the sequence until the 256th message. The particular order is unique to all other instances of this class.

Constants

SYMBOLIC_SEVERITIES

Severity:

11 is the minimum to transfer into a CATCH
19 or higher can only be raised by sysadmin
20 or higher is fatal to the connection

Attributes

message[RW]
severity[RW]
state_base[R]

Public Class Methods

new(message, severity: 11) click to toggle source
# File lib/mkxms/mssql/utils.rb, line 100
def initialize(message, severity: 11)
  # Get a unique prime to use as an ideal to generate the 0-255 state-value
  # space.  With luck, the number is fairly unique to the message.
  severity = map_severity(severity)
  @state_base = RAISERROR_STATE_BASE.next
  @index = 1 # Start at 1 because 0 is the kernel -- every @state_base * 0 == 0
  @message = message
  @severity = severity
end

Public Instance Methods

current_error_marker() click to toggle source
# File lib/mkxms/mssql/utils.rb, line 127
def current_error_marker
  "/*ERR*/ #{current_state} /*ERR*/"
end
current_state() click to toggle source
# File lib/mkxms/mssql/utils.rb, line 131
def current_state
  (state_base * @index) % 256
end
current_statement(*args, severity: nil) click to toggle source
# File lib/mkxms/mssql/utils.rb, line 117
def current_statement(*args, severity: nil)
  severity = map_severity(severity || self.severity)
  state_str = current_error_marker
  full_message = %Q{N'#{message.gsub("'", "''")} (search for "#{state_str}")'}
  trailing_args = ([state_str] + args.map(&:to_s)).join(', ')
  %Q{RAISERROR (#{full_message}, #{severity}, #{trailing_args})}.tap do |stmt|
    stmt.define_singleton_method(:error_marker) {state_str}
  end
end
map_severity(value) click to toggle source
# File lib/mkxms/mssql/utils.rb, line 113
def map_severity(value)
  SYMBOLIC_SEVERITIES.fetch(value, value)
end
next_statement(*args, **kwargs) click to toggle source
# File lib/mkxms/mssql/utils.rb, line 135
def next_statement(*args, **kwargs)
  current_statement(*args, **kwargs).tap {@index += 1}
end