Module: ActiveRecord::ConnectionAdapters::MSSQL::Quoting

Included in:
ActiveRecord::ConnectionAdapters::MSSQLAdapter
Defined in:
lib/arjdbc/mssql/quoting.rb

Constant Summary collapse

QUOTED_TRUE =
'1'
QUOTED_FALSE =
'0'

Instance Method Summary collapse

Instance Method Details

#column_name_matcherObject



91
92
93
# File 'lib/arjdbc/mssql/quoting.rb', line 91

def column_name_matcher
  COLUMN_NAME
end

#column_name_with_order_matcherObject



95
96
97
# File 'lib/arjdbc/mssql/quoting.rb', line 95

def column_name_with_order_matcher
  COLUMN_NAME_WITH_ORDER
end

#quote(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/arjdbc/mssql/quoting.rb', line 10

def quote(value)
  # FIXME: this needs improvements to handle other custom types.
  # Also check if it's possible insert integer into a NVARCHAR
  case value
  when ActiveRecord::Type::Binary::Data
    "0x#{value.hex}"
  # when SomeOtherBinaryData then BLOB_VALUE_MARKER
  # when SomeOtherData then "yyy"
  when String, ActiveSupport::Multibyte::Chars
    "N'#{quote_string(value)}'"
  # when OnlyTimeType then "'#{quoted_time(value)}'"
  when Date, Time
    "'#{quoted_date(value)}'"
  when TrueClass
    quoted_true
  when FalseClass
    quoted_false
  else
    super
  end
end

#quote_default_expression(value, column) ⇒ Object

Does not quote function default values for UUID columns



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/arjdbc/mssql/quoting.rb', line 55

def quote_default_expression(value, column)
  cast_type = lookup_cast_type(column.sql_type)
  if cast_type.type == :uuid && value =~ /\(\)/
    value
  elsif column.type == :datetime_basic && value.is_a?(String)
    # let's trust the user to set a right default value for this
    # legacy type something like: '2017-02-28 01:59:19.789'
    quote(value)
  else
    super
  end
end

#quote_string(s) ⇒ Object

Quotes strings for use in SQL input.



50
51
52
# File 'lib/arjdbc/mssql/quoting.rb', line 50

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

#quoted_date(value) ⇒ Object

Quote date/time values for use in SQL input, includes microseconds with three digits only if the value is a Time responding to usec. The JDBC drivers does not work with 6 digits microseconds



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/arjdbc/mssql/quoting.rb', line 35

def quoted_date(value)
  if value.acts_like?(:time)
    value = time_with_db_timezone(value)
  end

  result = value.to_fs(:db)

  if value.respond_to?(:usec) && value.usec > 0
    "#{result}.#{sprintf("%06d", value.usec)}"
  else
    result
  end
end

#quoted_falseObject



72
73
74
# File 'lib/arjdbc/mssql/quoting.rb', line 72

def quoted_false
  QUOTED_FALSE
end

#quoted_time(value) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/arjdbc/mssql/quoting.rb', line 77

def quoted_time(value)
  if value.acts_like?(:time)
    tz_value = time_with_db_timezone(value)
    usec = value.respond_to?(:usec) ? value.usec : 0
    sprintf('%02d:%02d:%02d.%06d', tz_value.hour, tz_value.min, tz_value.sec, usec)
  else
    quoted_date(value)
  end
end

#quoted_trueObject



68
69
70
# File 'lib/arjdbc/mssql/quoting.rb', line 68

def quoted_true
  QUOTED_TRUE
end