Class: Mkxms::Mssql::Utils::RaiserrorWriter
- Inherits:
-
Object
- Object
- Mkxms::Mssql::Utils::RaiserrorWriter
- 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
-
#message ⇒ Object
Returns the value of attribute message.
-
#severity ⇒ Object
Returns the value of attribute severity.
-
#state_base ⇒ Object
readonly
Returns the value of attribute state_base.
Instance Method Summary collapse
- #current_error_marker ⇒ Object
- #current_state ⇒ Object
- #current_statement(*args, severity: nil) ⇒ Object
-
#initialize(message, severity: 11) ⇒ RaiserrorWriter
constructor
A new instance of RaiserrorWriter.
- #map_severity(value) ⇒ Object
- #next_statement(*args, **kwargs) ⇒ Object
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(, 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 = @severity = severity end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
111 112 113 |
# File 'lib/mkxms/mssql/utils.rb', line 111 def @message end |
#severity ⇒ Object
Returns the value of attribute severity.
111 112 113 |
# File 'lib/mkxms/mssql/utils.rb', line 111 def severity @severity end |
#state_base ⇒ Object (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_marker ⇒ Object
127 128 129 |
# File 'lib/mkxms/mssql/utils.rb', line 127 def current_error_marker "/*ERR*/ #{current_state} /*ERR*/" end |
#current_state ⇒ Object
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 = %Q{N'#{.gsub("'", "''")} (search for "#{state_str}")'} trailing_args = ([state_str] + args.map(&:to_s)).join(', ') %Q{RAISERROR (#{}, #{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 |