activerecord 2.3.18 → 3.2.22
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1014 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +222 -0
- data/examples/performance.rb +100 -126
- data/examples/simple.rb +14 -0
- data/lib/active_record/aggregations.rb +93 -99
- data/lib/active_record/associations/alias_tracker.rb +76 -0
- data/lib/active_record/associations/association.rb +247 -0
- data/lib/active_record/associations/association_scope.rb +134 -0
- data/lib/active_record/associations/belongs_to_association.rb +54 -61
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +17 -59
- data/lib/active_record/associations/builder/association.rb +55 -0
- data/lib/active_record/associations/builder/belongs_to.rb +88 -0
- data/lib/active_record/associations/builder/collection_association.rb +75 -0
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +57 -0
- data/lib/active_record/associations/builder/has_many.rb +71 -0
- data/lib/active_record/associations/builder/has_one.rb +62 -0
- data/lib/active_record/associations/builder/singular_association.rb +32 -0
- data/lib/active_record/associations/collection_association.rb +580 -0
- data/lib/active_record/associations/collection_proxy.rb +133 -0
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +39 -119
- data/lib/active_record/associations/has_many_association.rb +60 -79
- data/lib/active_record/associations/has_many_through_association.rb +127 -206
- data/lib/active_record/associations/has_one_association.rb +55 -114
- data/lib/active_record/associations/has_one_through_association.rb +25 -26
- data/lib/active_record/associations/join_dependency/join_association.rb +159 -0
- data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
- data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
- data/lib/active_record/associations/join_dependency.rb +214 -0
- data/lib/active_record/associations/join_helper.rb +55 -0
- data/lib/active_record/associations/preloader/association.rb +125 -0
- data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
- data/lib/active_record/associations/preloader/collection_association.rb +24 -0
- data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
- data/lib/active_record/associations/preloader/has_many.rb +17 -0
- data/lib/active_record/associations/preloader/has_many_through.rb +15 -0
- data/lib/active_record/associations/preloader/has_one.rb +23 -0
- data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
- data/lib/active_record/associations/preloader/singular_association.rb +21 -0
- data/lib/active_record/associations/preloader/through_association.rb +67 -0
- data/lib/active_record/associations/preloader.rb +181 -0
- data/lib/active_record/associations/singular_association.rb +64 -0
- data/lib/active_record/associations/through_association.rb +87 -0
- data/lib/active_record/associations.rb +693 -1337
- data/lib/active_record/attribute_assignment.rb +221 -0
- data/lib/active_record/attribute_methods/before_type_cast.rb +31 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
- data/lib/active_record/attribute_methods/dirty.rb +111 -0
- data/lib/active_record/attribute_methods/primary_key.rb +114 -0
- data/lib/active_record/attribute_methods/query.rb +39 -0
- data/lib/active_record/attribute_methods/read.rb +136 -0
- data/lib/active_record/attribute_methods/serialization.rb +120 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +65 -0
- data/lib/active_record/attribute_methods/write.rb +70 -0
- data/lib/active_record/attribute_methods.rb +211 -339
- data/lib/active_record/autosave_association.rb +179 -149
- data/lib/active_record/base.rb +401 -2907
- data/lib/active_record/callbacks.rb +91 -176
- data/lib/active_record/coders/yaml_column.rb +41 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +236 -119
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +110 -58
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +12 -11
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +175 -74
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +31 -35
- data/lib/active_record/connection_adapters/abstract/quoting.rb +71 -21
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +81 -311
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +194 -78
- data/lib/active_record/connection_adapters/abstract_adapter.rb +130 -83
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +676 -0
- data/lib/active_record/connection_adapters/column.rb +296 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +280 -0
- data/lib/active_record/connection_adapters/mysql_adapter.rb +272 -493
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +650 -405
- data/lib/active_record/connection_adapters/schema_cache.rb +69 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +30 -9
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +276 -147
- data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
- data/lib/active_record/counter_cache.rb +123 -0
- data/lib/active_record/dynamic_finder_match.rb +41 -14
- data/lib/active_record/dynamic_matchers.rb +84 -0
- data/lib/active_record/dynamic_scope_match.rb +13 -15
- data/lib/active_record/errors.rb +195 -0
- data/lib/active_record/explain.rb +86 -0
- data/lib/active_record/explain_subscriber.rb +25 -0
- data/lib/active_record/fixtures/file.rb +65 -0
- data/lib/active_record/fixtures.rb +695 -770
- data/lib/active_record/identity_map.rb +162 -0
- data/lib/active_record/inheritance.rb +174 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locale/en.yml +9 -27
- data/lib/active_record/locking/optimistic.rb +76 -73
- data/lib/active_record/locking/pessimistic.rb +32 -10
- data/lib/active_record/log_subscriber.rb +72 -0
- data/lib/active_record/migration/command_recorder.rb +105 -0
- data/lib/active_record/migration.rb +415 -205
- data/lib/active_record/model_schema.rb +368 -0
- data/lib/active_record/nested_attributes.rb +153 -63
- data/lib/active_record/observer.rb +27 -103
- data/lib/active_record/persistence.rb +376 -0
- data/lib/active_record/query_cache.rb +49 -8
- data/lib/active_record/querying.rb +58 -0
- data/lib/active_record/railtie.rb +131 -0
- data/lib/active_record/railties/console_sandbox.rb +6 -0
- data/lib/active_record/railties/controller_runtime.rb +49 -0
- data/lib/active_record/railties/databases.rake +659 -0
- data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
- data/lib/active_record/readonly_attributes.rb +26 -0
- data/lib/active_record/reflection.rb +269 -120
- data/lib/active_record/relation/batches.rb +90 -0
- data/lib/active_record/relation/calculations.rb +372 -0
- data/lib/active_record/relation/delegation.rb +49 -0
- data/lib/active_record/relation/finder_methods.rb +402 -0
- data/lib/active_record/relation/predicate_builder.rb +63 -0
- data/lib/active_record/relation/query_methods.rb +417 -0
- data/lib/active_record/relation/spawn_methods.rb +180 -0
- data/lib/active_record/relation.rb +537 -0
- data/lib/active_record/result.rb +40 -0
- data/lib/active_record/sanitization.rb +194 -0
- data/lib/active_record/schema.rb +9 -6
- data/lib/active_record/schema_dumper.rb +55 -32
- data/lib/active_record/scoping/default.rb +142 -0
- data/lib/active_record/scoping/named.rb +200 -0
- data/lib/active_record/scoping.rb +152 -0
- data/lib/active_record/serialization.rb +8 -91
- data/lib/active_record/serializers/xml_serializer.rb +43 -197
- data/lib/active_record/session_store.rb +129 -103
- data/lib/active_record/store.rb +52 -0
- data/lib/active_record/test_case.rb +30 -23
- data/lib/active_record/timestamp.rb +95 -52
- data/lib/active_record/transactions.rb +212 -66
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +43 -0
- data/lib/active_record/validations/uniqueness.rb +180 -0
- data/lib/active_record/validations.rb +43 -1106
- data/lib/active_record/version.rb +5 -4
- data/lib/active_record.rb +121 -48
- data/lib/rails/generators/active_record/migration/migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/migration/templates/migration.rb +34 -0
- data/lib/rails/generators/active_record/migration.rb +15 -0
- data/lib/rails/generators/active_record/model/model_generator.rb +47 -0
- data/lib/rails/generators/active_record/model/templates/migration.rb +15 -0
- data/lib/rails/generators/active_record/model/templates/model.rb +12 -0
- data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
- data/lib/rails/generators/active_record/observer/observer_generator.rb +15 -0
- data/lib/rails/generators/active_record/observer/templates/observer.rb +4 -0
- data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +25 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +12 -0
- data/lib/rails/generators/active_record.rb +25 -0
- metadata +187 -363
- data/CHANGELOG +0 -5904
- data/README +0 -351
- data/RUNNING_UNIT_TESTS +0 -36
- data/Rakefile +0 -268
- data/install.rb +0 -30
- data/lib/active_record/association_preload.rb +0 -406
- data/lib/active_record/associations/association_collection.rb +0 -533
- data/lib/active_record/associations/association_proxy.rb +0 -288
- data/lib/active_record/batches.rb +0 -85
- data/lib/active_record/calculations.rb +0 -321
- data/lib/active_record/dirty.rb +0 -183
- data/lib/active_record/named_scope.rb +0 -197
- data/lib/active_record/serializers/json_serializer.rb +0 -91
- data/lib/activerecord.rb +0 -2
- data/test/assets/example.log +0 -1
- data/test/assets/flowers.jpg +0 -0
- data/test/cases/aaa_create_tables_test.rb +0 -24
- data/test/cases/active_schema_test_mysql.rb +0 -122
- data/test/cases/active_schema_test_postgresql.rb +0 -24
- data/test/cases/adapter_test.rb +0 -144
- data/test/cases/aggregations_test.rb +0 -167
- data/test/cases/ar_schema_test.rb +0 -32
- data/test/cases/associations/belongs_to_associations_test.rb +0 -438
- data/test/cases/associations/callbacks_test.rb +0 -161
- data/test/cases/associations/cascaded_eager_loading_test.rb +0 -131
- data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +0 -36
- data/test/cases/associations/eager_load_nested_include_test.rb +0 -131
- data/test/cases/associations/eager_load_nested_polymorphic_include.rb +0 -19
- data/test/cases/associations/eager_singularization_test.rb +0 -145
- data/test/cases/associations/eager_test.rb +0 -852
- data/test/cases/associations/extension_test.rb +0 -62
- data/test/cases/associations/habtm_join_table_test.rb +0 -56
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +0 -827
- data/test/cases/associations/has_many_associations_test.rb +0 -1273
- data/test/cases/associations/has_many_through_associations_test.rb +0 -360
- data/test/cases/associations/has_one_associations_test.rb +0 -330
- data/test/cases/associations/has_one_through_associations_test.rb +0 -209
- data/test/cases/associations/inner_join_association_test.rb +0 -93
- data/test/cases/associations/inverse_associations_test.rb +0 -566
- data/test/cases/associations/join_model_test.rb +0 -712
- data/test/cases/associations_test.rb +0 -282
- data/test/cases/attribute_methods_test.rb +0 -305
- data/test/cases/autosave_association_test.rb +0 -1218
- data/test/cases/base_test.rb +0 -2166
- data/test/cases/batches_test.rb +0 -81
- data/test/cases/binary_test.rb +0 -30
- data/test/cases/calculations_test.rb +0 -360
- data/test/cases/callbacks_observers_test.rb +0 -38
- data/test/cases/callbacks_test.rb +0 -438
- data/test/cases/class_inheritable_attributes_test.rb +0 -32
- data/test/cases/column_alias_test.rb +0 -17
- data/test/cases/column_definition_test.rb +0 -70
- data/test/cases/connection_pool_test.rb +0 -25
- data/test/cases/connection_test_firebird.rb +0 -8
- data/test/cases/connection_test_mysql.rb +0 -65
- data/test/cases/copy_table_test_sqlite.rb +0 -80
- data/test/cases/counter_cache_test.rb +0 -84
- data/test/cases/database_statements_test.rb +0 -12
- data/test/cases/datatype_test_postgresql.rb +0 -204
- data/test/cases/date_time_test.rb +0 -37
- data/test/cases/default_test_firebird.rb +0 -16
- data/test/cases/defaults_test.rb +0 -111
- data/test/cases/deprecated_finder_test.rb +0 -30
- data/test/cases/dirty_test.rb +0 -316
- data/test/cases/finder_respond_to_test.rb +0 -76
- data/test/cases/finder_test.rb +0 -1098
- data/test/cases/fixtures_test.rb +0 -661
- data/test/cases/helper.rb +0 -68
- data/test/cases/i18n_test.rb +0 -46
- data/test/cases/inheritance_test.rb +0 -262
- data/test/cases/invalid_date_test.rb +0 -24
- data/test/cases/json_serialization_test.rb +0 -219
- data/test/cases/lifecycle_test.rb +0 -193
- data/test/cases/locking_test.rb +0 -350
- data/test/cases/method_scoping_test.rb +0 -704
- data/test/cases/migration_test.rb +0 -1649
- data/test/cases/migration_test_firebird.rb +0 -124
- data/test/cases/mixin_test.rb +0 -96
- data/test/cases/modules_test.rb +0 -109
- data/test/cases/multiple_db_test.rb +0 -85
- data/test/cases/named_scope_test.rb +0 -372
- data/test/cases/nested_attributes_test.rb +0 -840
- data/test/cases/pk_test.rb +0 -119
- data/test/cases/pooled_connections_test.rb +0 -103
- data/test/cases/query_cache_test.rb +0 -129
- data/test/cases/readonly_test.rb +0 -107
- data/test/cases/reflection_test.rb +0 -234
- data/test/cases/reload_models_test.rb +0 -22
- data/test/cases/repair_helper.rb +0 -50
- data/test/cases/reserved_word_test_mysql.rb +0 -176
- data/test/cases/sanitize_test.rb +0 -25
- data/test/cases/schema_authorization_test_postgresql.rb +0 -75
- data/test/cases/schema_dumper_test.rb +0 -211
- data/test/cases/schema_test_postgresql.rb +0 -178
- data/test/cases/serialization_test.rb +0 -47
- data/test/cases/sp_test_mysql.rb +0 -16
- data/test/cases/synonym_test_oracle.rb +0 -17
- data/test/cases/timestamp_test.rb +0 -75
- data/test/cases/transactions_test.rb +0 -543
- data/test/cases/unconnected_test.rb +0 -32
- data/test/cases/validations_i18n_test.rb +0 -925
- data/test/cases/validations_test.rb +0 -1684
- data/test/cases/xml_serialization_test.rb +0 -240
- data/test/cases/yaml_serialization_test.rb +0 -11
- data/test/config.rb +0 -5
- data/test/connections/jdbc_jdbcderby/connection.rb +0 -18
- data/test/connections/jdbc_jdbch2/connection.rb +0 -18
- data/test/connections/jdbc_jdbchsqldb/connection.rb +0 -18
- data/test/connections/jdbc_jdbcmysql/connection.rb +0 -26
- data/test/connections/jdbc_jdbcpostgresql/connection.rb +0 -26
- data/test/connections/jdbc_jdbcsqlite3/connection.rb +0 -25
- data/test/connections/native_db2/connection.rb +0 -25
- data/test/connections/native_firebird/connection.rb +0 -26
- data/test/connections/native_frontbase/connection.rb +0 -27
- data/test/connections/native_mysql/connection.rb +0 -25
- data/test/connections/native_openbase/connection.rb +0 -21
- data/test/connections/native_oracle/connection.rb +0 -27
- data/test/connections/native_postgresql/connection.rb +0 -21
- data/test/connections/native_sqlite/connection.rb +0 -25
- data/test/connections/native_sqlite3/connection.rb +0 -25
- data/test/connections/native_sqlite3/in_memory_connection.rb +0 -18
- data/test/connections/native_sybase/connection.rb +0 -23
- data/test/fixtures/accounts.yml +0 -29
- data/test/fixtures/all/developers.yml +0 -0
- data/test/fixtures/all/people.csv +0 -0
- data/test/fixtures/all/tasks.yml +0 -0
- data/test/fixtures/author_addresses.yml +0 -5
- data/test/fixtures/author_favorites.yml +0 -4
- data/test/fixtures/authors.yml +0 -9
- data/test/fixtures/binaries.yml +0 -132
- data/test/fixtures/books.yml +0 -7
- data/test/fixtures/categories/special_categories.yml +0 -9
- data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +0 -4
- data/test/fixtures/categories.yml +0 -14
- data/test/fixtures/categories_ordered.yml +0 -7
- data/test/fixtures/categories_posts.yml +0 -23
- data/test/fixtures/categorizations.yml +0 -17
- data/test/fixtures/clubs.yml +0 -6
- data/test/fixtures/comments.yml +0 -59
- data/test/fixtures/companies.yml +0 -56
- data/test/fixtures/computers.yml +0 -4
- data/test/fixtures/courses.yml +0 -7
- data/test/fixtures/customers.yml +0 -26
- data/test/fixtures/developers.yml +0 -21
- data/test/fixtures/developers_projects.yml +0 -17
- data/test/fixtures/edges.yml +0 -6
- data/test/fixtures/entrants.yml +0 -14
- data/test/fixtures/faces.yml +0 -11
- data/test/fixtures/fk_test_has_fk.yml +0 -3
- data/test/fixtures/fk_test_has_pk.yml +0 -2
- data/test/fixtures/funny_jokes.yml +0 -10
- data/test/fixtures/interests.yml +0 -33
- data/test/fixtures/items.yml +0 -4
- data/test/fixtures/jobs.yml +0 -7
- data/test/fixtures/legacy_things.yml +0 -3
- data/test/fixtures/mateys.yml +0 -4
- data/test/fixtures/member_types.yml +0 -6
- data/test/fixtures/members.yml +0 -6
- data/test/fixtures/memberships.yml +0 -20
- data/test/fixtures/men.yml +0 -5
- data/test/fixtures/minimalistics.yml +0 -2
- data/test/fixtures/mixed_case_monkeys.yml +0 -6
- data/test/fixtures/mixins.yml +0 -29
- data/test/fixtures/movies.yml +0 -7
- data/test/fixtures/naked/csv/accounts.csv +0 -1
- data/test/fixtures/naked/yml/accounts.yml +0 -1
- data/test/fixtures/naked/yml/companies.yml +0 -1
- data/test/fixtures/naked/yml/courses.yml +0 -1
- data/test/fixtures/organizations.yml +0 -5
- data/test/fixtures/owners.yml +0 -7
- data/test/fixtures/parrots.yml +0 -27
- data/test/fixtures/parrots_pirates.yml +0 -7
- data/test/fixtures/people.yml +0 -15
- data/test/fixtures/pets.yml +0 -14
- data/test/fixtures/pirates.yml +0 -9
- data/test/fixtures/polymorphic_designs.yml +0 -19
- data/test/fixtures/polymorphic_prices.yml +0 -19
- data/test/fixtures/posts.yml +0 -52
- data/test/fixtures/price_estimates.yml +0 -7
- data/test/fixtures/projects.yml +0 -7
- data/test/fixtures/readers.yml +0 -9
- data/test/fixtures/references.yml +0 -17
- data/test/fixtures/reserved_words/distinct.yml +0 -5
- data/test/fixtures/reserved_words/distincts_selects.yml +0 -11
- data/test/fixtures/reserved_words/group.yml +0 -14
- data/test/fixtures/reserved_words/select.yml +0 -8
- data/test/fixtures/reserved_words/values.yml +0 -7
- data/test/fixtures/ships.yml +0 -5
- data/test/fixtures/sponsors.yml +0 -9
- data/test/fixtures/subscribers.yml +0 -7
- data/test/fixtures/subscriptions.yml +0 -12
- data/test/fixtures/taggings.yml +0 -28
- data/test/fixtures/tags.yml +0 -7
- data/test/fixtures/tasks.yml +0 -7
- data/test/fixtures/tees.yml +0 -4
- data/test/fixtures/ties.yml +0 -4
- data/test/fixtures/topics.yml +0 -42
- data/test/fixtures/toys.yml +0 -4
- data/test/fixtures/treasures.yml +0 -10
- data/test/fixtures/vertices.yml +0 -4
- data/test/fixtures/warehouse-things.yml +0 -3
- data/test/fixtures/zines.yml +0 -5
- data/test/migrations/broken/100_migration_that_raises_exception.rb +0 -10
- data/test/migrations/decimal/1_give_me_big_numbers.rb +0 -15
- data/test/migrations/duplicate/1_people_have_last_names.rb +0 -9
- data/test/migrations/duplicate/2_we_need_reminders.rb +0 -12
- data/test/migrations/duplicate/3_foo.rb +0 -7
- data/test/migrations/duplicate/3_innocent_jointable.rb +0 -12
- data/test/migrations/duplicate_names/20080507052938_chunky.rb +0 -7
- data/test/migrations/duplicate_names/20080507053028_chunky.rb +0 -7
- data/test/migrations/interleaved/pass_1/3_innocent_jointable.rb +0 -12
- data/test/migrations/interleaved/pass_2/1_people_have_last_names.rb +0 -9
- data/test/migrations/interleaved/pass_2/3_innocent_jointable.rb +0 -12
- data/test/migrations/interleaved/pass_3/1_people_have_last_names.rb +0 -9
- data/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb +0 -8
- data/test/migrations/interleaved/pass_3/3_innocent_jointable.rb +0 -12
- data/test/migrations/missing/1000_people_have_middle_names.rb +0 -9
- data/test/migrations/missing/1_people_have_last_names.rb +0 -9
- data/test/migrations/missing/3_we_need_reminders.rb +0 -12
- data/test/migrations/missing/4_innocent_jointable.rb +0 -12
- data/test/migrations/valid/1_people_have_last_names.rb +0 -9
- data/test/migrations/valid/2_we_need_reminders.rb +0 -12
- data/test/migrations/valid/3_innocent_jointable.rb +0 -12
- data/test/models/author.rb +0 -151
- data/test/models/auto_id.rb +0 -4
- data/test/models/binary.rb +0 -2
- data/test/models/bird.rb +0 -9
- data/test/models/book.rb +0 -4
- data/test/models/categorization.rb +0 -5
- data/test/models/category.rb +0 -34
- data/test/models/citation.rb +0 -6
- data/test/models/club.rb +0 -13
- data/test/models/column_name.rb +0 -3
- data/test/models/comment.rb +0 -29
- data/test/models/company.rb +0 -173
- data/test/models/company_in_module.rb +0 -78
- data/test/models/computer.rb +0 -3
- data/test/models/contact.rb +0 -16
- data/test/models/contract.rb +0 -5
- data/test/models/course.rb +0 -3
- data/test/models/customer.rb +0 -73
- data/test/models/default.rb +0 -2
- data/test/models/developer.rb +0 -101
- data/test/models/edge.rb +0 -5
- data/test/models/entrant.rb +0 -3
- data/test/models/essay.rb +0 -3
- data/test/models/event.rb +0 -3
- data/test/models/event_author.rb +0 -8
- data/test/models/face.rb +0 -7
- data/test/models/guid.rb +0 -2
- data/test/models/interest.rb +0 -5
- data/test/models/invoice.rb +0 -4
- data/test/models/item.rb +0 -7
- data/test/models/job.rb +0 -5
- data/test/models/joke.rb +0 -3
- data/test/models/keyboard.rb +0 -3
- data/test/models/legacy_thing.rb +0 -3
- data/test/models/line_item.rb +0 -3
- data/test/models/man.rb +0 -9
- data/test/models/matey.rb +0 -4
- data/test/models/member.rb +0 -12
- data/test/models/member_detail.rb +0 -5
- data/test/models/member_type.rb +0 -3
- data/test/models/membership.rb +0 -9
- data/test/models/minimalistic.rb +0 -2
- data/test/models/mixed_case_monkey.rb +0 -3
- data/test/models/movie.rb +0 -5
- data/test/models/order.rb +0 -4
- data/test/models/organization.rb +0 -6
- data/test/models/owner.rb +0 -5
- data/test/models/parrot.rb +0 -22
- data/test/models/person.rb +0 -16
- data/test/models/pet.rb +0 -5
- data/test/models/pirate.rb +0 -80
- data/test/models/polymorphic_design.rb +0 -3
- data/test/models/polymorphic_price.rb +0 -3
- data/test/models/post.rb +0 -102
- data/test/models/price_estimate.rb +0 -3
- data/test/models/project.rb +0 -30
- data/test/models/reader.rb +0 -4
- data/test/models/reference.rb +0 -4
- data/test/models/reply.rb +0 -46
- data/test/models/ship.rb +0 -19
- data/test/models/ship_part.rb +0 -7
- data/test/models/sponsor.rb +0 -4
- data/test/models/subject.rb +0 -4
- data/test/models/subscriber.rb +0 -8
- data/test/models/subscription.rb +0 -4
- data/test/models/tag.rb +0 -7
- data/test/models/tagging.rb +0 -10
- data/test/models/task.rb +0 -3
- data/test/models/tee.rb +0 -4
- data/test/models/tie.rb +0 -4
- data/test/models/topic.rb +0 -80
- data/test/models/toy.rb +0 -6
- data/test/models/treasure.rb +0 -8
- data/test/models/vertex.rb +0 -9
- data/test/models/warehouse_thing.rb +0 -5
- data/test/models/zine.rb +0 -3
- data/test/schema/mysql_specific_schema.rb +0 -31
- data/test/schema/postgresql_specific_schema.rb +0 -114
- data/test/schema/schema.rb +0 -550
- data/test/schema/schema2.rb +0 -6
- data/test/schema/sqlite_specific_schema.rb +0 -25
@@ -0,0 +1,537 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
|
4
|
+
module ActiveRecord
|
5
|
+
# = Active Record Relation
|
6
|
+
class Relation
|
7
|
+
JoinOperation = Struct.new(:relation, :join_class, :on)
|
8
|
+
ASSOCIATION_METHODS = [:includes, :eager_load, :preload]
|
9
|
+
MULTI_VALUE_METHODS = [:select, :group, :order, :joins, :where, :having, :bind]
|
10
|
+
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :from, :reordering, :reverse_order, :uniq]
|
11
|
+
|
12
|
+
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
|
13
|
+
|
14
|
+
attr_reader :table, :klass, :loaded
|
15
|
+
attr_accessor :extensions, :default_scoped
|
16
|
+
alias :loaded? :loaded
|
17
|
+
alias :default_scoped? :default_scoped
|
18
|
+
|
19
|
+
def initialize(klass, table)
|
20
|
+
@klass, @table = klass, table
|
21
|
+
|
22
|
+
@implicit_readonly = nil
|
23
|
+
@loaded = false
|
24
|
+
@default_scoped = false
|
25
|
+
|
26
|
+
SINGLE_VALUE_METHODS.each {|v| instance_variable_set(:"@#{v}_value", nil)}
|
27
|
+
(ASSOCIATION_METHODS + MULTI_VALUE_METHODS).each {|v| instance_variable_set(:"@#{v}_values", [])}
|
28
|
+
@extensions = []
|
29
|
+
@create_with_value = {}
|
30
|
+
end
|
31
|
+
|
32
|
+
def insert(values)
|
33
|
+
primary_key_value = nil
|
34
|
+
|
35
|
+
if primary_key && Hash === values
|
36
|
+
primary_key_value = values[values.keys.find { |k|
|
37
|
+
k.name == primary_key
|
38
|
+
}]
|
39
|
+
|
40
|
+
if !primary_key_value && connection.prefetch_primary_key?(klass.table_name)
|
41
|
+
primary_key_value = connection.next_sequence_value(klass.sequence_name)
|
42
|
+
values[klass.arel_table[klass.primary_key]] = primary_key_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
im = arel.create_insert
|
47
|
+
im.into @table
|
48
|
+
|
49
|
+
conn = @klass.connection
|
50
|
+
|
51
|
+
substitutes = values.sort_by { |arel_attr,_| arel_attr.name }
|
52
|
+
binds = substitutes.map do |arel_attr, value|
|
53
|
+
[@klass.columns_hash[arel_attr.name], value]
|
54
|
+
end
|
55
|
+
|
56
|
+
substitutes.each_with_index do |tuple, i|
|
57
|
+
tuple[1] = conn.substitute_at(binds[i][0], i)
|
58
|
+
end
|
59
|
+
|
60
|
+
if values.empty? # empty insert
|
61
|
+
im.values = Arel.sql(connection.empty_insert_statement_value)
|
62
|
+
else
|
63
|
+
im.insert substitutes
|
64
|
+
end
|
65
|
+
|
66
|
+
conn.insert(
|
67
|
+
im,
|
68
|
+
'SQL',
|
69
|
+
primary_key,
|
70
|
+
primary_key_value,
|
71
|
+
nil,
|
72
|
+
binds)
|
73
|
+
end
|
74
|
+
|
75
|
+
def new(*args, &block)
|
76
|
+
scoping { @klass.new(*args, &block) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize_copy(other)
|
80
|
+
@bind_values = @bind_values.dup
|
81
|
+
reset
|
82
|
+
end
|
83
|
+
|
84
|
+
alias build new
|
85
|
+
|
86
|
+
def create(*args, &block)
|
87
|
+
scoping { @klass.create(*args, &block) }
|
88
|
+
end
|
89
|
+
|
90
|
+
def create!(*args, &block)
|
91
|
+
scoping { @klass.create!(*args, &block) }
|
92
|
+
end
|
93
|
+
|
94
|
+
# Tries to load the first record; if it fails, then <tt>create</tt> is called with the same arguments as this method.
|
95
|
+
#
|
96
|
+
# Expects arguments in the same format as <tt>Base.create</tt>.
|
97
|
+
#
|
98
|
+
# ==== Examples
|
99
|
+
# # Find the first user named Penélope or create a new one.
|
100
|
+
# User.where(:first_name => 'Penélope').first_or_create
|
101
|
+
# # => <User id: 1, first_name: 'Penélope', last_name: nil>
|
102
|
+
#
|
103
|
+
# # Find the first user named Penélope or create a new one.
|
104
|
+
# # We already have one so the existing record will be returned.
|
105
|
+
# User.where(:first_name => 'Penélope').first_or_create
|
106
|
+
# # => <User id: 1, first_name: 'Penélope', last_name: nil>
|
107
|
+
#
|
108
|
+
# # Find the first user named Scarlett or create a new one with a particular last name.
|
109
|
+
# User.where(:first_name => 'Scarlett').first_or_create(:last_name => 'Johansson')
|
110
|
+
# # => <User id: 2, first_name: 'Scarlett', last_name: 'Johansson'>
|
111
|
+
#
|
112
|
+
# # Find the first user named Scarlett or create a new one with a different last name.
|
113
|
+
# # We already have one so the existing record will be returned.
|
114
|
+
# User.where(:first_name => 'Scarlett').first_or_create do |user|
|
115
|
+
# user.last_name = "O'Hara"
|
116
|
+
# end
|
117
|
+
# # => <User id: 2, first_name: 'Scarlett', last_name: 'Johansson'>
|
118
|
+
def first_or_create(attributes = nil, options = {}, &block)
|
119
|
+
first || create(attributes, options, &block)
|
120
|
+
end
|
121
|
+
|
122
|
+
# Like <tt>first_or_create</tt> but calls <tt>create!</tt> so an exception is raised if the created record is invalid.
|
123
|
+
#
|
124
|
+
# Expects arguments in the same format as <tt>Base.create!</tt>.
|
125
|
+
def first_or_create!(attributes = nil, options = {}, &block)
|
126
|
+
first || create!(attributes, options, &block)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Like <tt>first_or_create</tt> but calls <tt>new</tt> instead of <tt>create</tt>.
|
130
|
+
#
|
131
|
+
# Expects arguments in the same format as <tt>Base.new</tt>.
|
132
|
+
def first_or_initialize(attributes = nil, options = {}, &block)
|
133
|
+
first || new(attributes, options, &block)
|
134
|
+
end
|
135
|
+
|
136
|
+
# Runs EXPLAIN on the query or queries triggered by this relation and
|
137
|
+
# returns the result as a string. The string is formatted imitating the
|
138
|
+
# ones printed by the database shell.
|
139
|
+
#
|
140
|
+
# Note that this method actually runs the queries, since the results of some
|
141
|
+
# are needed by the next ones when eager loading is going on.
|
142
|
+
#
|
143
|
+
# Please see further details in the
|
144
|
+
# {Active Record Query Interface guide}[http://edgeguides.rubyonrails.org/active_record_querying.html#running-explain].
|
145
|
+
def explain
|
146
|
+
_, queries = collecting_queries_for_explain { exec_queries }
|
147
|
+
exec_explain(queries)
|
148
|
+
end
|
149
|
+
|
150
|
+
def to_a
|
151
|
+
# We monitor here the entire execution rather than individual SELECTs
|
152
|
+
# because from the point of view of the user fetching the records of a
|
153
|
+
# relation is a single unit of work. You want to know if this call takes
|
154
|
+
# too long, not if the individual queries take too long.
|
155
|
+
#
|
156
|
+
# It could be the case that none of the queries involved surpass the
|
157
|
+
# threshold, and at the same time the sum of them all does. The user
|
158
|
+
# should get a query plan logged in that case.
|
159
|
+
logging_query_plan do
|
160
|
+
exec_queries
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def exec_queries
|
165
|
+
return @records if loaded?
|
166
|
+
|
167
|
+
default_scoped = with_default_scope
|
168
|
+
|
169
|
+
if default_scoped.equal?(self)
|
170
|
+
@records = if @readonly_value.nil? && !@klass.locking_enabled?
|
171
|
+
eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
|
172
|
+
else
|
173
|
+
IdentityMap.without do
|
174
|
+
eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
preload = @preload_values
|
179
|
+
preload += @includes_values unless eager_loading?
|
180
|
+
preload.each do |associations|
|
181
|
+
ActiveRecord::Associations::Preloader.new(@records, associations).run
|
182
|
+
end
|
183
|
+
|
184
|
+
# @readonly_value is true only if set explicitly. @implicit_readonly is true if there
|
185
|
+
# are JOINS and no explicit SELECT.
|
186
|
+
readonly = @readonly_value.nil? ? @implicit_readonly : @readonly_value
|
187
|
+
@records.each { |record| record.readonly! } if readonly
|
188
|
+
else
|
189
|
+
@records = default_scoped.to_a
|
190
|
+
end
|
191
|
+
|
192
|
+
@loaded = true
|
193
|
+
@records
|
194
|
+
end
|
195
|
+
private :exec_queries
|
196
|
+
|
197
|
+
def as_json(options = nil) #:nodoc:
|
198
|
+
to_a.as_json(options)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Returns size of the records.
|
202
|
+
def size
|
203
|
+
loaded? ? @records.length : count
|
204
|
+
end
|
205
|
+
|
206
|
+
# Returns true if there are no records.
|
207
|
+
def empty?
|
208
|
+
return @records.empty? if loaded?
|
209
|
+
|
210
|
+
c = count
|
211
|
+
c.respond_to?(:zero?) ? c.zero? : c.empty?
|
212
|
+
end
|
213
|
+
|
214
|
+
def any?
|
215
|
+
if block_given?
|
216
|
+
to_a.any? { |*block_args| yield(*block_args) }
|
217
|
+
else
|
218
|
+
!empty?
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def many?
|
223
|
+
if block_given?
|
224
|
+
to_a.many? { |*block_args| yield(*block_args) }
|
225
|
+
else
|
226
|
+
@limit_value ? to_a.many? : size > 1
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
# Scope all queries to the current scope.
|
231
|
+
#
|
232
|
+
# ==== Example
|
233
|
+
#
|
234
|
+
# Comment.where(:post_id => 1).scoping do
|
235
|
+
# Comment.first # SELECT * FROM comments WHERE post_id = 1
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# Please check unscoped if you want to remove all previous scopes (including
|
239
|
+
# the default_scope) during the execution of a block.
|
240
|
+
def scoping
|
241
|
+
@klass.with_scope(self, :overwrite) { yield }
|
242
|
+
end
|
243
|
+
|
244
|
+
# Updates all records with details given if they match a set of conditions supplied, limits and order can
|
245
|
+
# also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the
|
246
|
+
# database. It does not instantiate the involved models and it does not trigger Active Record callbacks
|
247
|
+
# or validations.
|
248
|
+
#
|
249
|
+
# ==== Parameters
|
250
|
+
#
|
251
|
+
# * +updates+ - A string, array, or hash representing the SET part of an SQL statement.
|
252
|
+
# * +conditions+ - A string, array, or hash representing the WHERE part of an SQL statement.
|
253
|
+
# See conditions in the intro.
|
254
|
+
# * +options+ - Additional options are <tt>:limit</tt> and <tt>:order</tt>, see the examples for usage.
|
255
|
+
#
|
256
|
+
# ==== Examples
|
257
|
+
#
|
258
|
+
# # Update all customers with the given attributes
|
259
|
+
# Customer.update_all :wants_email => true
|
260
|
+
#
|
261
|
+
# # Update all books with 'Rails' in their title
|
262
|
+
# Book.update_all "author = 'David'", "title LIKE '%Rails%'"
|
263
|
+
#
|
264
|
+
# # Update all avatars migrated more than a week ago
|
265
|
+
# Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago]
|
266
|
+
#
|
267
|
+
# # Update all books that match conditions, but limit it to 5 ordered by date
|
268
|
+
# Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5
|
269
|
+
#
|
270
|
+
# # Conditions from the current relation also works
|
271
|
+
# Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David')
|
272
|
+
#
|
273
|
+
# # The same idea applies to limit and order
|
274
|
+
# Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')
|
275
|
+
def update_all(updates, conditions = nil, options = {})
|
276
|
+
IdentityMap.repository[symbolized_base_class].clear if IdentityMap.enabled?
|
277
|
+
if conditions || options.present?
|
278
|
+
where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates)
|
279
|
+
else
|
280
|
+
stmt = Arel::UpdateManager.new(arel.engine)
|
281
|
+
|
282
|
+
stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
|
283
|
+
stmt.table(table)
|
284
|
+
stmt.key = table[primary_key]
|
285
|
+
|
286
|
+
if joins_values.any?
|
287
|
+
@klass.connection.join_to_update(stmt, arel)
|
288
|
+
else
|
289
|
+
stmt.take(arel.limit)
|
290
|
+
stmt.order(*arel.orders)
|
291
|
+
stmt.wheres = arel.constraints
|
292
|
+
end
|
293
|
+
|
294
|
+
@klass.connection.update stmt, 'SQL', bind_values
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
# Updates an object (or multiple objects) and saves it to the database, if validations pass.
|
299
|
+
# The resulting object is returned whether the object was saved successfully to the database or not.
|
300
|
+
#
|
301
|
+
# ==== Parameters
|
302
|
+
#
|
303
|
+
# * +id+ - This should be the id or an array of ids to be updated.
|
304
|
+
# * +attributes+ - This should be a hash of attributes or an array of hashes.
|
305
|
+
#
|
306
|
+
# ==== Examples
|
307
|
+
#
|
308
|
+
# # Updates one record
|
309
|
+
# Person.update(15, :user_name => 'Samuel', :group => 'expert')
|
310
|
+
#
|
311
|
+
# # Updates multiple records
|
312
|
+
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
|
313
|
+
# Person.update(people.keys, people.values)
|
314
|
+
def update(id, attributes)
|
315
|
+
if id.is_a?(Array)
|
316
|
+
id.each.with_index.map {|one_id, idx| update(one_id, attributes[idx])}
|
317
|
+
else
|
318
|
+
object = find(id)
|
319
|
+
object.update_attributes(attributes)
|
320
|
+
object
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
# Destroys the records matching +conditions+ by instantiating each
|
325
|
+
# record and calling its +destroy+ method. Each object's callbacks are
|
326
|
+
# executed (including <tt>:dependent</tt> association options and
|
327
|
+
# +before_destroy+/+after_destroy+ Observer methods). Returns the
|
328
|
+
# collection of objects that were destroyed; each will be frozen, to
|
329
|
+
# reflect that no changes should be made (since they can't be
|
330
|
+
# persisted).
|
331
|
+
#
|
332
|
+
# Note: Instantiation, callback execution, and deletion of each
|
333
|
+
# record can be time consuming when you're removing many records at
|
334
|
+
# once. It generates at least one SQL +DELETE+ query per record (or
|
335
|
+
# possibly more, to enforce your callbacks). If you want to delete many
|
336
|
+
# rows quickly, without concern for their associations or callbacks, use
|
337
|
+
# +delete_all+ instead.
|
338
|
+
#
|
339
|
+
# ==== Parameters
|
340
|
+
#
|
341
|
+
# * +conditions+ - A string, array, or hash that specifies which records
|
342
|
+
# to destroy. If omitted, all records are destroyed. See the
|
343
|
+
# Conditions section in the introduction to ActiveRecord::Base for
|
344
|
+
# more information.
|
345
|
+
#
|
346
|
+
# ==== Examples
|
347
|
+
#
|
348
|
+
# Person.destroy_all("last_login < '2004-04-04'")
|
349
|
+
# Person.destroy_all(:status => "inactive")
|
350
|
+
# Person.where(:age => 0..18).destroy_all
|
351
|
+
def destroy_all(conditions = nil)
|
352
|
+
if conditions
|
353
|
+
where(conditions).destroy_all
|
354
|
+
else
|
355
|
+
to_a.each {|object| object.destroy }.tap { reset }
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
# Destroy an object (or multiple objects) that has the given id, the object is instantiated first,
|
360
|
+
# therefore all callbacks and filters are fired off before the object is deleted. This method is
|
361
|
+
# less efficient than ActiveRecord#delete but allows cleanup methods and other actions to be run.
|
362
|
+
#
|
363
|
+
# This essentially finds the object (or multiple objects) with the given id, creates a new object
|
364
|
+
# from the attributes, and then calls destroy on it.
|
365
|
+
#
|
366
|
+
# ==== Parameters
|
367
|
+
#
|
368
|
+
# * +id+ - Can be either an Integer or an Array of Integers.
|
369
|
+
#
|
370
|
+
# ==== Examples
|
371
|
+
#
|
372
|
+
# # Destroy a single object
|
373
|
+
# Todo.destroy(1)
|
374
|
+
#
|
375
|
+
# # Destroy multiple objects
|
376
|
+
# todos = [1,2,3]
|
377
|
+
# Todo.destroy(todos)
|
378
|
+
def destroy(id)
|
379
|
+
if id.is_a?(Array)
|
380
|
+
id.map { |one_id| destroy(one_id) }
|
381
|
+
else
|
382
|
+
find(id).destroy
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
# Deletes the records matching +conditions+ without instantiating the records first, and hence not
|
387
|
+
# calling the +destroy+ method nor invoking callbacks. This is a single SQL DELETE statement that
|
388
|
+
# goes straight to the database, much more efficient than +destroy_all+. Be careful with relations
|
389
|
+
# though, in particular <tt>:dependent</tt> rules defined on associations are not honored. Returns
|
390
|
+
# the number of rows affected.
|
391
|
+
#
|
392
|
+
# ==== Parameters
|
393
|
+
#
|
394
|
+
# * +conditions+ - Conditions are specified the same way as with +find+ method.
|
395
|
+
#
|
396
|
+
# ==== Example
|
397
|
+
#
|
398
|
+
# Post.delete_all("person_id = 5 AND (category = 'Something' OR category = 'Else')")
|
399
|
+
# Post.delete_all(["person_id = ? AND (category = ? OR category = ?)", 5, 'Something', 'Else'])
|
400
|
+
# Post.where(:person_id => 5).where(:category => ['Something', 'Else']).delete_all
|
401
|
+
#
|
402
|
+
# Both calls delete the affected posts all at once with a single DELETE statement.
|
403
|
+
# If you need to destroy dependent associations or call your <tt>before_*</tt> or
|
404
|
+
# +after_destroy+ callbacks, use the +destroy_all+ method instead.
|
405
|
+
def delete_all(conditions = nil)
|
406
|
+
raise ActiveRecordError.new("delete_all doesn't support limit scope") if self.limit_value
|
407
|
+
|
408
|
+
IdentityMap.repository[symbolized_base_class] = {} if IdentityMap.enabled?
|
409
|
+
if conditions
|
410
|
+
where(conditions).delete_all
|
411
|
+
else
|
412
|
+
statement = arel.compile_delete
|
413
|
+
affected = @klass.connection.delete(statement, 'SQL', bind_values)
|
414
|
+
|
415
|
+
reset
|
416
|
+
affected
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
# Deletes the row with a primary key matching the +id+ argument, using a
|
421
|
+
# SQL +DELETE+ statement, and returns the number of rows deleted. Active
|
422
|
+
# Record objects are not instantiated, so the object's callbacks are not
|
423
|
+
# executed, including any <tt>:dependent</tt> association options or
|
424
|
+
# Observer methods.
|
425
|
+
#
|
426
|
+
# You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
|
427
|
+
#
|
428
|
+
# Note: Although it is often much faster than the alternative,
|
429
|
+
# <tt>#destroy</tt>, skipping callbacks might bypass business logic in
|
430
|
+
# your application that ensures referential integrity or performs other
|
431
|
+
# essential jobs.
|
432
|
+
#
|
433
|
+
# ==== Examples
|
434
|
+
#
|
435
|
+
# # Delete a single row
|
436
|
+
# Todo.delete(1)
|
437
|
+
#
|
438
|
+
# # Delete multiple rows
|
439
|
+
# Todo.delete([2,3,4])
|
440
|
+
def delete(id_or_array)
|
441
|
+
IdentityMap.remove_by_id(self.symbolized_base_class, id_or_array) if IdentityMap.enabled?
|
442
|
+
where(primary_key => id_or_array).delete_all
|
443
|
+
end
|
444
|
+
|
445
|
+
def reload
|
446
|
+
reset
|
447
|
+
to_a # force reload
|
448
|
+
self
|
449
|
+
end
|
450
|
+
|
451
|
+
def reset
|
452
|
+
@first = @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil
|
453
|
+
@should_eager_load = @join_dependency = nil
|
454
|
+
@records = []
|
455
|
+
self
|
456
|
+
end
|
457
|
+
|
458
|
+
def to_sql
|
459
|
+
@to_sql ||= klass.connection.to_sql(arel, @bind_values.dup)
|
460
|
+
end
|
461
|
+
|
462
|
+
def where_values_hash
|
463
|
+
equalities = with_default_scope.where_values.grep(Arel::Nodes::Equality).find_all { |node|
|
464
|
+
node.left.relation.name == table_name
|
465
|
+
}
|
466
|
+
|
467
|
+
Hash[equalities.map { |where| [where.left.name, where.right] }].with_indifferent_access
|
468
|
+
end
|
469
|
+
|
470
|
+
def scope_for_create
|
471
|
+
@scope_for_create ||= where_values_hash.merge(create_with_value)
|
472
|
+
end
|
473
|
+
|
474
|
+
def eager_loading?
|
475
|
+
@should_eager_load ||=
|
476
|
+
@eager_load_values.any? ||
|
477
|
+
@includes_values.any? && (joined_includes_values.any? || references_eager_loaded_tables?)
|
478
|
+
end
|
479
|
+
|
480
|
+
# Joins that are also marked for preloading. In which case we should just eager load them.
|
481
|
+
# Note that this is a naive implementation because we could have strings and symbols which
|
482
|
+
# represent the same association, but that aren't matched by this. Also, we could have
|
483
|
+
# nested hashes which partially match, e.g. { :a => :b } & { :a => [:b, :c] }
|
484
|
+
def joined_includes_values
|
485
|
+
@includes_values & @joins_values
|
486
|
+
end
|
487
|
+
|
488
|
+
def ==(other)
|
489
|
+
case other
|
490
|
+
when Relation
|
491
|
+
other.to_sql == to_sql
|
492
|
+
when Array
|
493
|
+
to_a == other
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
def inspect
|
498
|
+
to_a.inspect
|
499
|
+
end
|
500
|
+
|
501
|
+
def with_default_scope #:nodoc:
|
502
|
+
if default_scoped? && default_scope = klass.send(:build_default_scope)
|
503
|
+
default_scope = default_scope.merge(self)
|
504
|
+
default_scope.default_scoped = false
|
505
|
+
default_scope
|
506
|
+
else
|
507
|
+
self
|
508
|
+
end
|
509
|
+
end
|
510
|
+
|
511
|
+
private
|
512
|
+
|
513
|
+
def references_eager_loaded_tables?
|
514
|
+
joined_tables = arel.join_sources.map do |join|
|
515
|
+
if join.is_a?(Arel::Nodes::StringJoin)
|
516
|
+
tables_in_string(join.left)
|
517
|
+
else
|
518
|
+
[join.left.table_name, join.left.table_alias]
|
519
|
+
end
|
520
|
+
end
|
521
|
+
|
522
|
+
joined_tables += [table.name, table.table_alias]
|
523
|
+
|
524
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
525
|
+
joined_tables = joined_tables.flatten.compact.map { |t| t.downcase }.uniq
|
526
|
+
|
527
|
+
(tables_in_string(to_sql) - joined_tables).any?
|
528
|
+
end
|
529
|
+
|
530
|
+
def tables_in_string(string)
|
531
|
+
return [] if string.blank?
|
532
|
+
# always convert table names to downcase as in Oracle quoted table names are in uppercase
|
533
|
+
# ignore raw_sql_ that is used by Oracle adapter as alias for limit/offset subqueries
|
534
|
+
string.scan(/([a-zA-Z_][.\w]+).?\./).flatten.map{ |s| s.downcase }.uniq - ['raw_sql_']
|
535
|
+
end
|
536
|
+
end
|
537
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
###
|
3
|
+
# This class encapsulates a Result returned from calling +exec_query+ on any
|
4
|
+
# database connection adapter. For example:
|
5
|
+
#
|
6
|
+
# x = ActiveRecord::Base.connection.exec_query('SELECT * FROM foo')
|
7
|
+
# x # => #<ActiveRecord::Result:0xdeadbeef>
|
8
|
+
class Result
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
attr_reader :columns, :rows
|
12
|
+
|
13
|
+
def initialize(columns, rows)
|
14
|
+
@columns = columns
|
15
|
+
@rows = rows
|
16
|
+
@hash_rows = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def each
|
20
|
+
hash_rows.each { |row| yield row }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
hash_rows
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def hash_rows
|
29
|
+
@hash_rows ||=
|
30
|
+
begin
|
31
|
+
# We freeze the strings to prevent them getting duped when
|
32
|
+
# used as keys in ActiveRecord::Model's @attributes hash
|
33
|
+
columns = @columns.map { |c| c.dup.freeze }
|
34
|
+
@rows.map { |row|
|
35
|
+
Hash[columns.zip(row)]
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|