Module: Simple::SQL::Helpers::Encoder

Extended by:
Encoder
Included in:
Encoder
Defined in:
lib/simple/sql/helpers/encoder.rb

Overview

private

Instance Method Summary collapse

Instance Method Details

#encode_arg(connection, arg) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/simple/sql/helpers/encoder.rb', line 9

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

#encode_args(connection, args) ⇒ Object



5
6
7
# File 'lib/simple/sql/helpers/encoder.rb', line 5

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

#encode_array(connection, ary) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple/sql/helpers/encoder.rb', line 17

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