Module: PgArrayParser

Constant Summary collapse

CURLY_BRACKETS =
'{}'.freeze
SQUARE_BRACKETS =
'[]'.freeze
NULL =
'NULL'.freeze
NIL =
'nil'.freeze
ESCAPE_HASH =
{'\\'.freeze=>'\\\\'.freeze, '"'.freeze=>'\\"'.freeze}

Instance Method Summary collapse

Instance Method Details

#_parse_pgarray(text, &block) ⇒ Object



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
# File 'lib/ar_jdbc_pg_array/parser.rb', line 62

def _parse_pgarray(text, &block)
  values = []
  return values  if text =~ /^\}\s*/
  if text =~ /^\{\s*/
    text = $'
    while true
      ar, rest = _parse_pgarray(text, &block)
      values << ar
      if rest =~ /^\}\s*/
        return values, $'
      elsif rest =~ /^,\s*\{\s*/
        rest = $'
      else
        raise "Mailformed postgres array"
      end
      text = rest
    end
  else
    while true
      if text =~ /^"((?:\\.|[^"\\])*)"([,}])\s*/
        val, sep, rest = $1, $2, $'
        val.gsub!(/\\(.)/, '\1')
        val = yield val
      elsif text =~ /^([^,\}]*)([,}])\s*/
        val, sep, rest = $1, $2, $'
        val = val == NULL ? nil : yield(val)
      else
        raise "Mailformed postgres array"
      end
      values << val
      if sep == '}'
        return values, rest
      end
      text = rest
    end
  end
end

#_parse_safe_pgarray(text, &block) ⇒ Object



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
# File 'lib/ar_jdbc_pg_array/parser.rb', line 26

def _parse_safe_pgarray(text, &block)
  values = []
  return values  if text =~ /^\}\s*/
  if text =~ /^\{\s*/
    text = $'
    while true
      ar, rest = _parse_safe_pgarray(text, &block)
      values << ar
      if rest =~ /^\}\s*/
        return values, $'
      elsif rest =~ /^,\s*/
        rest = $'
      else
        raise "Mailformed postgres array"
      end
      text = rest
    end
  else
    while true
      raise 'Mailformed Array' unless text =~ /^([^,\}]*)([,\}])\s*/
      val, sep, rest = $1, $2, $'
      values << (val == NULL ? nil : yield(val))
      if sep == '}'
        return values, rest
      end
      text = rest
    end
  end
end

#_prepare_pg_string_array(value, &block) ⇒ Object Also known as: prepare_pg_string_array



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ar_jdbc_pg_array/parser.rb', line 155

def _prepare_pg_string_array(value, &block)
  value = value.map{|val|
    case val
    when Array
      _prepare_pg_string_array(val, &block)
    when nil
      NULL
    else
      val = yield val
      if val =~ /^'(.*)'$/m
        "\"#{ $1.gsub(/\\|"/){|s| ESCAPE_HASH[s]} }\""
      else
        val
      end
    end
  }.join(',')
  "{#{value}}"
end

#_remap_array(array, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ar_jdbc_pg_array/parser.rb', line 100

def _remap_array(array, &block)
  array.map{|v|
    case v
    when Array
      _remap_array(v, &block)
    when nil
      nil
    else
      yield v
    end
  }
end

#parse_numeric_pgarray(text) ⇒ Object



10
11
12
13
14
# File 'lib/ar_jdbc_pg_array/parser.rb', line 10

def parse_numeric_pgarray(text)
  text = text.tr(CURLY_BRACKETS, SQUARE_BRACKETS)
  text.downcase!
  JSON.load(text)
end

#parse_pgarray(text, &block) ⇒ Object



56
57
58
59
60
# File 'lib/ar_jdbc_pg_array/parser.rb', line 56

def parse_pgarray(text, &block)
  raise "Mailformed postgres array" unless text =~ /^\{\s*/
  ar, rest = _parse_pgarray($', &block)
  ar
end

#parse_safe_pgarray(text, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ar_jdbc_pg_array/parser.rb', line 16

def parse_safe_pgarray(text, &block)
  if text =~ /^\{([^\}]*)\}$/
    $1.split(/,\s*/).map!{|v| v == NULL ? nil : yield(v)}
  else
    raise "Mailformed array" unless text =~ /^\{\s*/
    ar, rest = _parse_safe_pgarray($')
    ar
  end
end

#prepare_pg_float_array(value) ⇒ Object



120
121
122
123
124
125
# File 'lib/ar_jdbc_pg_array/parser.rb', line 120

def prepare_pg_float_array(value)
  val = _remap_array(value){|v| v.to_f}.inspect
  val.gsub!(NIL, NULL)
  val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
  val
end

#prepare_pg_integer_array(value) ⇒ Object



113
114
115
116
117
118
# File 'lib/ar_jdbc_pg_array/parser.rb', line 113

def prepare_pg_integer_array(value)
  val = _remap_array(value){|v| v.to_i}.inspect
  val.gsub!(NIL, NULL)
  val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS)
  val
end

#prepare_pg_safe_array(value) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ar_jdbc_pg_array/parser.rb', line 127

def prepare_pg_safe_array(value)
  value = value.map{|val|
      case val
      when Array
        prepare_pg_safe_array(val)
      when nil
        NULL
      else
        val.to_s
      end
  }.join(',')
  "{#{value}}"
end

#prepare_pg_text_array(value) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ar_jdbc_pg_array/parser.rb', line 141

def prepare_pg_text_array(value)
  value = value.map{|val|
    case val
    when Array
      prepare_pg_text_array(val)
    when nil
      NULL
    else
       "\"#{val.to_s.gsub(/\\|"/){|s| ESCAPE_HASH[s]}}\""
    end
  }.join(',')
  "{#{value}}"
end