sequel 3.21.0 → 3.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +855 -3
- data/MIT-LICENSE +1 -1
- data/README.rdoc +23 -7
- data/Rakefile +42 -24
- data/bin/sequel +26 -8
- data/doc/active_record.rdoc +1 -0
- data/doc/advanced_associations.rdoc +171 -11
- data/doc/association_basics.rdoc +293 -43
- data/doc/dataset_basics.rdoc +5 -5
- data/doc/dataset_filtering.rdoc +8 -0
- data/doc/mass_assignment.rdoc +56 -0
- data/doc/migration.rdoc +85 -552
- data/doc/model_hooks.rdoc +78 -27
- data/doc/object_model.rdoc +541 -0
- data/doc/opening_databases.rdoc +97 -50
- data/doc/prepared_statements.rdoc +40 -14
- data/doc/querying.rdoc +28 -4
- data/doc/reflection.rdoc +30 -4
- data/doc/release_notes/3.22.0.txt +39 -0
- data/doc/release_notes/3.23.0.txt +172 -0
- data/doc/release_notes/3.24.0.txt +420 -0
- data/doc/release_notes/3.25.0.txt +88 -0
- data/doc/release_notes/3.26.0.txt +88 -0
- data/doc/release_notes/3.27.0.txt +82 -0
- data/doc/release_notes/3.28.0.txt +304 -0
- data/doc/release_notes/3.29.0.txt +459 -0
- data/doc/release_notes/3.30.0.txt +135 -0
- data/doc/release_notes/3.31.0.txt +146 -0
- data/doc/release_notes/3.32.0.txt +202 -0
- data/doc/release_notes/3.33.0.txt +157 -0
- data/doc/release_notes/3.34.0.txt +671 -0
- data/doc/release_notes/3.35.0.txt +144 -0
- data/doc/release_notes/3.36.0.txt +245 -0
- data/doc/schema_modification.rdoc +572 -0
- data/doc/sharding.rdoc +95 -3
- data/doc/testing.rdoc +129 -0
- data/doc/thread_safety.rdoc +17 -0
- data/doc/transactions.rdoc +137 -0
- data/doc/virtual_rows.rdoc +51 -2
- data/lib/sequel/adapters/ado/mssql.rb +18 -6
- data/lib/sequel/adapters/ado.rb +40 -18
- data/lib/sequel/adapters/amalgalite.rb +17 -9
- data/lib/sequel/adapters/db2.rb +175 -86
- data/lib/sequel/adapters/dbi.rb +15 -15
- data/lib/sequel/adapters/do/mysql.rb +17 -11
- data/lib/sequel/adapters/do/postgres.rb +1 -57
- data/lib/sequel/adapters/do/sqlite.rb +0 -10
- data/lib/sequel/adapters/do.rb +18 -36
- data/lib/sequel/adapters/firebird.rb +27 -208
- data/lib/sequel/adapters/ibmdb.rb +453 -0
- data/lib/sequel/adapters/informix.rb +6 -23
- data/lib/sequel/adapters/jdbc/as400.rb +16 -34
- data/lib/sequel/adapters/jdbc/db2.rb +47 -0
- data/lib/sequel/adapters/jdbc/derby.rb +287 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +24 -0
- data/lib/sequel/adapters/jdbc/h2.rb +73 -31
- data/lib/sequel/adapters/jdbc/hsqldb.rb +184 -0
- data/lib/sequel/adapters/jdbc/informix.rb +21 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +29 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
- data/lib/sequel/adapters/jdbc/mysql.rb +7 -24
- data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
- data/lib/sequel/adapters/jdbc/postgresql.rb +58 -48
- data/lib/sequel/adapters/jdbc/progress.rb +21 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +20 -10
- data/lib/sequel/adapters/jdbc/sqlserver.rb +66 -0
- data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
- data/lib/sequel/adapters/jdbc.rb +265 -94
- data/lib/sequel/adapters/mock.rb +361 -0
- data/lib/sequel/adapters/mysql.rb +118 -222
- data/lib/sequel/adapters/mysql2.rb +58 -57
- data/lib/sequel/adapters/odbc/mssql.rb +15 -7
- data/lib/sequel/adapters/odbc.rb +28 -9
- data/lib/sequel/adapters/openbase.rb +3 -5
- data/lib/sequel/adapters/oracle.rb +349 -53
- data/lib/sequel/adapters/postgres.rb +356 -107
- data/lib/sequel/adapters/shared/access.rb +32 -9
- data/lib/sequel/adapters/shared/db2.rb +342 -0
- data/lib/sequel/adapters/shared/firebird.rb +221 -0
- data/lib/sequel/adapters/shared/informix.rb +53 -0
- data/lib/sequel/adapters/shared/mssql.rb +335 -118
- data/lib/sequel/adapters/shared/mysql.rb +340 -76
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +155 -0
- data/lib/sequel/adapters/shared/oracle.rb +243 -45
- data/lib/sequel/adapters/shared/postgres.rb +524 -279
- data/lib/sequel/adapters/shared/progress.rb +5 -8
- data/lib/sequel/adapters/shared/sqlite.rb +265 -89
- data/lib/sequel/adapters/sqlite.rb +101 -39
- data/lib/sequel/adapters/swift/mysql.rb +9 -12
- data/lib/sequel/adapters/swift/postgres.rb +9 -74
- data/lib/sequel/adapters/swift/sqlite.rb +8 -6
- data/lib/sequel/adapters/swift.rb +8 -7
- data/lib/sequel/adapters/tinytds.rb +161 -21
- data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
- data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
- data/lib/sequel/ast_transformer.rb +194 -0
- data/lib/sequel/connection_pool/sharded_single.rb +11 -1
- data/lib/sequel/connection_pool/sharded_threaded.rb +28 -2
- data/lib/sequel/connection_pool/single.rb +5 -0
- data/lib/sequel/connection_pool/threaded.rb +38 -14
- data/lib/sequel/connection_pool.rb +10 -2
- data/lib/sequel/core.rb +118 -11
- data/lib/sequel/database/connecting.rb +38 -28
- data/lib/sequel/database/dataset.rb +4 -4
- data/lib/sequel/database/dataset_defaults.rb +63 -2
- data/lib/sequel/database/logging.rb +7 -2
- data/lib/sequel/database/misc.rb +150 -46
- data/lib/sequel/database/query.rb +215 -65
- data/lib/sequel/database/schema_generator.rb +64 -20
- data/lib/sequel/database/schema_methods.rb +212 -54
- data/lib/sequel/dataset/actions.rb +322 -85
- data/lib/sequel/dataset/features.rb +96 -8
- data/lib/sequel/dataset/graph.rb +38 -11
- data/lib/sequel/dataset/misc.rb +50 -23
- data/lib/sequel/dataset/mutation.rb +12 -7
- data/lib/sequel/dataset/prepared_statements.rb +68 -30
- data/lib/sequel/dataset/query.rb +237 -44
- data/lib/sequel/dataset/sql.rb +698 -278
- data/lib/sequel/dataset.rb +8 -0
- data/lib/sequel/exceptions.rb +4 -0
- data/lib/sequel/extensions/_pretty_table.rb +79 -0
- data/lib/sequel/extensions/arbitrary_servers.rb +108 -0
- data/lib/sequel/extensions/columns_introspection.rb +61 -0
- data/lib/sequel/{core_sql.rb → extensions/core_extensions.rb} +16 -34
- data/lib/sequel/extensions/migration.rb +97 -11
- data/lib/sequel/extensions/named_timezones.rb +5 -0
- data/lib/sequel/extensions/null_dataset.rb +90 -0
- data/lib/sequel/extensions/pg_array.rb +460 -0
- data/lib/sequel/extensions/pg_array_ops.rb +220 -0
- data/lib/sequel/extensions/pg_auto_parameterize.rb +169 -0
- data/lib/sequel/extensions/pg_hstore.rb +296 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +259 -0
- data/lib/sequel/extensions/pg_inet.rb +89 -0
- data/lib/sequel/extensions/pg_json.rb +178 -0
- data/lib/sequel/extensions/pg_statement_cache.rb +316 -0
- data/lib/sequel/extensions/pretty_table.rb +5 -71
- data/lib/sequel/extensions/query.rb +1 -1
- data/lib/sequel/extensions/query_literals.rb +79 -0
- data/lib/sequel/extensions/schema_caching.rb +76 -0
- data/lib/sequel/extensions/schema_dumper.rb +251 -37
- data/lib/sequel/extensions/select_remove.rb +35 -0
- data/lib/sequel/extensions/server_block.rb +139 -0
- data/lib/sequel/extensions/sql_expr.rb +4 -110
- data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
- data/lib/sequel/extensions/to_dot.rb +95 -83
- data/lib/sequel/model/associations.rb +1187 -393
- data/lib/sequel/model/base.rb +578 -170
- data/lib/sequel/model/errors.rb +1 -1
- data/lib/sequel/model/exceptions.rb +5 -1
- data/lib/sequel/model/inflections.rb +1 -1
- data/lib/sequel/model.rb +30 -11
- data/lib/sequel/no_core_ext.rb +2 -0
- data/lib/sequel/plugins/active_model.rb +13 -1
- data/lib/sequel/plugins/association_pks.rb +22 -4
- data/lib/sequel/plugins/caching.rb +25 -18
- data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
- data/lib/sequel/plugins/composition.rb +44 -12
- data/lib/sequel/plugins/dataset_associations.rb +100 -0
- data/lib/sequel/plugins/defaults_setter.rb +58 -0
- data/lib/sequel/plugins/dirty.rb +214 -0
- data/lib/sequel/plugins/eager_each.rb +59 -0
- data/lib/sequel/plugins/force_encoding.rb +6 -6
- data/lib/sequel/plugins/hook_class_methods.rb +1 -1
- data/lib/sequel/plugins/identity_map.rb +123 -8
- data/lib/sequel/plugins/instance_filters.rb +10 -0
- data/lib/sequel/plugins/instance_hooks.rb +1 -1
- data/lib/sequel/plugins/json_serializer.rb +58 -6
- data/lib/sequel/plugins/list.rb +12 -2
- data/lib/sequel/plugins/many_through_many.rb +79 -28
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +71 -0
- data/lib/sequel/plugins/nested_attributes.rb +139 -63
- data/lib/sequel/plugins/optimistic_locking.rb +8 -0
- data/lib/sequel/plugins/prepared_statements.rb +167 -0
- data/lib/sequel/plugins/prepared_statements_associations.rb +87 -0
- data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
- data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
- data/lib/sequel/plugins/rcte_tree.rb +29 -15
- data/lib/sequel/plugins/schema.rb +7 -2
- data/lib/sequel/plugins/serialization.rb +98 -49
- data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
- data/lib/sequel/plugins/sharding.rb +21 -54
- data/lib/sequel/plugins/single_table_inheritance.rb +5 -3
- data/lib/sequel/plugins/static_cache.rb +99 -0
- data/lib/sequel/plugins/subclasses.rb +10 -2
- data/lib/sequel/plugins/tactical_eager_loading.rb +7 -7
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +9 -12
- data/lib/sequel/plugins/update_primary_key.rb +2 -2
- data/lib/sequel/plugins/validation_class_methods.rb +1 -1
- data/lib/sequel/plugins/xml_serializer.rb +15 -4
- data/lib/sequel/sql.rb +526 -59
- data/lib/sequel/timezones.rb +57 -38
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/db2_spec.rb +146 -0
- data/spec/adapters/firebird_spec.rb +1 -1
- data/spec/adapters/mssql_spec.rb +142 -40
- data/spec/adapters/mysql_spec.rb +380 -234
- data/spec/adapters/oracle_spec.rb +67 -77
- data/spec/adapters/postgres_spec.rb +1050 -212
- data/spec/adapters/spec_helper.rb +8 -5
- data/spec/adapters/sqlite_spec.rb +139 -27
- data/spec/core/connection_pool_spec.rb +196 -93
- data/spec/core/database_spec.rb +879 -510
- data/spec/core/dataset_spec.rb +1415 -1086
- data/spec/core/expression_filters_spec.rb +554 -109
- data/spec/core/mock_adapter_spec.rb +449 -0
- data/spec/core/object_graph_spec.rb +86 -114
- data/spec/core/schema_generator_spec.rb +4 -4
- data/spec/core/schema_spec.rb +482 -38
- data/spec/core/spec_helper.rb +6 -48
- data/spec/extensions/active_model_spec.rb +13 -0
- data/spec/extensions/arbitrary_servers_spec.rb +110 -0
- data/spec/extensions/association_autoreloading_spec.rb +18 -10
- data/spec/extensions/association_dependencies_spec.rb +15 -25
- data/spec/extensions/association_pks_spec.rb +66 -32
- data/spec/extensions/association_proxies_spec.rb +4 -4
- data/spec/extensions/caching_spec.rb +47 -51
- data/spec/extensions/class_table_inheritance_spec.rb +31 -83
- data/spec/extensions/columns_introspection_spec.rb +91 -0
- data/spec/extensions/composition_spec.rb +18 -13
- data/spec/{core/core_sql_spec.rb → extensions/core_extensions_spec.rb} +119 -139
- data/spec/extensions/dataset_associations_spec.rb +199 -0
- data/spec/extensions/defaults_setter_spec.rb +64 -0
- data/spec/extensions/dirty_spec.rb +155 -0
- data/spec/extensions/eager_each_spec.rb +34 -0
- data/spec/extensions/force_encoding_spec.rb +4 -2
- data/spec/extensions/hook_class_methods_spec.rb +69 -92
- data/spec/extensions/identity_map_spec.rb +131 -22
- data/spec/extensions/inflector_spec.rb +0 -4
- data/spec/extensions/instance_filters_spec.rb +11 -21
- data/spec/extensions/instance_hooks_spec.rb +72 -0
- data/spec/extensions/json_serializer_spec.rb +49 -6
- data/spec/extensions/lazy_attributes_spec.rb +16 -20
- data/spec/extensions/list_spec.rb +48 -39
- data/spec/extensions/many_through_many_spec.rb +283 -250
- data/spec/extensions/many_to_one_pk_lookup_spec.rb +140 -0
- data/spec/extensions/migration_spec.rb +140 -35
- data/spec/extensions/named_timezones_spec.rb +29 -11
- data/spec/extensions/nested_attributes_spec.rb +251 -89
- data/spec/extensions/null_dataset_spec.rb +85 -0
- data/spec/extensions/optimistic_locking_spec.rb +24 -21
- data/spec/extensions/pg_array_ops_spec.rb +105 -0
- data/spec/extensions/pg_array_spec.rb +196 -0
- data/spec/extensions/pg_auto_parameterize_spec.rb +65 -0
- data/spec/extensions/pg_hstore_ops_spec.rb +136 -0
- data/spec/extensions/pg_hstore_spec.rb +195 -0
- data/spec/extensions/pg_inet_spec.rb +44 -0
- data/spec/extensions/pg_json_spec.rb +101 -0
- data/spec/extensions/pg_statement_cache_spec.rb +209 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
- data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
- data/spec/extensions/prepared_statements_spec.rb +87 -0
- data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
- data/spec/extensions/pretty_table_spec.rb +7 -6
- data/spec/extensions/query_literals_spec.rb +168 -0
- data/spec/extensions/query_spec.rb +6 -0
- data/spec/extensions/rcte_tree_spec.rb +50 -43
- data/spec/extensions/schema_caching_spec.rb +41 -0
- data/spec/extensions/schema_dumper_spec.rb +429 -22
- data/spec/extensions/schema_spec.rb +25 -21
- data/spec/extensions/select_remove_spec.rb +38 -0
- data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
- data/spec/extensions/serialization_spec.rb +85 -43
- data/spec/extensions/server_block_spec.rb +90 -0
- data/spec/extensions/sharding_spec.rb +69 -143
- data/spec/extensions/single_table_inheritance_spec.rb +23 -29
- data/spec/extensions/skip_create_refresh_spec.rb +1 -1
- data/spec/extensions/spec_helper.rb +22 -61
- data/spec/extensions/sql_expr_spec.rb +29 -60
- data/spec/extensions/static_cache_spec.rb +145 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +17 -19
- data/spec/extensions/thread_local_timezones_spec.rb +22 -2
- data/spec/extensions/timestamps_spec.rb +6 -6
- data/spec/extensions/to_dot_spec.rb +4 -7
- data/spec/extensions/touch_spec.rb +13 -14
- data/spec/extensions/tree_spec.rb +11 -26
- data/spec/extensions/typecast_on_load_spec.rb +9 -6
- data/spec/extensions/update_primary_key_spec.rb +30 -24
- data/spec/extensions/validation_class_methods_spec.rb +30 -51
- data/spec/extensions/validation_helpers_spec.rb +16 -35
- data/spec/extensions/xml_serializer_spec.rb +29 -4
- data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +4 -0
- data/spec/files/transaction_specified_migrations/002_create_basic.rb +4 -0
- data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +3 -0
- data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +3 -0
- data/spec/integration/associations_test.rb +659 -21
- data/spec/integration/database_test.rb +9 -3
- data/spec/integration/dataset_test.rb +605 -91
- data/spec/integration/eager_loader_test.rb +19 -21
- data/spec/integration/migrator_test.rb +2 -3
- data/spec/integration/model_test.rb +81 -2
- data/spec/integration/plugin_test.rb +454 -143
- data/spec/integration/prepared_statement_test.rb +263 -128
- data/spec/integration/schema_test.rb +309 -43
- data/spec/integration/spec_helper.rb +40 -30
- data/spec/integration/timezone_test.rb +38 -12
- data/spec/integration/transaction_test.rb +183 -8
- data/spec/integration/type_test.rb +18 -4
- data/spec/model/association_reflection_spec.rb +201 -7
- data/spec/model/associations_spec.rb +1067 -661
- data/spec/model/base_spec.rb +329 -95
- data/spec/model/dataset_methods_spec.rb +7 -27
- data/spec/model/eager_loading_spec.rb +677 -676
- data/spec/model/hooks_spec.rb +349 -67
- data/spec/model/model_spec.rb +240 -168
- data/spec/model/plugins_spec.rb +24 -13
- data/spec/model/record_spec.rb +490 -232
- data/spec/model/spec_helper.rb +14 -71
- data/spec/model/validations_spec.rb +11 -0
- data/spec/rcov.opts +2 -0
- metadata +178 -80
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
module Sequel
|
|
2
|
+
module Plugins
|
|
3
|
+
# The dirty plugin makes Sequel save the initial value of
|
|
4
|
+
# a column when setting a new value for the column. This
|
|
5
|
+
# makes it easier to see what changes were made to the object:
|
|
6
|
+
#
|
|
7
|
+
# artist.name # => 'Foo'
|
|
8
|
+
# artist.name = 'Bar'
|
|
9
|
+
# artist.initial_value(:name) # 'Foo'
|
|
10
|
+
# artist.column_change(:name) # ['Foo', 'Bar']
|
|
11
|
+
# artist.column_changes # {:name => ['Foo', 'Bar']}
|
|
12
|
+
# artist.column_changed?(:name) # true
|
|
13
|
+
# artist.reset_column(:name)
|
|
14
|
+
# artist.name # => 'Foo'
|
|
15
|
+
# artist.column_changed?(:name) # false
|
|
16
|
+
#
|
|
17
|
+
# It allows makes changed_columns more accurate in that it
|
|
18
|
+
# can detect when a the column value is changed and then
|
|
19
|
+
# changed back:
|
|
20
|
+
#
|
|
21
|
+
# artist.name # => 'Foo'
|
|
22
|
+
# artist.name = 'Bar'
|
|
23
|
+
# artist.changed_columns # => [:name]
|
|
24
|
+
# artist.name = 'Foo'
|
|
25
|
+
# artist.changed_columns # => []
|
|
26
|
+
#
|
|
27
|
+
# It can handle situations where a column value is
|
|
28
|
+
# modified in place:
|
|
29
|
+
#
|
|
30
|
+
# artist.will_change_column(:name)
|
|
31
|
+
# artist.name.gsub!(/o/, 'u')
|
|
32
|
+
# artist.changed_columns # => [:name]
|
|
33
|
+
# artist.initial_value(:name) # => 'Foo'
|
|
34
|
+
# artist.column_change(:name) # => ['Foo', 'Fuu']
|
|
35
|
+
#
|
|
36
|
+
# It also saves the previously changed values after an update:
|
|
37
|
+
#
|
|
38
|
+
# artist.update(:name=>'Bar')
|
|
39
|
+
# artist.column_changes # => {}
|
|
40
|
+
# artist.previous_changes # => {:name=>['Foo', 'Bar']}
|
|
41
|
+
#
|
|
42
|
+
# Usage:
|
|
43
|
+
#
|
|
44
|
+
# # Make all model subclass instances record previous values (called before loading subclasses)
|
|
45
|
+
# Sequel::Model.plugin :dirty
|
|
46
|
+
#
|
|
47
|
+
# # Make the Album class record previous values
|
|
48
|
+
# Album.plugin :dirty
|
|
49
|
+
module Dirty
|
|
50
|
+
module InstanceMethods
|
|
51
|
+
# A hash of previous changes before the object was
|
|
52
|
+
# saved, in the same format as #column_changes.
|
|
53
|
+
# Note that this is not necessarily the same as the columns
|
|
54
|
+
# that were used in the update statement.
|
|
55
|
+
attr_reader :previous_changes
|
|
56
|
+
|
|
57
|
+
# An array with the initial value and the current value
|
|
58
|
+
# of the column, if the column has been changed. If the
|
|
59
|
+
# column has not been changed, returns nil.
|
|
60
|
+
#
|
|
61
|
+
# column_change(:name) # => ['Initial', 'Current']
|
|
62
|
+
def column_change(column)
|
|
63
|
+
[initial_value(column), send(column)] if column_changed?(column)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# A hash with column symbol keys and pairs of initial and
|
|
67
|
+
# current values for all changed columns.
|
|
68
|
+
#
|
|
69
|
+
# column_changes # => {:name => ['Initial', 'Current']}
|
|
70
|
+
def column_changes
|
|
71
|
+
h = {}
|
|
72
|
+
initial_values.each do |column, value|
|
|
73
|
+
h[column] = [value, send(column)]
|
|
74
|
+
end
|
|
75
|
+
h
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Either true or false depending on whether the column has
|
|
79
|
+
# changed. Note that this is not exactly the same as checking if
|
|
80
|
+
# the column is in changed_columns, if the column was not set
|
|
81
|
+
# initially.
|
|
82
|
+
#
|
|
83
|
+
# column_changed?(:name) # => true
|
|
84
|
+
def column_changed?(column)
|
|
85
|
+
initial_values.has_key?(column)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The initial value of the given column. If the column value has
|
|
89
|
+
# not changed, this will be the same as the current value of the
|
|
90
|
+
# column.
|
|
91
|
+
#
|
|
92
|
+
# initial_value(:name) # => 'Initial'
|
|
93
|
+
def initial_value(column)
|
|
94
|
+
initial_values.fetch(column){send(column)}
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# A hash with column symbol keys and initial values.
|
|
98
|
+
#
|
|
99
|
+
# initial_values # {:name => 'Initial'}
|
|
100
|
+
def initial_values
|
|
101
|
+
@initial_values ||= {}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Reset the column to its initial value. If the column was not set
|
|
105
|
+
# initial, removes it from the values.
|
|
106
|
+
#
|
|
107
|
+
# reset_column(:name)
|
|
108
|
+
# name # => 'Initial'
|
|
109
|
+
def reset_column(column)
|
|
110
|
+
if initial_values.has_key?(column)
|
|
111
|
+
send(:"#{column}=", initial_values[column])
|
|
112
|
+
end
|
|
113
|
+
if missing_initial_values.include?(column)
|
|
114
|
+
values.delete(column)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Manually specify that a column will change. This should only be used
|
|
119
|
+
# if you plan to modify a column value in place, which is not recommended.
|
|
120
|
+
#
|
|
121
|
+
# will_change_column(:name)
|
|
122
|
+
# name.gsub(/i/i, 'o')
|
|
123
|
+
# column_change(:name) # => ['Initial', 'onotoal']
|
|
124
|
+
def will_change_column(column)
|
|
125
|
+
changed_columns << column unless changed_columns.include?(column)
|
|
126
|
+
check_missing_initial_value(column)
|
|
127
|
+
|
|
128
|
+
value = if initial_values.has_key?(column)
|
|
129
|
+
initial_values[column]
|
|
130
|
+
else
|
|
131
|
+
send(column)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
initial_values[column] = if value && value != true && value.respond_to?(:clone)
|
|
135
|
+
begin
|
|
136
|
+
value.clone
|
|
137
|
+
rescue TypeError
|
|
138
|
+
value
|
|
139
|
+
end
|
|
140
|
+
else
|
|
141
|
+
value
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
private
|
|
146
|
+
|
|
147
|
+
# Reset the initial values after saving.
|
|
148
|
+
def after_save
|
|
149
|
+
super
|
|
150
|
+
reset_initial_values
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Save the current changes so they are available after updating. This happens
|
|
154
|
+
# before after_save resets them.
|
|
155
|
+
def after_update
|
|
156
|
+
super
|
|
157
|
+
@previous_changes = column_changes
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Reset the initial values when refreshing.
|
|
161
|
+
def _refresh(dataset)
|
|
162
|
+
super
|
|
163
|
+
reset_initial_values
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# When changing the column value, save the initial column value. If the column
|
|
167
|
+
# value is changed back to the initial value, update changed columns to remove
|
|
168
|
+
# the column.
|
|
169
|
+
def change_column_value(column, value)
|
|
170
|
+
if (iv = initial_values).has_key?(column)
|
|
171
|
+
initial = iv[column]
|
|
172
|
+
super
|
|
173
|
+
if value == initial
|
|
174
|
+
changed_columns.delete(column) unless missing_initial_values.include?(column)
|
|
175
|
+
iv.delete(column)
|
|
176
|
+
end
|
|
177
|
+
else
|
|
178
|
+
check_missing_initial_value(column)
|
|
179
|
+
iv[column] = send(column)
|
|
180
|
+
super
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# If the values hash does not contain the column, make sure missing_initial_values
|
|
185
|
+
# does so that it doesn't get deleted from changed_columns if changed back,
|
|
186
|
+
# and so that reseting the column value can be handled correctly.
|
|
187
|
+
def check_missing_initial_value(column)
|
|
188
|
+
unless values.has_key?(column) || (miv = missing_initial_values).include?(column)
|
|
189
|
+
miv << column
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Reset the initial values when initializing.
|
|
194
|
+
def initialize_set(h)
|
|
195
|
+
super
|
|
196
|
+
reset_initial_values
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Array holding column symbols that were not present initially. This is necessary
|
|
200
|
+
# to differentiate between values that were not present and values that were
|
|
201
|
+
# present but equal to nil.
|
|
202
|
+
def missing_initial_values
|
|
203
|
+
@missing_initial_values ||= []
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Clear the data structures that store the initial values.
|
|
207
|
+
def reset_initial_values
|
|
208
|
+
@initial_values.clear if @initial_values
|
|
209
|
+
@missing_initial_values.clear if @missing_initial_values
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module Sequel
|
|
2
|
+
module Plugins
|
|
3
|
+
# The eager_each plugin makes calling each on an eager loaded dataset do eager loading.
|
|
4
|
+
# By default, each does not work on an eager loaded dataset, because each iterates
|
|
5
|
+
# over rows of the dataset as they come in, and to eagerly load you need to have all
|
|
6
|
+
# values up front. With the default associations code, you must call #all on an eagerly
|
|
7
|
+
# loaded dataset, as calling #each on an #eager dataset skips the eager loading, and calling
|
|
8
|
+
# #each on an #eager_graph dataset makes it yield plain hashes with columns from all
|
|
9
|
+
# tables, instead of yielding the instances of the main model.
|
|
10
|
+
#
|
|
11
|
+
# This plugin makes #each call #all for eagerly loaded datasets. As #all usually calls
|
|
12
|
+
# #each, this is a bit of issue, but this plugin resolves the issue by cloning the dataset
|
|
13
|
+
# and setting a new flag in the cloned dataset, so that each can check with the flag to
|
|
14
|
+
# determine whether it should call all.
|
|
15
|
+
#
|
|
16
|
+
# Usage:
|
|
17
|
+
#
|
|
18
|
+
# # Make all model subclass instances eagerly load for each (called before loading subclasses)
|
|
19
|
+
# Sequel::Model.plugin :eager_each
|
|
20
|
+
#
|
|
21
|
+
# # Make the Album class eagerly load for each
|
|
22
|
+
# Album.plugin :eager_each
|
|
23
|
+
module EagerEach
|
|
24
|
+
# Methods added to eagerly loaded datasets when the eager_each plugin is in use.
|
|
25
|
+
module EagerDatasetMethods
|
|
26
|
+
# Call #all instead of #each unless #each is being called by #all.
|
|
27
|
+
def each(&block)
|
|
28
|
+
if opts[:all_called]
|
|
29
|
+
super
|
|
30
|
+
else
|
|
31
|
+
all(&block)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Clone the dataset and set a flag to let #each know not to call #all,
|
|
36
|
+
# to avoid the infinite loop.
|
|
37
|
+
def all(&block)
|
|
38
|
+
if opts[:all_called]
|
|
39
|
+
super
|
|
40
|
+
else
|
|
41
|
+
clone(:all_called=>true).all(&block)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
module DatasetMethods
|
|
47
|
+
# Make sure calling each on this dataset will eagerly load the dataset.
|
|
48
|
+
def eager(*)
|
|
49
|
+
super.extend(EagerDatasetMethods)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Make sure calling each on this dataset will eagerly load the dataset.
|
|
53
|
+
def eager_graph(*)
|
|
54
|
+
super.extend(EagerDatasetMethods)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -8,7 +8,7 @@ module Sequel
|
|
|
8
8
|
# attribute, the resulting value is forced to a given encoding if the
|
|
9
9
|
# value is a string. There are two ways to specify the encoding. You
|
|
10
10
|
# can either do so in the plugin call itself, or via the
|
|
11
|
-
# forced_encoding class accessor
|
|
11
|
+
# forced_encoding class accessor.
|
|
12
12
|
#
|
|
13
13
|
# Usage:
|
|
14
14
|
#
|
|
@@ -46,6 +46,11 @@ module Sequel
|
|
|
46
46
|
super(force_hash_encoding(row))
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
# Force the encoding of all string values when setting the instance's values.
|
|
50
|
+
def set_values(row)
|
|
51
|
+
super(force_hash_encoding(row))
|
|
52
|
+
end
|
|
53
|
+
|
|
49
54
|
private
|
|
50
55
|
|
|
51
56
|
# Force the encoding for all string values in the given row hash.
|
|
@@ -55,11 +60,6 @@ module Sequel
|
|
|
55
60
|
row
|
|
56
61
|
end
|
|
57
62
|
|
|
58
|
-
# Force the encoding of all string values when setting the instance's values.
|
|
59
|
-
def set_values(row)
|
|
60
|
-
super(force_hash_encoding(row))
|
|
61
|
-
end
|
|
62
|
-
|
|
63
63
|
# Force the encoding of all returned strings to the model's forced_encoding.
|
|
64
64
|
def typecast_value(column, value)
|
|
65
65
|
s = super
|
|
@@ -90,9 +90,9 @@ module Sequel
|
|
|
90
90
|
|
|
91
91
|
# Make a copy of the current class's hooks for the subclass.
|
|
92
92
|
def inherited(subclass)
|
|
93
|
-
super
|
|
94
93
|
hooks = subclass.instance_variable_set(:@hooks, {})
|
|
95
94
|
instance_variable_get(:@hooks).each{|k,v| hooks[k] = v.dup}
|
|
95
|
+
super
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
private
|
|
@@ -22,10 +22,7 @@ module Sequel
|
|
|
22
22
|
# Identity maps are thread-local and only persist for the duration of the block,
|
|
23
23
|
# so they should only be considered as a possible performance enhancer.
|
|
24
24
|
#
|
|
25
|
-
# The identity_map plugin is not compatible with the
|
|
26
|
-
# many_to_many and many_through_many associations. If you want to use the identity_map plugin,
|
|
27
|
-
# you should use +eager_graph+ instead of +eager+ for those associations. It is also
|
|
28
|
-
# not compatible with the eager loading in the +rcte_tree+ plugin.
|
|
25
|
+
# The identity_map plugin is not compatible with the eager loading in the +rcte_tree+ plugin.
|
|
29
26
|
#
|
|
30
27
|
# Usage:
|
|
31
28
|
#
|
|
@@ -37,6 +34,116 @@ module Sequel
|
|
|
37
34
|
# # would need to do Album.with_identity_map{} to use the identity map
|
|
38
35
|
module IdentityMap
|
|
39
36
|
module ClassMethods
|
|
37
|
+
# Override the default :eager_loader option for many_*_many associations to work
|
|
38
|
+
# with an identity_map. If the :eager_graph association option is used, you'll probably have to use
|
|
39
|
+
# :uniq=>true on the current association amd :cartesian_product_number=>2 on the association
|
|
40
|
+
# mentioned by :eager_graph, otherwise you'll end up with duplicates because the row proc will be
|
|
41
|
+
# getting called multiple times for the same object. If you do have duplicates and you use :eager_graph,
|
|
42
|
+
# they'll probably be lost. Making that work correctly would require changing a lot of the core
|
|
43
|
+
# architecture, such as how graphing and eager graphing work.
|
|
44
|
+
def associate(type, name, opts = {}, &block)
|
|
45
|
+
if opts[:eager_loader]
|
|
46
|
+
super
|
|
47
|
+
elsif type == :many_to_many
|
|
48
|
+
opts = super
|
|
49
|
+
el = opts[:eager_loader]
|
|
50
|
+
model = self
|
|
51
|
+
left_pk = opts[:left_primary_key]
|
|
52
|
+
uses_lcks = opts[:uses_left_composite_keys]
|
|
53
|
+
uses_rcks = opts[:uses_right_composite_keys]
|
|
54
|
+
right = opts[:right_key]
|
|
55
|
+
join_table = opts[:join_table]
|
|
56
|
+
left = opts[:left_key]
|
|
57
|
+
lcks = opts[:left_keys]
|
|
58
|
+
left_key_alias = opts[:left_key_alias] ||= opts.default_associated_key_alias
|
|
59
|
+
opts[:eager_loader] = lambda do |eo|
|
|
60
|
+
return el.call(eo) unless model.identity_map
|
|
61
|
+
h = eo[:key_hash][left_pk]
|
|
62
|
+
eo[:rows].each{|object| object.associations[name] = []}
|
|
63
|
+
r = uses_rcks ? rcks.zip(opts.right_primary_keys) : [[right, opts.right_primary_key]]
|
|
64
|
+
l = uses_lcks ? [[lcks.map{|k| SQL::QualifiedIdentifier.new(join_table, k)}, h.keys]] : [[left, h.keys]]
|
|
65
|
+
|
|
66
|
+
# Replace the row proc to remove the left key alias before calling the previous row proc.
|
|
67
|
+
# Associate the value of the left key alias with the associated object (through its object_id).
|
|
68
|
+
# When loading the associated objects, lookup the left key alias value and associate the
|
|
69
|
+
# associated objects to the main objects if the left key alias value matches the left primary key
|
|
70
|
+
# value of the main object.
|
|
71
|
+
#
|
|
72
|
+
# The deleting of the left key alias from the hash before calling the previous row proc
|
|
73
|
+
# is necessary when an identity map is used, otherwise if the same associated object is returned more than
|
|
74
|
+
# once for the association, it won't know which of current objects to associate it to.
|
|
75
|
+
ds = opts.associated_class.inner_join(join_table, r + l)
|
|
76
|
+
pr = ds.row_proc
|
|
77
|
+
h2 = {}
|
|
78
|
+
ds.row_proc = proc do |hash|
|
|
79
|
+
hash_key = if uses_lcks
|
|
80
|
+
left_key_alias.map{|k| hash.delete(k)}
|
|
81
|
+
else
|
|
82
|
+
hash.delete(left_key_alias)
|
|
83
|
+
end
|
|
84
|
+
obj = pr.call(hash)
|
|
85
|
+
(h2[obj.object_id] ||= []) << hash_key
|
|
86
|
+
obj
|
|
87
|
+
end
|
|
88
|
+
model.eager_loading_dataset(opts, ds, Array(opts.select), eo[:associations], eo) .all do |assoc_record|
|
|
89
|
+
if hash_keys = h2.delete(assoc_record.object_id)
|
|
90
|
+
hash_keys.each do |hash_key|
|
|
91
|
+
if objects = h[hash_key]
|
|
92
|
+
objects.each{|object| object.associations[name].push(assoc_record)}
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
opts
|
|
99
|
+
elsif type == :many_through_many
|
|
100
|
+
opts = super
|
|
101
|
+
el = opts[:eager_loader]
|
|
102
|
+
model = self
|
|
103
|
+
left_pk = opts[:left_primary_key]
|
|
104
|
+
left_key = opts[:left_key]
|
|
105
|
+
uses_lcks = opts[:uses_left_composite_keys]
|
|
106
|
+
left_keys = Array(left_key)
|
|
107
|
+
left_key_alias = opts[:left_key_alias]
|
|
108
|
+
opts[:eager_loader] = lambda do |eo|
|
|
109
|
+
return el.call(eo) unless model.identity_map
|
|
110
|
+
h = eo[:key_hash][left_pk]
|
|
111
|
+
eo[:rows].each{|object| object.associations[name] = []}
|
|
112
|
+
ds = opts.associated_class
|
|
113
|
+
opts.reverse_edges.each{|t| ds = ds.join(t[:table], Array(t[:left]).zip(Array(t[:right])), :table_alias=>t[:alias])}
|
|
114
|
+
ft = opts.final_reverse_edge
|
|
115
|
+
conds = uses_lcks ? [[left_keys.map{|k| SQL::QualifiedIdentifier.new(ft[:table], k)}, h.keys]] : [[left_key, h.keys]]
|
|
116
|
+
|
|
117
|
+
# See above comment in many_to_many eager_loader
|
|
118
|
+
ds = ds.join(ft[:table], Array(ft[:left]).zip(Array(ft[:right])) + conds, :table_alias=>ft[:alias])
|
|
119
|
+
pr = ds.row_proc
|
|
120
|
+
h2 = {}
|
|
121
|
+
ds.row_proc = proc do |hash|
|
|
122
|
+
hash_key = if uses_lcks
|
|
123
|
+
left_key_alias.map{|k| hash.delete(k)}
|
|
124
|
+
else
|
|
125
|
+
hash.delete(left_key_alias)
|
|
126
|
+
end
|
|
127
|
+
obj = pr.call(hash)
|
|
128
|
+
(h2[obj.object_id] ||= []) << hash_key
|
|
129
|
+
obj
|
|
130
|
+
end
|
|
131
|
+
model.eager_loading_dataset(opts, ds, Array(opts.select), eo[:associations], eo).all do |assoc_record|
|
|
132
|
+
if hash_keys = h2.delete(assoc_record.object_id)
|
|
133
|
+
hash_keys.each do |hash_key|
|
|
134
|
+
if objects = h[hash_key]
|
|
135
|
+
objects.each{|object| object.associations[name].push(assoc_record)}
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
opts
|
|
142
|
+
else
|
|
143
|
+
super
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
40
147
|
# Returns the current thread-local identity map. Should be a hash if
|
|
41
148
|
# there is an active identity map, and nil otherwise.
|
|
42
149
|
def identity_map
|
|
@@ -56,7 +163,7 @@ module Sequel
|
|
|
56
163
|
# certain fields in an initial query, make modifications to some of those
|
|
57
164
|
# fields and request other, potentially overlapping fields in a new query,
|
|
58
165
|
# and not have the second query override fields you modified.
|
|
59
|
-
def
|
|
166
|
+
def call(row)
|
|
60
167
|
return super unless idm = identity_map
|
|
61
168
|
if o = idm[identity_map_key(Array(primary_key).map{|x| row[x]})]
|
|
62
169
|
o.merge_db_update(row)
|
|
@@ -121,10 +228,18 @@ module Sequel
|
|
|
121
228
|
# key option has a value and the association uses the primary key of
|
|
122
229
|
# the associated class as the :primary_key option, check the identity
|
|
123
230
|
# map for the associated object and return it if present.
|
|
124
|
-
def
|
|
231
|
+
def _load_associated_object(opts, dynamic_opts)
|
|
125
232
|
klass = opts.associated_class
|
|
126
|
-
|
|
127
|
-
|
|
233
|
+
cache_lookup = opts.fetch(:idm_cache_lookup) do
|
|
234
|
+
opts[:idm_cache_lookup] = klass.respond_to?(:identity_map) &&
|
|
235
|
+
opts[:type] == :many_to_one &&
|
|
236
|
+
opts[:key] &&
|
|
237
|
+
opts.primary_key == klass.primary_key
|
|
238
|
+
end
|
|
239
|
+
if cache_lookup &&
|
|
240
|
+
!dynamic_opts[:callback] &&
|
|
241
|
+
(idm = klass.identity_map) &&
|
|
242
|
+
(o = idm[klass.identity_map_key(_associated_object_pk(opts[:key]))])
|
|
128
243
|
o
|
|
129
244
|
else
|
|
130
245
|
super
|
|
@@ -68,6 +68,16 @@ module Sequel
|
|
|
68
68
|
|
|
69
69
|
private
|
|
70
70
|
|
|
71
|
+
# If there are any instance filters, make sure not to use the
|
|
72
|
+
# instance delete optimization.
|
|
73
|
+
def _delete_without_checking
|
|
74
|
+
if @instance_filters && !@instance_filters.empty?
|
|
75
|
+
_delete_dataset.delete
|
|
76
|
+
else
|
|
77
|
+
super
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
71
81
|
# Lazily initialize the instance filter array.
|
|
72
82
|
def instance_filters
|
|
73
83
|
@instance_filters ||= []
|
|
@@ -29,7 +29,7 @@ module Sequel
|
|
|
29
29
|
BEFORE_HOOKS = Sequel::Model::BEFORE_HOOKS
|
|
30
30
|
AFTER_HOOKS = Sequel::Model::AFTER_HOOKS - [:after_initialize]
|
|
31
31
|
HOOKS = BEFORE_HOOKS + AFTER_HOOKS
|
|
32
|
-
HOOKS.each{|h| class_eval("def #{h}_hook(&block); add_instance_hook(:#{h}, &block) end", __FILE__, __LINE__)}
|
|
32
|
+
HOOKS.each{|h| class_eval("def #{h}_hook(&block); add_instance_hook(:#{h}, &block); self end", __FILE__, __LINE__)}
|
|
33
33
|
|
|
34
34
|
BEFORE_HOOKS.each{|h| class_eval("def #{h}; run_before_instance_hooks(:#{h}) == false ? false : super end", __FILE__, __LINE__)}
|
|
35
35
|
AFTER_HOOKS.each{|h| class_eval(<<-END, __FILE__, __LINE__ + 1)}
|
|
@@ -56,6 +56,11 @@ module Sequel
|
|
|
56
56
|
# Album.to_json
|
|
57
57
|
# Album.filter(:artist_id=>1).to_json(:include=>:tags)
|
|
58
58
|
#
|
|
59
|
+
# If you have an existing array of model instances you want to convert to
|
|
60
|
+
# JSON, you can call the class to_json method with the :array option:
|
|
61
|
+
#
|
|
62
|
+
# Album.to_json(:array=>[Album[1], Album[2]])
|
|
63
|
+
#
|
|
59
64
|
# Usage:
|
|
60
65
|
#
|
|
61
66
|
# # Add JSON output capability to all model subclass instances (called before loading subclasses)
|
|
@@ -134,8 +139,13 @@ module Sequel
|
|
|
134
139
|
module InstanceMethods
|
|
135
140
|
# Parse the provided JSON, which should return a hash,
|
|
136
141
|
# and call +set+ with that hash.
|
|
137
|
-
def from_json(json)
|
|
138
|
-
|
|
142
|
+
def from_json(json, opts={})
|
|
143
|
+
h = JSON.parse(json)
|
|
144
|
+
if fields = opts[:fields]
|
|
145
|
+
set_fields(h, fields, opts)
|
|
146
|
+
else
|
|
147
|
+
set(h)
|
|
148
|
+
end
|
|
139
149
|
end
|
|
140
150
|
|
|
141
151
|
# Return a string in JSON format. Accepts the following
|
|
@@ -149,7 +159,7 @@ module Sequel
|
|
|
149
159
|
# to include in the JSON output. Using a nested
|
|
150
160
|
# hash, you can pass options to associations
|
|
151
161
|
# to affect the JSON used for associated objects.
|
|
152
|
-
# :naked :: Not to add the JSON.create_id key to the JSON
|
|
162
|
+
# :naked :: Not to add the JSON.create_id (json_class) key to the JSON
|
|
153
163
|
# output hash, so when the JSON is parsed, it
|
|
154
164
|
# will yield a hash instead of a model object.
|
|
155
165
|
# :only :: Symbol or Array of Symbols of columns to only
|
|
@@ -195,7 +205,21 @@ module Sequel
|
|
|
195
205
|
module DatasetMethods
|
|
196
206
|
# Return a JSON string representing an array of all objects in
|
|
197
207
|
# this dataset. Takes the same options as the the instance
|
|
198
|
-
# method, and passes them to every instance.
|
|
208
|
+
# method, and passes them to every instance. Additionally,
|
|
209
|
+
# respects the following options:
|
|
210
|
+
#
|
|
211
|
+
# :array :: An array of instances. If this is not provided,
|
|
212
|
+
# calls #all on the receiver to get the array.
|
|
213
|
+
# :root :: If set to :collection, only wraps the collection
|
|
214
|
+
# in a root object. If set to :instance, only wraps
|
|
215
|
+
# the instances in a root object. If set to :both,
|
|
216
|
+
# wraps both the collection and instances in a root
|
|
217
|
+
# object. Unfortunately, for backwards compatibility,
|
|
218
|
+
# if this option is true and doesn't match one of those
|
|
219
|
+
# symbols, it defaults to both. That may change in a
|
|
220
|
+
# future version, so for forwards compatibility, you
|
|
221
|
+
# should pick a specific symbol for your desired
|
|
222
|
+
# behavior.
|
|
199
223
|
def to_json(*a)
|
|
200
224
|
if opts = a.first.is_a?(Hash)
|
|
201
225
|
opts = model.json_serializer_opts.merge(a.first)
|
|
@@ -203,8 +227,36 @@ module Sequel
|
|
|
203
227
|
else
|
|
204
228
|
opts = model.json_serializer_opts
|
|
205
229
|
end
|
|
206
|
-
|
|
207
|
-
opts[:root]
|
|
230
|
+
|
|
231
|
+
collection_root = case opts[:root]
|
|
232
|
+
when nil, false, :instance
|
|
233
|
+
false
|
|
234
|
+
when :collection
|
|
235
|
+
opts = opts.dup
|
|
236
|
+
opts.delete(:root)
|
|
237
|
+
opts[:naked] = true unless opts.has_key?(:naked)
|
|
238
|
+
true
|
|
239
|
+
else
|
|
240
|
+
true
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
res = if row_proc
|
|
244
|
+
array = if opts[:array]
|
|
245
|
+
opts = opts.dup
|
|
246
|
+
opts.delete(:array)
|
|
247
|
+
else
|
|
248
|
+
all
|
|
249
|
+
end
|
|
250
|
+
array.map{|obj| Literal.new(obj.to_json(opts))}
|
|
251
|
+
else
|
|
252
|
+
all
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
if collection_root
|
|
256
|
+
{model.send(:pluralize, model.send(:underscore, model.to_s)) => res}.to_json(*a)
|
|
257
|
+
else
|
|
258
|
+
res.to_json(*a)
|
|
259
|
+
end
|
|
208
260
|
end
|
|
209
261
|
end
|
|
210
262
|
end
|
data/lib/sequel/plugins/list.rb
CHANGED
|
@@ -39,9 +39,11 @@ module Sequel
|
|
|
39
39
|
# Item.plugin :list, :scope=>:user_id
|
|
40
40
|
#
|
|
41
41
|
# Note that using this plugin modifies the order of the model's dataset to
|
|
42
|
-
# sort by the position and scope fields.
|
|
42
|
+
# sort by the position and scope fields. Also note that this plugin is subject to
|
|
43
|
+
# race conditions, and is not safe when concurrent modifications are made
|
|
44
|
+
# to the same list.
|
|
43
45
|
#
|
|
44
|
-
#
|
|
46
|
+
# Additionally, note that unlike ruby arrays, the list plugin assumes that the
|
|
45
47
|
# first entry in the list has position 1, not position 0.
|
|
46
48
|
#
|
|
47
49
|
# Copyright (c) 2007-2010 Sharon Rosner, Wayne E. Seguin, Aman Gupta, Adrian Madrid, Jeremy Evans
|
|
@@ -90,6 +92,14 @@ module Sequel
|
|
|
90
92
|
list_dataset.first(position_field => p)
|
|
91
93
|
end
|
|
92
94
|
|
|
95
|
+
# Set the value of the position_field to the maximum value plus 1 unless the
|
|
96
|
+
# position field already has a value.
|
|
97
|
+
def before_create
|
|
98
|
+
unless send(position_field)
|
|
99
|
+
send("#{position_field}=", list_dataset.max(position_field).to_i+1)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
93
103
|
# Find the last position in the list containing this instance.
|
|
94
104
|
def last_position
|
|
95
105
|
list_dataset.max(position_field).to_i
|