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



8
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
35
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 8

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 column && [:uuid, :uniqueidentifier].include?(column.type)
      "'#{quote_string(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



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

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

#quote_database_name(name) ⇒ Object



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

def quote_database_name(name)
  schema_cache.quote_name(name, false)
end

#quote_default_value(value, column) ⇒ Object

Does not quote function default values for UUID columns



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

def quote_default_value(value, column)
  if column.type == :uuid && value =~ /\(\)/
    value
  else
    quote(value)
  end
end

#quote_string(string) ⇒ Object



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

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

#quote_table_name(name) ⇒ Object



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

def quote_table_name(name)
  quote_column_name(name)
end

#quoted_date(value) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 103

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



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

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.iso8601(3).to(18)
    else
      time_zone_qualified_value.iso8601(3).to(22)
    end
  else
    quoted_date(value)
  end
end

#quoted_falseObject



78
79
80
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 78

def quoted_false
  QUOTED_FALSE
end

#quoted_full_iso8601(value) ⇒ Object



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

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



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

def quoted_string_prefix
  QUOTED_STRING_PREFIX
end

#quoted_trueObject



74
75
76
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 74

def quoted_true
  QUOTED_TRUE
end

#substitute_at(column, index) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/active_record/connection_adapters/sqlserver/quoting.rb', line 66

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