From 35e304193b8513e1da523a9c1c34bf323e52b9e6 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 13 Jul 2010 10:57:15 -0700 Subject: only loop through all rows and columns once in the postgres adapter --- .../connection_adapters/postgresql_adapter.rb | 46 +++++++++++----------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 4baf99610e..d773be0ca6 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -432,8 +432,7 @@ module ActiveRecord # check if we have any binary column and if they need escaping unescape_col = [] res.nfields.times do |j| - # unescape string passed BYTEA field (OID == 17) - unescape_col << ( res.ftype(j) == BYTEA_COLUMN_TYPE_OID ) + unescape_col << res.ftype(j) end ary = [] @@ -441,7 +440,28 @@ module ActiveRecord ary << [] res.nfields.times do |j| data = res.getvalue(i,j) - data = unescape_bytea(data) if unescape_col[j] and data.is_a?(String) + case unescape_col[j] + + # unescape string passed BYTEA field (OID == 17) + when BYTEA_COLUMN_TYPE_OID + data = unescape_bytea(data) if String === data + + # If this is a money type column and there are any currency symbols, + # then strip them off. Indeed it would be prettier to do this in + # PostgreSQLColumn.string_to_decimal but would break form input + # fields that call value_before_type_cast. + when MONEY_COLUMN_TYPE_OID + # Because money output is formatted according to the locale, there are two + # cases to consider (note the decimal separators): + # (1) $12,345,678.12 + # (2) $12.345.678,12 + case data + when /^-?\D+[\d,]+\.\d{2}$/ # (1) + data.gsub!(/[^-\d\.]/, '') + when /^-?\D+[\d\.]+,\d{2}$/ # (2) + data.gsub!(/[^-\d,]/, '').sub!(/,/, '.') + end + end ary[i] << data end end @@ -952,26 +972,6 @@ module ActiveRecord res = execute(sql, name) results = result_as_array(res) fields = res.fields - results.each do |row| - row.each_with_index do |cell, cell_index| - # If this is a money type column and there are any currency symbols, - # then strip them off. Indeed it would be prettier to do this in - # PostgreSQLColumn.string_to_decimal but would break form input - # fields that call value_before_type_cast. - if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID - # Because money output is formatted according to the locale, there are two - # cases to consider (note the decimal separators): - # (1) $12,345,678.12 - # (2) $12.345.678,12 - case cell - when /^-?\D+[\d,]+\.\d{2}$/ # (1) - cell.gsub!(/[^-\d\.]/, '') - when /^-?\D+[\d\.]+,\d{2}$/ # (2) - cell.gsub!(/[^-\d,]/, '').sub!(/,/, '.') - end - end - end - end res.clear return fields, results end -- cgit v1.2.3