From a8b948d81c6c7aa5beac0e98259c12c067f4bd32 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 20 Dec 2011 13:59:08 -0600 Subject: do not compile regexp on every call --- .../connection_adapters/postgresql_adapter.rb | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 705fc8135d..d7adcdc5d4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -64,27 +64,25 @@ module ActiveRecord end private + HSTORE_ESCAPE = { + ' ' => '\\ ', + '\\' => '\\\\', + '"' => '\\"', + '=' => '\\=', + } + HSTORE_ESCAPE_RE = Regexp.union(HSTORE_ESCAPE.keys) + HSTORE_UNESCAPE = HSTORE_ESCAPE.invert + HSTORE_UNESCAPE_RE = Regexp.union(HSTORE_UNESCAPE.keys) + def unescape_hstore(value) - escape_values = { - '\\ ' => ' ', - '\\\\' => '\\', - '\\"' => '"', - '\\=' => '=', - } - value.gsub(Regexp.union(escape_values.keys)) do |match| - escape_values[match] + value.gsub(HSTORE_UNESCAPE_RE) do |match| + HSTORE_UNESCAPE[match] end end def escape_hstore(value) - escape_values = { - ' ' => '\\ ', - '\\' => '\\\\', - '"' => '\\"', - '=' => '\\=', - } - value.gsub(Regexp.union(escape_values.keys)) do |match| - escape_values[match] + value.gsub(HSTORE_ESCAPE_RE) do |match| + HSTORE_ESCAPE[match] end end end -- cgit v1.2.3