method
result_as_array
rails latest stable - Class:
ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements
Method deprecated or moved
This method is deprecated or moved on the latest stable version. The last existing version (v6.0.0) is shown here.
result_as_array(res)public
create a 2D array representing the result set
# File activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb, line 18
def result_as_array(res) #:nodoc:
# check if we have any binary column and if they need escaping
ftypes = Array.new(res.nfields) do |i|
[i, res.ftype(i)]
end
rows = res.values
return rows unless ftypes.any? { |_, x|
x == BYTEA_COLUMN_TYPE_OID || x == MONEY_COLUMN_TYPE_OID
}
typehash = ftypes.group_by { |_, type| type }
binaries = typehash[BYTEA_COLUMN_TYPE_OID] || []
monies = typehash[MONEY_COLUMN_TYPE_OID] || []
rows.each do |row|
# unescape string passed BYTEA field (OID == 17)
binaries.each do |index, _|
row[index] = unescape_bytea(row[index])
end
# 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.
monies.each do |index, _|
data = row[index]
# 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
end
end Related methods
- Instance methods
- begin_db_transaction
- begin_isolated_db_transaction
- commit_db_transaction
- exec_delete
- exec_insert
- exec_query
- exec_rollback_db_transaction
- exec_update
- execute
- explain
- query
- result_as_array
- write_query?
- Private methods
-
build_truncate_statements -
last_insert_id_result -
sql_for_insert -
suppress_composite_primary_key