2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-10-12 18:57:26 -04:00
|
|
|
module ActiveRecord
|
|
|
|
###
|
2015-07-08 06:16:16 -04:00
|
|
|
# This class encapsulates a result returned from calling
|
|
|
|
# {#exec_query}[rdoc-ref:ConnectionAdapters::DatabaseStatements#exec_query]
|
|
|
|
# on any database connection adapter. For example:
|
2010-10-12 18:57:26 -04:00
|
|
|
#
|
2013-07-22 08:30:55 -04:00
|
|
|
# result = ActiveRecord::Base.connection.exec_query('SELECT id, title, body FROM posts')
|
|
|
|
# result # => #<ActiveRecord::Result:0xdeadbeef>
|
|
|
|
#
|
|
|
|
# # Get the column names of the result:
|
|
|
|
# result.columns
|
|
|
|
# # => ["id", "title", "body"]
|
|
|
|
#
|
|
|
|
# # Get the record values of the result:
|
|
|
|
# result.rows
|
|
|
|
# # => [[1, "title_1", "body_1"],
|
|
|
|
# [2, "title_2", "body_2"],
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
#
|
|
|
|
# # Get an array of hashes representing the result (column => value):
|
2016-01-29 17:44:04 -05:00
|
|
|
# result.to_a
|
2013-07-22 08:30:55 -04:00
|
|
|
# # => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
|
|
|
|
# {"id" => 2, "title" => "title_2", "body" => "body_2"},
|
|
|
|
# ...
|
|
|
|
# ]
|
|
|
|
#
|
|
|
|
# # ActiveRecord::Result also includes Enumerable.
|
|
|
|
# result.each do |row|
|
|
|
|
# puts row['title'] + " " + row['body']
|
|
|
|
# end
|
2010-10-12 18:57:26 -04:00
|
|
|
class Result
|
|
|
|
include Enumerable
|
|
|
|
|
2012-02-07 14:51:30 -05:00
|
|
|
attr_reader :columns, :rows, :column_types
|
2010-10-12 18:57:26 -04:00
|
|
|
|
2012-08-21 12:23:40 -04:00
|
|
|
def initialize(columns, rows, column_types = {})
|
2012-09-20 00:43:36 -04:00
|
|
|
@columns = columns
|
2012-02-07 14:51:30 -05:00
|
|
|
@rows = rows
|
|
|
|
@hash_rows = nil
|
2012-08-21 12:23:40 -04:00
|
|
|
@column_types = column_types
|
2010-10-12 18:57:26 -04:00
|
|
|
end
|
|
|
|
|
2018-06-26 14:53:59 -04:00
|
|
|
# Returns true if this result set includes the column named +name+
|
2018-06-25 18:52:59 -04:00
|
|
|
def includes_column?(name)
|
|
|
|
@columns.include? name
|
|
|
|
end
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Returns the number of elements in the rows array.
|
2014-10-13 15:01:41 -04:00
|
|
|
def length
|
|
|
|
@rows.length
|
|
|
|
end
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Calls the given block once for each element in row collection, passing
|
|
|
|
# row as parameter.
|
|
|
|
#
|
|
|
|
# Returns an +Enumerator+ if no block is given.
|
2010-10-12 18:57:26 -04:00
|
|
|
def each
|
2013-06-18 06:32:01 -04:00
|
|
|
if block_given?
|
|
|
|
hash_rows.each { |row| yield row }
|
|
|
|
else
|
2014-01-29 15:42:07 -05:00
|
|
|
hash_rows.to_enum { @rows.size }
|
2013-06-18 06:32:01 -04:00
|
|
|
end
|
2010-10-12 18:57:26 -04:00
|
|
|
end
|
|
|
|
|
2016-01-29 17:44:04 -05:00
|
|
|
def to_hash
|
|
|
|
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
|
|
|
`ActiveRecord::Result#to_hash` has been renamed to `to_a`.
|
|
|
|
`to_hash` is deprecated and will be removed in Rails 6.1.
|
|
|
|
MSG
|
|
|
|
to_a
|
|
|
|
end
|
|
|
|
|
2012-01-26 18:16:12 -05:00
|
|
|
alias :map! :map
|
|
|
|
alias :collect! :map
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Returns true if there are no records, otherwise false.
|
2012-01-26 18:16:12 -05:00
|
|
|
def empty?
|
|
|
|
rows.empty?
|
|
|
|
end
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Returns an array of hashes representing each row record.
|
2012-01-26 18:16:12 -05:00
|
|
|
def to_ary
|
|
|
|
hash_rows
|
|
|
|
end
|
|
|
|
|
2018-09-20 05:16:51 -04:00
|
|
|
alias :to_a :to_ary
|
|
|
|
|
2012-01-26 18:16:12 -05:00
|
|
|
def [](idx)
|
|
|
|
hash_rows[idx]
|
|
|
|
end
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Returns the first record from the rows collection.
|
|
|
|
# If the rows collection is empty, returns +nil+.
|
2016-06-24 03:50:41 -04:00
|
|
|
def first
|
|
|
|
return nil if @rows.empty?
|
|
|
|
Hash[@columns.zip(@rows.first)]
|
|
|
|
end
|
|
|
|
|
2017-02-23 22:00:59 -05:00
|
|
|
# Returns the last record from the rows collection.
|
|
|
|
# If the rows collection is empty, returns +nil+.
|
2012-01-26 18:16:12 -05:00
|
|
|
def last
|
2016-06-24 03:49:42 -04:00
|
|
|
return nil if @rows.empty?
|
|
|
|
Hash[@columns.zip(@rows.last)]
|
2012-01-26 18:16:12 -05:00
|
|
|
end
|
|
|
|
|
2014-06-21 13:31:57 -04:00
|
|
|
def cast_values(type_overrides = {}) # :nodoc:
|
2018-06-18 21:11:46 -04:00
|
|
|
if columns.one?
|
|
|
|
# Separated to avoid allocating an array per row
|
|
|
|
|
|
|
|
type = column_type(columns.first, type_overrides)
|
2014-06-21 13:31:57 -04:00
|
|
|
|
2018-06-18 21:11:46 -04:00
|
|
|
rows.map do |(value)|
|
|
|
|
type.deserialize(value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
types = columns.map { |name| column_type(name, type_overrides) }
|
|
|
|
|
|
|
|
rows.map do |values|
|
|
|
|
Array.new(values.size) { |i| types[i].deserialize(values[i]) }
|
|
|
|
end
|
|
|
|
end
|
2014-06-21 13:31:57 -04:00
|
|
|
end
|
|
|
|
|
2012-01-26 19:51:54 -05:00
|
|
|
def initialize_copy(other)
|
2013-11-09 02:56:11 -05:00
|
|
|
@columns = columns.dup
|
|
|
|
@rows = rows.dup
|
|
|
|
@column_types = column_types.dup
|
|
|
|
@hash_rows = nil
|
2012-01-26 19:51:54 -05:00
|
|
|
end
|
|
|
|
|
2010-10-12 18:57:26 -04:00
|
|
|
private
|
2013-07-22 08:30:55 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def column_type(name, type_overrides = {})
|
|
|
|
type_overrides.fetch(name) do
|
2016-08-25 13:07:44 -04:00
|
|
|
column_types.fetch(name, Type.default_value)
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2014-06-21 13:31:57 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def hash_rows
|
|
|
|
@hash_rows ||=
|
|
|
|
begin
|
|
|
|
# We freeze the strings to prevent them getting duped when
|
|
|
|
# used as keys in ActiveRecord::Base's @attributes hash
|
2018-06-05 19:50:58 -04:00
|
|
|
columns = @columns.map(&:-@)
|
2018-08-30 10:57:01 -04:00
|
|
|
length = columns.length
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
@rows.map { |row|
|
|
|
|
# In the past we used Hash[columns.zip(row)]
|
|
|
|
# though elegant, the verbose way is much more efficient
|
|
|
|
# both time and memory wise cause it avoids a big array allocation
|
|
|
|
# this method is called a lot and needs to be micro optimised
|
|
|
|
hash = {}
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
while index < length
|
|
|
|
hash[columns[index]] = row[index]
|
|
|
|
index += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
hash
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2010-10-12 18:57:26 -04:00
|
|
|
end
|
|
|
|
end
|