class ActiveRecord::ConnectionAdapters::SQLServer::Utils::Name

Value object to return identifiers from SQL Server names bit.ly/1CZ3EiL Inspired from Rails PostgreSQL::Name adapter object in their own Utils.

Constants

QUOTED_CHECKER
QUOTED_SCANNER
UNQUOTED_SCANNER

Attributes

database[R]
object[R]
raw_name[R]
schema[R]
server[R]

Public Class Methods

new(name) click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 19
def initialize(name)
  @raw_name = name.to_s
  parse_raw_name
end

Public Instance Methods

==(other) click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 75
def ==(other)
  other.class == self.class && other.parts == parts
end
Also aliased as: eql?
database_quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 32
def database_quoted
  database ? quote(database) : database
end
eql?(other)
Alias for: ==
fully_qualified?() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 44
def fully_qualified?
  qualified_level == :fully
end
fully_qualified_database_quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 40
def fully_qualified_database_quoted
  [server_quoted, database_quoted].compact.join('.')
end
hash() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 80
def hash
  parts.hash
end
object_quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 24
def object_quoted
  quote object
end
qualified_level() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 48
def qualified_level
  case parts.compact.size
  when 4
    :fully
  when 3
    :database
  when 2
    :schema
  when 1
    :table
  else
    :none
  end
end
quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 67
def quoted
  parts.map { |p| quote(p) if p }.join('.')
end
quoted_raw() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 71
def quoted_raw
  quote @raw_name
end
schema_quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 28
def schema_quoted
  schema ? quote(schema) : schema
end
server_quoted() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 36
def server_quoted
  server ? quote(server) : server
end
to_s() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 63
def to_s
  quoted
end

Protected Instance Methods

parse_raw_name() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 86
def parse_raw_name
  @parts = []
  return if raw_name.blank?

  scanner = StringScanner.new(raw_name)
  matched = scanner.exist?(QUOTED_CHECKER) ? scanner.scan_until(QUOTED_SCANNER) : scanner.scan_until(UNQUOTED_SCANNER)
  while matched
    part = matched[0..-2]
    @parts << (part.blank? ? nil : unquote(part))
    matched = scanner.exist?(QUOTED_CHECKER) ? scanner.scan_until(QUOTED_SCANNER) : scanner.scan_until(UNQUOTED_SCANNER)
  end
  case @parts.length
  when 3
    @server, @database, @schema = @parts
  when 2
    @database, @schema = @parts
  when 1
    @schema = @parts.first
  end
  rest = scanner.rest
  rest = rest.start_with?(".") ? rest[1..-1] : rest[0..-1]
  @object = unquote(rest)
  @parts << @object
end
parts() click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 123
def parts
  @parts
end
quote(part) click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 111
def quote(part)
  part =~ /\A\[.*\]\z/ ? part : "[#{part.to_s.gsub(']', ']]')}]"
end
unquote(part) click to toggle source
# File lib/active_record/connection_adapters/sqlserver/utils.rb, line 115
def unquote(part)
  if part && part.start_with?("[")
    part[1..-2]
  else
    part
  end
end