Class: Mkxms::Mssql::Utils::RaiserrorWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/mkxms/mssql/utils.rb

Overview

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.

Constant Summary collapse

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
{
  :warning => 1,
  :error => 11,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, severity: 11) ⇒ RaiserrorWriter

Returns a new instance of RaiserrorWriter.



100
101
102
103
104
105
106
107
108
# 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

Instance Attribute Details

#messageObject

Returns the value of attribute message.



111
112
113
# File 'lib/mkxms/mssql/utils.rb', line 111

def message
  @message
end

#severityObject

Returns the value of attribute severity.



111
112
113
# File 'lib/mkxms/mssql/utils.rb', line 111

def severity
  @severity
end

#state_baseObject (readonly)

Returns the value of attribute state_base.



110
111
112
# File 'lib/mkxms/mssql/utils.rb', line 110

def state_base
  @state_base
end

Instance Method Details

#current_error_markerObject



127
128
129
# File 'lib/mkxms/mssql/utils.rb', line 127

def current_error_marker
  "/*ERR*/ #{current_state} /*ERR*/"
end

#current_stateObject



131
132
133
# File 'lib/mkxms/mssql/utils.rb', line 131

def current_state
  (state_base * @index) % 256
end

#current_statement(*args, severity: nil) ⇒ Object



117
118
119
120
121
122
123
124
125
# 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) ⇒ Object



113
114
115
# File 'lib/mkxms/mssql/utils.rb', line 113

def map_severity(value)
  SYMBOLIC_SEVERITIES.fetch(value, value)
end

#next_statement(*args, **kwargs) ⇒ Object



135
136
137
# File 'lib/mkxms/mssql/utils.rb', line 135

def next_statement(*args, **kwargs)
  current_statement(*args, **kwargs).tap {@index += 1}
end