diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-07-30 18:55:18 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-07-30 18:55:18 -0700 |
commit | 47f6d37c269499e56f4c23d40f6440b91c271cb8 (patch) | |
tree | ebff11b4c24569f749f3b9f48e625948d79f39d4 | |
parent | 0cdac3e029d0e99fb618d6e900ec66d48938ed63 (diff) | |
parent | a03cfdedddbfb9566e73335cf730310c99810045 (diff) | |
download | rails-47f6d37c269499e56f4c23d40f6440b91c271cb8.tar.gz rails-47f6d37c269499e56f4c23d40f6440b91c271cb8.tar.bz2 rails-47f6d37c269499e56f4c23d40f6440b91c271cb8.zip |
Merge pull request #11606 from vipulnsward/refactor_array_parser
Refactor `ArrayParser`
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb b/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb index b7d24f2bb3..20de8d1982 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/array_parser.rb @@ -2,6 +2,13 @@ module ActiveRecord module ConnectionAdapters class PostgreSQLColumn < Column module ArrayParser + + DOUBLE_QUOTE = '"' + BACKSLASH = "\\" + COMMA = ',' + BRACKET_OPEN = '{' + BRACKET_CLOSE = '}' + private # Loads pg_array_parser if available. String parsing can be # performed quicker by a native extension, which will not create @@ -12,18 +19,18 @@ module ActiveRecord include PgArrayParser rescue LoadError def parse_pg_array(string) - parse_data(string, 0) + parse_data(string) end end - def parse_data(string, index) - local_index = index + def parse_data(string) + local_index = 0 array = [] while(local_index < string.length) case string[local_index] - when '{' + when BRACKET_OPEN local_index,array = parse_array_contents(array, string, local_index + 1) - when '}' + when BRACKET_CLOSE return array end local_index += 1 @@ -33,9 +40,9 @@ module ActiveRecord end def parse_array_contents(array, string, index) - is_escaping = false - is_quoted = false - was_quoted = false + is_escaping = false + is_quoted = false + was_quoted = false current_item = '' local_index = index @@ -47,29 +54,29 @@ module ActiveRecord else if is_quoted case token - when '"' + when DOUBLE_QUOTE is_quoted = false was_quoted = true - when "\\" + when BACKSLASH is_escaping = true else current_item << token end else case token - when "\\" + when BACKSLASH is_escaping = true - when ',' + when COMMA add_item_to_array(array, current_item, was_quoted) current_item = '' was_quoted = false - when '"' + when DOUBLE_QUOTE is_quoted = true - when '{' + when BRACKET_OPEN internal_items = [] local_index,internal_items = parse_array_contents(internal_items, string, local_index + 1) array.push(internal_items) - when '}' + when BRACKET_CLOSE add_item_to_array(array, current_item, was_quoted) return local_index,array else |