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.6.8 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.7 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.6 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.5 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.4 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.3 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.2 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.1 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.6.0 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.21 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.20 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.17 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.16 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.15 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.14 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.13 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.12 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.11 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.10 flydata-core/lib/flydata-core/redshift/string.rb
flydata-0.5.9 flydata-core/lib/flydata-core/redshift/string.rb