From 8521cdf03d2b9761f21b593e10a31ca96d05092c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 12 Jul 2010 17:44:15 -0700 Subject: PostgreSQLAdapter#select_raw fields and results are empty even if ntuples is 0 --- .../connection_adapters/postgresql_adapter.rb | 46 ++++++++++------------ 1 file changed, 21 insertions(+), 25 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 9bf08a7671..1429f44ff3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -949,34 +949,30 @@ module ActiveRecord def select_raw(sql, name = nil) res = execute(sql, name) results = result_as_array(res) - fields = [] - rows = [] - if res.ntuples > 0 - fields = res.fields - results.each do |row| - hashed_row = {} - row.each_index do |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 column = row[cell_index] - when /^-?\D+[\d,]+\.\d{2}$/ # (1) - row[cell_index] = column.gsub(/[^-\d\.]/, '') - when /^-?\D+[\d\.]+,\d{2}$/ # (2) - row[cell_index] = column.gsub(/[^-\d,]/, '').sub(/,/, '.') - end + fields = res.fields + rows = results.map do |row| + hashed_row = {} + row.each_index do |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 column = row[cell_index] + when /^-?\D+[\d,]+\.\d{2}$/ # (1) + row[cell_index] = column.gsub(/[^-\d\.]/, '') + when /^-?\D+[\d\.]+,\d{2}$/ # (2) + row[cell_index] = column.gsub(/[^-\d,]/, '').sub(/,/, '.') end - - hashed_row[fields[cell_index]] = column end - rows << row + + hashed_row[fields[cell_index]] = column end + row end res.clear return fields, rows -- cgit v1.2.3