Module: ActiveRecord::ConnectionAdapters::Sqlserver::Quoting

Included in:
ActiveRecord::ConnectionAdapters::SQLServerAdapter
Defined in:
lib/active_record/connection_adapters/sqlserver/quoting.rb

Constant Summary collapse

QUOTED_STRING_PREFIX =
'N'

Instance Method Summary collapse

Instance Method Details

#quote(value, column = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 9

def quote(value, column = nil)
  case value
  when String, ActiveSupport::Multibyte::Chars
    if column && column.type == :integer && value.blank?
      value.to_i.to_s
    elsif column && column.type == :binary
      column.class.string_to_binary(value)
    elsif value.is_utf8? || (column && column.type == :string)
      "#{quoted_string_prefix}'#{quote_string(value)}'"
    else
      super
    end
  when Date, Time
    if column && column.sql_type == 'datetime'
      "'#{quoted_datetime(value)}'"
    elsif column && (column.sql_type == 'datetimeoffset' || column.sql_type == 'time')
      "'#{quoted_full_iso8601(value)}'"
    else
      super
    end
  when nil
    column.respond_to?(:sql_type) && column.sql_type == 'timestamp' ? 'DEFAULT' : super
  else
    super
  end
end

#quote_column_name(name) ⇒ Object



44
45
46
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 44

def quote_column_name(name)
  schema_cache.quote_name(name)
end

#quote_string(string) ⇒ Object



40
41
42
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 40

def quote_string(string)
  string.to_s.gsub(/\'/, "''")
end

#quote_table_name(name) ⇒ Object



48
49
50
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 48

def quote_table_name(name)
  quote_column_name(name)
end

#quoted_date(value) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 93

def quoted_date(value)
  if value.acts_like?(:time) && value.respond_to?(:usec)
    "#{super}.#{sprintf("%03d",value.usec/1000)}"
  elsif value.acts_like?(:date)
    value.to_s(:_sqlserver_dateformat)
  else
    super
  end
end

#quoted_datetime(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 68

def quoted_datetime(value)
  if value.acts_like?(:time)
    time_zone_qualified_value = quoted_value_acts_like_time_filter(value)
    if value.is_a?(Date)
      time_zone_qualified_value.to_time.xmlschema.to(18)
    else
      # CHANGED [Ruby 1.8] Not needed when 1.8 is dropped.
      if value.is_a?(ActiveSupport::TimeWithZone) && RUBY_VERSION < '1.9'
        time_zone_qualified_value = time_zone_qualified_value.to_time 
      end
      time_zone_qualified_value.iso8601(3).to(22)
    end
  else
    quoted_date(value)
  end
end

#quoted_falseObject



64
65
66
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 64

def quoted_false
  QUOTED_FALSE
end

#quoted_full_iso8601(value) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 85

def quoted_full_iso8601(value)
  if value.acts_like?(:time)
    value.is_a?(Date) ? quoted_value_acts_like_time_filter(value).to_time.xmlschema.to(18) : quoted_value_acts_like_time_filter(value).iso8601(7).to(22)
  else
    quoted_date(value)
  end
end

#quoted_string_prefixObject



36
37
38
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 36

def quoted_string_prefix
  QUOTED_STRING_PREFIX
end

#quoted_trueObject



60
61
62
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 60

def quoted_true
  QUOTED_TRUE
end

#substitute_at(column, index) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 52

def substitute_at(column, index)
  if column.respond_to?(:sql_type) && column.sql_type == 'timestamp'
    nil
  else
    Arel.sql "@#{index}"
  end
end