Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

only loop through all rows and columns once in the postgres adapter

  • Loading branch information...
commit 35e304193b8513e1da523a9c1c34bf323e52b9e6 1 parent bc35631
Aaron Patterson tenderlove authored
46 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
View
@@ -432,8 +432,7 @@ def result_as_array(res) #:nodoc:
# 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 @@ def result_as_array(res) #:nodoc:
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 @@ def select_raw(sql, name = nil)
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
Please sign in to comment.
Something went wrong with that request. Please try again.