sequel 4.49.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +70 -0
- data/README.rdoc +195 -136
- data/Rakefile +26 -42
- data/bin/sequel +3 -5
- data/doc/advanced_associations.rdoc +86 -163
- data/doc/association_basics.rdoc +197 -274
- data/doc/bin_sequel.rdoc +5 -3
- data/doc/cheat_sheet.rdoc +66 -43
- data/doc/code_order.rdoc +1 -8
- data/doc/core_extensions.rdoc +81 -56
- data/doc/dataset_basics.rdoc +8 -17
- data/doc/dataset_filtering.rdoc +81 -86
- data/doc/extensions.rdoc +3 -10
- data/doc/mass_assignment.rdoc +73 -30
- data/doc/migration.rdoc +19 -36
- data/doc/model_dataset_method_design.rdoc +14 -17
- data/doc/model_hooks.rdoc +15 -25
- data/doc/model_plugins.rdoc +10 -10
- data/doc/mssql_stored_procedures.rdoc +3 -3
- data/doc/object_model.rdoc +52 -70
- data/doc/opening_databases.rdoc +39 -32
- data/doc/postgresql.rdoc +48 -38
- data/doc/prepared_statements.rdoc +27 -22
- data/doc/querying.rdoc +173 -150
- data/doc/reflection.rdoc +5 -6
- data/doc/release_notes/5.0.0.txt +159 -0
- data/doc/schema_modification.rdoc +63 -60
- data/doc/security.rdoc +97 -88
- data/doc/sharding.rdoc +43 -30
- data/doc/sql.rdoc +53 -65
- data/doc/testing.rdoc +3 -5
- data/doc/thread_safety.rdoc +2 -4
- data/doc/transactions.rdoc +18 -17
- data/doc/validations.rdoc +48 -45
- data/doc/virtual_rows.rdoc +87 -115
- data/lib/sequel.rb +1 -1
- data/lib/sequel/adapters/ado.rb +9 -25
- data/lib/sequel/adapters/ado/access.rb +7 -13
- data/lib/sequel/adapters/ado/mssql.rb +2 -9
- data/lib/sequel/adapters/amalgalite.rb +3 -18
- data/lib/sequel/adapters/ibmdb.rb +9 -45
- data/lib/sequel/adapters/jdbc.rb +13 -73
- data/lib/sequel/adapters/jdbc/db2.rb +8 -37
- data/lib/sequel/adapters/jdbc/derby.rb +4 -50
- data/lib/sequel/adapters/jdbc/h2.rb +4 -25
- data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -26
- data/lib/sequel/adapters/jdbc/jtds.rb +2 -9
- data/lib/sequel/adapters/jdbc/mssql.rb +1 -11
- data/lib/sequel/adapters/jdbc/mysql.rb +1 -15
- data/lib/sequel/adapters/jdbc/oracle.rb +4 -26
- data/lib/sequel/adapters/jdbc/postgresql.rb +2 -31
- data/lib/sequel/adapters/jdbc/sqlanywhere.rb +4 -17
- data/lib/sequel/adapters/jdbc/sqlite.rb +1 -7
- data/lib/sequel/adapters/jdbc/sqlserver.rb +1 -13
- data/lib/sequel/adapters/jdbc/transactions.rb +1 -14
- data/lib/sequel/adapters/mock.rb +4 -30
- data/lib/sequel/adapters/mysql.rb +7 -44
- data/lib/sequel/adapters/mysql2.rb +5 -23
- data/lib/sequel/adapters/odbc.rb +0 -19
- data/lib/sequel/adapters/odbc/db2.rb +1 -1
- data/lib/sequel/adapters/odbc/mssql.rb +4 -12
- data/lib/sequel/adapters/odbc/oracle.rb +1 -1
- data/lib/sequel/adapters/oracle.rb +7 -13
- data/lib/sequel/adapters/postgres.rb +13 -57
- data/lib/sequel/adapters/postgresql.rb +1 -1
- data/lib/sequel/adapters/shared/access.rb +11 -51
- data/lib/sequel/adapters/shared/db2.rb +3 -61
- data/lib/sequel/adapters/shared/mssql.rb +21 -157
- data/lib/sequel/adapters/shared/mysql.rb +23 -224
- data/lib/sequel/adapters/shared/oracle.rb +13 -41
- data/lib/sequel/adapters/shared/postgres.rb +44 -259
- data/lib/sequel/adapters/shared/sqlanywhere.rb +4 -96
- data/lib/sequel/adapters/shared/sqlite.rb +12 -101
- data/lib/sequel/adapters/sqlanywhere.rb +4 -23
- data/lib/sequel/adapters/sqlite.rb +2 -19
- data/lib/sequel/adapters/tinytds.rb +5 -15
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +1 -1
- data/lib/sequel/adapters/utils/mysql_mysql2.rb +2 -4
- data/lib/sequel/adapters/utils/mysql_prepared_statements.rb +3 -6
- data/lib/sequel/adapters/utils/replace.rb +0 -5
- data/lib/sequel/adapters/utils/stored_procedures.rb +0 -2
- data/lib/sequel/adapters/utils/unmodified_identifiers.rb +2 -0
- data/lib/sequel/ast_transformer.rb +3 -94
- data/lib/sequel/connection_pool.rb +26 -28
- data/lib/sequel/connection_pool/sharded_single.rb +1 -4
- data/lib/sequel/connection_pool/sharded_threaded.rb +97 -95
- data/lib/sequel/connection_pool/single.rb +0 -2
- data/lib/sequel/connection_pool/threaded.rb +94 -110
- data/lib/sequel/core.rb +42 -101
- data/lib/sequel/database.rb +12 -2
- data/lib/sequel/database/connecting.rb +23 -60
- data/lib/sequel/database/dataset.rb +6 -9
- data/lib/sequel/database/dataset_defaults.rb +4 -48
- data/lib/sequel/database/features.rb +5 -4
- data/lib/sequel/database/logging.rb +2 -9
- data/lib/sequel/database/misc.rb +23 -55
- data/lib/sequel/database/query.rb +8 -13
- data/lib/sequel/database/schema_generator.rb +89 -64
- data/lib/sequel/database/schema_methods.rb +61 -79
- data/lib/sequel/database/transactions.rb +4 -24
- data/lib/sequel/dataset.rb +18 -10
- data/lib/sequel/dataset/actions.rb +53 -107
- data/lib/sequel/dataset/dataset_module.rb +3 -15
- data/lib/sequel/dataset/features.rb +30 -30
- data/lib/sequel/dataset/graph.rb +40 -49
- data/lib/sequel/dataset/misc.rb +12 -37
- data/lib/sequel/dataset/placeholder_literalizer.rb +4 -4
- data/lib/sequel/dataset/prepared_statements.rb +23 -51
- data/lib/sequel/dataset/query.rb +71 -155
- data/lib/sequel/dataset/sql.rb +30 -225
- data/lib/sequel/deprecated.rb +18 -27
- data/lib/sequel/exceptions.rb +1 -17
- data/lib/sequel/extensions/_model_pg_row.rb +0 -7
- data/lib/sequel/extensions/_pretty_table.rb +1 -3
- data/lib/sequel/extensions/arbitrary_servers.rb +10 -10
- data/lib/sequel/extensions/connection_expiration.rb +1 -1
- data/lib/sequel/extensions/connection_validator.rb +1 -1
- data/lib/sequel/extensions/constraint_validations.rb +11 -11
- data/lib/sequel/extensions/core_extensions.rb +39 -49
- data/lib/sequel/extensions/core_refinements.rb +39 -45
- data/lib/sequel/extensions/current_datetime_timestamp.rb +0 -4
- data/lib/sequel/extensions/date_arithmetic.rb +7 -7
- data/lib/sequel/extensions/duplicate_columns_handler.rb +12 -9
- data/lib/sequel/extensions/empty_array_consider_nulls.rb +2 -2
- data/lib/sequel/extensions/eval_inspect.rb +4 -11
- data/lib/sequel/extensions/freeze_datasets.rb +1 -69
- data/lib/sequel/extensions/from_block.rb +1 -35
- data/lib/sequel/extensions/graph_each.rb +2 -2
- data/lib/sequel/extensions/identifier_mangling.rb +9 -19
- data/lib/sequel/extensions/implicit_subquery.rb +2 -2
- data/lib/sequel/extensions/inflector.rb +4 -4
- data/lib/sequel/extensions/migration.rb +23 -40
- data/lib/sequel/extensions/no_auto_literal_strings.rb +2 -84
- data/lib/sequel/extensions/null_dataset.rb +2 -8
- data/lib/sequel/extensions/pagination.rb +1 -17
- data/lib/sequel/extensions/pg_array.rb +20 -189
- data/lib/sequel/extensions/pg_hstore.rb +11 -50
- data/lib/sequel/extensions/pg_hstore_ops.rb +2 -2
- data/lib/sequel/extensions/pg_inet.rb +2 -15
- data/lib/sequel/extensions/pg_interval.rb +1 -20
- data/lib/sequel/extensions/pg_json.rb +7 -27
- data/lib/sequel/extensions/pg_loose_count.rb +1 -1
- data/lib/sequel/extensions/pg_range.rb +6 -121
- data/lib/sequel/extensions/pg_range_ops.rb +1 -3
- data/lib/sequel/extensions/pg_row.rb +5 -77
- data/lib/sequel/extensions/pg_row_ops.rb +2 -13
- data/lib/sequel/extensions/query.rb +3 -4
- data/lib/sequel/extensions/round_timestamps.rb +0 -6
- data/lib/sequel/extensions/schema_dumper.rb +13 -13
- data/lib/sequel/extensions/select_remove.rb +3 -3
- data/lib/sequel/extensions/split_array_nil.rb +2 -2
- data/lib/sequel/extensions/sql_comments.rb +2 -2
- data/lib/sequel/extensions/string_agg.rb +11 -8
- data/lib/sequel/extensions/symbol_aref.rb +6 -20
- data/lib/sequel/model.rb +27 -62
- data/lib/sequel/model/associations.rb +128 -131
- data/lib/sequel/model/base.rb +171 -711
- data/lib/sequel/model/default_inflections.rb +1 -1
- data/lib/sequel/model/errors.rb +0 -3
- data/lib/sequel/model/exceptions.rb +2 -6
- data/lib/sequel/model/inflections.rb +1 -26
- data/lib/sequel/model/plugins.rb +1 -0
- data/lib/sequel/plugins/active_model.rb +2 -5
- data/lib/sequel/plugins/association_dependencies.rb +15 -15
- data/lib/sequel/plugins/association_pks.rb +14 -28
- data/lib/sequel/plugins/association_proxies.rb +6 -7
- data/lib/sequel/plugins/auto_validations.rb +4 -4
- data/lib/sequel/plugins/before_after_save.rb +0 -43
- data/lib/sequel/plugins/blacklist_security.rb +9 -8
- data/lib/sequel/plugins/boolean_readers.rb +3 -3
- data/lib/sequel/plugins/boolean_subsets.rb +2 -2
- data/lib/sequel/plugins/caching.rb +5 -5
- data/lib/sequel/plugins/class_table_inheritance.rb +71 -102
- data/lib/sequel/plugins/column_conflicts.rb +2 -2
- data/lib/sequel/plugins/column_select.rb +2 -2
- data/lib/sequel/plugins/composition.rb +15 -24
- data/lib/sequel/plugins/constraint_validations.rb +4 -3
- data/lib/sequel/plugins/csv_serializer.rb +13 -20
- data/lib/sequel/plugins/dataset_associations.rb +2 -2
- data/lib/sequel/plugins/def_dataset_method.rb +5 -5
- data/lib/sequel/plugins/defaults_setter.rb +1 -1
- data/lib/sequel/plugins/delay_add_association.rb +1 -1
- data/lib/sequel/plugins/finder.rb +16 -10
- data/lib/sequel/plugins/force_encoding.rb +1 -7
- data/lib/sequel/plugins/hook_class_methods.rb +4 -106
- data/lib/sequel/plugins/input_transformer.rb +10 -11
- data/lib/sequel/plugins/insert_returning_select.rb +1 -9
- data/lib/sequel/plugins/instance_filters.rb +5 -5
- data/lib/sequel/plugins/instance_hooks.rb +7 -52
- data/lib/sequel/plugins/inverted_subsets.rb +3 -1
- data/lib/sequel/plugins/json_serializer.rb +19 -19
- data/lib/sequel/plugins/lazy_attributes.rb +1 -10
- data/lib/sequel/plugins/list.rb +6 -6
- data/lib/sequel/plugins/many_through_many.rb +11 -8
- data/lib/sequel/plugins/mssql_optimistic_locking.rb +3 -3
- data/lib/sequel/plugins/nested_attributes.rb +18 -31
- data/lib/sequel/plugins/optimistic_locking.rb +3 -3
- data/lib/sequel/plugins/pg_array_associations.rb +8 -2
- data/lib/sequel/plugins/pg_row.rb +2 -11
- data/lib/sequel/plugins/prepared_statements.rb +13 -66
- data/lib/sequel/plugins/prepared_statements_safe.rb +1 -1
- data/lib/sequel/plugins/rcte_tree.rb +7 -7
- data/lib/sequel/plugins/serialization.rb +15 -33
- data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
- data/lib/sequel/plugins/sharding.rb +2 -8
- data/lib/sequel/plugins/single_table_inheritance.rb +10 -13
- data/lib/sequel/plugins/skip_create_refresh.rb +3 -3
- data/lib/sequel/plugins/static_cache.rb +8 -9
- data/lib/sequel/plugins/string_stripper.rb +3 -3
- data/lib/sequel/plugins/subclasses.rb +1 -1
- data/lib/sequel/plugins/subset_conditions.rb +2 -2
- data/lib/sequel/plugins/table_select.rb +2 -2
- data/lib/sequel/plugins/tactical_eager_loading.rb +4 -4
- data/lib/sequel/plugins/timestamps.rb +6 -7
- data/lib/sequel/plugins/touch.rb +4 -8
- data/lib/sequel/plugins/tree.rb +3 -3
- data/lib/sequel/plugins/typecast_on_load.rb +2 -2
- data/lib/sequel/plugins/unlimited_update.rb +1 -7
- data/lib/sequel/plugins/update_or_create.rb +3 -3
- data/lib/sequel/plugins/update_refresh.rb +3 -3
- data/lib/sequel/plugins/uuid.rb +7 -11
- data/lib/sequel/plugins/validation_class_methods.rb +10 -9
- data/lib/sequel/plugins/validation_contexts.rb +4 -4
- data/lib/sequel/plugins/validation_helpers.rb +26 -25
- data/lib/sequel/plugins/whitelist_security.rb +13 -9
- data/lib/sequel/plugins/xml_serializer.rb +24 -25
- data/lib/sequel/sql.rb +145 -276
- data/lib/sequel/timezones.rb +8 -22
- data/lib/sequel/version.rb +2 -2
- data/spec/adapter_spec.rb +1 -1
- data/spec/adapters/db2_spec.rb +2 -103
- data/spec/adapters/mssql_spec.rb +89 -68
- data/spec/adapters/mysql_spec.rb +101 -480
- data/spec/adapters/oracle_spec.rb +1 -9
- data/spec/adapters/postgres_spec.rb +312 -565
- data/spec/adapters/spec_helper.rb +12 -31
- data/spec/adapters/sqlanywhere_spec.rb +2 -77
- data/spec/adapters/sqlite_spec.rb +8 -146
- data/spec/bin_spec.rb +11 -16
- data/spec/core/connection_pool_spec.rb +173 -74
- data/spec/core/database_spec.rb +64 -244
- data/spec/core/dataset_spec.rb +81 -415
- data/spec/core/deprecated_spec.rb +3 -3
- data/spec/core/expression_filters_spec.rb +37 -144
- data/spec/core/mock_adapter_spec.rb +176 -4
- data/spec/core/object_graph_spec.rb +11 -60
- data/spec/core/placeholder_literalizer_spec.rb +1 -14
- data/spec/core/schema_generator_spec.rb +51 -40
- data/spec/core/schema_spec.rb +74 -77
- data/spec/core/spec_helper.rb +6 -24
- data/spec/core/version_spec.rb +1 -1
- data/spec/core_extensions_spec.rb +7 -83
- data/spec/core_model_spec.rb +2 -2
- data/spec/deprecation_helper.rb +2 -14
- data/spec/extensions/accessed_columns_spec.rb +1 -1
- data/spec/extensions/active_model_spec.rb +3 -3
- data/spec/extensions/after_initialize_spec.rb +1 -1
- data/spec/extensions/arbitrary_servers_spec.rb +2 -2
- data/spec/extensions/association_dependencies_spec.rb +1 -1
- data/spec/extensions/association_pks_spec.rb +4 -59
- data/spec/extensions/association_proxies_spec.rb +1 -1
- data/spec/extensions/auto_literal_strings_spec.rb +1 -12
- data/spec/extensions/auto_validations_spec.rb +1 -1
- data/spec/extensions/blacklist_security_spec.rb +1 -1
- data/spec/extensions/blank_spec.rb +1 -1
- data/spec/extensions/boolean_readers_spec.rb +1 -1
- data/spec/extensions/boolean_subsets_spec.rb +1 -1
- data/spec/extensions/caching_spec.rb +1 -1
- data/spec/extensions/class_table_inheritance_spec.rb +35 -1086
- data/spec/extensions/column_conflicts_spec.rb +1 -1
- data/spec/extensions/column_select_spec.rb +4 -4
- data/spec/extensions/columns_introspection_spec.rb +1 -1
- data/spec/extensions/columns_updated_spec.rb +1 -1
- data/spec/extensions/composition_spec.rb +1 -7
- data/spec/extensions/connection_expiration_spec.rb +3 -3
- data/spec/extensions/connection_validator_spec.rb +3 -3
- data/spec/extensions/constraint_validations_plugin_spec.rb +1 -1
- data/spec/extensions/constraint_validations_spec.rb +1 -1
- data/spec/extensions/core_refinements_spec.rb +1 -3
- data/spec/extensions/csv_serializer_spec.rb +4 -9
- data/spec/extensions/current_datetime_timestamp_spec.rb +1 -1
- data/spec/extensions/dataset_associations_spec.rb +2 -1
- data/spec/extensions/dataset_source_alias_spec.rb +1 -1
- data/spec/extensions/date_arithmetic_spec.rb +3 -3
- data/spec/extensions/def_dataset_method_spec.rb +1 -1
- data/spec/extensions/defaults_setter_spec.rb +2 -2
- data/spec/extensions/delay_add_association_spec.rb +8 -9
- data/spec/extensions/dirty_spec.rb +1 -1
- data/spec/extensions/duplicate_columns_handler_spec.rb +1 -1
- data/spec/extensions/eager_each_spec.rb +2 -2
- data/spec/extensions/empty_array_consider_nulls_spec.rb +1 -1
- data/spec/extensions/error_splitter_spec.rb +1 -1
- data/spec/extensions/error_sql_spec.rb +1 -1
- data/spec/extensions/eval_inspect_spec.rb +1 -1
- data/spec/extensions/finder_spec.rb +1 -1
- data/spec/extensions/force_encoding_spec.rb +2 -5
- data/spec/extensions/freeze_datasets_spec.rb +1 -1
- data/spec/extensions/graph_each_spec.rb +5 -5
- data/spec/extensions/hook_class_methods_spec.rb +1 -194
- data/spec/extensions/identifier_mangling_spec.rb +17 -170
- data/spec/extensions/implicit_subquery_spec.rb +1 -5
- data/spec/extensions/inflector_spec.rb +1 -1
- data/spec/extensions/input_transformer_spec.rb +7 -2
- data/spec/extensions/insert_returning_select_spec.rb +1 -1
- data/spec/extensions/instance_filters_spec.rb +1 -1
- data/spec/extensions/instance_hooks_spec.rb +1 -95
- data/spec/extensions/inverted_subsets_spec.rb +1 -1
- data/spec/extensions/json_serializer_spec.rb +1 -1
- data/spec/extensions/lazy_attributes_spec.rb +1 -7
- data/spec/extensions/list_spec.rb +1 -1
- data/spec/extensions/looser_typecasting_spec.rb +1 -1
- data/spec/extensions/many_through_many_spec.rb +1 -1
- data/spec/extensions/migration_spec.rb +2 -2
- data/spec/extensions/modification_detection_spec.rb +1 -1
- data/spec/extensions/mssql_optimistic_locking_spec.rb +1 -1
- data/spec/extensions/named_timezones_spec.rb +3 -3
- data/spec/extensions/nested_attributes_spec.rb +1 -29
- data/spec/extensions/null_dataset_spec.rb +1 -11
- data/spec/extensions/optimistic_locking_spec.rb +1 -1
- data/spec/extensions/pagination_spec.rb +1 -1
- data/spec/extensions/pg_array_associations_spec.rb +4 -1
- data/spec/extensions/pg_array_ops_spec.rb +1 -1
- data/spec/extensions/pg_array_spec.rb +3 -48
- data/spec/extensions/pg_enum_spec.rb +1 -1
- data/spec/extensions/pg_hstore_ops_spec.rb +1 -1
- data/spec/extensions/pg_hstore_spec.rb +23 -32
- data/spec/extensions/pg_inet_ops_spec.rb +1 -1
- data/spec/extensions/pg_inet_spec.rb +1 -14
- data/spec/extensions/pg_interval_spec.rb +3 -13
- data/spec/extensions/pg_json_ops_spec.rb +1 -1
- data/spec/extensions/pg_json_spec.rb +1 -13
- data/spec/extensions/pg_loose_count_spec.rb +1 -1
- data/spec/extensions/pg_range_ops_spec.rb +1 -1
- data/spec/extensions/pg_range_spec.rb +3 -88
- data/spec/extensions/pg_row_ops_spec.rb +1 -1
- data/spec/extensions/pg_row_plugin_spec.rb +1 -1
- data/spec/extensions/pg_row_spec.rb +1 -44
- data/spec/extensions/pg_static_cache_updater_spec.rb +1 -1
- data/spec/extensions/prepared_statements_safe_spec.rb +1 -1
- data/spec/extensions/prepared_statements_spec.rb +13 -48
- data/spec/extensions/pretty_table_spec.rb +1 -1
- data/spec/extensions/query_spec.rb +1 -12
- data/spec/extensions/rcte_tree_spec.rb +1 -1
- data/spec/extensions/round_timestamps_spec.rb +1 -5
- data/spec/extensions/s_spec.rb +1 -1
- data/spec/extensions/schema_caching_spec.rb +1 -1
- data/spec/extensions/schema_dumper_spec.rb +1 -1
- data/spec/extensions/select_remove_spec.rb +1 -1
- data/spec/extensions/sequel_4_dataset_methods_spec.rb +1 -1
- data/spec/extensions/serialization_modification_detection_spec.rb +1 -1
- data/spec/extensions/serialization_spec.rb +2 -14
- data/spec/extensions/server_block_spec.rb +1 -1
- data/spec/extensions/server_logging_spec.rb +2 -2
- data/spec/extensions/sharding_spec.rb +1 -1
- data/spec/extensions/shared_caching_spec.rb +1 -28
- data/spec/extensions/single_table_inheritance_spec.rb +2 -5
- data/spec/extensions/singular_table_names_spec.rb +1 -1
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +5 -27
- data/spec/extensions/split_array_nil_spec.rb +1 -1
- data/spec/extensions/split_values_spec.rb +1 -1
- data/spec/extensions/sql_comments_spec.rb +1 -1
- data/spec/extensions/sql_expr_spec.rb +1 -1
- data/spec/extensions/static_cache_spec.rb +1 -1
- data/spec/extensions/string_agg_spec.rb +2 -2
- data/spec/extensions/string_date_time_spec.rb +1 -1
- data/spec/extensions/string_stripper_spec.rb +1 -1
- data/spec/extensions/subclasses_spec.rb +1 -1
- data/spec/extensions/subset_conditions_spec.rb +1 -1
- data/spec/extensions/symbol_aref_refinement_spec.rb +1 -1
- data/spec/extensions/symbol_as_refinement_spec.rb +1 -1
- data/spec/extensions/table_select_spec.rb +4 -4
- data/spec/extensions/tactical_eager_loading_spec.rb +1 -6
- data/spec/extensions/thread_local_timezones_spec.rb +1 -1
- data/spec/extensions/timestamps_spec.rb +3 -3
- data/spec/extensions/to_dot_spec.rb +1 -1
- data/spec/extensions/touch_spec.rb +1 -1
- data/spec/extensions/tree_spec.rb +1 -1
- data/spec/extensions/typecast_on_load_spec.rb +1 -1
- data/spec/extensions/unlimited_update_spec.rb +1 -1
- data/spec/extensions/update_or_create_spec.rb +1 -1
- data/spec/extensions/update_primary_key_spec.rb +4 -3
- data/spec/extensions/update_refresh_spec.rb +1 -1
- data/spec/extensions/uuid_spec.rb +10 -12
- data/spec/extensions/validate_associated_spec.rb +1 -1
- data/spec/extensions/validation_class_methods_spec.rb +3 -3
- data/spec/extensions/validation_contexts_spec.rb +1 -1
- data/spec/extensions/validation_helpers_spec.rb +10 -44
- data/spec/extensions/whitelist_security_spec.rb +5 -5
- data/spec/extensions/xml_serializer_spec.rb +3 -3
- data/spec/guards_helper.rb +2 -1
- data/spec/integration/associations_test.rb +1 -23
- data/spec/integration/database_test.rb +7 -7
- data/spec/integration/dataset_test.rb +5 -47
- data/spec/integration/eager_loader_test.rb +1 -1
- data/spec/integration/migrator_test.rb +1 -1
- data/spec/integration/model_test.rb +4 -82
- data/spec/integration/plugin_test.rb +6 -22
- data/spec/integration/prepared_statement_test.rb +8 -88
- data/spec/integration/schema_test.rb +6 -6
- data/spec/integration/spec_helper.rb +13 -21
- data/spec/integration/timezone_test.rb +5 -5
- data/spec/integration/transaction_test.rb +3 -55
- data/spec/integration/type_test.rb +9 -9
- data/spec/model/association_reflection_spec.rb +24 -9
- data/spec/model/associations_spec.rb +124 -303
- data/spec/model/base_spec.rb +18 -137
- data/spec/model/class_dataset_methods_spec.rb +2 -20
- data/spec/model/dataset_methods_spec.rb +1 -20
- data/spec/model/eager_loading_spec.rb +17 -11
- data/spec/model/hooks_spec.rb +5 -300
- data/spec/model/inflector_spec.rb +1 -1
- data/spec/model/model_spec.rb +15 -320
- data/spec/model/plugins_spec.rb +2 -16
- data/spec/model/record_spec.rb +29 -121
- data/spec/model/spec_helper.rb +5 -15
- data/spec/model/validations_spec.rb +1 -1
- data/spec/sequel_warning.rb +1 -12
- metadata +8 -64
- data/doc/active_record.rdoc +0 -927
- data/lib/sequel/adapters/cubrid.rb +0 -160
- data/lib/sequel/adapters/do.rb +0 -166
- data/lib/sequel/adapters/do/mysql.rb +0 -69
- data/lib/sequel/adapters/do/postgres.rb +0 -46
- data/lib/sequel/adapters/do/sqlite3.rb +0 -41
- data/lib/sequel/adapters/jdbc/as400.rb +0 -92
- data/lib/sequel/adapters/jdbc/cubrid.rb +0 -65
- data/lib/sequel/adapters/jdbc/firebirdsql.rb +0 -37
- data/lib/sequel/adapters/jdbc/informix-sqli.rb +0 -34
- data/lib/sequel/adapters/jdbc/jdbcprogress.rb +0 -34
- data/lib/sequel/adapters/odbc/progress.rb +0 -12
- data/lib/sequel/adapters/shared/cubrid.rb +0 -245
- data/lib/sequel/adapters/shared/firebird.rb +0 -261
- data/lib/sequel/adapters/shared/informix.rb +0 -63
- data/lib/sequel/adapters/shared/progress.rb +0 -40
- data/lib/sequel/adapters/swift.rb +0 -169
- data/lib/sequel/adapters/swift/mysql.rb +0 -50
- data/lib/sequel/adapters/swift/postgres.rb +0 -49
- data/lib/sequel/adapters/swift/sqlite.rb +0 -48
- data/lib/sequel/adapters/utils/pg_types.rb +0 -4
- data/lib/sequel/dataset/mutation.rb +0 -98
- data/lib/sequel/extensions/_deprecated_identifier_mangling.rb +0 -117
- data/lib/sequel/extensions/empty_array_ignore_nulls.rb +0 -8
- data/lib/sequel/extensions/filter_having.rb +0 -65
- data/lib/sequel/extensions/hash_aliases.rb +0 -51
- data/lib/sequel/extensions/meta_def.rb +0 -37
- data/lib/sequel/extensions/query_literals.rb +0 -86
- data/lib/sequel/extensions/ruby18_symbol_extensions.rb +0 -26
- data/lib/sequel/extensions/sequel_3_dataset_methods.rb +0 -133
- data/lib/sequel/extensions/set_overrides.rb +0 -82
- data/lib/sequel/no_core_ext.rb +0 -4
- data/lib/sequel/plugins/association_autoreloading.rb +0 -11
- data/lib/sequel/plugins/identifier_columns.rb +0 -49
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +0 -11
- data/lib/sequel/plugins/pg_typecast_on_load.rb +0 -90
- data/lib/sequel/plugins/prepared_statements_associations.rb +0 -137
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +0 -71
- data/lib/sequel/plugins/schema.rb +0 -84
- data/lib/sequel/plugins/scissors.rb +0 -37
- data/spec/core/dataset_mutation_spec.rb +0 -253
- data/spec/extensions/_deprecated_identifier_mangling_spec.rb +0 -314
- data/spec/extensions/before_after_save_spec.rb +0 -40
- data/spec/extensions/filter_having_spec.rb +0 -42
- data/spec/extensions/from_block_spec.rb +0 -21
- data/spec/extensions/hash_aliases_spec.rb +0 -26
- data/spec/extensions/identifier_columns_spec.rb +0 -19
- data/spec/extensions/meta_def_spec.rb +0 -35
- data/spec/extensions/no_auto_literal_strings_spec.rb +0 -69
- data/spec/extensions/pg_typecast_on_load_spec.rb +0 -70
- data/spec/extensions/prepared_statements_associations_spec.rb +0 -212
- data/spec/extensions/prepared_statements_with_pk_spec.rb +0 -40
- data/spec/extensions/query_literals_spec.rb +0 -185
- data/spec/extensions/schema_spec.rb +0 -123
- data/spec/extensions/scissors_spec.rb +0 -27
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +0 -118
- data/spec/extensions/set_overrides_spec.rb +0 -75
data/doc/active_record.rdoc
DELETED
@@ -1,927 +0,0 @@
|
|
1
|
-
= Sequel for ActiveRecord Users
|
2
|
-
|
3
|
-
This guide is aimed at helping ActiveRecord users transition to Sequel. It assumes the user is familiar with ActiveRecord 2, but if you are familiar with a newer ActiveRecord version, the transition should be even easier.
|
4
|
-
|
5
|
-
== Introduction
|
6
|
-
|
7
|
-
Both Sequel and ActiveRecord use the active record pattern of database access, where model instances are objects that wrap a row in a database table or view, encapsulating the database access, and adding domain logic on that data. Just like ActiveRecord, Sequel supports both associations and inheritance, though Sequel does so in a more flexible manner than ActiveRecord.
|
8
|
-
|
9
|
-
Let's quickly run through the ActiveRecord README and show how it applies to Sequel.
|
10
|
-
|
11
|
-
== Automatic Mapping
|
12
|
-
|
13
|
-
Just like ActiveRecord, Sequel maps classes to tables and automatically creates accessor methods for the columns in the table, so if you have an albums table with a primary key named "id" and a string/varchar column named "name", the minimal model class is:
|
14
|
-
|
15
|
-
class Album < Sequel::Model
|
16
|
-
end
|
17
|
-
|
18
|
-
Sequel will autogenerate the column accessors, so you can do:
|
19
|
-
|
20
|
-
album = Album.new
|
21
|
-
album.name = 'RF'
|
22
|
-
|
23
|
-
If the table name for the class doesn't match the default one Sequel will choose, you can specify it manually:
|
24
|
-
|
25
|
-
class Album < Sequel::Model(:records)
|
26
|
-
end
|
27
|
-
|
28
|
-
== Associations
|
29
|
-
|
30
|
-
Sequel supports most of the same association types as ActiveRecord, but it uses names that reflect the database relationships instead of ones that imply ownership:
|
31
|
-
|
32
|
-
class Album < Sequel::Model
|
33
|
-
many_to_one :artist
|
34
|
-
one_to_many :tracks
|
35
|
-
many_to_many :tags
|
36
|
-
end
|
37
|
-
|
38
|
-
== Compositions
|
39
|
-
|
40
|
-
Sequel's +composition+ plugin allows you to easily create accessor methods that are composed of one or more of the database's columns, similar to ActiveRecord's +composed_of+:
|
41
|
-
|
42
|
-
class Artist < Sequel::Model
|
43
|
-
plugin :composition
|
44
|
-
composition :address, :mapping=>[:street, :city, :state, :zip]
|
45
|
-
end
|
46
|
-
|
47
|
-
== Validations
|
48
|
-
|
49
|
-
Sequel's +validation_class_methods+ plugin is modeled directly on ActiveRecord's validations, but the recommended approach is to use the +validation_helpers+ plugin inside a +validate+ instance method:
|
50
|
-
|
51
|
-
class Album < Sequel::Model
|
52
|
-
plugin :validation_helpers
|
53
|
-
|
54
|
-
def validate
|
55
|
-
super
|
56
|
-
validates_presence [:name, :copies_sold]
|
57
|
-
validates_unique [:name, :artist_id]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
== Hooks/Callbacks
|
62
|
-
|
63
|
-
Sequel's +hook_class_methods+ plugin is modeled directly on ActiveRecord's callbacks, but the recommended approach is to define your hooks/callbacks as instance methods:
|
64
|
-
|
65
|
-
class Album < Sequel::Model
|
66
|
-
def before_create
|
67
|
-
self.copies_sold ||= 0
|
68
|
-
super
|
69
|
-
end
|
70
|
-
|
71
|
-
def after_update
|
72
|
-
super
|
73
|
-
AuditLog.create(:log=>"Updated Album #{id}")
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
Observers can be implemented completely by hooks, so Sequel doesn't offer a separate observer class.
|
78
|
-
|
79
|
-
== Inheritance
|
80
|
-
|
81
|
-
Sequel supports both single table inheritance and class table inheritance using plugins:
|
82
|
-
|
83
|
-
class Employee < Sequel::Model
|
84
|
-
plugin :single_table_inheritance, :class_name_column
|
85
|
-
# or
|
86
|
-
plugin :class_table_inheritance
|
87
|
-
end
|
88
|
-
|
89
|
-
class Staff < Employee
|
90
|
-
end
|
91
|
-
|
92
|
-
class Manager < Employee
|
93
|
-
end
|
94
|
-
|
95
|
-
class Executive < Manager
|
96
|
-
end
|
97
|
-
|
98
|
-
== Transactions
|
99
|
-
|
100
|
-
Sequel supports transactions via the Database object (we recommend using the DB constant for single database applications):
|
101
|
-
|
102
|
-
DB.transaction do
|
103
|
-
album.artist.num_albums -= 1
|
104
|
-
album.artist.save
|
105
|
-
album.delete
|
106
|
-
end
|
107
|
-
|
108
|
-
For model classes, you can always access the database via +db+:
|
109
|
-
|
110
|
-
Album.db.transaction do
|
111
|
-
album.artist.num_albums -= 1
|
112
|
-
album.artist.save
|
113
|
-
album.delete
|
114
|
-
end
|
115
|
-
|
116
|
-
== Reflection
|
117
|
-
|
118
|
-
Just like ActiveRecord, Sequel has full reflection support for columns, associations, and many other things:
|
119
|
-
|
120
|
-
Album.columns # => [:id, :name, :artist_id, :copies_sold]
|
121
|
-
reflection = Album.association_reflection(:artist)
|
122
|
-
reflection[:type] == :many_to_one
|
123
|
-
|
124
|
-
== Direct Manipulation
|
125
|
-
|
126
|
-
Just like ActiveRecord, Sequel doesn't use sessions, it lets you modify objects and have them be saved inside the call to +save+:
|
127
|
-
|
128
|
-
album = Album[1234]
|
129
|
-
# modify album
|
130
|
-
album.save
|
131
|
-
|
132
|
-
== Database Abstraction
|
133
|
-
|
134
|
-
Sequel supports far more database abstractions than ActiveRecord, and setting up the database connection is easy:
|
135
|
-
|
136
|
-
DB = Sequel.sqlite # memory database
|
137
|
-
DB = Sequel.connect('postgres://user:pass@host/database') # connection string
|
138
|
-
DB = Sequel.connect(:adapter=>'postgres', :user=>'?', :password=>'?',
|
139
|
-
:host=>'?', :database=>'?') # option hash
|
140
|
-
|
141
|
-
== Logging
|
142
|
-
|
143
|
-
Sequel supports logging of all database queries by allowing multiple loggers for each database:
|
144
|
-
|
145
|
-
DB.loggers << Logger.new($stdout)
|
146
|
-
|
147
|
-
== Migrations
|
148
|
-
|
149
|
-
Sequel supports migrations and has a migrator similar to ActiveRecord:
|
150
|
-
|
151
|
-
Sequel.migration do
|
152
|
-
change do
|
153
|
-
create_table(:albums) do
|
154
|
-
primary_key :id
|
155
|
-
String :name
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
== Differences
|
161
|
-
|
162
|
-
By now, you should have the idea that Sequel supports most things that ActiveRecord supports. The rest of this guide is going to go over how Sequel differs from ActiveRecord in terms of operation.
|
163
|
-
|
164
|
-
=== Method Chaining
|
165
|
-
|
166
|
-
Unlike ActiveRecord 2 (and similar to ActiveRecord 3+), Sequel uses method chains on datasets for retrieving objects, so instead of:
|
167
|
-
|
168
|
-
Album.find(:all, :conditions=>['name > ? AND artist_id = ?', 'RF', 1],
|
169
|
-
:order=>'copies_sold', :select=>'id, name')
|
170
|
-
|
171
|
-
Sequel uses:
|
172
|
-
|
173
|
-
Album.where{name > 'RF'}.where(:artist_id=>1).order(:copies_sold).
|
174
|
-
select(:id, :name).all
|
175
|
-
|
176
|
-
Note that the records aren't retrieved until +all+ is called.
|
177
|
-
|
178
|
-
ActiveRecord 3 adopts this method chaining approach, so if you are familiar with it, it should be even easier to transition to Sequel.
|
179
|
-
|
180
|
-
=== No Need for SQL String Fragments
|
181
|
-
|
182
|
-
Like the example above, most ActiveRecord code uses SQL string fragments. With Sequel, you rarely need to. Sequel's DSL allows you to create complex queries without ever specifying SQL string fragments (called literal strings in Sequel).
|
183
|
-
|
184
|
-
If you want to use SQL string fragments, Sequel makes it easy by using the <tt>Sequel.lit</tt> method:
|
185
|
-
|
186
|
-
Album.select(Sequel.lit('id, name'))
|
187
|
-
|
188
|
-
This usage is not encouraged, though. The recommended way is to use symbols to represent the columns:
|
189
|
-
|
190
|
-
Album.select(:id, :name)
|
191
|
-
|
192
|
-
Sequel keeps datasets in an abstract format, allowing for powerful capabilities. For example, let's say you wanted to join to the artists table. Sequel can automatically qualify all references in the current dataset, so that it can be safely joined:
|
193
|
-
|
194
|
-
Album.select(:id, :name).qualify.join(:artists, :id=>:artist_id)
|
195
|
-
|
196
|
-
This isn't possible when you use an SQL string fragment. Another case where using an SQL string fragment hurts you is when the SQL syntax cannot handle all cases:
|
197
|
-
|
198
|
-
Album.where('id NOT IN ?', ids_array)
|
199
|
-
|
200
|
-
That will work fine if +ids_array+ is not empty, but will not work correctly if it is empty. With Sequel, you do:
|
201
|
-
|
202
|
-
Album.exclude(:id=>ids_array)
|
203
|
-
|
204
|
-
That will handle cases where +ids_array+ is empty correctly.
|
205
|
-
|
206
|
-
A third reason to not use SQL string fragments is database independence. For example, if you want a case insensitive search that works on both PostgreSQL and MySQL, the following won't work:
|
207
|
-
|
208
|
-
Album.where('name LIKE ?', 'A%')
|
209
|
-
|
210
|
-
This is because LIKE is case sensitive on PostgreSQL, but case insensitive on MySQL. With Sequel, you would do:
|
211
|
-
|
212
|
-
Album.where(Sequel.ilike(:name, 'A%'))
|
213
|
-
|
214
|
-
This will do a case insensitive search on both databases. If you want a case sensitive search on both, you can use +like+ instead of +ilike+.
|
215
|
-
|
216
|
-
String concatenation is a similar area, where MySQL and PostgreSQL handle things differently. With Sequel, the same code can work on both databases:
|
217
|
-
|
218
|
-
Album.select(Sequel.join([:name, ' - Name']))
|
219
|
-
|
220
|
-
== Flexible Overriding
|
221
|
-
|
222
|
-
Unlike ActiveRecord 2, which forces you to alias methods if you want to override them, with Sequel you just override the methods and call super:
|
223
|
-
|
224
|
-
class Sequel::Model
|
225
|
-
def after_update
|
226
|
-
super
|
227
|
-
AuditLog.create(:log=>"#{model.name} with primary key #{pk} updated")
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
With that code, you have enabled auditing for all model object updates.
|
232
|
-
|
233
|
-
Let's say you want to override accessor methods. In Sequel, instead of using +read_attribute+ and +write_attribute+, you can just call super:
|
234
|
-
|
235
|
-
class Track < Sequel::Model
|
236
|
-
# database holds length in integer seconds,
|
237
|
-
# but you want it in minutes as a float
|
238
|
-
|
239
|
-
def length=(minutes)
|
240
|
-
super((minutes*60).to_i)
|
241
|
-
end
|
242
|
-
|
243
|
-
def length
|
244
|
-
super/60.0
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
You can override almost all model class or instance methods this way, just remember to call +super+.
|
249
|
-
|
250
|
-
== +method_missing+ Missing
|
251
|
-
|
252
|
-
Sequel does not use +method_missing+ unless it's required that the object respond to potentially any method. Neither <tt>Sequel::Model</tt> nor <tt>Sequel::Dataset</tt> nor <tt>Sequel::Database</tt> implement +method_missing+ at either a class or instance level. So if you call +methods+, you can see which methods are available, and if they aren't listed, then the object won't respond to them. Among other things, this means Sequel does not support dynamic finders. So instead of:
|
253
|
-
|
254
|
-
Album.find_or_create_by_name("RF")
|
255
|
-
|
256
|
-
You just use:
|
257
|
-
|
258
|
-
Album.find_or_create(:name=>"RF")
|
259
|
-
|
260
|
-
At the instance level, this means that if you select columns that aren't in the models table, you need to use <tt>Model#[]</tt> to access them:
|
261
|
-
|
262
|
-
album = Album.join(:artist, :id=>:artist_id).
|
263
|
-
select{[albums[:id], albums[:name], artists[:name].as(:artist)]}.first
|
264
|
-
# SELECT albums.id, albums.name, artists.name AS artist
|
265
|
-
|
266
|
-
album.artist # Error!
|
267
|
-
album[:artist] # Works
|
268
|
-
|
269
|
-
== Associations
|
270
|
-
|
271
|
-
Sequel associations are similar to ActiveRecord associations in some ways, and much different in others. Sequel provides four association creation methods that map to ActiveRecord's associations:
|
272
|
-
|
273
|
-
ActiveRecord :: Sequel
|
274
|
-
+belongs_to+ :: +many_to_one+
|
275
|
-
+has_one+ :: +one_to_one+
|
276
|
-
+has_many+ :: +one_to_many+
|
277
|
-
+has_and_belongs_to_many+ :: +many_to_many+
|
278
|
-
|
279
|
-
Like ActiveRecord, when you create an association in Sequel, it creates an instance method with the same name that returns either the matching object or nil for the <tt>*_to_one</tt> associations, or an array of matching objects for the <tt>*_to_many</tt> associations.
|
280
|
-
|
281
|
-
Updating <tt>*_to_many</tt> associations is very different, however. ActiveRecord makes the association method returns an association proxy that looks like an array, but has a bunch of added methods to manipulate the associated records. Sequel uses instance methods on the object instead of a proxy to modify the association. Here's a basic example:
|
282
|
-
|
283
|
-
Artist.one_to_many :albums
|
284
|
-
Album.many_to_one :artist
|
285
|
-
|
286
|
-
artist = Artist[1]
|
287
|
-
album = Album[1]
|
288
|
-
|
289
|
-
artist.albums # array of albums
|
290
|
-
album.artist # Artist instance or nil
|
291
|
-
|
292
|
-
artist.add_album(album) # associate album to artist
|
293
|
-
artist.remove_album(album) # disassociate album from artist
|
294
|
-
artist.remove_all_albums # disassociate all albums from artist
|
295
|
-
|
296
|
-
Sequel doesn't have a <tt>has_many :through</tt> association, instead you can use a +many_to_many+ association in most cases. Sequel ships with a +many_through_many+ plugin that allows you to set up a many to many relationship through an arbitrary number of join tables.
|
297
|
-
|
298
|
-
Sequel doesn't come with support for polymorphic associations. Using polymorphic associations is generally bad from a database design perspective, as it violates referential integrity. If you have an old database and must have polymorphic associations, there is an external +sequel_polymorphic+ plugin that can handle them, just by using the standard association options provided by Sequel.
|
299
|
-
|
300
|
-
Sequel doesn't directly support creating a bunch of associated objects and delaying saving them to the database until the main object is saved, like you can with the <tt>association.build</tt> methods in ActiveRecord. You can use +before_save+ or +after_save+ hooks, or the +nested_attributes+ or +instance_hooks+ plugins to get similar support.
|
301
|
-
|
302
|
-
Sequel supports the same basic association hooks/callbacks as ActiveRecord. It also supports <tt>:after_load</tt>, which is run after the associated objects are loaded. For <tt>*_to_one</tt> associations, it supports +before_set+ and +after_set+ hooks, since a setter method is used instead of an add/remove method pair.
|
303
|
-
|
304
|
-
If you pass a block to an association method, it's used to return a modified dataset used for the association, instead of to create an association extension:
|
305
|
-
|
306
|
-
Artist.one_to_many :gold_albums, :class=>:Album do |ds|
|
307
|
-
ds.where{copies_sold > 500000}
|
308
|
-
end
|
309
|
-
|
310
|
-
If you want to create an association extension, you can use the <tt>:extend</tt> association option with a module, which ActiveRecord also supports. In Sequel, the extensions are applied to the association dataset, not to the array of associated objects. You can access the association dataset using the +association_dataset+ method:
|
311
|
-
|
312
|
-
artist.albums_dataset
|
313
|
-
album.artist_dataset
|
314
|
-
|
315
|
-
Association datasets are just like any other Sequel dataset, in that you can filter them and manipulate them further:
|
316
|
-
|
317
|
-
gold_albums = artist.albums_dataset.where{copies_sold > 500000}.order(:name).all
|
318
|
-
|
319
|
-
Sequel caches associated objects similarly to ActiveRecord, and you can skip the cache by passing +:reload=>true+ to the association method.
|
320
|
-
|
321
|
-
=== Eager Loading
|
322
|
-
|
323
|
-
ActiveRecord 2 tries to guess whether to use preloading or JOINs for eager loading by scanning the SQL string fragments you provide for table names. This is error prone and Sequel avoids it by giving you separate methods. In Sequel, +eager+ is used for preloading and +eager_graph+ is used for JOINs. Both have the same API:
|
324
|
-
|
325
|
-
Artist.eager(:albums=>[:tags, :tracks])
|
326
|
-
Album.eager_graph(:artist, :tracks)
|
327
|
-
|
328
|
-
With either way of eager loading, you must call +all+ to retrieve all records at once. You cannot use +each+, +map+, or one of the other Enumerable methods. Just like +each+, +all+ takes a block that iterates over the records:
|
329
|
-
|
330
|
-
Artist.eager(:albums=>[:tags, :tracks]).each{|a| } # No cookie
|
331
|
-
Artist.eager(:albums=>[:tags, :tracks]).all{|a| } # Cookie
|
332
|
-
|
333
|
-
Like ActiveRecord, Sequel supports cascading of eager loading for both methods of eager loading.
|
334
|
-
|
335
|
-
Unlike ActiveRecord, Sequel allows you to eager load custom associations using the <tt>:eager_loader</tt> and <tt>:eager_grapher</tt> association options. See the {Advanced Associations guide}[rdoc-ref:doc/advanced_associations.rdoc] for more details.
|
336
|
-
|
337
|
-
Table aliasing when eager loading via +eager_graph+ is different in Sequel than ActiveRecord. Sequel will always attempt to use the association name, not the table name, for any associations. If the association name has already been used, Sequel will append _N to it, where N starts at 0 and increases by 1. For example, for a self referential association:
|
338
|
-
|
339
|
-
class Node < Sequel::Model
|
340
|
-
many_to_one :parent, :class=>self
|
341
|
-
one_to_many :children, :class=>self, :key=>:parent_id
|
342
|
-
end
|
343
|
-
|
344
|
-
Node.eager_graph(:parent=>:parent, :children=>{:children=>:children}).all
|
345
|
-
|
346
|
-
# SELECT nodes.id, nodes.parent_id, -- main table
|
347
|
-
# parent.id AS parent_id_0, parent.parent_id AS parent_parent_id, -- parent
|
348
|
-
# parent_0.id AS parent_0_id, parent_0.parent_id AS parent_0_parent_id, -- grandparent
|
349
|
-
# children.id AS children_id, children.parent_id AS children_parent_id, -- children
|
350
|
-
# children_0.id AS children_0_id, children_0.parent_id AS children_0_parent_id, -- grandchildren
|
351
|
-
# children_1.id AS children_1_id, children_1.parent_id AS children_1_parent_id -- great grandchidren
|
352
|
-
# FROM nodes -- main table
|
353
|
-
# LEFT OUTER JOIN nodes AS parent ON (parent.id = nodes.parent_id) -- parent
|
354
|
-
# LEFT OUTER JOIN nodes AS parent_0 ON (parent_0.id = parent.parent_id) -- grandparent
|
355
|
-
# LEFT OUTER JOIN nodes AS children ON (children.parent_id = nodes.id) -- children
|
356
|
-
# LEFT OUTER JOIN nodes AS children_0 ON (children_0.parent_id = children.id) -- grandchildren
|
357
|
-
# LEFT OUTER JOIN nodes AS children_1 ON (children_1.parent_id = children_0.id) -- great grandchildren
|
358
|
-
|
359
|
-
You can specify aliases on a per join basis, too:
|
360
|
-
|
361
|
-
Node.eager_graph(:parent=>Sequel.as(:parent, :grandparent),
|
362
|
-
:children=>{Sequel.as(:children, :grandchildren)=>Sequel.as(:children, :great_grandchildren)}).all
|
363
|
-
|
364
|
-
# SELECT nodes.id, nodes.parent_id,
|
365
|
-
# parent.id AS parent_id_0, parent.parent_id AS parent_parent_id,
|
366
|
-
# grandparent.id AS grandparent_id, grandparent.parent_id AS grandparent_parent_id,
|
367
|
-
# children.id AS children_id, children.parent_id AS children_parent_id,
|
368
|
-
# grandchildren.id AS grandchildren_id, grandchildren.parent_id AS grandchildren_parent_id,
|
369
|
-
# great_grandchildren.id AS great_grandchildren_id, great_grandchildren.parent_id AS great_grandchildren_parent_id
|
370
|
-
# FROM nodes
|
371
|
-
# LEFT OUTER JOIN nodes AS parent ON (parent.id = nodes.parent_id)
|
372
|
-
# LEFT OUTER JOIN nodes AS grandparent ON (grandparent.id = parent.parent_id)
|
373
|
-
# LEFT OUTER JOIN nodes AS children ON (children.parent_id = nodes.id)
|
374
|
-
# LEFT OUTER JOIN nodes AS grandchildren ON (grandchildren.parent_id = children.id)
|
375
|
-
# LEFT OUTER JOIN nodes AS great_grandchildren ON (great_grandchildren.parent_id = grandchildren.id)
|
376
|
-
|
377
|
-
=== Options
|
378
|
-
|
379
|
-
Sequel supports many more association options than ActiveRecord, but here's a mapping of ActiveRecord association options to Sequel association options. Note that when you specify columns in Sequel, you use symbols, not strings. Where ActiveRecord would use an SQL string fragment with embedded commas for multiple columns, Sequel would use an array of column symbols.
|
380
|
-
|
381
|
-
=== Shared options
|
382
|
-
|
383
|
-
These options are shared by more than one ActiveRecord association.
|
384
|
-
|
385
|
-
ActiveRecord option :: Sequel option
|
386
|
-
<tt>:class_name</tt> :: <tt>:class</tt>
|
387
|
-
<tt>:conditions</tt> :: <tt>:conditions</tt>
|
388
|
-
<tt>:select</tt> :: <tt>:select</tt>
|
389
|
-
<tt>:order</tt> :: <tt>:order</tt>
|
390
|
-
<tt>:extend</tt> :: <tt>:extend</tt>
|
391
|
-
<tt>:limit</tt> :: <tt>:limit</tt>
|
392
|
-
<tt>:offset</tt> :: <tt>:limit</tt> with an array with the second element being the offset
|
393
|
-
<tt>:uniq</tt> :: <tt>:uniq</tt>
|
394
|
-
<tt>:validate</tt> :: <tt>:validate</tt>
|
395
|
-
<tt>:dependent</tt> :: The +associations_dependencies+ plugin
|
396
|
-
<tt>:polymorphic</tt>, <tt>:as</tt>, <tt>:source_type</tt> :: The +sequel_polymorphic+ external plugin
|
397
|
-
<tt>:include</tt> :: <tt>:eager</tt>, <tt>:eager_graph</tt>
|
398
|
-
<tt>:readonly</tt> :: No equivalent, the Sequel <tt>:read_only</tt> option just means the modification methods are not created (it makes the association read only, not records retrieved through the association)
|
399
|
-
<tt>:through</tt>, <tt>:source</tt> :: Use a +many_to_many+ or +one_through_one+ association, or the +many_through_many+ plugin
|
400
|
-
<tt>:touch</tt> :: The +touch+ plugin
|
401
|
-
<tt>:autosave</tt> :: A +before_save+ or +after_save+ hook
|
402
|
-
<tt>:finder_sql</tt> :: <tt>:dataset</tt> to set a custom dataset
|
403
|
-
<tt>:counter_sql</tt> :: No direct equivalent, but a count on the dataset will use the custom dataset specified by <tt>:dataset</tt>
|
404
|
-
<tt>:group</tt> :: Use the association block to add the group to the dataset
|
405
|
-
<tt>:having</tt> :: Use the association block to add the having to the dataset
|
406
|
-
|
407
|
-
=== +belongs_to+
|
408
|
-
|
409
|
-
+belongs_to+ option :: +many_to_one+ option
|
410
|
-
<tt>:foreign_key</tt> :: <tt>:key</tt>
|
411
|
-
<tt>:primary_key</tt> :: <tt>:primary_key</tt>
|
412
|
-
<tt>:counter_cache</tt> :: No equivalent
|
413
|
-
|
414
|
-
=== +has_one+, +has_many+
|
415
|
-
|
416
|
-
+has_one+, +has_many+ option :: +one_to_one+, +one_to_many+ option
|
417
|
-
<tt>:foreign_key</tt> :: <tt>:key</tt>
|
418
|
-
|
419
|
-
=== +has_and_belongs_to_many+
|
420
|
-
|
421
|
-
+has_and_belongs_to_many+ option :: +many_to_many+ option
|
422
|
-
<tt>:foreign_key</tt> :: <tt>:left_key</tt>
|
423
|
-
<tt>:association_foreign_key</tt> :: <tt>:right_key</tt>
|
424
|
-
<tt>:join_table</tt> :: <tt>:join_table</tt>
|
425
|
-
<tt>:delete_sql</tt> :: <tt>:remover</tt>
|
426
|
-
<tt>:insert_sql</tt> :: <tt>:adder</tt>
|
427
|
-
|
428
|
-
== Validation Errors
|
429
|
-
|
430
|
-
If there are errors when validating an object in Sequel, they are stored in a <tt>Sequel::Model::Errors</tt> instance. It's mostly similar to <tt>ActiveRecord::Errors</tt>, so this section will just go over the differences.
|
431
|
-
|
432
|
-
<tt>Sequel::Model::Errors</tt> is a hash subclass where keys are usually column symbols (not required), and values are arrays of error messages. So if you have two error messages on the same column, +each+ will only yield once, not twice.
|
433
|
-
|
434
|
-
The +add_on_blank+, +add_on_empty+, +add_to_base+, +each_full+, +generate_message+, <tt>invalid?</tt>, +on_base+, and +to_xml+ methods don't exist. <tt>[]</tt> should not be used directly, instead you should use +on+.
|
435
|
-
|
436
|
-
You can think of <tt>Sequel::Model::Errors</tt> as a subset of <tt>ActiveRecord::Errors</tt> if you stick to +on+, +add+, and +full_messages+.
|
437
|
-
|
438
|
-
== Sequel Configuration Flags
|
439
|
-
|
440
|
-
Unlike ActiveRecord, Sequel's behavior depends on how you configure it. In Sequel, you can set flags at the global, class, and instance level that change the behavior of Sequel. Here's a brief description of the flags:
|
441
|
-
|
442
|
-
+raise_on_save_failure+ :: Whether to raise an error instead of returning nil on a failure to save/create/save_changes/etc due to a validation failure or a before_* hook returning false. By default, an error is raised, when this is set to false, nil is returned instead.
|
443
|
-
+raise_on_typecast_failure+ :: Whether to raise an error when unable to typecast data for a column (default: true). This should be set to false if you want to use validations to display nice error messages to the user (e.g. most web applications). You can use the +validates_schema_types+ validation in connection with this option to check for typecast failures.
|
444
|
-
+require_modification+ :: Whether to raise an error if an UPDATE or DELETE query related to a model instance does not modify exactly 1 row. If set to false, Sequel will not check the number of rows modified (default: true if the database supports it).
|
445
|
-
+strict_param_setting+ :: Whether new/set/update and their variants should raise an error if an invalid key is used. A key is invalid if no setter method exists for that key or the access to the setter method is restricted (e.g. due to it being a primary key field). If set to false, silently skip any key where the setter method doesn't exist or access to it is restricted.
|
446
|
-
+typecast_empty_string_to_nil+ :: Whether to typecast the empty string ('') to nil for columns that are not string or blob. In most cases the empty string would be the way to specify a NULL SQL value in string form (nil.to_s == ''), and an empty string would not usually be typecast correctly for other types, so the default is true.
|
447
|
-
+typecast_on_assignment+ :: Whether to typecast attribute values on assignment (default: true). If set to false, no typecasting is done, so it will be left up to the database to typecast the value correctly.
|
448
|
-
+use_transactions+ :: Whether to use a transaction by default when saving/deleting records (default: true). If you are sending database queries in before or after hooks, you shouldn't change the default setting without a good reason.
|
449
|
-
|
450
|
-
== ActiveRecord Method to Sequel Method Mapping
|
451
|
-
|
452
|
-
This part of the guide will list Sequel equivalents for ActiveRecord methods, hopefully allowing you to convert your existing ActiveRecord code to Sequel code more easily.
|
453
|
-
|
454
|
-
=== Class Methods with Significantly Different Behavior
|
455
|
-
|
456
|
-
==== +abstract_class+, <tt>abstract_class=</tt>, <tt>abstract_class?</tt>
|
457
|
-
|
458
|
-
With Sequel, these methods don't exist because it doesn't default to using single table inheritance in subclasses. ActiveRecord assumes that subclasses of Model classes use single table inheritance, and you have to set <tt>abstract_class = true</tt> to use an abstract class. In Sequel, you must use the +single_table_inheritance+ or +class_table_inheritance+ plugin to configure inheritance in the database.
|
459
|
-
|
460
|
-
==== +all+
|
461
|
-
|
462
|
-
In both Sequel and ActiveRecord, calling +all+ will give you an array of all records. However, while in ActiveRecord you pass options to +all+ to filter or order the results, in Sequel you call dataset methods to filter or order the results, and then end the method chain with a call to +all+ to return the records.
|
463
|
-
|
464
|
-
==== +column_names+
|
465
|
-
|
466
|
-
Sequel uses symbols for columns, so the +columns+ method returns an array of symbols. If you want an array of strings:
|
467
|
-
|
468
|
-
Album.columns.map{|x| x.to_s}
|
469
|
-
|
470
|
-
==== +columns+
|
471
|
-
|
472
|
-
<tt>Sequel::Model.columns</tt> returns an array of column name symbols. The closest similar thing would be to get the database schema hash for each column:
|
473
|
-
|
474
|
-
Album.columns.map{|x| Album.db_schema[x]}
|
475
|
-
|
476
|
-
==== +composed_of+
|
477
|
-
|
478
|
-
As mentioned earlier, Sequel uses the +composition+ plugin for this:
|
479
|
-
|
480
|
-
class Artist < Sequel::Model
|
481
|
-
plugin :composition
|
482
|
-
composition :address, :mapping=>[:street, :city, :state, :zip]
|
483
|
-
end
|
484
|
-
|
485
|
-
==== <tt>connected?</tt>
|
486
|
-
|
487
|
-
<tt>Sequel::Model</tt> raises an exception if you haven't instantiated a <tt>Sequel::Database</tt> object before loading the model class. However, if you want to test the connection to the database:
|
488
|
-
|
489
|
-
Sequel::Model.db.test_connection
|
490
|
-
|
491
|
-
Note that +test_connection+ will return true if a connection can be made, but will probably raise an exception if it cannot be made.
|
492
|
-
|
493
|
-
==== +connection+
|
494
|
-
|
495
|
-
Sequel only uses connections for the minimum amount of time necessary, checking them out to do a query, and returning them as soon as the query finishes. If you do want direct access to the connection object:
|
496
|
-
|
497
|
-
Sequel::Model.db.synchronize do |connection|
|
498
|
-
# ...
|
499
|
-
end
|
500
|
-
|
501
|
-
Note that the connection is yielded to the block, and the block ensures it is returned to the pool. Sequel doesn't have a method that returns a connection, since that would check it out with no ability to ensure it is returned to the pool.
|
502
|
-
|
503
|
-
==== +count_by_sql+
|
504
|
-
|
505
|
-
You can call +with_sql+ to set the SQL to use, and the +single_value+ to retrieve the result.
|
506
|
-
|
507
|
-
Album.with_sql("SELECT COUNT(*) ...").single_value
|
508
|
-
|
509
|
-
==== +delete+, +delete_all+
|
510
|
-
|
511
|
-
You probably want to filter first, then call +delete+:
|
512
|
-
|
513
|
-
Album.where(:id=>id).delete
|
514
|
-
Album.where("artist_id = ?", 5).delete
|
515
|
-
|
516
|
-
If you really want to delete all rows in the table,call +delete+ on the Model's dataset:
|
517
|
-
|
518
|
-
Album.dataset.delete
|
519
|
-
|
520
|
-
==== +destroy+, +destroy_all+
|
521
|
-
|
522
|
-
Similar to +delete+, you filter first, then +destroy+:
|
523
|
-
|
524
|
-
Album.where(:id=>id).destroy
|
525
|
-
Album.where("artist_id = ?", 5).destroy
|
526
|
-
|
527
|
-
If you really want to destroy all rows in the table,call +destroy+ on the Model's dataset:
|
528
|
-
|
529
|
-
Album.dataset.destroy
|
530
|
-
|
531
|
-
==== +establish_connection+
|
532
|
-
|
533
|
-
If you want to use a specific <tt>Sequel::Database</tt> object, you can use <tt>db=</tt>:
|
534
|
-
|
535
|
-
BACKUP_DB = Sequel.connect('postgres://...')
|
536
|
-
Album.db = BACKUP_DB
|
537
|
-
|
538
|
-
If you want a specific dataset in that database for a model, setup the model with a specific dataset:
|
539
|
-
|
540
|
-
class Album < Sequel::Model(BACKUP_DB[:albums]); end
|
541
|
-
|
542
|
-
==== <tt>exists?</tt>
|
543
|
-
|
544
|
-
You need to filter the dataset first, then call <tt>empty?</tt> and invert the result:
|
545
|
-
|
546
|
-
!Album.where(:id=>1).empty?
|
547
|
-
|
548
|
-
==== +find+
|
549
|
-
|
550
|
-
ActiveRecord's +find+ can be used for a lot of different things. If you are trying to find a single object given a primary key:
|
551
|
-
|
552
|
-
Album[1]
|
553
|
-
|
554
|
-
Note that Sequel returns nil if no record is found, it doesn't raise an exception. To raise an exception if no record
|
555
|
-
is found:
|
556
|
-
|
557
|
-
Album.with_pk!(1)
|
558
|
-
|
559
|
-
If you want to find multiple objects using an array of primary keys:
|
560
|
-
|
561
|
-
Album.where(:id=>[1, 2, 3]).all
|
562
|
-
|
563
|
-
If you are using <tt>find(:first, ...)</tt>, you use a method chain instead of passing the options, and end it with +first+:
|
564
|
-
|
565
|
-
Album.where(:artist_id=>1).order(:name).first
|
566
|
-
|
567
|
-
If you are using <tt>find(:last, ...)</tt>, you need to specify an order in Sequel, but the same method chain approach is used, which you end with +last+:
|
568
|
-
|
569
|
-
Album.where(:artist_id=>1).order(:name).last
|
570
|
-
# You could also do:
|
571
|
-
Album.where(:artist_id=>1).reverse(:name).first
|
572
|
-
|
573
|
-
If you are using <tt>find(:all, ...)</tt>, you use a method chain instead of passing the options, and end it with +all+:
|
574
|
-
|
575
|
-
Album.where(:artist_id=>1).order(:name).all
|
576
|
-
|
577
|
-
Here's a mapping of ActiveRecord +find+ options to <tt>Sequel::Dataset</tt> methods:
|
578
|
-
|
579
|
-
:conditions :: filter, where
|
580
|
-
:order :: order
|
581
|
-
:group :: group
|
582
|
-
:limit :: limit
|
583
|
-
:offset :: offset
|
584
|
-
:joins :: join, left_join, etc. # many other join methods
|
585
|
-
:include :: eager, eager_graph # eager does preloading, eager_graph does JOINs
|
586
|
-
:select :: select
|
587
|
-
:from :: from
|
588
|
-
:read_only :: # No Sequel equivalent
|
589
|
-
:lock :: for_update, lock_style
|
590
|
-
|
591
|
-
==== +find_by_sql+
|
592
|
-
|
593
|
-
Similar to +count_by_sql+, you use +with_sql+, followed by +all+:
|
594
|
-
|
595
|
-
Album.with_sql("SELECT * FROM albums WHERE ...").all
|
596
|
-
|
597
|
-
==== +first+
|
598
|
-
|
599
|
-
Just like with <tt>find(:first, ...)</tt>, you use a method chain instead of passing the options, and end it with +first+:
|
600
|
-
|
601
|
-
Album.where(:artist_id=>1).order(:name).first
|
602
|
-
|
603
|
-
==== +last+
|
604
|
-
|
605
|
-
Just like with <tt>find(:last, ...)</tt>, you use a method chain instead of passing the options, make sure it includes an order, and end it with +last+:
|
606
|
-
|
607
|
-
Album.where(:artist_id=>1).order(:name).last
|
608
|
-
|
609
|
-
==== +named_scope+
|
610
|
-
|
611
|
-
For a pure filter, you can use +subset+:
|
612
|
-
|
613
|
-
Album.subset(:debut, :position => 1)
|
614
|
-
Album.subset(:gold){copies_sold > 500000}
|
615
|
-
|
616
|
-
For anything more complex, you can use +dataset_module+:
|
617
|
-
|
618
|
-
Album.dataset_module do
|
619
|
-
def by_artist(artist_id)
|
620
|
-
where(:artist_id=>artist_id)
|
621
|
-
end
|
622
|
-
|
623
|
-
def by_release_date
|
624
|
-
order(:release_date)
|
625
|
-
end
|
626
|
-
end
|
627
|
-
|
628
|
-
=== +none+
|
629
|
-
|
630
|
-
If you want to return an empty dataset, but still preserve chaining, you can use the +null_dataset+ extension:
|
631
|
-
|
632
|
-
DB.extension(:null_dataset)
|
633
|
-
|
634
|
-
class User < Sequel::Model
|
635
|
-
def self.nullify
|
636
|
-
dataset.nullify
|
637
|
-
end
|
638
|
-
end
|
639
|
-
|
640
|
-
user = User.create(name: 'John Doe')
|
641
|
-
User.nullify.where(name: 'John Doe').all
|
642
|
-
# => []
|
643
|
-
|
644
|
-
==== +reset_column_information+
|
645
|
-
|
646
|
-
If you want to completely reload the schema for the table:
|
647
|
-
|
648
|
-
Album.instance_variable_set(:@db_schema, nil)
|
649
|
-
Album.send(:get_db_schema, true)
|
650
|
-
|
651
|
-
==== +serialize+, +seralized_attributes+
|
652
|
-
|
653
|
-
Sequel ships with a +serialization+ plugin that you can use.
|
654
|
-
|
655
|
-
class Album < Sequel::Model
|
656
|
-
plugin :serialization, :json, :permissions
|
657
|
-
end
|
658
|
-
|
659
|
-
For +serialized_attributes+, you can use +serialization_map+, which is also a hash, but keys are column symbols and values are callable objects used to serialize the values.
|
660
|
-
|
661
|
-
==== +set_inheritance_column+
|
662
|
-
|
663
|
-
This is something that must be specified when you are loading the +single_table_inheritance+ plugin:
|
664
|
-
|
665
|
-
class Album < Sequel::Model
|
666
|
-
plugin :single_table_inheritance, :column
|
667
|
-
end
|
668
|
-
|
669
|
-
==== +set_sequence_name+
|
670
|
-
|
671
|
-
Sequel will usually auto discover the sequence to use. However, on Oracle this should be specified by making sure the model's dataset includes a sequence:
|
672
|
-
|
673
|
-
class Album < Sequel::Model(ORACLE_DB[:albums].sequence('albums_seq'))
|
674
|
-
end
|
675
|
-
|
676
|
-
==== <tt>table_exists?</tt>
|
677
|
-
|
678
|
-
This is a <tt>Sequel::Database</tt> method:
|
679
|
-
|
680
|
-
Album.db.table_exists?(Album.table_name)
|
681
|
-
|
682
|
-
With the +schema+ plugin, you can use it directly:
|
683
|
-
|
684
|
-
Album.plugin :schema
|
685
|
-
Album.table_exists?
|
686
|
-
|
687
|
-
==== +transaction+
|
688
|
-
|
689
|
-
As mentioned earlier, +transaction+ is a database method in Sequel, which you can access via the +db+ method:
|
690
|
-
|
691
|
-
Album.db.transaction{}
|
692
|
-
|
693
|
-
==== +update+, +update_all+
|
694
|
-
|
695
|
-
Just like +delete+ and +destroy+, you filter first, then +update+:
|
696
|
-
|
697
|
-
Album.where(:id=>id).update(:name=>'RF')
|
698
|
-
Album.where("artist_id = ?", 5).update(:copies_sold=>0)
|
699
|
-
|
700
|
-
Likewise, to update all rows in the model:
|
701
|
-
|
702
|
-
Album.dataset.update(:name=>'RF')
|
703
|
-
|
704
|
-
Note that +update+ in that case will operate on a dataset, so it won't run model validations or hooks. If you want those run:
|
705
|
-
|
706
|
-
Album[id].update(:name=>'RF')
|
707
|
-
Album.where("artist_id = ?", 5).all{|a| a.update(:copies_sold=>0)}
|
708
|
-
|
709
|
-
==== +with_scope+
|
710
|
-
|
711
|
-
Sequel works a little differently than with_scope. Instead of using nested blocks, you just use a cleaner method chain. +with_scope+ is often used as an around_filter or similar construct, where in Sequel, you would just need to assign to a dataset in a before filter, and use that dataset in the action.
|
712
|
-
|
713
|
-
=== Class Methods with Roughly the Same Behavior
|
714
|
-
|
715
|
-
Note that Sequel uses symbols almost everywhere to represent columns, while ActiveRecord often returns columns as strings.
|
716
|
-
|
717
|
-
ActiveRecord Method :: Sequel Method
|
718
|
-
+attr_accessible+ :: +set_allowed_columns+
|
719
|
-
+attr_protected+ :: +set_restricted_columns+
|
720
|
-
+average+ :: +avg+
|
721
|
-
+belongs_to+ :: +many_to_one+
|
722
|
-
+columns_hash+ :: +db_schema+
|
723
|
-
+count+ :: +count+
|
724
|
-
+changed+ :: +changed_columns+
|
725
|
-
+create+ :: +create+
|
726
|
-
+has_and_belongs_to_many+ :: +many_to_many+
|
727
|
-
+has_one+ :: +one_to_one+
|
728
|
-
+has_many+ :: +one_to_many+
|
729
|
-
+inheritance_column+ :: +sti_key+
|
730
|
-
+inspect+ :: +inspect+
|
731
|
-
+maximum+ :: +max+
|
732
|
-
+minimum+ :: +min+
|
733
|
-
+new+ :: +new+
|
734
|
-
+primary_key+ :: +primary_key+
|
735
|
-
<tt>respond_to?</tt> :: <tt>respond_to?</tt>
|
736
|
-
+set_primary_key+ :: +set_primary_key+
|
737
|
-
+sum+ :: +sum+
|
738
|
-
+table_name+ :: +table_name+
|
739
|
-
+unscoped+ :: +unfiltered+
|
740
|
-
|
741
|
-
=== Class Methods without an Equivalent
|
742
|
-
|
743
|
-
ActiveRecord Method :: Notes, Workarounds
|
744
|
-
+accepts_nested_attributes_for+ :: Use the +nested_attributes+ plugin
|
745
|
-
+attr_readonly+ :: Don't update the columns (duh!), or add a before_update hook that deletes them from the values hash
|
746
|
-
+attribute_method_suffix+ :: No equivalent
|
747
|
-
+alias_attribute_with_dirty+ :: No equivalent
|
748
|
-
+base_class+ :: Not needed internally, you can probably use <tt>sti_dataset.model</tt> if you are using single table inheritance
|
749
|
-
+benchmark+ :: Just use the +benchmark+ library from ruby's stdlib
|
750
|
-
+calculate+ :: No direct equivalent, just build the query manually and execute it
|
751
|
-
+cache+ :: No equivalent
|
752
|
-
<tt>cache_attribute?</tt> :: No equivalent
|
753
|
-
+cache_attributes+ :: No equivalent
|
754
|
-
+cached_attributes+ :: No equivalent
|
755
|
-
<tt>changed?</tt> :: <tt>changed_columns.include?(column)</tt>
|
756
|
-
+changes+ :: No equivalent
|
757
|
-
<tt>clear_active_connections!</tt> :: Sequel doesn't leak connections like ActiveRecord, so you don't need to worry about this
|
758
|
-
<tt>clear_reloadable_connections!</tt> :: Sequel doesn't leak connections like ActiveRecord, so you don't need to worry about this
|
759
|
-
+content_columns+ :: Not needed internally, you can probably do <tt>Album.columns.map{|x| x.to_s}.delete_if{|x| x == Album.primary_key || x =~ /_(id|count)\z/}</tt>
|
760
|
-
+decrement_counter+ :: <tt>Album.where(:id=>:id).update(:counter_name=>Sequel.-(:counter_name, 1))</tt>
|
761
|
-
+define_attribute_methods+, +define_read_methods+ :: <tt>def_column_accessor(*columns)</tt>, a private method
|
762
|
-
<tt>descends_from_active_record?</tt> :: Not needed internally, if using single table inheritance, <tt>Album.sti_dataset.model == Album</tt>
|
763
|
-
+find_each+, +find_in_batches+ :: Use the +pagination+ extension
|
764
|
-
<tt>generated_methods?</tt> :: No equivalent
|
765
|
-
+increment_counter+ :: <tt>Album.where(:id=>:id).update(:counter_name=>Sequel.+(:counter_name, 1))</tt>
|
766
|
-
<tt>instance_method_already_implemented?</tt> :: No equivalent, Sequel does not create column accessors that override other methods, it just skips them.
|
767
|
-
<tt>match_attribute_method?</tt> :: No equivalent
|
768
|
-
+readonly_attributes+ :: No equivalent
|
769
|
-
+remove_connection+ :: Not necessary in Sequel. If you want to disconnect an existing connection: <tt>Album.db.disconnect</tt>
|
770
|
-
+require_mysql+ :: A public method, really?
|
771
|
-
+silence+ :: No equivalent. Because the logger is handled at the <tt>Sequel::Database</tt> level, there is no thread-safe way to turn it off for specific blocks.
|
772
|
-
+scopes+ :: No equivalent
|
773
|
-
+sti_name+ :: No equivalent.
|
774
|
-
+update_counters+ :: <tt>Album.where(:id=>:id).update(:counter_name=>Sequel.+(:counter_name, 1), :other_counter=>Sequel.-(:other_counter, 1))</tt>
|
775
|
-
+uncached+ :: No equivalent
|
776
|
-
|
777
|
-
=== Instance Methods with Significantly Different Behavior
|
778
|
-
|
779
|
-
==== +attribute_names+
|
780
|
-
|
781
|
-
+keys+ returns the columns as unsorted symbols, so:
|
782
|
-
|
783
|
-
album.keys.map{|x| x.to_s}.sort
|
784
|
-
|
785
|
-
==== +becomes+
|
786
|
-
|
787
|
-
Assuming the record already exists in the database:
|
788
|
-
|
789
|
-
gold_album = GoldAlbum[1]
|
790
|
-
album = Album.load(gold_album.values)
|
791
|
-
|
792
|
-
If it is a new record:
|
793
|
-
|
794
|
-
gold_album = GoldAlbum.new(:name=>'a')
|
795
|
-
album = Album.new
|
796
|
-
album.send(:set_values, gold_album.values)
|
797
|
-
|
798
|
-
==== +column_for_attribute+
|
799
|
-
|
800
|
-
You can access this through the +db_schema+ hash:
|
801
|
-
|
802
|
-
album.db_schema[:column]
|
803
|
-
|
804
|
-
==== +connection+
|
805
|
-
|
806
|
-
Just like in the class method, you have to access it through the database:
|
807
|
-
|
808
|
-
album.db.synchronize do |connection|
|
809
|
-
end
|
810
|
-
|
811
|
-
==== +decrement+, +increment+
|
812
|
-
|
813
|
-
You can just modify the values hash directly:
|
814
|
-
|
815
|
-
album.values[:column] ||= 0
|
816
|
-
album.values[:column] -= 1 # or += 1 for increment
|
817
|
-
|
818
|
-
==== <tt>decrement!</tt>, <tt>increment!</tt>
|
819
|
-
|
820
|
-
Assuming you want the full behavior of saving just one column without validating:
|
821
|
-
|
822
|
-
album.values[:column] ||= 0
|
823
|
-
album.values[:column] -= 1 # or += 1 for increment!
|
824
|
-
album.save(:columns=>[:column], :validate=>false)
|
825
|
-
|
826
|
-
==== <tt>has_attribute?</tt>
|
827
|
-
|
828
|
-
You have to check the values hash:
|
829
|
-
|
830
|
-
album.values.has_key?(:column)
|
831
|
-
|
832
|
-
==== <tt>invalid?</tt>
|
833
|
-
|
834
|
-
You can use <tt>unless valid?</tt> or <tt>!valid?</tt>.
|
835
|
-
|
836
|
-
==== +save+, <tt>save!</tt>, +save_with_validation+, <tt>save_with_validation!</tt>
|
837
|
-
|
838
|
-
Sequel defaults to raising exceptions when +save+ fails, but this is configurable behavior by setting the +raise_on_save_failure+ flag on the class or instance:
|
839
|
-
|
840
|
-
album.raise_on_save_failure = true
|
841
|
-
album.save # raise exception if failure
|
842
|
-
album.raise_on_save_failure = false
|
843
|
-
album.save # return nil if failure
|
844
|
-
|
845
|
-
You can pass the <tt>:validate=>false</tt> option to not validate the object when saving.
|
846
|
-
|
847
|
-
==== +toggle+, <tt>toggle</tt>
|
848
|
-
|
849
|
-
No equivalent, but very easy to add:
|
850
|
-
|
851
|
-
album.column = !album.column
|
852
|
-
|
853
|
-
If you want to save just that column:
|
854
|
-
|
855
|
-
album.save(:columns=>[:column], :validate=>false)
|
856
|
-
|
857
|
-
==== +transaction+
|
858
|
-
|
859
|
-
Just like in the class, you can access the transaction method through the +db+:
|
860
|
-
|
861
|
-
album.db.transaction{}
|
862
|
-
|
863
|
-
==== +update_attribute+
|
864
|
-
|
865
|
-
To only set and save a specific column:
|
866
|
-
|
867
|
-
album.set(:column => value)
|
868
|
-
album.save(:columns=>[:column], :validate=>false)
|
869
|
-
|
870
|
-
==== +update_attributes+, <tt>update_attributes!</tt>
|
871
|
-
|
872
|
-
These would both use +update+, but see the notes on the +raise_on_save_failure+ flag:
|
873
|
-
|
874
|
-
album.update(:column1=>value1, :column2=>value2)
|
875
|
-
|
876
|
-
=== Instance Methods with Roughly the Same Behavior
|
877
|
-
|
878
|
-
Note that Sequel uses symbols almost everywhere to represent columns, while ActiveRecord often returns columns as strings.
|
879
|
-
|
880
|
-
ActiveRecord Method :: Sequel Method
|
881
|
-
<tt>==</tt> :: <tt>===</tt>, <tt>==</tt> compares by all values, not just id
|
882
|
-
<tt>[]</tt> :: <tt>[]</tt>
|
883
|
-
<tt>[]=</tt> :: <tt>[]=</tt>
|
884
|
-
+after_create+ :: +after_create+
|
885
|
-
+after_destroy+ :: +after_destroy+
|
886
|
-
+after_save+ :: +after_save+
|
887
|
-
+after_update+ :: +after_update+
|
888
|
-
+after_validation+ :: +after_validation+
|
889
|
-
+attributes+ :: +values+
|
890
|
-
<tt>attributes=</tt> :: +set+
|
891
|
-
+before_create+ :: +before_create+
|
892
|
-
+before_destroy+ :: +before_destroy+
|
893
|
-
+before_save+ :: +before_save+
|
894
|
-
+before_update+ :: +before_update+
|
895
|
-
+before_validation+ :: +before_validation+
|
896
|
-
+cache_key+ :: +cache_key+, if using the +caching+ plugin
|
897
|
-
+destroy+ :: +destroy+
|
898
|
-
<tt>eql?</tt> :: <tt>===</tt>
|
899
|
-
+errors+ :: +errors+
|
900
|
-
+freeze+ :: +freeze+
|
901
|
-
<tt>frozen?</tt> :: <tt>frozen?</tt>
|
902
|
-
+hash+ :: +hash+
|
903
|
-
+id+ :: +pk+
|
904
|
-
+inspect+ :: +inspect+
|
905
|
-
<tt>lock!</tt> :: <tt>lock!</tt>
|
906
|
-
<tt>new_record?</tt> :: <tt>new?</tt>
|
907
|
-
+reload_with_autosave_associations+ :: +reload+
|
908
|
-
+to_param+ :: +to_param+, if using the +active_model+ plugin
|
909
|
-
+touch+ :: +touch+, if using the +touch+ plugin
|
910
|
-
<tt>valid?</tt> :: <tt>valid?</tt>
|
911
|
-
|
912
|
-
=== Instance Methods without an Equivalent
|
913
|
-
|
914
|
-
ActiveRecord Method :: Notes, Workarounds
|
915
|
-
+after_validation_on_create+, +after_validation_on_update+ :: Use +after_validation+ and <tt>if new?</tt> or <tt>unless new?</tt>
|
916
|
-
+as_json+, +from_json+, +to_json+ :: Use the +json_serializer+ plugin
|
917
|
-
+from_xml+, +to_xml+ :: Use the +xml_serializer+ plugin
|
918
|
-
+attribute_for_inspect+ :: <tt>album[:column].inspect</tt>
|
919
|
-
<tt>attribute_present?</tt> :: <tt>!album[:column].blank?</tt> if using the +blank+ extension
|
920
|
-
+attributes_before_type_cast+ :: Sequel typecasts at a low level, so model objects never see values before they are type cast
|
921
|
-
+before_validation_on_create+, +before_validation_on_update+ :: Use +before_validation+ and <tt>if new?</tt> or <tt>unless new?</tt>
|
922
|
-
<tt>id=</tt> :: Sequel doesn't have a special primary key setter method, but you can use: <tt>album.send("#{Album.primary_key}=", value)</tt>
|
923
|
-
+mark_for_destruction+, <tt>marked_for_destruction?</tt> :: Use a +before_save+ or +after_save+ hook or the +instance_hooks+ plugin
|
924
|
-
<tt>readonly!</tt> :: No equivalent
|
925
|
-
<tt>readonly?</tt> :: No equivalent
|
926
|
-
<tt>rollback_active_record_state!</tt> :: No equivalent
|
927
|
-
+with_transaction_returning_status+ :: No equivalent
|