diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index fc172fee00d32..d02466ffa3ab1 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -691,6 +691,8 @@ def table_structure_with_collation(table_name, basic_structure) end basic_structure.map do |column| + column = column.to_hash + column_name = column["name"] if collation_hash.has_key? column_name diff --git a/activerecord/lib/active_record/result.rb b/activerecord/lib/active_record/result.rb index 617c53797ecd3..1d2ee445d2cc6 100644 --- a/activerecord/lib/active_record/result.rb +++ b/activerecord/lib/active_record/result.rb @@ -36,6 +36,53 @@ module ActiveRecord class Result include Enumerable + class IndexedRow + def initialize(column_indexes, row) + @column_indexes = column_indexes + @row = row + end + + def each_key(&block) + @column_indexes.each_key(&block) + end + + def keys + @column_indexes.keys + end + + def ==(other) + if other.is_a?(Hash) + to_hash == other + else + super + end + end + + def key?(column) + @column_indexes.key?(column) + end + + def fetch(column) + if index = @column_indexes[column] + @row[index] + elsif block_given? + yield + else + raise KeyError, "key not found: #{column.inspect}" + end + end + + def [](column) + if index = @column_indexes[column] + @row[index] + end + end + + def to_hash + @column_indexes.transform_values { |index| @row[index] } + end + end + attr_reader :columns, :rows, :column_types def self.empty(async: false) # :nodoc: @@ -170,8 +217,9 @@ def column_type(name, index, type_overrides) def hash_rows # We use transform_values to rows. # This is faster because we avoid any reallocs and avoid hashing entirely. - @hash_rows ||= @rows.map do |row| - column_indexes.transform_values { |index| row[index] } + @hash_rows ||= begin + columns = column_indexes + @rows.map { |row| IndexedRow.new(columns, row) } end end