Skip to content

Instantly share code, notes, and snippets.

@msepcot
Created August 28, 2012 14:59
Show Gist options
  • Save msepcot/3498804 to your computer and use it in GitHub Desktop.
Save msepcot/3498804 to your computer and use it in GitHub Desktop.
Force a string to identify itself as non-UTF8 (used with ActiveRecord::ConnectionAdapters::Sqlserver::Quoting)
module ActiveRecord
module ConnectionAdapters
module Sqlserver
module Quoting
def quote(value, column = nil)
case value
when String, ActiveSupport::Multibyte::Chars
if column && column.type == :integer && value.blank?
nil
elsif column && column.type == :binary
column.class.string_to_binary(value)
elsif (value.is_utf8? && !column) || (column && column.type == :string && column.sql_type =~ /^n/)
# NOTE enter here if the value is_utf8? and column is nil, or the column is multibyte
# NOTE non-multibyte string columns will default to super's behavior
"#{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
end
end
end
end
# NOTE used with ActiveRecord::ConnectionAdapters::Sqlserver::Quoting above
# force a string to identify itself as non-UTF8
class String
def ascii!
def self.is_utf8?
false
end
self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment