diff options
Diffstat (limited to 'activerecord/lib')
7 files changed, 48 insertions, 41 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 2176fc4e40..2fb80fdc4c 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -522,7 +522,7 @@ module ActiveRecord # # #<Pet id: 3, name: "Choo-Choo", person_id: 1> # # ] # - # person.pets.delete([Pet.find(1), Pet.find(3)]) + # person.pets.delete(Pet.find(1), Pet.find(3)) # # => [ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>, # # #<Pet id: 3, name: "Choo-Choo", person_id: 1> diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 9794c5663e..692473abc5 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -313,10 +313,10 @@ module ActiveRecord sql = "SHOW TABLES" end - select_all(sql).map { |table| + select_all(sql, 'SCHEMA').map { |table| table.delete('Table_type') sql = "SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}" - exec_without_stmt(sql).first['Create Table'] + ";\n\n" + exec_without_stmt(sql, 'SCHEMA').first['Create Table'] + ";\n\n" }.join end @@ -508,7 +508,7 @@ module ActiveRecord # SHOW VARIABLES LIKE 'name' def show_variable(name) - variables = select_all("SHOW VARIABLES LIKE '#{name}'") + variables = select_all("SHOW VARIABLES LIKE '#{name}'", 'SCHEMA') variables.first['Value'] unless variables.empty? end @@ -630,7 +630,7 @@ module ActiveRecord raise ActiveRecordError, "No such column: #{table_name}.#{column_name}" end - current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"] + current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'", 'SCHEMA')["Type"] rename_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" add_column_options!(rename_column_sql, options) rename_column_sql diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 7dcea375e1..cf4a213580 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -523,7 +523,7 @@ module ActiveRecord # Returns the configured supported identifier length supported by PostgreSQL def table_alias_length - @table_alias_length ||= query('SHOW max_identifier_length')[0][0].to_i + @table_alias_length ||= query('SHOW max_identifier_length', 'SCHEMA')[0][0].to_i end # QUOTING ================================================== @@ -985,7 +985,7 @@ module ActiveRecord # Returns an array of indexes for the given table. def indexes(table_name, name = nil) - result = query(<<-SQL, name) + result = query(<<-SQL, 'SCHEMA') SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid FROM pg_class t INNER JOIN pg_index d ON t.oid = d.indrelid @@ -997,7 +997,6 @@ module ActiveRecord ORDER BY i.relname SQL - result.map do |row| index_name = row[0] unique = row[1] == 't' @@ -1036,7 +1035,7 @@ module ActiveRecord # Returns the current database name. def current_database - query('select current_database()')[0][0] + query('select current_database()', 'SCHEMA')[0][0] end # Returns the current schema name. @@ -1046,7 +1045,7 @@ module ActiveRecord # Returns the current database encoding format. def encoding - query(<<-end_sql)[0][0] + query(<<-end_sql, 'SCHEMA')[0][0] SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database WHERE pg_database.datname LIKE '#{current_database}' end_sql @@ -1054,7 +1053,7 @@ module ActiveRecord # Returns an array of schema names. def schema_names - query(<<-SQL).flatten + query(<<-SQL, 'SCHEMA').flatten SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index d4ffa82b17..a0c7e559ce 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -208,7 +208,6 @@ module ActiveRecord true end - # QUOTING ================================================== def quote(value, column = nil) @@ -220,7 +219,6 @@ module ActiveRecord end end - def quote_string(s) #:nodoc: @connection.class.quote(s) end @@ -359,7 +357,7 @@ module ActiveRecord # SCHEMA STATEMENTS ======================================== - def tables(name = 'SCHEMA', table_name = nil) #:nodoc: + def tables(name = nil, table_name = nil) #:nodoc: sql = <<-SQL SELECT name FROM sqlite_master @@ -367,13 +365,13 @@ module ActiveRecord SQL sql << " AND name = #{quote_table_name(table_name)}" if table_name - exec_query(sql, name).map do |row| + exec_query(sql, 'SCHEMA').map do |row| row['name'] end end - def table_exists?(name) - name && tables('SCHEMA', name).any? + def table_exists?(table_name) + table_name && tables(nil, table_name).any? end # Returns an array of +SQLite3Column+ objects for the table specified by +table_name+. @@ -394,12 +392,12 @@ module ActiveRecord # Returns an array of indexes for the given table. def indexes(table_name, name = nil) #:nodoc: - exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", name).map do |row| + exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", 'SCHEMA').map do |row| IndexDefinition.new( table_name, row['name'], row['unique'] != 0, - exec_query("PRAGMA index_info('#{row['name']}')").map { |col| + exec_query("PRAGMA index_info('#{row['name']}')", "Columns for index #{row['name']} on #{table_name}").map { |col| col['name'] }) end diff --git a/activerecord/lib/active_record/relation/batches.rb b/activerecord/lib/active_record/relation/batches.rb index 15f838a5ab..fb4388d4b2 100644 --- a/activerecord/lib/active_record/relation/batches.rb +++ b/activerecord/lib/active_record/relation/batches.rb @@ -2,20 +2,26 @@ require 'active_support/core_ext/object/blank' module ActiveRecord module Batches - # Yields each record that was found by the find +options+. The find is - # performed by find_in_batches with a batch size of 1000 (or as + # Looping through a collection of records from the database + # (using the +all+ method, for example) is very inefficient + # since it will try to instantiate all the objects at once. + # + # In that case, batch processing methods allow you to work + # with the records in batches, thereby greatly reducing memory consumption. + # + # The <tt>find_each</tt> method uses <tt>find_in_batches</tt> with a batch size of 1000 (or as # specified by the <tt>:batch_size</tt> option). # - # Example: + # Person.all.find_each do |person| + # person.do_awesome_stuff + # end # # Person.where("age > 21").find_each do |person| # person.party_all_night! # end # - # Note: This method is only intended to use for batch processing of - # large amounts of records that wouldn't fit in memory all at once. If - # you just need to loop over less than 1000 records, it's probably - # better just to use the regular find methods. + # You can also pass the <tt>:start</tt> option to specify + # an offset to control the starting point. def find_each(options = {}) find_in_batches(options) do |records| records.each { |record| yield record } @@ -39,12 +45,15 @@ module ActiveRecord # primary keys. You can't set the limit either, that's used to control # the batch sizes. # - # Example: - # # Person.where("age > 21").find_in_batches do |group| # sleep(50) # Make sure it doesn't get too crowded in there! # group.each { |person| person.party_all_night! } # end + # + # # Let's process the next 2000 records + # Person.all.find_in_batches(start: 2000, batch_size: 2000) do |group| + # group.each { |person| person.party_all_night! } + # end def find_in_batches(options = {}) options.assert_valid_keys(:start, :batch_size) diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 04b4fcf379..ad49c80e4f 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -129,7 +129,7 @@ module ActiveRecord # Person.all.map(&:name) # # Pluck returns an <tt>Array</tt> of attribute values type-casted to match - # the plucked column name, if it can be deduced. Plucking a SQL fragment + # the plucked column name, if it can be deduced. Plucking an SQL fragment # returns String values by default. # # Examples: diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 4fedd33d64..5f6898b45a 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -7,8 +7,6 @@ module ActiveRecord # If no record can be found for all of the listed ids, then RecordNotFound will be raised. If the primary key # is an integer, find by id coerces its arguments using +to_i+. # - # ==== Examples - # # Person.find(1) # returns the object for ID = 1 # Person.find("1") # returns the object for ID = 1 # Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6) @@ -49,7 +47,6 @@ module ActiveRecord # # Post.find_by name: 'Spartacus', rating: 4 # Post.find_by "published_at < ?", 2.weeks.ago - # def find_by(*args) where(*args).take end @@ -64,8 +61,6 @@ module ActiveRecord # order. The order will depend on the database implementation. # If an order is supplied it will be respected. # - # Examples: - # # Person.take # returns an object fetched by SELECT * FROM people # Person.take(5) # returns 5 objects fetched by SELECT * FROM people LIMIT 5 # Person.where(["name LIKE '%?'", name]).take @@ -82,12 +77,11 @@ module ActiveRecord # Find the first record (or first N records if a parameter is supplied). # If no order is defined it will order by primary key. # - # Examples: - # # Person.first # returns the first object fetched by SELECT * FROM people # Person.where(["user_name = ?", user_name]).first # Person.where(["user_name = :u", { :u => user_name }]).first # Person.order("created_on DESC").offset(5).first + # Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3 def first(limit = nil) if limit if order_values.empty? && primary_key @@ -109,11 +103,18 @@ module ActiveRecord # Find the last record (or last N records if a parameter is supplied). # If no order is defined it will order by primary key. # - # Examples: - # # Person.last # returns the last object fetched by SELECT * FROM people # Person.where(["user_name = ?", user_name]).last # Person.order("created_on DESC").offset(5).last + # Person.last(3) # returns the last three objects fetched by SELECT * FROM people. + # + # Take note that in that last case, the results are sorted in ascending order: + # + # [#<Person id:2>, #<Person id:3>, #<Person id:4>] + # + # and not: + # + # [#<Person id:4>, #<Person id:3>, #<Person id:2>] def last(limit = nil) if limit if order_values.empty? && primary_key @@ -132,7 +133,8 @@ module ActiveRecord last or raise RecordNotFound end - # Examples: + # Runs the query on the database and returns records with the used query + # methods. # # Person.all # returns an array of objects for all the rows fetched by SELECT * FROM people # Person.where(["category IN (?)", categories]).limit(50).all @@ -163,11 +165,10 @@ module ActiveRecord # 'Jamie'</tt>), since it would be sanitized and then queried against # the primary key column, like <tt>id = 'name = \'Jamie\''</tt>. # - # ==== Examples # Person.exists?(5) # Person.exists?('5') - # Person.exists?(:name => "David") # Person.exists?(['name LIKE ?', "%#{query}%"]) + # Person.exists?(:name => "David") # Person.exists? def exists?(id = false) id = id.id if ActiveRecord::Model === id |