|
| 1 | +require 'strscan' |
| 2 | + |
1 | 3 | module ActiveRecord
|
2 | 4 | module ConnectionAdapters
|
3 | 5 | module Sqlserver
|
4 |
| - class Utils |
5 |
| - class << self |
6 |
| - def unquote_string(string) |
7 |
| - string.to_s.gsub(/\'\'/, "'") |
| 6 | + module Utils |
| 7 | + |
| 8 | + # Value object to return identifiers from SQL Server names http://bit.ly/1CZ3EiL |
| 9 | + # Inspiried from Rails PostgreSQL::Name adapter object in their own Utils. |
| 10 | + # |
| 11 | + class Name |
| 12 | + |
| 13 | + SEPARATOR = "." |
| 14 | + SCANNER = /\]?\./ |
| 15 | + |
| 16 | + attr_reader :server, :database, :schema, :object |
| 17 | + attr_reader :raw_name |
| 18 | + |
| 19 | + def initialize(name) |
| 20 | + @raw_name = name.to_s |
| 21 | + parse_raw_name |
| 22 | + end |
| 23 | + |
| 24 | + def object_quoted |
| 25 | + quote object |
| 26 | + end |
| 27 | + |
| 28 | + def schema_quoted |
| 29 | + schema ? quote(schema) : schema |
8 | 30 | end
|
9 | 31 |
|
10 |
| - def unqualify_table_name(table_name) |
11 |
| - table_name.to_s.split('.').last.tr('[]', '') |
| 32 | + def database_quoted |
| 33 | + database ? quote(database) : database |
12 | 34 | end
|
13 | 35 |
|
14 |
| - def unqualify_table_schema(table_name) |
15 |
| - table_name.to_s.split('.')[-2].gsub(/[\[\]]/, '') rescue nil |
| 36 | + def server_quoted |
| 37 | + server ? quote(server) : server |
16 | 38 | end
|
17 | 39 |
|
18 |
| - def unqualify_db_name(table_name) |
19 |
| - table_names = table_name.to_s.split('.') |
20 |
| - table_names.length == 3 ? table_names.first.tr('[]', '') : nil |
| 40 | + def to_s |
| 41 | + quoted |
21 | 42 | end
|
| 43 | + |
| 44 | + def quoted |
| 45 | + parts.map{ |p| quote(p) if p }.join SEPARATOR |
| 46 | + end |
| 47 | + |
| 48 | + def ==(o) |
| 49 | + o.class == self.class && o.parts == parts |
| 50 | + end |
| 51 | + alias_method :eql?, :== |
| 52 | + |
| 53 | + def hash |
| 54 | + parts.hash |
| 55 | + end |
| 56 | + |
| 57 | + protected |
| 58 | + |
| 59 | + def parse_raw_name |
| 60 | + @parts = [] |
| 61 | + return if raw_name.blank? |
| 62 | + scanner = StringScanner.new(raw_name) |
| 63 | + matched = scanner.scan_until(SCANNER) |
| 64 | + while matched |
| 65 | + part = matched[0..-2] |
| 66 | + @parts << (part.blank? ? nil : unquote(part)) |
| 67 | + matched = scanner.scan_until(SCANNER) |
| 68 | + end |
| 69 | + case @parts.length |
| 70 | + when 3 |
| 71 | + @server, @database, @schema = @parts |
| 72 | + when 2 |
| 73 | + @database, @schema = @parts |
| 74 | + when 1 |
| 75 | + @schema = @parts.first |
| 76 | + end |
| 77 | + rest = scanner.rest |
| 78 | + rest = rest.starts_with?('.') ? rest[1..-1] : rest[0..-1] |
| 79 | + @object = unquote(rest) |
| 80 | + @parts << @object |
| 81 | + end |
| 82 | + |
| 83 | + def quote(part) |
| 84 | + part =~ /\A\[.*\]\z/ ? part : "[#{part.to_s.gsub(']', ']]')}]" |
| 85 | + end |
| 86 | + |
| 87 | + def unquote(part) |
| 88 | + if part && part.start_with?('[') |
| 89 | + part[1..-2] |
| 90 | + else |
| 91 | + part |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def parts |
| 96 | + @parts |
| 97 | + end |
| 98 | + |
22 | 99 | end
|
| 100 | + |
| 101 | + extend self |
| 102 | + |
| 103 | + def quote_string(s) |
| 104 | + s.gsub /\'/, "''" |
| 105 | + end |
| 106 | + |
| 107 | + def unquote_string(s) |
| 108 | + s.to_s.gsub(/\'\'/, "'") |
| 109 | + end |
| 110 | + |
| 111 | + def extract_identifiers(name) |
| 112 | + Sqlserver::Utils::Name.new(name) |
| 113 | + end |
| 114 | + |
23 | 115 | end
|
24 | 116 | end
|
25 | 117 | end
|
|
0 commit comments