Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created October 23, 2010 15:57
Show Gist options
  • Save metaskills/bc33adb7d40dac1d0fbc to your computer and use it in GitHub Desktop.
Save metaskills/bc33adb7d40dac1d0fbc to your computer and use it in GitHub Desktop.
diff --git a/Gemfile b/Gemfile
index efbcecd..e88e2fe 100644
--- a/Gemfile
+++ b/Gemfile
@@ -46,7 +46,7 @@ platforms :ruby do
group :db do
gem "pg", ">= 0.9.0"
gem "mysql", ">= 2.8.1"
- gem "mysql2", ">= 0.2.6"
+ gem "mysql2", :path => "/Users/kencollins/Repositories/mysql2"
end
end
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index cb2d9e0..77f0361 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -67,6 +67,7 @@ module ActiveRecord
when :first, :last
relation.send(args.first)
when :all
+
records = relation.all
@reflection.options[:uniq] ? uniq(records) : records
else
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 053b796..ccd8ebf 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -449,7 +449,11 @@ module ActiveRecord #:nodoc:
# Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
# > [#<Post:0x36bff9c @attributes={"first_name"=>"The Cheap Man Buys Twice"}>, ...]
def find_by_sql(sql)
- connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
+ if connection.supports_lazy_row_yields? #&& block_given?
+ connection.select_all(sanitize_sql(sql), "#{name} Load"){ |record| instantiate(record) }.each
+ else
+ connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) }
+ end
end
# Creates an object (or multiple objects) and saves it to the database, if validations pass.
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index 646a786..d0bbe3e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -4,7 +4,7 @@ module ActiveRecord
# Returns an array of record hashes with the column names as keys and
# column values as values.
def select_all(sql, name = nil)
- select(sql, name)
+ block_given? ? select(sql, name) { |*block_args| yield(*block_args) } : select(sql, name)
end
# Returns a record hash with the column names as keys and column values
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index d8c92d0..e061299 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -81,6 +81,12 @@ module ActiveRecord
def supports_savepoints?
false
end
+
+ # Does this adapter's connection support yeilding each row lazily without
+ # the need to load the entire result set first?
+ def supports_lazy_row_yields?
+ false
+ end
# Should primary key values be selected from their corresponding
# sequence before the insert statement? If true, next_sequence_value
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index f129b54..83017f9 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -59,6 +59,7 @@ module ActiveRecord
end
def to_a
+
return @records if loaded?
@records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel.to_sql)
@@ -354,6 +355,7 @@ module ActiveRecord
protected
def method_missing(method, *args, &block)
+
if Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif @klass.scopes[method]
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index b763e22..2b1deea 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -93,6 +93,7 @@ module ActiveRecord
# person.save!
# end
def find(*args)
+
return to_a.find { |*block_args| yield(*block_args) } if block_given?
options = args.extract_options!
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 64d2cb0..7632f11 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -32,6 +32,7 @@ module ActiveRecord
end
def select(value = Proc.new)
+
if block_given?
to_a.select {|*block_args| value.call(*block_args) }
else
diff --git a/lib/active_record/connection_adapters/mysql2_adapter.rb b/lib/active_record/connection_adapters/mysql2_adapter.rb
index 828e948..7207549 100644
--- a/lib/active_record/connection_adapters/mysql2_adapter.rb
+++ b/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -184,6 +184,10 @@ module ActiveRecord
def supports_savepoints?
true
end
+
+ def supports_lazy_row_yields?
+ true
+ end
def native_database_types
NATIVE_DATABASE_TYPES
@@ -632,7 +636,13 @@ module ActiveRecord
# Returns an array of record hashes with the column names as keys and
# column values as values.
def select(sql, name = nil)
- execute(sql, name).each(:as => :hash)
+ result = execute(sql, name)
+ if block_given?
+ result.each(:as => :hash, :cache_rows => false) { |row| yield row }
+ else
+ result.each(:as => :hash)
+ end
end
def supports_views?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment