caching column values

This commit is contained in:
Aaron Patterson 2010-10-21 14:26:42 -07:00
parent 02128d628c
commit fca229e205
1 changed files with 11 additions and 4 deletions

View File

@ -314,8 +314,8 @@ module ActiveRecord
end
def clear_cache!
@statements.values.each do |stmt|
stmt.close
@statements.values.each do |cache|
cache[:stmt].close
end
@statements.clear
end
@ -324,17 +324,24 @@ module ActiveRecord
log(sql, name) do
result = nil
cache = {}
if bind_values.empty?
stmt = @connection.prepare(sql)
else
stmt = @statements[sql] ||= @connection.prepare(sql)
cache = @statements[sql] ||= {
:stmt => @connection.prepare(sql)
}
stmt = cache[:stmt]
end
stmt.execute(*bind_values.map { |col, val|
col ? col.type_cast(val) : val
})
if metadata = stmt.result_metadata
cols = metadata.fetch_fields.map { |field| field.name }
cols = cache[:cols] ||= metadata.fetch_fields.map { |field|
field.name
}
metadata.free
result = ActiveRecord::Result.new(cols, stmt.to_a)
end