Module: PgArrayParser
- Included in:
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, ActiveRecord::ConnectionAdapters::PostgreSQLColumn, ActiveRecord::ConnectionAdapters::PostgreSQLColumn
- Defined in:
- lib/ar_pg_array/parser.rb
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
- #_parse_pgarray(text, &block) ⇒ Object
- #_parse_safe_pgarray(text, &block) ⇒ Object
- #parse_numeric_pgarray(text) ⇒ Object
- #parse_pgarray(text, &block) ⇒ Object
- #parse_safe_pgarray(text, &block) ⇒ Object
- #prepare_pg_float_array(value) ⇒ Object
- #prepare_pg_integer_array(value) ⇒ Object
- #prepare_pg_safe_array(value) ⇒ Object
- #prepare_pg_string_array(value, &block) ⇒ Object
- #prepare_pg_text_array(value) ⇒ Object
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_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_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 |
#parse_numeric_pgarray(text) ⇒ Object
10 11 12 13 14 |
# File 'lib/ar_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_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_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
107 108 109 110 111 112 |
# File 'lib/ar_pg_array/parser.rb', line 107 def prepare_pg_float_array(value) val = value.map{|v| v.nil? ? nil : v.to_f}.inspect val.gsub!(NIL, NULL) val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS) val end |
#prepare_pg_integer_array(value) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/ar_pg_array/parser.rb', line 100 def prepare_pg_integer_array(value) val = value.map{|v| v.nil? ? nil : v.to_i}.inspect val.gsub!(NIL, NULL) val.tr!(SQUARE_BRACKETS, CURLY_BRACKETS) val end |
#prepare_pg_safe_array(value) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ar_pg_array/parser.rb', line 114 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_string_array(value, &block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ar_pg_array/parser.rb', line 142 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 |
#prepare_pg_text_array(value) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ar_pg_array/parser.rb', line 128 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 |