Sha256: 807fef89c384ae2cfc7d7829899796646ee3c1d5384128a4b5823442eb4552b6

Contents?: true

Size: 819 Bytes

Versions: 6

Compression:

Stored size: 819 Bytes

Contents

# private
module Simple::SQL::Helpers::Encoder
  extend self

  def encode_args(connection, args)
    args.map { |arg| encode_arg(connection, arg) }
  end

  def encode_arg(connection, arg)
    return arg unless arg.is_a?(Array)
    return "{}" if arg.empty?

    encoded_ary = encode_array(connection, arg)
    "{" + encoded_ary.join(",") + "}"
  end

  def encode_array(connection, ary)
    case ary.first
    when String
      ary.map do |str|
        str = connection.escape(str)

        # These fixes have been discovered during tests. see spec/simple/sql/conversion_spec.rb
        str = str.gsub("\"", "\\\"")
        str = str.gsub("''", "'")
        "\"#{str}\""
      end
    when Integer
      ary
    else
      raise ArgumentError, "Don't know how to encode array of #{ary.first.class}"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
simple-sql-0.5.37 lib/simple/sql/helpers/encoder.rb
simple-sql-0.5.36 lib/simple/sql/helpers/encoder.rb
simple-sql-0.5.35 lib/simple/sql/helpers/encoder.rb
simple-sql-0.5.34 lib/simple/sql/helpers/encoder.rb
simple-sql-0.5.33 lib/simple/sql/helpers/encoder.rb
simple-sql-0.5.32 lib/simple/sql/helpers/encoder.rb