Module: ArJdbc::MSSQL::Column::Cast

Defined in:
lib/arjdbc/mssql/old_column.rb

Constant Summary collapse

ISO_TIME =
/\A(\d\d)\:(\d\d)\:(\d\d)(\.\d+)?\z/

Instance Method Summary collapse

Instance Method Details

#binary_to_string(value) ⇒ Object



189
190
191
192
193
194
# File 'lib/arjdbc/mssql/old_column.rb', line 189

def binary_to_string(value)
  if value.respond_to?(:force_encoding) && value.encoding != Encoding::ASCII_8BIT
    value = value.force_encoding(Encoding::ASCII_8BIT)
  end
  value =~ /[^[:xdigit:]]/ ? value : [value].pack('H*')
end

#string_to_binary(value) ⇒ Object



183
184
185
186
187
# File 'lib/arjdbc/mssql/old_column.rb', line 183

def string_to_binary(value)
  # this will only allow the adapter to insert binary data with a length
  # of 7K or less because of a SQL Server statement length policy ...
  "0x#{value.unpack("H*")}" # "0x#{value.unpack("H*")[0]}"
end

#string_to_date(value) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/arjdbc/mssql/old_column.rb', line 154

def string_to_date(value)
  return value unless value.is_a?(String)
  return nil if value.empty?

  date = fast_string_to_date(value)
  date ? date : Date.parse(value) rescue nil
end

#string_to_dummy_time(value) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/arjdbc/mssql/old_column.rb', line 171

def string_to_dummy_time(value)
  return value unless value.is_a?(String)
  return nil if value.empty?

  if value =~ ISO_TIME # "12:34:56.1234560"
    microsec = ($4.to_f * 1_000_000).round.to_i
    new_time 2000, 1, 1, $1.to_i, $2.to_i, $3.to_i, microsec
  else
    super(value)
  end
end

#string_to_time(value) ⇒ Object



162
163
164
165
166
167
# File 'lib/arjdbc/mssql/old_column.rb', line 162

def string_to_time(value)
  return value unless value.is_a?(String)
  return nil if value.empty?

  fast_string_to_time(value) || DateTime.parse(value).to_time rescue nil
end