method
result_as_array
v3.1.0 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
result_as_array(res)public
create a 2D array representing the result set
# File activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, line 488
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
- active?
- adapter_name
- add_column
- begin_db_transaction
- change_column
- change_column_default
- change_column_null
- clear_cache!
- client_min_messages
- client_min_messages=
- columns
- commit_db_transaction
- create_database
- create_savepoint
- current_database
- default_sequence_name
- disable_referential_integrity
- disconnect!
- distinct
- drop_database
- encoding
- escape_bytea
- exec_delete
- exec_query
- exec_update
- execute
- extract_schema_and_table
- index_name_length
- indexes
- insert_sql
- native_database_types
- outside_transaction?
- pk_and_sequence_for
- primary_key
- query
- quote
- quote_column_name
- quote_string
- quote_table_name
- quoted_date
- reconnect!
- recreate_database
- release_savepoint
- remove_index!
- rename_column
- rename_index
- rename_table
- reset!
- reset_pk_sequence!
- result_as_array
- rollback_db_transaction
- rollback_to_savepoint
- schema_search_path
- schema_search_path=
- select_rows
- serial_sequence
- session_auth=
- set_standard_conforming_strings
- sql_for_insert
- substitute_at
- supports_ddl_transactions?
- supports_disable_referential_integrity?
- supports_insert_with_returning?
- supports_migrations?
- supports_primary_key?
- supports_savepoints?
- supports_statement_cache?
- table_alias_length
- table_exists?
- tables
- type_cast
- type_to_sql
- unescape_bytea
- update_sql
- Class methods
- new
- visitor_for
- Protected methods
-
postgresql_version -
translate_exception - Private methods
-
column_definitions -
configure_connection -
connect -
exec_cache -
exec_no_cache -
extract_pg_identifier_from_name -
last_insert_id -
select -
select_raw -
table_definition