Module: Mkxms::Mssql::Utils

Defined in:
lib/mkxms/mssql/utils.rb

Defined Under Namespace

Modules: FlagsQueries, InitializedAttributes, SchemaQualifiedName, StringHelpers Classes: NameRefGraph, RaiserrorWriter

Constant Summary collapse

INVALID_NAME_CHAR =
/[^A-Za-z0-9_]/
RAISERROR_STATE_BASE =

Primes in the interval [100, 255]. This enumerator can be queried by classes that generate RAISERROR statements to provide unique-ish context by passing the next multiple of one of the values taken from this enumerator for each RAISERROR statement output (as a literal number in the generated SQL). This will assist

[
  101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163 ,167, 173,
  179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251
].each

Class Method Summary collapse

Class Method Details

.chars_to_tab(prev, tab_width: 4) ⇒ Object



155
156
157
# File 'lib/mkxms/mssql/utils.rb', line 155

def chars_to_tab(prev, tab_width: 4)
  (prev.chars.length + 3) % 4 + 1
end

.code_sym_for(s) ⇒ Object



142
143
144
# File 'lib/mkxms/mssql/utils.rb', line 142

def code_sym_for(s)
  s.gsub(self::INVALID_NAME_CHAR, '_').downcase.to_sym
end

.dry_run?Boolean

Returns:

  • (Boolean)


170
171
172
173
174
# File 'lib/mkxms/mssql/utils.rb', line 170

def dry_run?
  @dry_run.tap do |v|
    (break @dry_run = !!(ENV.fetch('DRY_RUN', '') =~ /^(y(es)?|t(rue)?|1)$/i)) if v.nil?
  end
end

.expand_tabs(s, tab_width: 4) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/mkxms/mssql/utils.rb', line 159

def expand_tabs(s, tab_width: 4)
  return s unless s.include? "\t"
  
  s.each_line.map do |l|
    while l.include? "\t"
      l.sub!("\t") {|m| ' ' * chars_to_tab($`, tab_width: tab_width)}
    end
    l
  end.join('')
end

.newline_prefixed(s) ⇒ Object



151
152
153
# File 'lib/mkxms/mssql/utils.rb', line 151

def newline_prefixed(s)
  "\n" + s
end

.unquoted_name(s) ⇒ Object



146
147
148
149
# File 'lib/mkxms/mssql/utils.rb', line 146

def unquoted_name(s)
  return s unless s[0] == '[' && s[-1] == ']'
  return s[1...-1].gsub(']]', ']')
end