1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

made the result set object act more like an array

This commit is contained in:
Aaron Patterson 2012-01-26 15:16:12 -08:00
parent 77c6706dad
commit 40ce682048
5 changed files with 24 additions and 4 deletions

View file

@ -225,7 +225,7 @@ module ActiveRecord
# column values as values. # column values as values.
def select(sql, name = nil, binds = []) def select(sql, name = nil, binds = [])
binds = binds.dup binds = binds.dup
exec_query(sql.gsub("\0") { quote(*binds.shift.reverse) }, name).to_a exec_query(sql.gsub("\0") { quote(*binds.shift.reverse) }, name)
end end
def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)

View file

@ -408,7 +408,7 @@ module ActiveRecord
def select(sql, name = nil, binds = []) def select(sql, name = nil, binds = [])
@connection.query_with_result = true @connection.query_with_result = true
rows = exec_query(sql, name, binds).to_a rows = exec_query(sql, name, binds)
@connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
rows rows
end end

View file

@ -1249,7 +1249,7 @@ module ActiveRecord
# Executes a SELECT query and returns the results, performing any data type # Executes a SELECT query and returns the results, performing any data type
# conversions that are required to be performed here instead of in PostgreSQLColumn. # conversions that are required to be performed here instead of in PostgreSQLColumn.
def select(sql, name = nil, binds = []) def select(sql, name = nil, binds = [])
exec_query(sql, name, binds).to_a exec_query(sql, name, binds)
end end
def select_raw(sql, name = nil) def select_raw(sql, name = nil)

View file

@ -35,7 +35,8 @@ module ActiveRecord
# > [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...] # > [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...]
def find_by_sql(sql, binds = []) def find_by_sql(sql, binds = [])
logging_query_plan do logging_query_plan do
connection.select_all(sanitize_sql(sql), "#{name} Load", binds).collect! { |record| instantiate(record) } result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
result_set.map! { |record| instantiate(record) }
end end
end end

View file

@ -24,6 +24,25 @@ module ActiveRecord
hash_rows hash_rows
end end
alias :map! :map
alias :collect! :map
def empty?
rows.empty?
end
def to_ary
hash_rows
end
def [](idx)
hash_rows[idx]
end
def last
hash_rows.last
end
private private
def hash_rows def hash_rows
@hash_rows ||= @rows.map { |row| @hash_rows ||= @rows.map { |row|