aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
blob: fb4e0de2a8c5ec62ca9011e01eb5a5b187bb903d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class Array < Type::Value # :nodoc:
          include Type::Helpers::Mutable

          # Loads pg_array_parser if available. String parsing can be
          # performed quicker by a native extension, which will not create
          # a large amount of Ruby objects that will need to be garbage
          # collected. pg_array_parser has a C and Java extension
          begin
            require 'pg_array_parser'
            include PgArrayParser
          rescue LoadError
            require 'active_record/connection_adapters/postgresql/array_parser'
            include PostgreSQL::ArrayParser
          end

          attr_reader :subtype, :delimiter
          delegate :type, :user_input_in_time_zone, to: :subtype

          def initialize(subtype, delimiter = ',')
            @subtype = subtype
            @delimiter = delimiter
          end

          def deserialize(value)
            if value.is_a?(::String)
              type_cast_array(parse_pg_array(value), :deserialize)
            else
              super
            end
          end

          def cast(value)
            if value.is_a?(::String)
              value = parse_pg_array(value)
            end
            type_cast_array(value, :cast)
          end

          def serialize(value)
            if value.is_a?(::Array)
              cast_value_for_database(value)
            else
              super
            end
          end

          def ==(other)
            other.is_a?(Array) &&
              subtype == other.subtype &&
              delimiter == other.delimiter
          end

          private

          def type_cast_array(value, method)
            if value.is_a?(::Array)
              value.map { |item| type_cast_array(item, method) }
            else
              @subtype.public_send(method, value)
            end
          end

          def cast_value_for_database(value)
            if value.is_a?(::Array)
              casted_values = value.map { |item| cast_value_for_database(item) }
              "{#{casted_values.join(delimiter)}}"
            else
              quote_and_escape(subtype.serialize(value))
            end
          end

          ARRAY_ESCAPE = "\\" * 2 * 2 # escape the backslash twice for PG arrays

          def quote_and_escape(value)
            case value
            when ::String
              if string_requires_quoting?(value)
                value = value.gsub(/\\/, ARRAY_ESCAPE)
                value.gsub!(/"/,"\\\"")
                %("#{value}")
              else
                value
              end
            when nil then "NULL"
            else value
            end
          end

          # See http://www.postgresql.org/docs/9.2/static/arrays.html#ARRAYS-IO
          # for a list of all cases in which strings will be quoted.
          def string_requires_quoting?(string)
            string.empty? ||
              string == "NULL" ||
              string =~ /[\{\}"\\\s]/ ||
              string.include?(delimiter)
          end
        end
      end
    end
  end
end