sequel 3.48.0 → 4.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG +114 -0
- data/Rakefile +10 -7
- data/doc/association_basics.rdoc +25 -23
- data/doc/code_order.rdoc +7 -0
- data/doc/core_extensions.rdoc +0 -10
- data/doc/object_model.rdoc +4 -1
- data/doc/querying.rdoc +3 -3
- data/doc/release_notes/4.0.0.txt +262 -0
- data/doc/security.rdoc +0 -28
- data/doc/testing.rdoc +8 -14
- data/lib/sequel/adapters/ado.rb +7 -11
- data/lib/sequel/adapters/ado/access.rb +8 -8
- data/lib/sequel/adapters/ado/mssql.rb +4 -4
- data/lib/sequel/adapters/amalgalite.rb +6 -6
- data/lib/sequel/adapters/cubrid.rb +7 -7
- data/lib/sequel/adapters/db2.rb +5 -9
- data/lib/sequel/adapters/dbi.rb +2 -6
- data/lib/sequel/adapters/do.rb +4 -4
- data/lib/sequel/adapters/firebird.rb +4 -4
- data/lib/sequel/adapters/ibmdb.rb +8 -8
- data/lib/sequel/adapters/informix.rb +2 -10
- data/lib/sequel/adapters/jdbc.rb +17 -17
- data/lib/sequel/adapters/jdbc/as400.rb +2 -2
- data/lib/sequel/adapters/jdbc/cubrid.rb +1 -1
- data/lib/sequel/adapters/jdbc/db2.rb +1 -1
- data/lib/sequel/adapters/jdbc/derby.rb +1 -1
- data/lib/sequel/adapters/jdbc/h2.rb +2 -2
- data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -1
- data/lib/sequel/adapters/jdbc/informix.rb +1 -1
- data/lib/sequel/adapters/jdbc/mssql.rb +2 -2
- data/lib/sequel/adapters/jdbc/mysql.rb +1 -1
- data/lib/sequel/adapters/jdbc/oracle.rb +5 -1
- data/lib/sequel/adapters/jdbc/postgresql.rb +3 -3
- data/lib/sequel/adapters/jdbc/sqlite.rb +3 -3
- data/lib/sequel/adapters/jdbc/transactions.rb +3 -3
- data/lib/sequel/adapters/mock.rb +7 -7
- data/lib/sequel/adapters/mysql.rb +3 -3
- data/lib/sequel/adapters/mysql2.rb +4 -4
- data/lib/sequel/adapters/odbc.rb +2 -6
- data/lib/sequel/adapters/odbc/mssql.rb +1 -1
- data/lib/sequel/adapters/openbase.rb +1 -5
- data/lib/sequel/adapters/oracle.rb +13 -17
- data/lib/sequel/adapters/postgres.rb +20 -25
- data/lib/sequel/adapters/shared/cubrid.rb +3 -3
- data/lib/sequel/adapters/shared/db2.rb +2 -2
- data/lib/sequel/adapters/shared/firebird.rb +7 -7
- data/lib/sequel/adapters/shared/mssql.rb +9 -9
- data/lib/sequel/adapters/shared/mysql.rb +29 -13
- data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +7 -7
- data/lib/sequel/adapters/shared/oracle.rb +22 -13
- data/lib/sequel/adapters/shared/postgres.rb +61 -46
- data/lib/sequel/adapters/shared/sqlite.rb +9 -9
- data/lib/sequel/adapters/sqlite.rb +17 -11
- data/lib/sequel/adapters/swift.rb +3 -3
- data/lib/sequel/adapters/swift/mysql.rb +1 -1
- data/lib/sequel/adapters/swift/sqlite.rb +1 -1
- data/lib/sequel/adapters/tinytds.rb +8 -8
- data/lib/sequel/ast_transformer.rb +3 -1
- data/lib/sequel/connection_pool.rb +4 -2
- data/lib/sequel/connection_pool/sharded_single.rb +2 -2
- data/lib/sequel/connection_pool/sharded_threaded.rb +5 -5
- data/lib/sequel/connection_pool/threaded.rb +7 -7
- data/lib/sequel/core.rb +4 -67
- data/lib/sequel/database.rb +1 -0
- data/lib/sequel/database/connecting.rb +2 -8
- data/lib/sequel/database/dataset.rb +2 -7
- data/lib/sequel/database/dataset_defaults.rb +0 -18
- data/lib/sequel/database/features.rb +4 -4
- data/lib/sequel/database/misc.rb +6 -8
- data/lib/sequel/database/query.rb +5 -61
- data/lib/sequel/database/schema_generator.rb +22 -20
- data/lib/sequel/database/schema_methods.rb +48 -20
- data/lib/sequel/database/transactions.rb +7 -17
- data/lib/sequel/dataset.rb +2 -0
- data/lib/sequel/dataset/actions.rb +23 -91
- data/lib/sequel/dataset/features.rb +1 -4
- data/lib/sequel/dataset/graph.rb +3 -47
- data/lib/sequel/dataset/misc.rb +4 -33
- data/lib/sequel/dataset/prepared_statements.rb +3 -1
- data/lib/sequel/dataset/query.rb +116 -240
- data/lib/sequel/dataset/sql.rb +19 -97
- data/lib/sequel/deprecated.rb +0 -16
- data/lib/sequel/exceptions.rb +0 -3
- data/lib/sequel/extensions/_pretty_table.rb +1 -1
- data/lib/sequel/extensions/columns_introspection.rb +1 -12
- data/lib/sequel/extensions/constraint_validations.rb +3 -3
- data/lib/sequel/extensions/core_extensions.rb +0 -9
- data/lib/sequel/extensions/date_arithmetic.rb +1 -2
- data/lib/sequel/extensions/graph_each.rb +11 -0
- data/lib/sequel/extensions/migration.rb +5 -5
- data/lib/sequel/extensions/null_dataset.rb +11 -13
- data/lib/sequel/extensions/pagination.rb +3 -6
- data/lib/sequel/extensions/pg_array.rb +6 -4
- data/lib/sequel/extensions/pg_array_ops.rb +35 -1
- data/lib/sequel/extensions/pg_json.rb +12 -2
- data/lib/sequel/extensions/pg_json_ops.rb +266 -0
- data/lib/sequel/extensions/pg_range.rb +2 -2
- data/lib/sequel/extensions/pg_range_ops.rb +0 -8
- data/lib/sequel/extensions/pg_row.rb +2 -2
- data/lib/sequel/extensions/pretty_table.rb +0 -4
- data/lib/sequel/extensions/query.rb +3 -8
- data/lib/sequel/extensions/schema_caching.rb +0 -7
- data/lib/sequel/extensions/schema_dumper.rb +10 -17
- data/lib/sequel/extensions/select_remove.rb +0 -4
- data/lib/sequel/extensions/set_overrides.rb +28 -0
- data/lib/sequel/extensions/to_dot.rb +6 -10
- data/lib/sequel/model.rb +6 -7
- data/lib/sequel/model/associations.rb +127 -182
- data/lib/sequel/model/base.rb +88 -211
- data/lib/sequel/model/errors.rb +0 -13
- data/lib/sequel/model/plugins.rb +2 -2
- data/lib/sequel/no_core_ext.rb +0 -1
- data/lib/sequel/plugins/after_initialize.rb +11 -17
- data/lib/sequel/plugins/association_autoreloading.rb +1 -47
- data/lib/sequel/plugins/association_dependencies.rb +2 -2
- data/lib/sequel/plugins/auto_validations.rb +2 -8
- data/lib/sequel/plugins/blacklist_security.rb +32 -2
- data/lib/sequel/plugins/caching.rb +1 -1
- data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
- data/lib/sequel/plugins/composition.rb +10 -8
- data/lib/sequel/plugins/constraint_validations.rb +2 -2
- data/lib/sequel/plugins/dataset_associations.rb +4 -0
- data/lib/sequel/plugins/defaults_setter.rb +8 -6
- data/lib/sequel/plugins/dirty.rb +6 -6
- data/lib/sequel/plugins/force_encoding.rb +13 -8
- data/lib/sequel/plugins/hook_class_methods.rb +1 -7
- data/lib/sequel/plugins/json_serializer.rb +13 -74
- data/lib/sequel/plugins/lazy_attributes.rb +2 -4
- data/lib/sequel/plugins/list.rb +1 -1
- data/lib/sequel/plugins/many_through_many.rb +4 -11
- data/lib/sequel/plugins/many_to_one_pk_lookup.rb +1 -49
- data/lib/sequel/plugins/nested_attributes.rb +1 -1
- data/lib/sequel/plugins/optimistic_locking.rb +3 -5
- data/lib/sequel/plugins/pg_array_associations.rb +453 -0
- data/lib/sequel/plugins/pg_typecast_on_load.rb +23 -7
- data/lib/sequel/plugins/prepared_statements.rb +1 -1
- data/lib/sequel/plugins/prepared_statements_associations.rb +20 -14
- data/lib/sequel/plugins/prepared_statements_safe.rb +2 -2
- data/lib/sequel/plugins/rcte_tree.rb +1 -1
- data/lib/sequel/plugins/serialization.rb +5 -4
- data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
- data/lib/sequel/plugins/sharding.rb +7 -1
- data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
- data/lib/sequel/plugins/timestamps.rb +1 -1
- data/lib/sequel/plugins/touch.rb +2 -2
- data/lib/sequel/plugins/tree.rb +1 -1
- data/lib/sequel/plugins/typecast_on_load.rb +19 -4
- data/lib/sequel/plugins/validation_class_methods.rb +0 -30
- data/lib/sequel/plugins/validation_helpers.rb +13 -31
- data/lib/sequel/plugins/xml_serializer.rb +18 -57
- data/lib/sequel/sql.rb +20 -22
- data/lib/sequel/version.rb +2 -2
- data/spec/adapters/db2_spec.rb +14 -23
- data/spec/adapters/firebird_spec.rb +25 -29
- data/spec/adapters/informix_spec.rb +11 -14
- data/spec/adapters/mssql_spec.rb +71 -77
- data/spec/adapters/mysql_spec.rb +165 -172
- data/spec/adapters/oracle_spec.rb +36 -39
- data/spec/adapters/postgres_spec.rb +175 -100
- data/spec/adapters/spec_helper.rb +13 -11
- data/spec/adapters/sqlite_spec.rb +36 -44
- data/spec/core/connection_pool_spec.rb +2 -1
- data/spec/core/database_spec.rb +55 -55
- data/spec/core/dataset_spec.rb +45 -249
- data/spec/core/deprecated_spec.rb +0 -8
- data/spec/core/expression_filters_spec.rb +23 -5
- data/spec/core/object_graph_spec.rb +4 -66
- data/spec/core/schema_spec.rb +35 -12
- data/spec/core/spec_helper.rb +3 -2
- data/spec/core_extensions_spec.rb +17 -19
- data/spec/extensions/arbitrary_servers_spec.rb +2 -3
- data/spec/extensions/association_dependencies_spec.rb +14 -14
- data/spec/extensions/auto_validations_spec.rb +7 -0
- data/spec/extensions/blacklist_security_spec.rb +5 -5
- data/spec/extensions/blank_spec.rb +2 -0
- data/spec/extensions/class_table_inheritance_spec.rb +2 -2
- data/spec/extensions/columns_introspection_spec.rb +2 -29
- data/spec/extensions/composition_spec.rb +10 -17
- data/spec/extensions/core_refinements_spec.rb +5 -1
- data/spec/extensions/dataset_associations_spec.rb +18 -0
- data/spec/extensions/date_arithmetic_spec.rb +2 -2
- data/spec/extensions/defaults_setter_spec.rb +9 -9
- data/spec/extensions/dirty_spec.rb +0 -5
- data/spec/extensions/eval_inspect_spec.rb +2 -0
- data/spec/extensions/force_encoding_spec.rb +2 -18
- data/spec/extensions/hash_aliases_spec.rb +8 -0
- data/spec/extensions/hook_class_methods_spec.rb +39 -58
- data/spec/extensions/inflector_spec.rb +2 -0
- data/spec/extensions/instance_filters_spec.rb +8 -8
- data/spec/extensions/json_serializer_spec.rb +1 -41
- data/spec/extensions/list_spec.rb +1 -1
- data/spec/extensions/many_through_many_spec.rb +106 -109
- data/spec/extensions/migration_spec.rb +2 -0
- data/spec/extensions/named_timezones_spec.rb +1 -0
- data/spec/extensions/pg_array_associations_spec.rb +603 -0
- data/spec/extensions/pg_array_ops_spec.rb +25 -0
- data/spec/extensions/pg_array_spec.rb +9 -1
- data/spec/extensions/pg_hstore_ops_spec.rb +13 -0
- data/spec/extensions/pg_hstore_spec.rb +1 -0
- data/spec/extensions/pg_json_ops_spec.rb +131 -0
- data/spec/extensions/pg_json_spec.rb +10 -4
- data/spec/extensions/pg_range_ops_spec.rb +2 -5
- data/spec/extensions/pg_range_spec.rb +6 -2
- data/spec/extensions/pg_row_ops_spec.rb +2 -0
- data/spec/extensions/prepared_statements_associations_spec.rb +26 -5
- data/spec/extensions/rcte_tree_spec.rb +15 -15
- data/spec/extensions/schema_dumper_spec.rb +0 -1
- data/spec/extensions/schema_spec.rb +9 -9
- data/spec/extensions/serialization_modification_detection_spec.rb +1 -1
- data/spec/extensions/serialization_spec.rb +18 -29
- data/spec/extensions/set_overrides_spec.rb +4 -0
- data/spec/extensions/{many_to_one_pk_lookup_spec.rb → shared_caching_spec.rb} +1 -4
- data/spec/extensions/single_table_inheritance_spec.rb +4 -4
- data/spec/extensions/spec_helper.rb +8 -9
- data/spec/extensions/sql_expr_spec.rb +2 -0
- data/spec/extensions/string_date_time_spec.rb +2 -0
- data/spec/extensions/string_stripper_spec.rb +2 -0
- data/spec/extensions/tactical_eager_loading_spec.rb +12 -12
- data/spec/extensions/thread_local_timezones_spec.rb +2 -0
- data/spec/extensions/timestamps_spec.rb +1 -1
- data/spec/extensions/to_dot_spec.rb +1 -1
- data/spec/extensions/touch_spec.rb +24 -24
- data/spec/extensions/tree_spec.rb +7 -7
- data/spec/extensions/typecast_on_load_spec.rb +8 -1
- data/spec/extensions/update_primary_key_spec.rb +10 -10
- data/spec/extensions/validation_class_methods_spec.rb +10 -39
- data/spec/extensions/validation_helpers_spec.rb +29 -47
- data/spec/extensions/xml_serializer_spec.rb +1 -23
- data/spec/integration/associations_test.rb +231 -40
- data/spec/integration/database_test.rb +1 -1
- data/spec/integration/dataset_test.rb +64 -64
- data/spec/integration/eager_loader_test.rb +28 -28
- data/spec/integration/migrator_test.rb +1 -1
- data/spec/integration/model_test.rb +2 -2
- data/spec/integration/plugin_test.rb +21 -21
- data/spec/integration/prepared_statement_test.rb +7 -7
- data/spec/integration/schema_test.rb +115 -110
- data/spec/integration/spec_helper.rb +17 -27
- data/spec/integration/timezone_test.rb +1 -1
- data/spec/integration/transaction_test.rb +10 -10
- data/spec/integration/type_test.rb +2 -2
- data/spec/model/association_reflection_spec.rb +2 -28
- data/spec/model/associations_spec.rb +239 -188
- data/spec/model/base_spec.rb +27 -68
- data/spec/model/dataset_methods_spec.rb +4 -4
- data/spec/model/eager_loading_spec.rb +160 -172
- data/spec/model/hooks_spec.rb +62 -79
- data/spec/model/model_spec.rb +36 -51
- data/spec/model/plugins_spec.rb +5 -19
- data/spec/model/record_spec.rb +125 -151
- data/spec/model/spec_helper.rb +8 -6
- data/spec/model/validations_spec.rb +4 -17
- data/spec/spec_config.rb +2 -10
- metadata +50 -56
- data/lib/sequel/deprecated_core_extensions.rb +0 -135
- data/lib/sequel/extensions/pg_auto_parameterize.rb +0 -185
- data/lib/sequel/extensions/pg_statement_cache.rb +0 -318
- data/lib/sequel/plugins/identity_map.rb +0 -260
- data/lib/sequel_core.rb +0 -2
- data/lib/sequel_model.rb +0 -2
- data/spec/extensions/association_autoreloading_spec.rb +0 -102
- data/spec/extensions/identity_map_spec.rb +0 -337
- data/spec/extensions/pg_auto_parameterize_spec.rb +0 -70
- data/spec/extensions/pg_statement_cache_spec.rb +0 -208
- data/spec/rcov.opts +0 -8
- data/spec/spec_config.rb.example +0 -10
data/spec/model/plugins_spec.rb
CHANGED
|
@@ -115,7 +115,7 @@ describe Sequel::Model, ".plugin" do
|
|
|
115
115
|
m.args.should == [[b, 123, {1=>2}], [b2, 234, {2=>3}]]
|
|
116
116
|
end
|
|
117
117
|
|
|
118
|
-
it "should call things in the following order: apply,
|
|
118
|
+
it "should call things in the following order: apply, ClassMethods, InstanceMethods, DatasetMethods, configure" do
|
|
119
119
|
m = Module.new do
|
|
120
120
|
@args = []
|
|
121
121
|
def self.args; @args; end
|
|
@@ -144,9 +144,9 @@ describe Sequel::Model, ".plugin" do
|
|
|
144
144
|
|
|
145
145
|
b = lambda{44}
|
|
146
146
|
@c.plugin(m, 123, 1=>2, &b)
|
|
147
|
-
m.args.should == [:apply, :
|
|
147
|
+
m.args.should == [:apply, :cm, :im, :dm, :configure]
|
|
148
148
|
@c.plugin(m, 234, 2=>3, &b)
|
|
149
|
-
m.args.should == [:apply, :
|
|
149
|
+
m.args.should == [:apply, :cm, :im, :dm, :configure, :configure]
|
|
150
150
|
end
|
|
151
151
|
|
|
152
152
|
it "should include an InstanceMethods module in the class if the plugin includes it" do
|
|
@@ -173,30 +173,16 @@ describe Sequel::Model, ".plugin" do
|
|
|
173
173
|
it "should save the DatasetMethods module and apply it later if the class doesn't have a dataset" do
|
|
174
174
|
c = Class.new(Sequel::Model)
|
|
175
175
|
c.plugin @t
|
|
176
|
-
c.dataset =
|
|
176
|
+
c.dataset = DB[:i]
|
|
177
177
|
c.dataset.ghi.should == 345
|
|
178
178
|
end
|
|
179
179
|
|
|
180
180
|
it "should save the DatasetMethods module and apply it later if the class has a dataset" do
|
|
181
181
|
@c.plugin @t
|
|
182
|
-
@c.dataset =
|
|
182
|
+
@c.dataset = DB[:i]
|
|
183
183
|
@c.dataset.ghi.should == 345
|
|
184
184
|
end
|
|
185
185
|
|
|
186
|
-
qspecify "should define class methods for all public instance methods in DatasetMethod" do
|
|
187
|
-
m = Module.new do
|
|
188
|
-
self::DatasetMethods = Module.new do
|
|
189
|
-
def a; 1; end
|
|
190
|
-
def b; 2; end
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
@c.plugin m
|
|
194
|
-
@c.dataset.a.should == 1
|
|
195
|
-
@c.dataset.b.should == 2
|
|
196
|
-
@c.a.should == 1
|
|
197
|
-
@c.b.should == 2
|
|
198
|
-
end
|
|
199
|
-
|
|
200
186
|
it "should not define class methods for private instance methods in DatasetMethod" do
|
|
201
187
|
m = Module.new do
|
|
202
188
|
self::DatasetMethods = Module.new do
|
data/spec/model/record_spec.rb
CHANGED
|
@@ -43,13 +43,13 @@ describe "Model#save" do
|
|
|
43
43
|
columns :id, :x, :y
|
|
44
44
|
end
|
|
45
45
|
@c.instance_dataset.autoid = @c.dataset.autoid = 13
|
|
46
|
-
|
|
46
|
+
DB.reset
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "should insert a record for a new model instance" do
|
|
50
50
|
o = @c.new(:x => 1)
|
|
51
51
|
o.save
|
|
52
|
-
|
|
52
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 13) LIMIT 1"]
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it "should use dataset's insert_select method if present" do
|
|
@@ -63,7 +63,7 @@ describe "Model#save" do
|
|
|
63
63
|
o.save
|
|
64
64
|
|
|
65
65
|
o.values.should == {:y=>2}
|
|
66
|
-
|
|
66
|
+
DB.sqls.should == ["INSERT INTO items (y) VALUES (2) RETURNING *"]
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
it "should not use dataset's insert_select method if specific columns are selected" do
|
|
@@ -75,21 +75,21 @@ describe "Model#save" do
|
|
|
75
75
|
it "should use value returned by insert as the primary key and refresh the object" do
|
|
76
76
|
o = @c.new(:x => 11)
|
|
77
77
|
o.save
|
|
78
|
-
|
|
78
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)",
|
|
79
79
|
"SELECT * FROM items WHERE (id = 13) LIMIT 1"]
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
it "should allow you to skip refreshing by overridding _save_refresh" do
|
|
83
83
|
@c.send(:define_method, :_save_refresh){}
|
|
84
84
|
@c.create(:x => 11)
|
|
85
|
-
|
|
85
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
it "should work correctly for inserting a record without a primary key" do
|
|
89
89
|
@c.no_primary_key
|
|
90
90
|
o = @c.new(:x => 11)
|
|
91
91
|
o.save
|
|
92
|
-
|
|
92
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (11)"]
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
it "should set the autoincrementing_primary_key value to the value returned by insert" do
|
|
@@ -98,7 +98,7 @@ describe "Model#save" do
|
|
|
98
98
|
o = @c.new(:x => 11)
|
|
99
99
|
def o.autoincrementing_primary_key() :y end
|
|
100
100
|
o.save
|
|
101
|
-
sqls =
|
|
101
|
+
sqls = DB.sqls
|
|
102
102
|
sqls.length.should == 2
|
|
103
103
|
sqls.first.should == "INSERT INTO items (x) VALUES (11)"
|
|
104
104
|
sqls.last.should =~ %r{SELECT \* FROM items WHERE \(\([xy] = 1[13]\) AND \([xy] = 1[13]\)\) LIMIT 1}
|
|
@@ -107,7 +107,7 @@ describe "Model#save" do
|
|
|
107
107
|
it "should update a record for an existing model instance" do
|
|
108
108
|
o = @c.load(:id => 3, :x => 1)
|
|
109
109
|
o.save
|
|
110
|
-
|
|
110
|
+
DB.sqls.should == ["UPDATE items SET x = 1 WHERE (id = 3)"]
|
|
111
111
|
end
|
|
112
112
|
|
|
113
113
|
it "should raise a NoExistingObject exception if the dataset update call doesn't return 1, unless require_modification is false" do
|
|
@@ -127,16 +127,10 @@ describe "Model#save" do
|
|
|
127
127
|
proc{o.save}.should_not raise_error
|
|
128
128
|
end
|
|
129
129
|
|
|
130
|
-
qspecify "should update only the given columns if given" do
|
|
131
|
-
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
132
|
-
o.save(:y)
|
|
133
|
-
MODEL_DB.sqls.first.should == "UPDATE items SET y = NULL WHERE (id = 3)"
|
|
134
|
-
end
|
|
135
|
-
|
|
136
130
|
it "should respect the :columns option to specify the columns to save" do
|
|
137
131
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
138
132
|
o.save(:columns=>:y)
|
|
139
|
-
|
|
133
|
+
DB.sqls.first.should == "UPDATE items SET y = NULL WHERE (id = 3)"
|
|
140
134
|
end
|
|
141
135
|
|
|
142
136
|
it "should mark saved columns as not changed" do
|
|
@@ -197,19 +191,19 @@ describe "Model#save" do
|
|
|
197
191
|
it "should use Model's use_transactions setting by default" do
|
|
198
192
|
@c.use_transactions = true
|
|
199
193
|
@c.load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
|
|
200
|
-
|
|
194
|
+
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
|
|
201
195
|
@c.use_transactions = false
|
|
202
196
|
@c.load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
|
|
203
|
-
|
|
197
|
+
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
|
|
204
198
|
end
|
|
205
199
|
|
|
206
200
|
it "should inherit Model's use_transactions setting" do
|
|
207
201
|
@c.use_transactions = true
|
|
208
202
|
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
|
|
209
|
-
|
|
203
|
+
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
|
|
210
204
|
@c.use_transactions = false
|
|
211
205
|
Class.new(@c).load(:id => 3, :x => 1, :y => nil).save(:columns=>:y)
|
|
212
|
-
|
|
206
|
+
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
|
|
213
207
|
end
|
|
214
208
|
|
|
215
209
|
it "should use object's use_transactions setting" do
|
|
@@ -217,23 +211,23 @@ describe "Model#save" do
|
|
|
217
211
|
o.use_transactions = false
|
|
218
212
|
@c.use_transactions = true
|
|
219
213
|
o.save(:columns=>:y)
|
|
220
|
-
|
|
214
|
+
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
|
|
221
215
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
222
216
|
o.use_transactions = true
|
|
223
217
|
@c.use_transactions = false
|
|
224
218
|
o.save(:columns=>:y)
|
|
225
|
-
|
|
219
|
+
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
|
|
226
220
|
end
|
|
227
221
|
|
|
228
222
|
it "should use :transaction option if given" do
|
|
229
223
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
230
224
|
o.use_transactions = true
|
|
231
225
|
o.save(:columns=>:y, :transaction=>false)
|
|
232
|
-
|
|
226
|
+
DB.sqls.should == ["UPDATE items SET y = NULL WHERE (id = 3)"]
|
|
233
227
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
234
228
|
o.use_transactions = false
|
|
235
229
|
o.save(:columns=>:y, :transaction=>true)
|
|
236
|
-
|
|
230
|
+
DB.sqls.should == ["BEGIN", "UPDATE items SET y = NULL WHERE (id = 3)", "COMMIT"]
|
|
237
231
|
end
|
|
238
232
|
|
|
239
233
|
it "should rollback if before_save returns false and raise_on_save_failure = true" do
|
|
@@ -244,7 +238,7 @@ describe "Model#save" do
|
|
|
244
238
|
false
|
|
245
239
|
end
|
|
246
240
|
proc { o.save(:columns=>:y) }.should raise_error(Sequel::BeforeHookFailed)
|
|
247
|
-
|
|
241
|
+
DB.sqls.should == ["BEGIN", "ROLLBACK"]
|
|
248
242
|
end
|
|
249
243
|
|
|
250
244
|
it "should rollback if before_save returns false and :raise_on_failure option is true" do
|
|
@@ -255,7 +249,7 @@ describe "Model#save" do
|
|
|
255
249
|
false
|
|
256
250
|
end
|
|
257
251
|
proc { o.save(:columns=>:y, :raise_on_failure => true) }.should raise_error(Sequel::BeforeHookFailed)
|
|
258
|
-
|
|
252
|
+
DB.sqls.should == ["BEGIN", "ROLLBACK"]
|
|
259
253
|
end
|
|
260
254
|
|
|
261
255
|
it "should not rollback outer transactions if before_save returns false and raise_on_save_failure = false" do
|
|
@@ -265,11 +259,11 @@ describe "Model#save" do
|
|
|
265
259
|
def o.before_save
|
|
266
260
|
false
|
|
267
261
|
end
|
|
268
|
-
|
|
262
|
+
DB.transaction do
|
|
269
263
|
o.save(:columns=>:y).should == nil
|
|
270
|
-
|
|
264
|
+
DB.run "BLAH"
|
|
271
265
|
end
|
|
272
|
-
|
|
266
|
+
DB.sqls.should == ["BEGIN", "BLAH", "COMMIT"]
|
|
273
267
|
end
|
|
274
268
|
|
|
275
269
|
it "should rollback if before_save returns false and raise_on_save_failure = false" do
|
|
@@ -280,7 +274,7 @@ describe "Model#save" do
|
|
|
280
274
|
false
|
|
281
275
|
end
|
|
282
276
|
o.save(:columns=>:y).should == nil
|
|
283
|
-
|
|
277
|
+
DB.sqls.should == ["BEGIN", "ROLLBACK"]
|
|
284
278
|
end
|
|
285
279
|
|
|
286
280
|
it "should not rollback if before_save throws Rollback and use_transactions = false" do
|
|
@@ -290,7 +284,7 @@ describe "Model#save" do
|
|
|
290
284
|
raise Sequel::Rollback
|
|
291
285
|
end
|
|
292
286
|
proc { o.save(:columns=>:y) }.should raise_error(Sequel::Rollback)
|
|
293
|
-
|
|
287
|
+
DB.sqls.should == []
|
|
294
288
|
end
|
|
295
289
|
|
|
296
290
|
it "should support a :server option to set the server/shard to use" do
|
|
@@ -363,7 +357,7 @@ describe "Model#freeze" do
|
|
|
363
357
|
end
|
|
364
358
|
end
|
|
365
359
|
@o = Album.load(:id=>1).freeze
|
|
366
|
-
|
|
360
|
+
DB.sqls
|
|
367
361
|
end
|
|
368
362
|
after do
|
|
369
363
|
Object.send(:remove_const, :Album)
|
|
@@ -452,7 +446,7 @@ describe "Model#modified?" do
|
|
|
452
446
|
columns :id, :x
|
|
453
447
|
@db_schema = {:x => {:type => :integer}}
|
|
454
448
|
end
|
|
455
|
-
|
|
449
|
+
DB.reset
|
|
456
450
|
end
|
|
457
451
|
|
|
458
452
|
it "should be true if the object is new" do
|
|
@@ -510,7 +504,7 @@ describe "Model#modified!" do
|
|
|
510
504
|
@c.class_eval do
|
|
511
505
|
columns :id, :x
|
|
512
506
|
end
|
|
513
|
-
|
|
507
|
+
DB.reset
|
|
514
508
|
end
|
|
515
509
|
|
|
516
510
|
it "should mark the object as modified so that save_changes still runs the callbacks" do
|
|
@@ -542,35 +536,35 @@ describe "Model#save_changes" do
|
|
|
542
536
|
unrestrict_primary_key
|
|
543
537
|
columns :id, :x, :y
|
|
544
538
|
end
|
|
545
|
-
|
|
539
|
+
DB.reset
|
|
546
540
|
end
|
|
547
541
|
|
|
548
542
|
it "should always save if the object is new" do
|
|
549
543
|
o = @c.new(:x => 1)
|
|
550
544
|
o.save_changes
|
|
551
|
-
|
|
545
|
+
DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
|
|
552
546
|
end
|
|
553
547
|
|
|
554
548
|
it "should take options passed to save" do
|
|
555
549
|
o = @c.new(:x => 1)
|
|
556
550
|
def o.before_validation; false; end
|
|
557
551
|
proc{o.save_changes}.should raise_error(Sequel::Error)
|
|
558
|
-
|
|
552
|
+
DB.sqls.should == []
|
|
559
553
|
o.save_changes(:validate=>false)
|
|
560
|
-
|
|
554
|
+
DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
|
|
561
555
|
end
|
|
562
556
|
|
|
563
557
|
it "should do nothing if no changed columns" do
|
|
564
558
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
565
559
|
o.save_changes
|
|
566
|
-
|
|
560
|
+
DB.sqls.should == []
|
|
567
561
|
end
|
|
568
562
|
|
|
569
563
|
it "should do nothing if modified? is false" do
|
|
570
564
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
571
565
|
def o.modified?; false; end
|
|
572
566
|
o.save_changes
|
|
573
|
-
|
|
567
|
+
DB.sqls.should == []
|
|
574
568
|
end
|
|
575
569
|
|
|
576
570
|
it "should update only changed columns" do
|
|
@@ -578,17 +572,17 @@ describe "Model#save_changes" do
|
|
|
578
572
|
o.x = 2
|
|
579
573
|
|
|
580
574
|
o.save_changes
|
|
581
|
-
|
|
575
|
+
DB.sqls.should == ["UPDATE items SET x = 2 WHERE (id = 3)"]
|
|
582
576
|
o.save_changes
|
|
583
577
|
o.save_changes
|
|
584
|
-
|
|
578
|
+
DB.sqls.should == []
|
|
585
579
|
|
|
586
580
|
o.y = 4
|
|
587
581
|
o.save_changes
|
|
588
|
-
|
|
582
|
+
DB.sqls.should == ["UPDATE items SET y = 4 WHERE (id = 3)"]
|
|
589
583
|
o.save_changes
|
|
590
584
|
o.save_changes
|
|
591
|
-
|
|
585
|
+
DB.sqls.should == []
|
|
592
586
|
end
|
|
593
587
|
|
|
594
588
|
it "should not consider columns changed if the values did not change" do
|
|
@@ -596,17 +590,17 @@ describe "Model#save_changes" do
|
|
|
596
590
|
o.x = 1
|
|
597
591
|
|
|
598
592
|
o.save_changes
|
|
599
|
-
|
|
593
|
+
DB.sqls.should == []
|
|
600
594
|
o.x = 3
|
|
601
595
|
o.save_changes
|
|
602
|
-
|
|
596
|
+
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
|
|
603
597
|
|
|
604
598
|
o[:y] = nil
|
|
605
599
|
o.save_changes
|
|
606
|
-
|
|
600
|
+
DB.sqls.should == []
|
|
607
601
|
o[:y] = 4
|
|
608
602
|
o.save_changes
|
|
609
|
-
|
|
603
|
+
DB.sqls.should == ["UPDATE items SET y = 4 WHERE (id = 3)"]
|
|
610
604
|
end
|
|
611
605
|
|
|
612
606
|
it "should clear changed_columns" do
|
|
@@ -621,30 +615,30 @@ describe "Model#save_changes" do
|
|
|
621
615
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
622
616
|
@c.send(:define_method, :before_update){self.x += 1}
|
|
623
617
|
o.save_changes
|
|
624
|
-
|
|
618
|
+
DB.sqls.should == []
|
|
625
619
|
o.x = 2
|
|
626
620
|
o.save_changes
|
|
627
|
-
|
|
621
|
+
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
|
|
628
622
|
o.save_changes
|
|
629
|
-
|
|
623
|
+
DB.sqls.should == []
|
|
630
624
|
o.x = 4
|
|
631
625
|
o.save_changes
|
|
632
|
-
|
|
626
|
+
DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
|
|
633
627
|
end
|
|
634
628
|
|
|
635
629
|
it "should update columns changed in a before_save hook" do
|
|
636
630
|
o = @c.load(:id => 3, :x => 1, :y => nil)
|
|
637
631
|
@c.send(:define_method, :before_update){self.x += 1}
|
|
638
632
|
o.save_changes
|
|
639
|
-
|
|
633
|
+
DB.sqls.should == []
|
|
640
634
|
o.x = 2
|
|
641
635
|
o.save_changes
|
|
642
|
-
|
|
636
|
+
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 3)"]
|
|
643
637
|
o.save_changes
|
|
644
|
-
|
|
638
|
+
DB.sqls.should == []
|
|
645
639
|
o.x = 4
|
|
646
640
|
o.save_changes
|
|
647
|
-
|
|
641
|
+
DB.sqls.should == ["UPDATE items SET x = 5 WHERE (id = 3)"]
|
|
648
642
|
end
|
|
649
643
|
end
|
|
650
644
|
|
|
@@ -654,7 +648,7 @@ describe "Model#new?" do
|
|
|
654
648
|
unrestrict_primary_key
|
|
655
649
|
columns :x
|
|
656
650
|
end
|
|
657
|
-
|
|
651
|
+
DB.reset
|
|
658
652
|
end
|
|
659
653
|
|
|
660
654
|
it "should be true for a new instance" do
|
|
@@ -680,11 +674,6 @@ describe Sequel::Model, "with a primary key" do
|
|
|
680
674
|
model_a.primary_key.should == :a
|
|
681
675
|
end
|
|
682
676
|
|
|
683
|
-
qspecify "should support multi argument composite keys" do
|
|
684
|
-
model_a = Class.new(Sequel::Model){ set_primary_key :a, :b }
|
|
685
|
-
model_a.primary_key.should == [:a, :b]
|
|
686
|
-
end
|
|
687
|
-
|
|
688
677
|
it "should accept single argument composite keys" do
|
|
689
678
|
model_a = Class.new(Sequel::Model){ set_primary_key [:a, :b] }
|
|
690
679
|
model_a.primary_key.should == [:a, :b]
|
|
@@ -804,52 +793,52 @@ describe Sequel::Model, "#set" do
|
|
|
804
793
|
@c.strict_param_setting = false
|
|
805
794
|
@o1 = @c.new
|
|
806
795
|
@o2 = @c.load(:id => 5)
|
|
807
|
-
|
|
796
|
+
DB.reset
|
|
808
797
|
end
|
|
809
798
|
|
|
810
799
|
it "should filter the given params using the model columns" do
|
|
811
800
|
@o1.set(:x => 1, :z => 2)
|
|
812
801
|
@o1.values.should == {:x => 1}
|
|
813
|
-
|
|
802
|
+
DB.sqls.should == []
|
|
814
803
|
|
|
815
804
|
@o2.set(:y => 1, :abc => 2)
|
|
816
805
|
@o2.values.should == {:y => 1, :id=> 5}
|
|
817
|
-
|
|
806
|
+
DB.sqls.should == []
|
|
818
807
|
end
|
|
819
808
|
|
|
820
809
|
it "should work with both strings and symbols" do
|
|
821
810
|
@o1.set('x'=> 1, 'z'=> 2)
|
|
822
811
|
@o1.values.should == {:x => 1}
|
|
823
|
-
|
|
812
|
+
DB.sqls.should == []
|
|
824
813
|
|
|
825
814
|
@o2.set('y'=> 1, 'abc'=> 2)
|
|
826
815
|
@o2.values.should == {:y => 1, :id=> 5}
|
|
827
|
-
|
|
816
|
+
DB.sqls.should == []
|
|
828
817
|
end
|
|
829
818
|
|
|
830
819
|
it "should support virtual attributes" do
|
|
831
820
|
@c.send(:define_method, :blah=){|v| self.x = v}
|
|
832
821
|
@o1.set(:blah => 333)
|
|
833
822
|
@o1.values.should == {:x => 333}
|
|
834
|
-
|
|
823
|
+
DB.sqls.should == []
|
|
835
824
|
@o1.set('blah'=> 334)
|
|
836
825
|
@o1.values.should == {:x => 334}
|
|
837
|
-
|
|
826
|
+
DB.sqls.should == []
|
|
838
827
|
end
|
|
839
828
|
|
|
840
829
|
it "should not modify the primary key" do
|
|
841
830
|
@o1.set(:x => 1, :id => 2)
|
|
842
831
|
@o1.values.should == {:x => 1}
|
|
843
|
-
|
|
832
|
+
DB.sqls.should == []
|
|
844
833
|
@o2.set('y'=> 1, 'id'=> 2)
|
|
845
834
|
@o2.values.should == {:y => 1, :id=> 5}
|
|
846
|
-
|
|
835
|
+
DB.sqls.should == []
|
|
847
836
|
end
|
|
848
837
|
|
|
849
838
|
it "should return self" do
|
|
850
839
|
returned_value = @o1.set(:x => 1, :z => 2)
|
|
851
840
|
returned_value.should == @o1
|
|
852
|
-
|
|
841
|
+
DB.sqls.should == []
|
|
853
842
|
end
|
|
854
843
|
|
|
855
844
|
it "should raise error if strict_param_setting is true and method does not exist" do
|
|
@@ -935,31 +924,31 @@ describe Sequel::Model, "#update" do
|
|
|
935
924
|
@c.strict_param_setting = false
|
|
936
925
|
@o1 = @c.new
|
|
937
926
|
@o2 = @c.load(:id => 5)
|
|
938
|
-
|
|
927
|
+
DB.reset
|
|
939
928
|
end
|
|
940
929
|
|
|
941
930
|
it "should filter the given params using the model columns" do
|
|
942
931
|
@o1.update(:x => 1, :z => 2)
|
|
943
|
-
|
|
932
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
944
933
|
|
|
945
|
-
|
|
934
|
+
DB.reset
|
|
946
935
|
@o2.update(:y => 1, :abc => 2)
|
|
947
|
-
|
|
936
|
+
DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
|
|
948
937
|
end
|
|
949
938
|
|
|
950
939
|
it "should support virtual attributes" do
|
|
951
940
|
@c.send(:define_method, :blah=){|v| self.x = v}
|
|
952
941
|
@o1.update(:blah => 333)
|
|
953
|
-
|
|
942
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
954
943
|
end
|
|
955
944
|
|
|
956
945
|
it "should not modify the primary key" do
|
|
957
946
|
@o1.update(:x => 1, :id => 2)
|
|
958
|
-
|
|
959
|
-
|
|
947
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
948
|
+
DB.reset
|
|
960
949
|
@o2.update('y'=> 1, 'id'=> 2)
|
|
961
950
|
@o2.values.should == {:y => 1, :id=> 5}
|
|
962
|
-
|
|
951
|
+
DB.sqls.should == ["UPDATE items SET y = 1 WHERE (id = 5)"]
|
|
963
952
|
end
|
|
964
953
|
end
|
|
965
954
|
|
|
@@ -970,7 +959,7 @@ describe Sequel::Model, "#set_fields" do
|
|
|
970
959
|
columns :x, :y, :z, :id
|
|
971
960
|
end
|
|
972
961
|
@o1 = @c.new
|
|
973
|
-
|
|
962
|
+
DB.reset
|
|
974
963
|
end
|
|
975
964
|
|
|
976
965
|
it "should set only the given fields" do
|
|
@@ -978,7 +967,7 @@ describe Sequel::Model, "#set_fields" do
|
|
|
978
967
|
@o1.values.should == {:x => 1, :y => 2}
|
|
979
968
|
@o1.set_fields({:x => 9, :y => 8, :z=>6, :id=>7}, [:x, :y, :id])
|
|
980
969
|
@o1.values.should == {:x => 9, :y => 8, :id=>7}
|
|
981
|
-
|
|
970
|
+
DB.sqls.should == []
|
|
982
971
|
end
|
|
983
972
|
|
|
984
973
|
it "should lookup into the hash without checking if the entry exists" do
|
|
@@ -1014,7 +1003,7 @@ describe Sequel::Model, "#set_fields" do
|
|
|
1014
1003
|
@o1.values.should == {:x => 1, :y => 2}
|
|
1015
1004
|
@o1.set_fields({:x => 9, :y => 8, :z=>6, :id=>7}, [:x, :y, :id], :missing=>:foo)
|
|
1016
1005
|
@o1.values.should == {:x => 9, :y => 8, :id=>7}
|
|
1017
|
-
|
|
1006
|
+
DB.sqls.should == []
|
|
1018
1007
|
end
|
|
1019
1008
|
|
|
1020
1009
|
it "should respect model's default_set_fields_options" do
|
|
@@ -1031,7 +1020,7 @@ describe Sequel::Model, "#set_fields" do
|
|
|
1031
1020
|
@o1.values.should == {:x => 5}
|
|
1032
1021
|
@o1.set_fields({:x => 5}, [:x, :y], :missing=>nil)
|
|
1033
1022
|
@o1.values.should == {:x => 5, :y=>nil}
|
|
1034
|
-
|
|
1023
|
+
DB.sqls.should == []
|
|
1035
1024
|
end
|
|
1036
1025
|
|
|
1037
1026
|
it "should respect model's default_set_fields_options in a subclass" do
|
|
@@ -1050,25 +1039,25 @@ describe Sequel::Model, "#update_fields" do
|
|
|
1050
1039
|
end
|
|
1051
1040
|
@c.strict_param_setting = true
|
|
1052
1041
|
@o1 = @c.load(:id=>1)
|
|
1053
|
-
|
|
1042
|
+
DB.reset
|
|
1054
1043
|
end
|
|
1055
1044
|
|
|
1056
1045
|
it "should set only the given fields, and then save the changes to the record" do
|
|
1057
1046
|
@o1.update_fields({:x => 1, :y => 2, :z=>3, :id=>4}, [:x, :y])
|
|
1058
1047
|
@o1.values.should == {:x => 1, :y => 2, :id=>1}
|
|
1059
|
-
sqls =
|
|
1048
|
+
sqls = DB.sqls
|
|
1060
1049
|
sqls.pop.should =~ /UPDATE items SET [xy] = [12], [xy] = [12] WHERE \(id = 1\)/
|
|
1061
1050
|
sqls.should == []
|
|
1062
1051
|
|
|
1063
1052
|
@o1.update_fields({:x => 1, :y => 5, :z=>6, :id=>7}, [:x, :y])
|
|
1064
1053
|
@o1.values.should == {:x => 1, :y => 5, :id=>1}
|
|
1065
|
-
|
|
1054
|
+
DB.sqls.should == ["UPDATE items SET y = 5 WHERE (id = 1)"]
|
|
1066
1055
|
end
|
|
1067
1056
|
|
|
1068
1057
|
it "should support :missing=>:skip option" do
|
|
1069
1058
|
@o1.update_fields({:x => 1, :z=>3, :id=>4}, [:x, :y], :missing=>:skip)
|
|
1070
1059
|
@o1.values.should == {:x => 1, :id=>1}
|
|
1071
|
-
|
|
1060
|
+
DB.sqls.should == ["UPDATE items SET x = 1 WHERE (id = 1)"]
|
|
1072
1061
|
end
|
|
1073
1062
|
|
|
1074
1063
|
it "should support :missing=>:raise option" do
|
|
@@ -1079,15 +1068,15 @@ describe Sequel::Model, "#update_fields" do
|
|
|
1079
1068
|
@c.default_set_fields_options = {:missing=>:skip}
|
|
1080
1069
|
@o1.update_fields({:x => 3}, [:x, :y])
|
|
1081
1070
|
@o1.values.should == {:x => 3, :id=>1}
|
|
1082
|
-
|
|
1071
|
+
DB.sqls.should == ["UPDATE items SET x = 3 WHERE (id = 1)"]
|
|
1083
1072
|
|
|
1084
1073
|
@c.default_set_fields_options = {:missing=>:raise}
|
|
1085
1074
|
proc{@o1.update_fields({:x => 3}, [:x, :y])}.should raise_error(Sequel::Error)
|
|
1086
|
-
|
|
1075
|
+
DB.sqls.should == []
|
|
1087
1076
|
end
|
|
1088
1077
|
end
|
|
1089
1078
|
|
|
1090
|
-
describe Sequel::Model, "#(set|update)_(all|
|
|
1079
|
+
describe Sequel::Model, "#(set|update)_(all|only)" do
|
|
1091
1080
|
before do
|
|
1092
1081
|
@c = Class.new(Sequel::Model(:items)) do
|
|
1093
1082
|
set_primary_key :id
|
|
@@ -1096,13 +1085,13 @@ describe Sequel::Model, "#(set|update)_(all|except|only)" do
|
|
|
1096
1085
|
end
|
|
1097
1086
|
@c.strict_param_setting = false
|
|
1098
1087
|
@o1 = @c.new
|
|
1099
|
-
|
|
1088
|
+
DB.reset
|
|
1100
1089
|
end
|
|
1101
1090
|
|
|
1102
1091
|
it "should raise errors if not all hash fields can be set and strict_param_setting is true" do
|
|
1103
1092
|
@c.strict_param_setting = true
|
|
1104
1093
|
|
|
1105
|
-
proc{@c.new.set_all(:x => 1, :y => 2, :z=>3, :
|
|
1094
|
+
proc{@c.new.set_all(:x => 1, :y => 2, :z=>3, :use_after_commit_rollback => false)}.should raise_error(Sequel::Error)
|
|
1106
1095
|
(o = @c.new).set_all(:x => 1, :y => 2, :z=>3)
|
|
1107
1096
|
o.values.should == {:x => 1, :y => 2, :z=>3}
|
|
1108
1097
|
|
|
@@ -1112,17 +1101,15 @@ describe Sequel::Model, "#(set|update)_(all|except|only)" do
|
|
|
1112
1101
|
o.values.should == {:x => 1, :y => 2}
|
|
1113
1102
|
end
|
|
1114
1103
|
|
|
1115
|
-
|
|
1116
|
-
@
|
|
1117
|
-
|
|
1118
|
-
proc{@c.new.set_except({:x => 1, :y => 2, :z=>3}, :x, :y)}.should raise_error(Sequel::Error)
|
|
1119
|
-
(o = @c.new).set_except({:z => 3}, :x, :y)
|
|
1120
|
-
o.values.should == {:z=>3}
|
|
1104
|
+
it "#set_all should set all attributes including the primary key" do
|
|
1105
|
+
@o1.set_all(:x => 1, :y => 2, :z=>3, :id=>4)
|
|
1106
|
+
@o1.values.should == {:id =>4, :x => 1, :y => 2, :z=>3}
|
|
1121
1107
|
end
|
|
1122
1108
|
|
|
1123
|
-
it "#set_all should set
|
|
1124
|
-
@o1.set_all(:x => 1, :
|
|
1125
|
-
@o1.
|
|
1109
|
+
it "#set_all should set not set restricted fields" do
|
|
1110
|
+
@o1.set_all(:x => 1, :use_after_commit_rollback => false)
|
|
1111
|
+
@o1.use_after_commit_rollback.should be_true
|
|
1112
|
+
@o1.values.should == {:x => 1}
|
|
1126
1113
|
end
|
|
1127
1114
|
|
|
1128
1115
|
it "#set_only should only set given attributes" do
|
|
@@ -1134,43 +1121,29 @@ describe Sequel::Model, "#(set|update)_(all|except|only)" do
|
|
|
1134
1121
|
@o1.values.should == {:x => 9, :y => 8, :id=>7}
|
|
1135
1122
|
end
|
|
1136
1123
|
|
|
1137
|
-
qspecify "#set_except should not set given attributes or the primary key" do
|
|
1138
|
-
@o1.set_except({:x => 1, :y => 2, :z=>3, :id=>4}, [:y, :z])
|
|
1139
|
-
@o1.values.should == {:x => 1}
|
|
1140
|
-
@o1.set_except({:x => 4, :y => 2, :z=>3, :id=>4}, :y, :z)
|
|
1141
|
-
@o1.values.should == {:x => 4}
|
|
1142
|
-
end
|
|
1143
|
-
|
|
1144
1124
|
it "#update_all should update all attributes" do
|
|
1145
|
-
@c.new.update_all(:x => 1
|
|
1146
|
-
|
|
1147
|
-
@c.new.update_all(:y => 1
|
|
1148
|
-
|
|
1149
|
-
@c.new.update_all(:z => 1
|
|
1150
|
-
|
|
1125
|
+
@c.new.update_all(:x => 1)
|
|
1126
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1127
|
+
@c.new.update_all(:y => 1)
|
|
1128
|
+
DB.sqls.should == ["INSERT INTO items (y) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1129
|
+
@c.new.update_all(:z => 1)
|
|
1130
|
+
DB.sqls.should == ["INSERT INTO items (z) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1151
1131
|
end
|
|
1152
1132
|
|
|
1153
1133
|
it "#update_only should only update given attributes" do
|
|
1154
1134
|
@o1.update_only({:x => 1, :y => 2, :z=>3, :id=>4}, [:x])
|
|
1155
|
-
|
|
1135
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1156
1136
|
@c.new.update_only({:x => 1, :y => 2, :z=>3, :id=>4}, :x)
|
|
1157
|
-
|
|
1158
|
-
end
|
|
1159
|
-
|
|
1160
|
-
qspecify "#update_except should not update given attributes" do
|
|
1161
|
-
@o1.update_except({:x => 1, :y => 2, :z=>3, :id=>4}, [:y, :z])
|
|
1162
|
-
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1163
|
-
@c.new.update_except({:x => 1, :y => 2, :z=>3, :id=>4}, :y, :z)
|
|
1164
|
-
MODEL_DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1137
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (1)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1165
1138
|
end
|
|
1166
1139
|
end
|
|
1167
1140
|
|
|
1168
1141
|
describe Sequel::Model, "#destroy with filtered dataset" do
|
|
1169
1142
|
before do
|
|
1170
|
-
@model = Class.new(Sequel::Model(
|
|
1143
|
+
@model = Class.new(Sequel::Model(DB[:items].where(:a=>1)))
|
|
1171
1144
|
@model.columns :id, :a
|
|
1172
1145
|
@instance = @model.load(:id => 1234)
|
|
1173
|
-
|
|
1146
|
+
DB.reset
|
|
1174
1147
|
end
|
|
1175
1148
|
|
|
1176
1149
|
it "should raise a NoExistingObject exception if the dataset delete call doesn't return 1" do
|
|
@@ -1190,7 +1163,7 @@ describe Sequel::Model, "#destroy with filtered dataset" do
|
|
|
1190
1163
|
|
|
1191
1164
|
it "should include WHERE clause when deleting" do
|
|
1192
1165
|
@instance.destroy
|
|
1193
|
-
|
|
1166
|
+
DB.sqls.should == ["DELETE FROM items WHERE ((a = 1) AND (id = 1234))"]
|
|
1194
1167
|
end
|
|
1195
1168
|
end
|
|
1196
1169
|
|
|
@@ -1199,7 +1172,7 @@ describe Sequel::Model, "#destroy" do
|
|
|
1199
1172
|
@model = Class.new(Sequel::Model(:items))
|
|
1200
1173
|
@model.columns :id
|
|
1201
1174
|
@instance = @model.load(:id => 1234)
|
|
1202
|
-
|
|
1175
|
+
DB.reset
|
|
1203
1176
|
end
|
|
1204
1177
|
|
|
1205
1178
|
it "should return self" do
|
|
@@ -1225,32 +1198,32 @@ describe Sequel::Model, "#destroy" do
|
|
|
1225
1198
|
it "should run within a transaction if use_transactions is true" do
|
|
1226
1199
|
@instance.use_transactions = true
|
|
1227
1200
|
@instance.destroy
|
|
1228
|
-
|
|
1201
|
+
DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
|
|
1229
1202
|
end
|
|
1230
1203
|
|
|
1231
1204
|
it "should not run within a transaction if use_transactions is false" do
|
|
1232
1205
|
@instance.use_transactions = false
|
|
1233
1206
|
@instance.destroy
|
|
1234
|
-
|
|
1207
|
+
DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
|
|
1235
1208
|
end
|
|
1236
1209
|
|
|
1237
1210
|
it "should run within a transaction if :transaction option is true" do
|
|
1238
1211
|
@instance.use_transactions = false
|
|
1239
1212
|
@instance.destroy(:transaction => true)
|
|
1240
|
-
|
|
1213
|
+
DB.sqls.should == ["BEGIN", "DELETE FROM items WHERE id = 1234", "COMMIT"]
|
|
1241
1214
|
end
|
|
1242
1215
|
|
|
1243
1216
|
it "should not run within a transaction if :transaction option is false" do
|
|
1244
1217
|
@instance.use_transactions = true
|
|
1245
1218
|
@instance.destroy(:transaction => false)
|
|
1246
|
-
|
|
1219
|
+
DB.sqls.should == ["DELETE FROM items WHERE id = 1234"]
|
|
1247
1220
|
end
|
|
1248
1221
|
|
|
1249
1222
|
it "should run before_destroy and after_destroy hooks" do
|
|
1250
|
-
@model.send(:define_method, :before_destroy){
|
|
1251
|
-
@model.send(:define_method, :after_destroy){
|
|
1223
|
+
@model.send(:define_method, :before_destroy){DB.execute('before blah')}
|
|
1224
|
+
@model.send(:define_method, :after_destroy){DB.execute('after blah')}
|
|
1252
1225
|
@instance.destroy
|
|
1253
|
-
|
|
1226
|
+
DB.sqls.should == ["before blah", "DELETE FROM items WHERE id = 1234", "after blah"]
|
|
1254
1227
|
end
|
|
1255
1228
|
end
|
|
1256
1229
|
|
|
@@ -1258,22 +1231,22 @@ describe Sequel::Model, "#exists?" do
|
|
|
1258
1231
|
before do
|
|
1259
1232
|
@model = Class.new(Sequel::Model(:items))
|
|
1260
1233
|
@model.instance_dataset._fetch = @model.dataset._fetch = proc{|sql| {:x=>1} if sql =~ /id = 1/}
|
|
1261
|
-
|
|
1234
|
+
DB.reset
|
|
1262
1235
|
end
|
|
1263
1236
|
|
|
1264
1237
|
it "should do a query to check if the record exists" do
|
|
1265
1238
|
@model.load(:id=>1).exists?.should be_true
|
|
1266
|
-
|
|
1239
|
+
DB.sqls.should == ['SELECT 1 AS one FROM items WHERE (id = 1) LIMIT 1']
|
|
1267
1240
|
end
|
|
1268
1241
|
|
|
1269
1242
|
it "should return false when #this.count == 0" do
|
|
1270
1243
|
@model.load(:id=>2).exists?.should be_false
|
|
1271
|
-
|
|
1244
|
+
DB.sqls.should == ['SELECT 1 AS one FROM items WHERE (id = 2) LIMIT 1']
|
|
1272
1245
|
end
|
|
1273
1246
|
|
|
1274
1247
|
it "should return false without issuing a query if the model object is new" do
|
|
1275
1248
|
@model.new.exists?.should be_false
|
|
1276
|
-
|
|
1249
|
+
DB.sqls.should == []
|
|
1277
1250
|
end
|
|
1278
1251
|
end
|
|
1279
1252
|
|
|
@@ -1516,7 +1489,7 @@ end
|
|
|
1516
1489
|
|
|
1517
1490
|
describe Sequel::Model, ".create" do
|
|
1518
1491
|
before do
|
|
1519
|
-
|
|
1492
|
+
DB.reset
|
|
1520
1493
|
@c = Class.new(Sequel::Model(:items)) do
|
|
1521
1494
|
unrestrict_primary_key
|
|
1522
1495
|
columns :x
|
|
@@ -1526,13 +1499,13 @@ describe Sequel::Model, ".create" do
|
|
|
1526
1499
|
it "should be able to create rows in the associated table" do
|
|
1527
1500
|
o = @c.create(:x => 1)
|
|
1528
1501
|
o.class.should == @c
|
|
1529
|
-
|
|
1502
|
+
DB.sqls.should == ['INSERT INTO items (x) VALUES (1)', "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1530
1503
|
end
|
|
1531
1504
|
|
|
1532
1505
|
it "should be able to create rows without any values specified" do
|
|
1533
1506
|
o = @c.create
|
|
1534
1507
|
o.class.should == @c
|
|
1535
|
-
|
|
1508
|
+
DB.sqls.should == ["INSERT INTO items DEFAULT VALUES", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1536
1509
|
end
|
|
1537
1510
|
|
|
1538
1511
|
it "should accept a block and call it" do
|
|
@@ -1542,14 +1515,14 @@ describe Sequel::Model, ".create" do
|
|
|
1542
1515
|
o1.should === o
|
|
1543
1516
|
o3.should === o
|
|
1544
1517
|
o2.should == :blah
|
|
1545
|
-
|
|
1518
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (333)", "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
|
|
1546
1519
|
end
|
|
1547
1520
|
|
|
1548
1521
|
it "should create a row for a model with custom primary key" do
|
|
1549
1522
|
@c.set_primary_key :x
|
|
1550
1523
|
o = @c.create(:x => 30)
|
|
1551
1524
|
o.class.should == @c
|
|
1552
|
-
|
|
1525
|
+
DB.sqls.should == ["INSERT INTO items (x) VALUES (30)", "SELECT * FROM items WHERE (x = 30) LIMIT 1"]
|
|
1553
1526
|
end
|
|
1554
1527
|
end
|
|
1555
1528
|
|
|
@@ -1559,7 +1532,7 @@ describe Sequel::Model, "#refresh" do
|
|
|
1559
1532
|
unrestrict_primary_key
|
|
1560
1533
|
columns :id, :x
|
|
1561
1534
|
end
|
|
1562
|
-
|
|
1535
|
+
DB.reset
|
|
1563
1536
|
end
|
|
1564
1537
|
|
|
1565
1538
|
specify "should reload the instance values from the database" do
|
|
@@ -1568,14 +1541,14 @@ describe Sequel::Model, "#refresh" do
|
|
|
1568
1541
|
@c.instance_dataset._fetch = @c.dataset._fetch = {:x => 'kaboom', :id => 555}
|
|
1569
1542
|
@m.refresh
|
|
1570
1543
|
@m[:x].should == 'kaboom'
|
|
1571
|
-
|
|
1544
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (id = 555) LIMIT 1"]
|
|
1572
1545
|
end
|
|
1573
1546
|
|
|
1574
1547
|
specify "should raise if the instance is not found" do
|
|
1575
1548
|
@m = @c.new(:id => 555)
|
|
1576
1549
|
@c.instance_dataset._fetch =@c.dataset._fetch = []
|
|
1577
1550
|
proc {@m.refresh}.should raise_error(Sequel::Error)
|
|
1578
|
-
|
|
1551
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (id = 555) LIMIT 1"]
|
|
1579
1552
|
end
|
|
1580
1553
|
|
|
1581
1554
|
specify "should be aliased by #reload" do
|
|
@@ -1583,7 +1556,7 @@ describe Sequel::Model, "#refresh" do
|
|
|
1583
1556
|
@c.instance_dataset._fetch =@c.dataset._fetch = {:x => 'kaboom', :id => 555}
|
|
1584
1557
|
@m.reload
|
|
1585
1558
|
@m[:x].should == 'kaboom'
|
|
1586
|
-
|
|
1559
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (id = 555) LIMIT 1"]
|
|
1587
1560
|
end
|
|
1588
1561
|
end
|
|
1589
1562
|
|
|
@@ -1593,7 +1566,8 @@ describe Sequel::Model, "typecasting" do
|
|
|
1593
1566
|
columns :x
|
|
1594
1567
|
end
|
|
1595
1568
|
@c.db_schema = {:x=>{:type=>:integer}}
|
|
1596
|
-
|
|
1569
|
+
@c.raise_on_typecast_failure = true
|
|
1570
|
+
DB.reset
|
|
1597
1571
|
end
|
|
1598
1572
|
|
|
1599
1573
|
after do
|
|
@@ -1952,7 +1926,7 @@ describe "Model#lock!" do
|
|
|
1952
1926
|
columns :id
|
|
1953
1927
|
end
|
|
1954
1928
|
@c.dataset._fetch = {:id=>1}
|
|
1955
|
-
|
|
1929
|
+
DB.reset
|
|
1956
1930
|
end
|
|
1957
1931
|
|
|
1958
1932
|
it "should do nothing if the record is a new record" do
|
|
@@ -1960,7 +1934,7 @@ describe "Model#lock!" do
|
|
|
1960
1934
|
def o._refresh(x) raise Sequel::Error; super(x) end
|
|
1961
1935
|
x = o.lock!
|
|
1962
1936
|
x.should == o
|
|
1963
|
-
|
|
1937
|
+
DB.sqls.should == []
|
|
1964
1938
|
end
|
|
1965
1939
|
|
|
1966
1940
|
it "should refresh the record using for_update if it is not a new record" do
|
|
@@ -1969,7 +1943,7 @@ describe "Model#lock!" do
|
|
|
1969
1943
|
x = o.lock!
|
|
1970
1944
|
x.should == o
|
|
1971
1945
|
o.instance_variable_get(:@a).should == 1
|
|
1972
|
-
|
|
1946
|
+
DB.sqls.should == ["SELECT * FROM items WHERE (id = 1) LIMIT 1 FOR UPDATE"]
|
|
1973
1947
|
end
|
|
1974
1948
|
end
|
|
1975
1949
|
|