提交 40ce6820 编写于 作者: A Aaron Patterson

made the result set object act more like an array

上级 77c6706d
...@@ -225,7 +225,7 @@ def exec_query(sql, name = 'SQL', binds = []) ...@@ -225,7 +225,7 @@ def exec_query(sql, name = 'SQL', binds = [])
# 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)
......
...@@ -408,7 +408,7 @@ def configure_connection ...@@ -408,7 +408,7 @@ def configure_connection
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
......
...@@ -1249,7 +1249,7 @@ def last_insert_id(sequence_name) #:nodoc: ...@@ -1249,7 +1249,7 @@ def last_insert_id(sequence_name) #:nodoc:
# 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)
......
...@@ -35,7 +35,8 @@ module Querying ...@@ -35,7 +35,8 @@ module Querying
# > [#<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
......
...@@ -24,6 +24,25 @@ def to_hash ...@@ -24,6 +24,25 @@ def to_hash
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|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册