Sha256: d11a80d63f42aad1c7119d2f8c367d0a3c5be4c0d7caf3812c70c0e5f38afae6

Contents?: true

Size: 1.1 KB

Versions: 76

Compression:

Stored size: 1.1 KB

Contents

module FlydataCore
module Redshift

class String
  NULL_CHAR_CODE_POINTS = "\\0".codepoints

  # Redshift supports UTF-8 but it enforces stricter rule than other
  # implementations such as MySQL or Ruby.  This method returns a
  # Redshift-safe string from the given string.
  def self.encode(string, options = {})
    encoded_str = string.encoding == Encoding::UTF_8 ?
                  string.encode(Encoding::UTF_16, options).encode(Encoding::UTF_8) :
                  string.encode(Encoding::UTF_8, options)

    ret_cpts = []
    rep_str_cpts = (options[:replace] || "\uFFFD").codepoints
    replaced = false

    encoded_str.each_codepoint do |cp|
      # Per Redshift document
      # http://docs.aws.amazon.com/redshift/latest/dg/multi-byte-character-load-errors.html
      if cp >= 0xFDD0 && cp <= 0xFDEF || cp == 0xFFFE || cp == 0xFFFF
        ret_cpts.concat(rep_str_cpts)
        replaced = true
      elsif cp == 0x0000
        ret_cpts.concat(NULL_CHAR_CODE_POINTS)
        replaced = true
      else
        ret_cpts << cp
      end
    end

    replaced ? ret_cpts.pack("U*") : encoded_str
  end
end

end
end

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
flydata-0.8.10.2 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.10.1 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.9.11 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.10 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.9 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.8 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.7 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.6 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.5 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.4 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.3 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.2 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.1 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.8.0 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.19 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.18 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.17 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.16 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.15 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.7.14 flydata-core/lib/flydata-core/redshift/string.rb