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

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::ConnectionAdapters::MSSQLAdapter
Defined in:
lib/arjdbc/mssql/quoting.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

QUOTED_COLUMN_NAMES =

:nodoc:

Concurrent::Map.new
QUOTED_TABLE_NAMES =

:nodoc:

Concurrent::Map.new
QUOTED_TRUE =
'1'
QUOTED_FALSE =
'0'

Instance Method Summary collapse

Instance Method Details

#quote(value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/arjdbc/mssql/quoting.rb', line 75

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



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/arjdbc/mssql/quoting.rb', line 120

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.



115
116
117
# File 'lib/arjdbc/mssql/quoting.rb', line 115

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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arjdbc/mssql/quoting.rb', line 100

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



137
138
139
# File 'lib/arjdbc/mssql/quoting.rb', line 137

def quoted_false
  QUOTED_FALSE
end

#quoted_time(value) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/arjdbc/mssql/quoting.rb', line 142

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



133
134
135
# File 'lib/arjdbc/mssql/quoting.rb', line 133

def quoted_true
  QUOTED_TRUE
end

#type_cast(value) ⇒ Object

:nodoc:



63
64
65
66
67
68
69
70
# File 'lib/arjdbc/mssql/quoting.rb', line 63

def type_cast(value) # :nodoc:
  case value
  when BigDecimal
    value
  else
    super
  end
end