sequel 3.47.0 → 3.48.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +230 -0
- data/README.rdoc +31 -40
- data/Rakefile +1 -14
- data/doc/active_record.rdoc +29 -29
- data/doc/association_basics.rdoc +4 -13
- data/doc/cheat_sheet.rdoc +8 -6
- data/doc/code_order.rdoc +89 -0
- data/doc/core_extensions.rdoc +3 -3
- data/doc/dataset_basics.rdoc +7 -8
- data/doc/dataset_filtering.rdoc +7 -2
- data/doc/mass_assignment.rdoc +2 -3
- data/doc/migration.rdoc +8 -8
- data/doc/model_hooks.rdoc +11 -7
- data/doc/object_model.rdoc +2 -2
- data/doc/opening_databases.rdoc +5 -14
- data/doc/prepared_statements.rdoc +5 -9
- data/doc/querying.rdoc +23 -28
- data/doc/reflection.rdoc +11 -0
- data/doc/release_notes/3.48.0.txt +477 -0
- data/doc/schema_modification.rdoc +12 -5
- data/doc/security.rdoc +2 -2
- data/doc/sharding.rdoc +1 -2
- data/doc/sql.rdoc +10 -13
- data/doc/testing.rdoc +8 -4
- data/doc/transactions.rdoc +2 -2
- data/doc/validations.rdoc +40 -17
- data/doc/virtual_rows.rdoc +2 -2
- data/lib/sequel/adapters/ado.rb +25 -20
- data/lib/sequel/adapters/ado/access.rb +1 -0
- data/lib/sequel/adapters/ado/mssql.rb +1 -0
- data/lib/sequel/adapters/db2.rb +9 -7
- data/lib/sequel/adapters/dbi.rb +16 -16
- data/lib/sequel/adapters/do.rb +17 -18
- data/lib/sequel/adapters/do/mysql.rb +1 -0
- data/lib/sequel/adapters/do/postgres.rb +2 -0
- data/lib/sequel/adapters/do/sqlite.rb +1 -0
- data/lib/sequel/adapters/firebird.rb +5 -7
- data/lib/sequel/adapters/ibmdb.rb +23 -20
- data/lib/sequel/adapters/informix.rb +8 -2
- data/lib/sequel/adapters/jdbc.rb +39 -35
- data/lib/sequel/adapters/jdbc/as400.rb +1 -0
- data/lib/sequel/adapters/jdbc/cubrid.rb +1 -0
- data/lib/sequel/adapters/jdbc/db2.rb +1 -0
- data/lib/sequel/adapters/jdbc/derby.rb +1 -0
- data/lib/sequel/adapters/jdbc/firebird.rb +1 -0
- data/lib/sequel/adapters/jdbc/h2.rb +1 -0
- data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -0
- data/lib/sequel/adapters/jdbc/informix.rb +1 -0
- data/lib/sequel/adapters/jdbc/jtds.rb +1 -0
- data/lib/sequel/adapters/jdbc/mssql.rb +1 -0
- data/lib/sequel/adapters/jdbc/mysql.rb +1 -0
- data/lib/sequel/adapters/jdbc/oracle.rb +1 -0
- data/lib/sequel/adapters/jdbc/postgresql.rb +2 -0
- data/lib/sequel/adapters/jdbc/progress.rb +1 -0
- data/lib/sequel/adapters/jdbc/sqlite.rb +1 -0
- data/lib/sequel/adapters/jdbc/sqlserver.rb +1 -0
- data/lib/sequel/adapters/mock.rb +30 -31
- data/lib/sequel/adapters/mysql.rb +6 -7
- data/lib/sequel/adapters/mysql2.rb +5 -6
- data/lib/sequel/adapters/odbc.rb +22 -20
- data/lib/sequel/adapters/odbc/mssql.rb +1 -0
- data/lib/sequel/adapters/openbase.rb +4 -1
- data/lib/sequel/adapters/oracle.rb +10 -8
- data/lib/sequel/adapters/postgres.rb +12 -10
- data/lib/sequel/adapters/shared/access.rb +6 -0
- data/lib/sequel/adapters/shared/cubrid.rb +2 -0
- data/lib/sequel/adapters/shared/db2.rb +2 -0
- data/lib/sequel/adapters/shared/firebird.rb +2 -0
- data/lib/sequel/adapters/shared/informix.rb +2 -0
- data/lib/sequel/adapters/shared/mssql.rb +14 -8
- data/lib/sequel/adapters/shared/mysql.rb +6 -0
- data/lib/sequel/adapters/shared/oracle.rb +2 -0
- data/lib/sequel/adapters/shared/postgres.rb +14 -4
- data/lib/sequel/adapters/shared/progress.rb +1 -0
- data/lib/sequel/adapters/shared/sqlite.rb +4 -3
- data/lib/sequel/adapters/sqlite.rb +6 -7
- data/lib/sequel/adapters/swift.rb +20 -21
- data/lib/sequel/adapters/swift/mysql.rb +1 -0
- data/lib/sequel/adapters/swift/postgres.rb +2 -0
- data/lib/sequel/adapters/swift/sqlite.rb +1 -0
- data/lib/sequel/adapters/tinytds.rb +5 -6
- data/lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb +68 -0
- data/lib/sequel/connection_pool.rb +1 -1
- data/lib/sequel/core.rb +57 -50
- data/lib/sequel/database/connecting.rb +9 -10
- data/lib/sequel/database/dataset.rb +11 -6
- data/lib/sequel/database/dataset_defaults.rb +61 -69
- data/lib/sequel/database/features.rb +21 -0
- data/lib/sequel/database/misc.rb +23 -3
- data/lib/sequel/database/query.rb +13 -7
- data/lib/sequel/database/schema_methods.rb +6 -6
- data/lib/sequel/database/transactions.rb +1 -0
- data/lib/sequel/dataset/actions.rb +51 -38
- data/lib/sequel/dataset/features.rb +1 -0
- data/lib/sequel/dataset/graph.rb +9 -33
- data/lib/sequel/dataset/misc.rb +30 -5
- data/lib/sequel/dataset/mutation.rb +2 -3
- data/lib/sequel/dataset/prepared_statements.rb +1 -1
- data/lib/sequel/dataset/query.rb +91 -27
- data/lib/sequel/dataset/sql.rb +40 -6
- data/lib/sequel/deprecated.rb +74 -0
- data/lib/sequel/deprecated_core_extensions.rb +135 -0
- data/lib/sequel/extensions/columns_introspection.rb +1 -5
- data/lib/sequel/extensions/core_extensions.rb +10 -3
- data/lib/sequel/extensions/date_arithmetic.rb +1 -0
- data/lib/sequel/extensions/empty_array_ignore_nulls.rb +33 -0
- data/lib/sequel/extensions/filter_having.rb +58 -0
- data/lib/sequel/extensions/graph_each.rb +63 -0
- data/lib/sequel/extensions/hash_aliases.rb +44 -0
- data/lib/sequel/extensions/looser_typecasting.rb +14 -3
- data/lib/sequel/extensions/migration.rb +2 -3
- data/lib/sequel/extensions/named_timezones.rb +14 -1
- data/lib/sequel/extensions/null_dataset.rb +7 -1
- data/lib/sequel/extensions/pagination.rb +15 -5
- data/lib/sequel/extensions/pg_auto_parameterize.rb +1 -0
- data/lib/sequel/extensions/pg_hstore_ops.rb +48 -14
- data/lib/sequel/extensions/pg_json.rb +7 -7
- data/lib/sequel/extensions/pg_range_ops.rb +8 -2
- data/lib/sequel/extensions/pg_statement_cache.rb +1 -0
- data/lib/sequel/extensions/pretty_table.rb +13 -4
- data/lib/sequel/extensions/query.rb +21 -4
- data/lib/sequel/extensions/ruby18_symbol_extensions.rb +22 -0
- data/lib/sequel/extensions/schema_caching.rb +10 -7
- data/lib/sequel/extensions/schema_dumper.rb +35 -48
- data/lib/sequel/extensions/select_remove.rb +13 -4
- data/lib/sequel/extensions/sequel_3_dataset_methods.rb +117 -0
- data/lib/sequel/extensions/set_overrides.rb +43 -0
- data/lib/sequel/extensions/to_dot.rb +6 -0
- data/lib/sequel/model.rb +12 -6
- data/lib/sequel/model/associations.rb +80 -38
- data/lib/sequel/model/base.rb +137 -52
- data/lib/sequel/model/errors.rb +7 -2
- data/lib/sequel/plugins/active_model.rb +13 -0
- data/lib/sequel/plugins/after_initialize.rb +43 -0
- data/lib/sequel/plugins/association_proxies.rb +63 -7
- data/lib/sequel/plugins/auto_validations.rb +56 -16
- data/lib/sequel/plugins/blacklist_security.rb +63 -0
- data/lib/sequel/plugins/class_table_inheritance.rb +9 -0
- data/lib/sequel/plugins/constraint_validations.rb +50 -8
- data/lib/sequel/plugins/dataset_associations.rb +2 -0
- data/lib/sequel/plugins/hook_class_methods.rb +7 -1
- data/lib/sequel/plugins/identity_map.rb +4 -0
- data/lib/sequel/plugins/json_serializer.rb +32 -13
- data/lib/sequel/plugins/optimistic_locking.rb +1 -1
- data/lib/sequel/plugins/rcte_tree.rb +4 -4
- data/lib/sequel/plugins/scissors.rb +33 -0
- data/lib/sequel/plugins/serialization.rb +1 -1
- data/lib/sequel/plugins/single_table_inheritance.rb +6 -0
- data/lib/sequel/plugins/tree.rb +5 -1
- data/lib/sequel/plugins/validation_class_methods.rb +2 -1
- data/lib/sequel/plugins/validation_helpers.rb +15 -11
- data/lib/sequel/plugins/xml_serializer.rb +12 -3
- data/lib/sequel/sql.rb +12 -2
- data/lib/sequel/timezones.rb +1 -1
- data/lib/sequel/version.rb +1 -1
- data/lib/sequel_core.rb +1 -0
- data/lib/sequel_model.rb +1 -0
- data/spec/adapters/mssql_spec.rb +24 -57
- data/spec/adapters/postgres_spec.rb +27 -55
- data/spec/adapters/spec_helper.rb +1 -1
- data/spec/adapters/sqlite_spec.rb +1 -1
- data/spec/bin_spec.rb +251 -0
- data/spec/core/database_spec.rb +46 -32
- data/spec/core/dataset_spec.rb +233 -181
- data/spec/core/deprecated_spec.rb +78 -0
- data/spec/core/expression_filters_spec.rb +3 -4
- data/spec/core/mock_adapter_spec.rb +9 -9
- data/spec/core/object_graph_spec.rb +9 -19
- data/spec/core/schema_spec.rb +3 -1
- data/spec/core/spec_helper.rb +19 -0
- data/spec/core_extensions_spec.rb +80 -30
- data/spec/extensions/after_initialize_spec.rb +24 -0
- data/spec/extensions/association_proxies_spec.rb +37 -1
- data/spec/extensions/auto_validations_spec.rb +20 -4
- data/spec/extensions/blacklist_security_spec.rb +87 -0
- data/spec/extensions/boolean_readers_spec.rb +2 -1
- data/spec/extensions/class_table_inheritance_spec.rb +7 -0
- data/spec/extensions/columns_introspection_spec.rb +3 -3
- data/spec/extensions/constraint_validations_plugin_spec.rb +83 -5
- data/spec/extensions/core_refinements_spec.rb +7 -7
- data/spec/extensions/dataset_associations_spec.rb +2 -2
- data/spec/extensions/date_arithmetic_spec.rb +1 -1
- data/spec/extensions/defaults_setter_spec.rb +2 -1
- data/spec/extensions/empty_array_ignore_nulls_spec.rb +24 -0
- data/spec/extensions/filter_having_spec.rb +40 -0
- data/spec/extensions/graph_each_spec.rb +109 -0
- data/spec/extensions/hash_aliases_spec.rb +16 -0
- data/spec/extensions/hook_class_methods_spec.rb +2 -2
- data/spec/extensions/identity_map_spec.rb +3 -3
- data/spec/extensions/json_serializer_spec.rb +19 -19
- data/spec/extensions/lazy_attributes_spec.rb +1 -0
- data/spec/extensions/list_spec.rb +13 -13
- data/spec/extensions/looser_typecasting_spec.rb +10 -3
- data/spec/extensions/many_through_many_spec.rb +1 -1
- data/spec/extensions/migration_spec.rb +7 -7
- data/spec/extensions/named_timezones_spec.rb +6 -0
- data/spec/extensions/nested_attributes_spec.rb +2 -2
- data/spec/extensions/null_dataset_spec.rb +1 -1
- data/spec/extensions/pagination_spec.rb +2 -2
- data/spec/extensions/pg_hstore_ops_spec.rb +75 -0
- data/spec/extensions/pg_range_ops_spec.rb +4 -2
- data/spec/extensions/pg_row_plugin_spec.rb +1 -1
- data/spec/extensions/pretty_table_spec.rb +1 -1
- data/spec/extensions/query_literals_spec.rb +1 -1
- data/spec/extensions/query_spec.rb +3 -3
- data/spec/extensions/schema_caching_spec.rb +3 -3
- data/spec/extensions/schema_dumper_spec.rb +27 -2
- data/spec/extensions/schema_spec.rb +2 -2
- data/spec/extensions/scissors_spec.rb +26 -0
- data/spec/extensions/select_remove_spec.rb +1 -1
- data/spec/extensions/sequel_3_dataset_methods_spec.rb +102 -0
- data/spec/extensions/set_overrides_spec.rb +45 -0
- data/spec/extensions/single_table_inheritance_spec.rb +10 -0
- data/spec/extensions/spec_helper.rb +24 -1
- data/spec/extensions/static_cache_spec.rb +1 -1
- data/spec/extensions/string_stripper_spec.rb +2 -1
- data/spec/extensions/to_dot_spec.rb +1 -1
- data/spec/extensions/typecast_on_load_spec.rb +3 -2
- data/spec/extensions/update_primary_key_spec.rb +2 -2
- data/spec/extensions/validation_class_methods_spec.rb +19 -19
- data/spec/extensions/validation_helpers_spec.rb +30 -21
- data/spec/extensions/xml_serializer_spec.rb +5 -5
- data/spec/integration/associations_test.rb +10 -30
- data/spec/integration/dataset_test.rb +20 -24
- data/spec/integration/eager_loader_test.rb +5 -5
- data/spec/integration/model_test.rb +3 -3
- data/spec/integration/plugin_test.rb +7 -39
- data/spec/integration/schema_test.rb +4 -38
- data/spec/integration/spec_helper.rb +2 -1
- data/spec/model/association_reflection_spec.rb +70 -5
- data/spec/model/associations_spec.rb +11 -11
- data/spec/model/base_spec.rb +25 -8
- data/spec/model/class_dataset_methods_spec.rb +143 -0
- data/spec/model/dataset_methods_spec.rb +1 -1
- data/spec/model/eager_loading_spec.rb +25 -25
- data/spec/model/hooks_spec.rb +1 -1
- data/spec/model/model_spec.rb +22 -7
- data/spec/model/plugins_spec.rb +1 -6
- data/spec/model/record_spec.rb +37 -29
- data/spec/model/spec_helper.rb +23 -1
- data/spec/model/validations_spec.rb +15 -17
- metadata +32 -3
data/spec/core/database_spec.rb
CHANGED
@@ -81,7 +81,7 @@ describe "A new Database" do
|
|
81
81
|
|
82
82
|
specify "should respect the :identifier_input_method option" do
|
83
83
|
Sequel.identifier_input_method = nil
|
84
|
-
Sequel::Database.identifier_input_method.should ==
|
84
|
+
Sequel::Database.identifier_input_method.should == false
|
85
85
|
db = Sequel::Database.new(:identifier_input_method=>nil)
|
86
86
|
db.identifier_input_method.should be_nil
|
87
87
|
db.identifier_input_method = :downcase
|
@@ -104,7 +104,7 @@ describe "A new Database" do
|
|
104
104
|
|
105
105
|
specify "should respect the :identifier_output_method option" do
|
106
106
|
Sequel.identifier_output_method = nil
|
107
|
-
Sequel::Database.identifier_output_method.should ==
|
107
|
+
Sequel::Database.identifier_output_method.should == false
|
108
108
|
db = Sequel::Database.new(:identifier_output_method=>nil)
|
109
109
|
db.identifier_output_method.should be_nil
|
110
110
|
db.identifier_output_method = :downcase
|
@@ -169,7 +169,7 @@ describe "A new Database" do
|
|
169
169
|
|
170
170
|
specify "should respect the identifier_input_method_default method" do
|
171
171
|
class Sequel::Database
|
172
|
-
|
172
|
+
@identifier_input_method = nil
|
173
173
|
end
|
174
174
|
x = Class.new(Sequel::Database){def identifier_input_method_default; :downcase end}
|
175
175
|
x.new({}).identifier_input_method.should == :downcase
|
@@ -179,7 +179,7 @@ describe "A new Database" do
|
|
179
179
|
|
180
180
|
specify "should respect the identifier_output_method_default method if Sequel.identifier_output_method is not called" do
|
181
181
|
class Sequel::Database
|
182
|
-
|
182
|
+
@identifier_output_method = nil
|
183
183
|
end
|
184
184
|
x = Class.new(Sequel::Database){def identifier_output_method_default; :upcase end}
|
185
185
|
x.new({}).identifier_output_method.should == :upcase
|
@@ -227,7 +227,7 @@ describe "Sequel.extension" do
|
|
227
227
|
end
|
228
228
|
|
229
229
|
describe "Database#connect" do
|
230
|
-
|
230
|
+
qspecify "should raise Sequel::NotImplemented" do
|
231
231
|
proc {Sequel::Database.new.connect(:default)}.should raise_error(Sequel::NotImplemented)
|
232
232
|
end
|
233
233
|
end
|
@@ -505,31 +505,31 @@ describe "Database#valid_connection?" do
|
|
505
505
|
end
|
506
506
|
|
507
507
|
describe "Database#execute" do
|
508
|
-
|
508
|
+
qspecify "should raise Sequel::NotImplemented" do
|
509
509
|
proc {Sequel::Database.new.execute('blah blah')}.should raise_error(Sequel::NotImplemented)
|
510
510
|
end
|
511
511
|
end
|
512
512
|
|
513
513
|
describe "Database#tables" do
|
514
|
-
|
514
|
+
qspecify "should raise Sequel::NotImplemented" do
|
515
515
|
proc {Sequel::Database.new.tables}.should raise_error(Sequel::NotImplemented)
|
516
516
|
end
|
517
517
|
end
|
518
518
|
|
519
519
|
describe "Database#views" do
|
520
|
-
|
520
|
+
qspecify "should raise Sequel::NotImplemented" do
|
521
521
|
proc {Sequel::Database.new.views}.should raise_error(Sequel::NotImplemented)
|
522
522
|
end
|
523
523
|
end
|
524
524
|
|
525
525
|
describe "Database#indexes" do
|
526
|
-
|
526
|
+
qspecify "should raise Sequel::NotImplemented" do
|
527
527
|
proc {Sequel::Database.new.indexes(:table)}.should raise_error(Sequel::NotImplemented)
|
528
528
|
end
|
529
529
|
end
|
530
530
|
|
531
531
|
describe "Database#foreign_key_list" do
|
532
|
-
|
532
|
+
qspecify "should raise Sequel::NotImplemented" do
|
533
533
|
proc {Sequel::Database.new.foreign_key_list(:table)}.should raise_error(Sequel::NotImplemented)
|
534
534
|
end
|
535
535
|
end
|
@@ -611,7 +611,8 @@ describe "Database#test_connection" do
|
|
611
611
|
end
|
612
612
|
|
613
613
|
specify "should raise an error if the attempting to connect raises an error" do
|
614
|
-
|
614
|
+
def @db.connect(*) raise Sequel::Error end
|
615
|
+
proc{@db.test_connection}.should raise_error(Sequel::Error)
|
615
616
|
end
|
616
617
|
end
|
617
618
|
|
@@ -619,7 +620,7 @@ describe "Database#table_exists?" do
|
|
619
620
|
specify "should test existence by selecting a row from the table's dataset" do
|
620
621
|
db = Sequel.mock(:fetch=>[Sequel::Error, [], [{:a=>1}]])
|
621
622
|
db.table_exists?(:a).should be_false
|
622
|
-
db.sqls.should == ["SELECT NULL FROM a LIMIT 1"]
|
623
|
+
db.sqls.should == ["SELECT NULL AS nil FROM a LIMIT 1"]
|
623
624
|
db.table_exists?(:b).should be_true
|
624
625
|
db.table_exists?(:c).should be_true
|
625
626
|
end
|
@@ -654,7 +655,7 @@ shared_examples_for "Database#transaction" do
|
|
654
655
|
'BEGIN', 'SET TRANSACTION ISOLATION LEVEL SERIALIZABLE', 'DROP TABLE serializable', 'COMMIT']
|
655
656
|
end
|
656
657
|
|
657
|
-
|
658
|
+
qspecify "should support :disconnect=>:retry option for automatically retrying on disconnect" do
|
658
659
|
a = []
|
659
660
|
@db.transaction(:disconnect=>:retry){a << 1; raise Sequel::DatabaseDisconnectError if a.length < 2}
|
660
661
|
@db.sqls.should == ['BEGIN', 'ROLLBACK', 'BEGIN', 'COMMIT']
|
@@ -703,14 +704,12 @@ shared_examples_for "Database#transaction" do
|
|
703
704
|
a.should == [1] * 100
|
704
705
|
end
|
705
706
|
|
706
|
-
|
707
|
+
qspecify "should raise an error if using :disconnect=>:retry and :retry_on together" do
|
707
708
|
proc{@db.transaction(:disconnect=>:retry, :retry_on=>Sequel::ConstraintViolation){}}.should raise_error(Sequel::Error)
|
708
709
|
@db.sqls.should == []
|
709
710
|
end
|
710
711
|
|
711
712
|
specify "should raise an error if attempting to use :disconnect=>:retry or :retry_on inside another transaction" do
|
712
|
-
proc{@db.transaction{@db.transaction(:disconnect=>:retry){}}}.should raise_error(Sequel::Error)
|
713
|
-
@db.sqls.should == ['BEGIN', 'ROLLBACK']
|
714
713
|
proc{@db.transaction{@db.transaction(:retry_on=>Sequel::ConstraintViolation){}}}.should raise_error(Sequel::Error)
|
715
714
|
@db.sqls.should == ['BEGIN', 'ROLLBACK']
|
716
715
|
end
|
@@ -1145,7 +1144,7 @@ end
|
|
1145
1144
|
|
1146
1145
|
describe "A Database adapter with a scheme" do
|
1147
1146
|
before do
|
1148
|
-
@ccc = Class.new(Sequel::Database)
|
1147
|
+
@ccc = Class.new(Sequel::Mock::Database)
|
1149
1148
|
@ccc.send(:set_adapter_scheme, :ccc)
|
1150
1149
|
end
|
1151
1150
|
|
@@ -1210,6 +1209,7 @@ describe "A Database adapter with a scheme" do
|
|
1210
1209
|
|
1211
1210
|
# invalid parameters
|
1212
1211
|
proc {Sequel.ccc('abc', 'def')}.should raise_error(Sequel::Error)
|
1212
|
+
proc {Sequel.ccc(1)}.should raise_error(Sequel::Error)
|
1213
1213
|
|
1214
1214
|
c = Sequel.ccc('mydb')
|
1215
1215
|
c.should be_a_kind_of(@ccc)
|
@@ -1266,6 +1266,7 @@ describe "A Database adapter with a scheme" do
|
|
1266
1266
|
end
|
1267
1267
|
|
1268
1268
|
specify "should test the connection if test parameter is truthy" do
|
1269
|
+
@ccc.send(:define_method, :connect){}
|
1269
1270
|
proc{Sequel.connect 'ccc:///d%5bb%5d?test=t'}.should raise_error(Sequel::DatabaseConnectionError)
|
1270
1271
|
proc{Sequel.connect 'ccc:///d%5bb%5d?test=1'}.should raise_error(Sequel::DatabaseConnectionError)
|
1271
1272
|
proc{Sequel.connect 'ccc:///d%5bb%5d', :test=>true}.should raise_error(Sequel::DatabaseConnectionError)
|
@@ -1365,12 +1366,14 @@ describe "A single threaded database" do
|
|
1365
1366
|
end
|
1366
1367
|
|
1367
1368
|
specify "should convert an Exception on connection into a DatabaseConnectionError" do
|
1368
|
-
db = Sequel::Database.new(:single_threaded => true, :servers=>{})
|
1369
|
+
db = Sequel::Database.new(:single_threaded => true, :servers=>{})
|
1370
|
+
def db.connect(*) raise Exception end
|
1369
1371
|
proc {db.pool.hold {|c|}}.should raise_error(Sequel::DatabaseConnectionError)
|
1370
1372
|
end
|
1371
1373
|
|
1372
1374
|
specify "should raise a DatabaseConnectionError if the connection proc returns nil" do
|
1373
|
-
db = Sequel
|
1375
|
+
db = Sequel.mock(:single_threaded => true, :servers=>{})
|
1376
|
+
def db.connect(*) end
|
1374
1377
|
proc {db.pool.hold {|c|}}.should raise_error(Sequel::DatabaseConnectionError)
|
1375
1378
|
end
|
1376
1379
|
end
|
@@ -1504,19 +1507,24 @@ describe "Database#get" do
|
|
1504
1507
|
end
|
1505
1508
|
|
1506
1509
|
specify "should use Dataset#get to get a single value" do
|
1507
|
-
@db.get(
|
1508
|
-
@db.sqls.should == ['SELECT
|
1510
|
+
@db.get(:a).should == 1
|
1511
|
+
@db.sqls.should == ['SELECT a LIMIT 1']
|
1509
1512
|
|
1510
|
-
@db.get(Sequel.function(:version))
|
1511
|
-
@db.sqls.should == ['SELECT version() LIMIT 1']
|
1513
|
+
@db.get(Sequel.function(:version).as(:version))
|
1514
|
+
@db.sqls.should == ['SELECT version() AS version LIMIT 1']
|
1512
1515
|
end
|
1513
1516
|
|
1514
1517
|
specify "should accept a block" do
|
1515
|
-
@db.get{
|
1516
|
-
@db.sqls.should == ['SELECT
|
1518
|
+
@db.get{a}
|
1519
|
+
@db.sqls.should == ['SELECT a LIMIT 1']
|
1517
1520
|
|
1518
|
-
@db.get{version(
|
1519
|
-
@db.sqls.should == ['SELECT version(
|
1521
|
+
@db.get{version(a).as(version)}
|
1522
|
+
@db.sqls.should == ['SELECT version(a) AS version LIMIT 1']
|
1523
|
+
end
|
1524
|
+
|
1525
|
+
qspecify "should work when an alias cannot be determined" do
|
1526
|
+
@db.get(1).should == 1
|
1527
|
+
@db.sqls.should == ['SELECT 1 LIMIT 1']
|
1520
1528
|
end
|
1521
1529
|
end
|
1522
1530
|
|
@@ -2033,6 +2041,12 @@ describe "Database#typecast_value" do
|
|
2033
2041
|
end
|
2034
2042
|
end
|
2035
2043
|
|
2044
|
+
qspecify "should typecast hash and array values to String" do
|
2045
|
+
[[], {}].each do |i|
|
2046
|
+
@db.typecast_value(:string, i).should be_an_instance_of(String)
|
2047
|
+
end
|
2048
|
+
end
|
2049
|
+
|
2036
2050
|
specify "should typecast time values to SQLTime" do
|
2037
2051
|
t = Time.now
|
2038
2052
|
st = Sequel::SQLTime.local(t.year, t.month, t.day, 1, 2, 3)
|
@@ -2360,15 +2374,15 @@ describe "Database specific exception classes" do
|
|
2360
2374
|
specify "should use appropriate exception classes for given SQL states" do
|
2361
2375
|
@db.fetch = ArgumentError
|
2362
2376
|
@db.sql_state = '23502'
|
2363
|
-
proc{@db.get(
|
2377
|
+
proc{@db.get(:a)}.should raise_error(Sequel::NotNullConstraintViolation)
|
2364
2378
|
@db.sql_state = '23503'
|
2365
|
-
proc{@db.get(
|
2379
|
+
proc{@db.get(:a)}.should raise_error(Sequel::ForeignKeyConstraintViolation)
|
2366
2380
|
@db.sql_state = '23505'
|
2367
|
-
proc{@db.get(
|
2381
|
+
proc{@db.get(:a)}.should raise_error(Sequel::UniqueConstraintViolation)
|
2368
2382
|
@db.sql_state = '23513'
|
2369
|
-
proc{@db.get(
|
2383
|
+
proc{@db.get(:a)}.should raise_error(Sequel::CheckConstraintViolation)
|
2370
2384
|
@db.sql_state = '40001'
|
2371
|
-
proc{@db.get(
|
2385
|
+
proc{@db.get(:a)}.should raise_error(Sequel::SerializationFailure)
|
2372
2386
|
end
|
2373
2387
|
end
|
2374
2388
|
|
data/spec/core/dataset_spec.rb
CHANGED
@@ -2,16 +2,19 @@ require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
|
|
2
2
|
|
3
3
|
describe "Dataset" do
|
4
4
|
before do
|
5
|
-
@dataset = Sequel
|
5
|
+
@dataset = Sequel.mock.dataset
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
qspecify "should accept database and opts in initialize" do
|
9
9
|
db = "db"
|
10
10
|
opts = {:from => :test}
|
11
11
|
d = Sequel::Dataset.new(db, opts)
|
12
12
|
d.db.should be(db)
|
13
13
|
d.opts.should be(opts)
|
14
|
+
end
|
14
15
|
|
16
|
+
specify "should accept database in initialize" do
|
17
|
+
db = "db"
|
15
18
|
d = Sequel::Dataset.new(db)
|
16
19
|
d.db.should be(db)
|
17
20
|
d.opts.should be_a_kind_of(Hash)
|
@@ -108,7 +111,7 @@ end
|
|
108
111
|
|
109
112
|
describe "Dataset#clone" do
|
110
113
|
before do
|
111
|
-
@dataset = Sequel
|
114
|
+
@dataset = Sequel.mock.dataset.from(:items)
|
112
115
|
end
|
113
116
|
|
114
117
|
specify "should create an exact copy of the dataset" do
|
@@ -207,7 +210,7 @@ end
|
|
207
210
|
|
208
211
|
describe "A simple dataset" do
|
209
212
|
before do
|
210
|
-
@dataset = Sequel
|
213
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
211
214
|
end
|
212
215
|
|
213
216
|
specify "should format a select statement" do
|
@@ -283,7 +286,7 @@ end
|
|
283
286
|
|
284
287
|
describe "A dataset with multiple tables in its FROM clause" do
|
285
288
|
before do
|
286
|
-
@dataset = Sequel
|
289
|
+
@dataset = Sequel.mock.dataset.from(:t1, :t2)
|
287
290
|
end
|
288
291
|
|
289
292
|
specify "should raise on #update_sql" do
|
@@ -309,7 +312,7 @@ end
|
|
309
312
|
|
310
313
|
describe "Dataset#unused_table_alias" do
|
311
314
|
before do
|
312
|
-
@ds = Sequel
|
315
|
+
@ds = Sequel.mock.dataset.from(:test)
|
313
316
|
end
|
314
317
|
|
315
318
|
specify "should return given symbol if it hasn't already been used" do
|
@@ -409,6 +412,21 @@ describe "Dataset#where" do
|
|
409
412
|
@dataset.where('price < :price AND id in :ids', :price=>100).select_sql.should == "SELECT * FROM test WHERE (price < 100 AND id in :ids)"
|
410
413
|
end
|
411
414
|
|
415
|
+
qspecify "should handle a mismatched number of placeholders" do
|
416
|
+
@dataset.where('price < ? AND id in ?', 100).select_sql.should == "SELECT * FROM test WHERE (price < 100 AND id in NULL)"
|
417
|
+
@dataset.where('price < ? AND id in ?', 100, [1, 2, 3], 4).select_sql.should == "SELECT * FROM test WHERE (price < 100 AND id in (1, 2, 3))"
|
418
|
+
end
|
419
|
+
|
420
|
+
specify "should handle placeholders when using an array" do
|
421
|
+
@dataset.where(Sequel.lit(['price < ', ' AND id in '], 100, [1, 2, 3])).select_sql.should == "SELECT * FROM test WHERE price < 100 AND id in (1, 2, 3)"
|
422
|
+
@dataset.where(Sequel.lit(['price < ', ' AND id in '], 100)).select_sql.should == "SELECT * FROM test WHERE price < 100 AND id in "
|
423
|
+
end
|
424
|
+
|
425
|
+
qspecify "should handle a mismatched number of placeholders when using an array" do
|
426
|
+
@dataset.where(Sequel.lit(['a = ', ' AND price < ', ' AND id in '], 100)).select_sql.should == "SELECT * FROM test WHERE a = 100 AND price < AND id in NULL"
|
427
|
+
@dataset.where(Sequel.lit(['price < ', ' AND id in '], 100, [1, 2, 3], 4)).select_sql.should == "SELECT * FROM test WHERE price < 100 AND id in (1, 2, 3)"
|
428
|
+
end
|
429
|
+
|
412
430
|
specify "should handle partial names" do
|
413
431
|
@dataset.where('price < :price AND id = :p', :p=>2, :price=>100).select_sql.should == "SELECT * FROM test WHERE (price < 100 AND id = 2)"
|
414
432
|
end
|
@@ -467,7 +485,7 @@ describe "Dataset#where" do
|
|
467
485
|
@dataset.exclude([:id1, :id2] => []).sql.should == "SELECT * FROM test WHERE ((id1 = id1) AND (id2 = id2))"
|
468
486
|
end
|
469
487
|
|
470
|
-
|
488
|
+
qspecify "should handle all types of IN/NOT IN queries with empty arrays when empty_array_handle_nulls is false" do
|
471
489
|
begin
|
472
490
|
Sequel.empty_array_handle_nulls = false
|
473
491
|
@dataset.filter(:id => []).sql.should == "SELECT * FROM test WHERE (1 = 0)"
|
@@ -521,7 +539,7 @@ describe "Dataset#where" do
|
|
521
539
|
db.sqls.should == ["SELECT id1, id2 FROM test WHERE (region = 'Asia')"]
|
522
540
|
end
|
523
541
|
|
524
|
-
|
542
|
+
qspecify "should handle IN/NOT IN queries with multiple columns and an empty dataset where the database doesn't support it when empty_array_handle nulls is true" do
|
525
543
|
begin
|
526
544
|
Sequel.empty_array_handle_nulls = false
|
527
545
|
meta_def(@dataset, :supports_multiple_column_in?){false}
|
@@ -618,14 +636,20 @@ end
|
|
618
636
|
|
619
637
|
describe "Dataset#or" do
|
620
638
|
before do
|
621
|
-
@dataset = Sequel
|
639
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
622
640
|
@d1 = @dataset.where(:x => 1)
|
623
641
|
end
|
624
642
|
|
625
|
-
|
643
|
+
qspecify "should raise if no filter exists" do
|
626
644
|
proc {@dataset.or(:a => 1)}.should raise_error(Sequel::Error)
|
627
645
|
end
|
628
646
|
|
647
|
+
specify "should just clone if given an empty argument" do
|
648
|
+
@d1.or({}).sql.should == @d1.sql
|
649
|
+
@d1.or([]).sql.should == @d1.sql
|
650
|
+
@d1.or('').sql.should == @d1.sql
|
651
|
+
end
|
652
|
+
|
629
653
|
specify "should add an alternative expression to the where clause" do
|
630
654
|
@d1.or(:y => 2).sql.should == 'SELECT * FROM test WHERE ((x = 1) OR (y = 2))'
|
631
655
|
end
|
@@ -647,15 +671,19 @@ describe "Dataset#or" do
|
|
647
671
|
specify "should allow the use of blocks and arguments simultaneously" do
|
648
672
|
@d1.or(Sequel.expr(:zz) < 3){yy > 3}.sql.should == 'SELECT * FROM test WHERE ((x = 1) OR ((zz < 3) AND (yy > 3)))'
|
649
673
|
end
|
674
|
+
|
675
|
+
qspecify "should modify the having clause if there is already a having clause" do
|
676
|
+
@dataset.having(:x => 1).or(:y => 2).sql.should == 'SELECT * FROM test HAVING ((x = 1) OR (y = 2))'
|
677
|
+
end
|
650
678
|
end
|
651
679
|
|
652
680
|
describe "Dataset#and" do
|
653
681
|
before do
|
654
|
-
@dataset = Sequel
|
682
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
655
683
|
@d1 = @dataset.where(:x => 1)
|
656
684
|
end
|
657
685
|
|
658
|
-
|
686
|
+
qspecify "should raise if no filter exists" do
|
659
687
|
proc {@dataset.and(:a => 1)}.should raise_error(Sequel::Error)
|
660
688
|
proc {@dataset.where(:a => 1).group(:t).and(:b => 2)}.should_not raise_error(Sequel::Error)
|
661
689
|
@dataset.where(:a => 1).group(:t).and(:b => 2).sql.should == "SELECT * FROM test WHERE ((a = 1) AND (b = 2)) GROUP BY t"
|
@@ -682,14 +710,14 @@ end
|
|
682
710
|
|
683
711
|
describe "Dataset#exclude" do
|
684
712
|
before do
|
685
|
-
@dataset = Sequel
|
713
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
686
714
|
end
|
687
715
|
|
688
716
|
specify "should correctly negate the expression when one condition is given" do
|
689
717
|
@dataset.exclude(:region=>'Asia').select_sql.should == "SELECT * FROM test WHERE (region != 'Asia')"
|
690
718
|
end
|
691
719
|
|
692
|
-
|
720
|
+
qspecify "should affect the having clause if having clause is already used" do
|
693
721
|
@dataset.group_and_count(:name).having{count > 2}.exclude{count > 5}.sql.should == "SELECT name, count(*) AS count FROM test GROUP BY name HAVING ((count > 2) AND (count <= 5))"
|
694
722
|
end
|
695
723
|
|
@@ -723,7 +751,7 @@ end
|
|
723
751
|
|
724
752
|
describe "Dataset#exclude_where" do
|
725
753
|
before do
|
726
|
-
@dataset = Sequel
|
754
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
727
755
|
end
|
728
756
|
|
729
757
|
specify "should correctly negate the expression and add it to the where clause" do
|
@@ -739,16 +767,16 @@ end
|
|
739
767
|
|
740
768
|
describe "Dataset#exclude_having" do
|
741
769
|
specify "should correctly negate the expression and add it to the having clause" do
|
742
|
-
Sequel
|
770
|
+
Sequel.mock.dataset.from(:test).exclude_having{count > 2}.exclude_having{count < 0}.sql.should == "SELECT * FROM test HAVING ((count <= 2) AND (count >= 0))"
|
743
771
|
end
|
744
772
|
end
|
745
773
|
|
746
774
|
describe "Dataset#invert" do
|
747
775
|
before do
|
748
|
-
@d = Sequel
|
776
|
+
@d = Sequel.mock.dataset.from(:test)
|
749
777
|
end
|
750
778
|
|
751
|
-
|
779
|
+
qspecify "should raise error if the dataset is not filtered" do
|
752
780
|
proc{@d.invert}.should raise_error(Sequel::Error)
|
753
781
|
end
|
754
782
|
|
@@ -763,7 +791,7 @@ end
|
|
763
791
|
|
764
792
|
describe "Dataset#having" do
|
765
793
|
before do
|
766
|
-
@dataset = Sequel
|
794
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
767
795
|
@grouped = @dataset.group(:region).select(:region, Sequel.function(:sum, :population), Sequel.function(:avg, :gdp))
|
768
796
|
end
|
769
797
|
|
@@ -781,14 +809,18 @@ describe "Dataset#having" do
|
|
781
809
|
@grouped.having{Sequel.function(:sum, :population) > 10}.sql.should == "SELECT region, sum(population), avg(gdp) FROM test GROUP BY region HAVING (sum(population) > 10)"
|
782
810
|
end
|
783
811
|
|
784
|
-
|
812
|
+
qspecify "should work with and on the having clause" do
|
785
813
|
@grouped.having(Sequel.expr(:a) > 1).and(Sequel.expr(:b) < 2).sql.should == "SELECT region, sum(population), avg(gdp) FROM test GROUP BY region HAVING ((a > 1) AND (b < 2))"
|
786
814
|
end
|
815
|
+
|
816
|
+
qspecify "should work with filter on the having clause" do
|
817
|
+
@grouped.having(Sequel.expr(:a) > 1).filter(Sequel.expr(:b) < 2).sql.should == "SELECT region, sum(population), avg(gdp) FROM test GROUP BY region HAVING ((a > 1) AND (b < 2))"
|
818
|
+
end
|
787
819
|
end
|
788
820
|
|
789
821
|
describe "a grouped dataset" do
|
790
822
|
before do
|
791
|
-
@dataset = Sequel
|
823
|
+
@dataset = Sequel.mock.dataset.from(:test).group(:type_id)
|
792
824
|
end
|
793
825
|
|
794
826
|
specify "should raise when trying to generate an update statement" do
|
@@ -815,7 +847,7 @@ describe "a grouped dataset" do
|
|
815
847
|
specify "should format the right statement for counting (as a subquery)" do
|
816
848
|
db = Sequel.mock
|
817
849
|
db[:test].select(:name).group(:name).count
|
818
|
-
db.sqls.should == ["SELECT
|
850
|
+
db.sqls.should == ["SELECT count(*) AS count FROM (SELECT name FROM test GROUP BY name) AS t1 LIMIT 1"]
|
819
851
|
end
|
820
852
|
end
|
821
853
|
|
@@ -885,7 +917,7 @@ end
|
|
885
917
|
|
886
918
|
describe "Dataset#as" do
|
887
919
|
specify "should set up an alias" do
|
888
|
-
dataset = Sequel
|
920
|
+
dataset = Sequel.mock.dataset.from(:test)
|
889
921
|
dataset.select(dataset.limit(1).select(:name).as(:n)).sql.should == 'SELECT (SELECT name FROM test LIMIT 1) AS n FROM test'
|
890
922
|
end
|
891
923
|
end
|
@@ -1110,7 +1142,7 @@ end
|
|
1110
1142
|
|
1111
1143
|
describe "Dataset#from" do
|
1112
1144
|
before do
|
1113
|
-
@dataset = Sequel
|
1145
|
+
@dataset = Sequel.mock.dataset
|
1114
1146
|
end
|
1115
1147
|
|
1116
1148
|
specify "should accept a Dataset" do
|
@@ -1129,7 +1161,7 @@ describe "Dataset#from" do
|
|
1129
1161
|
@dataset.from(d1, d2).sql.should == "SELECT * FROM (SELECT * FROM a GROUP BY b) AS t1, (SELECT * FROM c GROUP BY d) AS t2"
|
1130
1162
|
end
|
1131
1163
|
|
1132
|
-
|
1164
|
+
qspecify "should accept a hash for aliasing" do
|
1133
1165
|
@dataset.from(:a => :b).sql.should == "SELECT * FROM a AS b"
|
1134
1166
|
@dataset.from(:a => 'b').sql.should == "SELECT * FROM a AS b"
|
1135
1167
|
@dataset.from(@dataset.from(:a).group(:b) => :c).sql.should == "SELECT * FROM (SELECT * FROM a GROUP BY b) AS c"
|
@@ -1185,7 +1217,7 @@ end
|
|
1185
1217
|
|
1186
1218
|
describe "Dataset#select" do
|
1187
1219
|
before do
|
1188
|
-
@d = Sequel
|
1220
|
+
@d = Sequel.mock.dataset.from(:test)
|
1189
1221
|
end
|
1190
1222
|
|
1191
1223
|
specify "should accept variable arity" do
|
@@ -1217,7 +1249,11 @@ describe "Dataset#select" do
|
|
1217
1249
|
@d.select(Sequel.as([[:b, :c]], :n)).sql.should == 'SELECT (b = c) AS n FROM test'
|
1218
1250
|
end
|
1219
1251
|
|
1220
|
-
|
1252
|
+
qspecify "should handle hashes returned from virtual row blocks" do
|
1253
|
+
@d.select{{:b=>:c}}.sql.should == 'SELECT b AS c FROM test'
|
1254
|
+
end
|
1255
|
+
|
1256
|
+
qspecify "should accept a hash for AS values" do
|
1221
1257
|
@d.select(:name => 'n', :__ggh => 'age').sql.should =~ /SELECT ((name AS n, __ggh AS age)|(__ggh AS age, name AS n)) FROM test/
|
1222
1258
|
end
|
1223
1259
|
|
@@ -1229,7 +1265,7 @@ describe "Dataset#select" do
|
|
1229
1265
|
specify "should accept arbitrary objects and literalize them correctly" do
|
1230
1266
|
@d.select(1, :a, 't').sql.should == "SELECT 1, a, 't' FROM test"
|
1231
1267
|
@d.select(nil, Sequel.function(:sum, :t), :x___y).sql.should == "SELECT NULL, sum(t), x AS y FROM test"
|
1232
|
-
@d.select(nil, 1, :x
|
1268
|
+
@d.select(nil, 1, Sequel.as(:x, :y)).sql.should == "SELECT NULL, 1, x AS y FROM test"
|
1233
1269
|
end
|
1234
1270
|
|
1235
1271
|
specify "should accept a block that yields a virtual row" do
|
@@ -1249,7 +1285,7 @@ end
|
|
1249
1285
|
|
1250
1286
|
describe "Dataset#select_group" do
|
1251
1287
|
before do
|
1252
|
-
@d = Sequel
|
1288
|
+
@d = Sequel.mock.dataset.from(:test)
|
1253
1289
|
end
|
1254
1290
|
|
1255
1291
|
specify "should set both SELECT and GROUP" do
|
@@ -1270,7 +1306,7 @@ end
|
|
1270
1306
|
|
1271
1307
|
describe "Dataset#select_all" do
|
1272
1308
|
before do
|
1273
|
-
@d = Sequel
|
1309
|
+
@d = Sequel.mock.dataset.from(:test)
|
1274
1310
|
end
|
1275
1311
|
|
1276
1312
|
specify "should select the wildcard" do
|
@@ -1318,10 +1354,10 @@ end
|
|
1318
1354
|
|
1319
1355
|
describe "Dataset#select_more" do
|
1320
1356
|
before do
|
1321
|
-
@d = Sequel
|
1357
|
+
@d = Sequel.mock.dataset.from(:test)
|
1322
1358
|
end
|
1323
1359
|
|
1324
|
-
|
1360
|
+
qspecify "should act like #select for datasets with no selection" do
|
1325
1361
|
@d.select_more(:a, :b).sql.should == 'SELECT a, b FROM test'
|
1326
1362
|
@d.select_all.select_more(:a, :b).sql.should == 'SELECT a, b FROM test'
|
1327
1363
|
@d.select(:blah).select_all.select_more(:a, :b).sql.should == 'SELECT a, b FROM test'
|
@@ -1340,7 +1376,7 @@ end
|
|
1340
1376
|
|
1341
1377
|
describe "Dataset#select_append" do
|
1342
1378
|
before do
|
1343
|
-
@d = Sequel
|
1379
|
+
@d = Sequel.mock.dataset.from(:test)
|
1344
1380
|
end
|
1345
1381
|
|
1346
1382
|
specify "should select * in addition to columns if no columns selected" do
|
@@ -1370,7 +1406,7 @@ end
|
|
1370
1406
|
|
1371
1407
|
describe "Dataset#order" do
|
1372
1408
|
before do
|
1373
|
-
@dataset = Sequel
|
1409
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1374
1410
|
end
|
1375
1411
|
|
1376
1412
|
specify "should include an ORDER BY clause in the select statement" do
|
@@ -1418,31 +1454,31 @@ end
|
|
1418
1454
|
|
1419
1455
|
describe "Dataset#unfiltered" do
|
1420
1456
|
specify "should remove filtering from the dataset" do
|
1421
|
-
Sequel
|
1457
|
+
Sequel.mock.dataset.from(:test).filter(:score=>1).unfiltered.sql.should == 'SELECT * FROM test'
|
1422
1458
|
end
|
1423
1459
|
end
|
1424
1460
|
|
1425
1461
|
describe "Dataset#unlimited" do
|
1426
1462
|
specify "should remove limit and offset from the dataset" do
|
1427
|
-
Sequel
|
1463
|
+
Sequel.mock.dataset.from(:test).limit(1, 2).unlimited.sql.should == 'SELECT * FROM test'
|
1428
1464
|
end
|
1429
1465
|
end
|
1430
1466
|
|
1431
1467
|
describe "Dataset#ungrouped" do
|
1432
1468
|
specify "should remove group and having clauses from the dataset" do
|
1433
|
-
Sequel
|
1469
|
+
Sequel.mock.dataset.from(:test).group(:a).having(:b).ungrouped.sql.should == 'SELECT * FROM test'
|
1434
1470
|
end
|
1435
1471
|
end
|
1436
1472
|
|
1437
1473
|
describe "Dataset#unordered" do
|
1438
1474
|
specify "should remove ordering from the dataset" do
|
1439
|
-
Sequel
|
1475
|
+
Sequel.mock.dataset.from(:test).order(:name).unordered.sql.should == 'SELECT * FROM test'
|
1440
1476
|
end
|
1441
1477
|
end
|
1442
1478
|
|
1443
1479
|
describe "Dataset#with_sql" do
|
1444
1480
|
before do
|
1445
|
-
@dataset = Sequel
|
1481
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1446
1482
|
end
|
1447
1483
|
|
1448
1484
|
specify "should use static sql" do
|
@@ -1471,7 +1507,7 @@ end
|
|
1471
1507
|
|
1472
1508
|
describe "Dataset#order_by" do
|
1473
1509
|
before do
|
1474
|
-
@dataset = Sequel
|
1510
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1475
1511
|
end
|
1476
1512
|
|
1477
1513
|
specify "should include an ORDER BY clause in the select statement" do
|
@@ -1497,7 +1533,7 @@ end
|
|
1497
1533
|
|
1498
1534
|
describe "Dataset#order_more and order_append" do
|
1499
1535
|
before do
|
1500
|
-
@dataset = Sequel
|
1536
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1501
1537
|
end
|
1502
1538
|
|
1503
1539
|
specify "should include an ORDER BY clause in the select statement" do
|
@@ -1520,7 +1556,7 @@ end
|
|
1520
1556
|
|
1521
1557
|
describe "Dataset#order_prepend" do
|
1522
1558
|
before do
|
1523
|
-
@dataset = Sequel
|
1559
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1524
1560
|
end
|
1525
1561
|
|
1526
1562
|
specify "should include an ORDER BY clause in the select statement" do
|
@@ -1539,7 +1575,7 @@ end
|
|
1539
1575
|
|
1540
1576
|
describe "Dataset#reverse_order" do
|
1541
1577
|
before do
|
1542
|
-
@dataset = Sequel
|
1578
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1543
1579
|
end
|
1544
1580
|
|
1545
1581
|
specify "should use DESC as default order" do
|
@@ -1587,7 +1623,7 @@ end
|
|
1587
1623
|
|
1588
1624
|
describe "Dataset#limit" do
|
1589
1625
|
before do
|
1590
|
-
@dataset = Sequel
|
1626
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1591
1627
|
end
|
1592
1628
|
|
1593
1629
|
specify "should include a LIMIT clause in the select statement" do
|
@@ -1638,7 +1674,7 @@ end
|
|
1638
1674
|
|
1639
1675
|
describe "Dataset#naked" do
|
1640
1676
|
specify "should returned clone dataset without row_proc" do
|
1641
|
-
d = Sequel
|
1677
|
+
d = Sequel.mock.dataset
|
1642
1678
|
d.row_proc = Proc.new{|r| r}
|
1643
1679
|
d.naked.row_proc.should be_nil
|
1644
1680
|
d.row_proc.should_not be_nil
|
@@ -1647,7 +1683,7 @@ end
|
|
1647
1683
|
|
1648
1684
|
describe "Dataset#naked!" do
|
1649
1685
|
specify "should remove any existing row_proc" do
|
1650
|
-
d = Sequel
|
1686
|
+
d = Sequel.mock.dataset
|
1651
1687
|
d.row_proc = Proc.new{|r| r}
|
1652
1688
|
d.naked!.row_proc.should be_nil
|
1653
1689
|
d.row_proc.should be_nil
|
@@ -1656,7 +1692,7 @@ end
|
|
1656
1692
|
|
1657
1693
|
describe "Dataset#qualified_column_name" do
|
1658
1694
|
before do
|
1659
|
-
@dataset = Sequel
|
1695
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
1660
1696
|
end
|
1661
1697
|
|
1662
1698
|
specify "should return the literal value if not given a symbol" do
|
@@ -1812,7 +1848,7 @@ describe "Dataset#distinct" do
|
|
1812
1848
|
|
1813
1849
|
specify "should do a subselect for count" do
|
1814
1850
|
@dataset.distinct.count
|
1815
|
-
@db.sqls.should == ['SELECT
|
1851
|
+
@db.sqls.should == ['SELECT count(*) AS count FROM (SELECT DISTINCT name FROM test) AS t1 LIMIT 1']
|
1816
1852
|
end
|
1817
1853
|
end
|
1818
1854
|
|
@@ -1824,22 +1860,22 @@ describe "Dataset#count" do
|
|
1824
1860
|
|
1825
1861
|
specify "should format SQL properly" do
|
1826
1862
|
@dataset.count.should == 1
|
1827
|
-
@db.sqls.should == ['SELECT
|
1863
|
+
@db.sqls.should == ['SELECT count(*) AS count FROM test LIMIT 1']
|
1828
1864
|
end
|
1829
1865
|
|
1830
1866
|
specify "should accept an argument" do
|
1831
1867
|
@dataset.count(:foo).should == 1
|
1832
|
-
@db.sqls.should == ['SELECT
|
1868
|
+
@db.sqls.should == ['SELECT count(foo) AS count FROM test LIMIT 1']
|
1833
1869
|
end
|
1834
1870
|
|
1835
1871
|
specify "should work with a nil argument" do
|
1836
1872
|
@dataset.count(nil).should == 1
|
1837
|
-
@db.sqls.should == ['SELECT
|
1873
|
+
@db.sqls.should == ['SELECT count(NULL) AS count FROM test LIMIT 1']
|
1838
1874
|
end
|
1839
1875
|
|
1840
1876
|
specify "should accept a virtual row block" do
|
1841
1877
|
@dataset.count{foo(bar)}.should == 1
|
1842
|
-
@db.sqls.should == ['SELECT
|
1878
|
+
@db.sqls.should == ['SELECT count(foo(bar)) AS count FROM test LIMIT 1']
|
1843
1879
|
end
|
1844
1880
|
|
1845
1881
|
specify "should raise an Error if given an argument and a block" do
|
@@ -1848,52 +1884,52 @@ describe "Dataset#count" do
|
|
1848
1884
|
|
1849
1885
|
specify "should include the where clause if it's there" do
|
1850
1886
|
@dataset.filter(Sequel.expr(:abc) < 30).count.should == 1
|
1851
|
-
@db.sqls.should == ['SELECT
|
1887
|
+
@db.sqls.should == ['SELECT count(*) AS count FROM test WHERE (abc < 30) LIMIT 1']
|
1852
1888
|
end
|
1853
1889
|
|
1854
1890
|
specify "should count properly for datasets with fixed sql" do
|
1855
1891
|
@dataset.opts[:sql] = "select abc from xyz"
|
1856
1892
|
@dataset.count.should == 1
|
1857
|
-
@db.sqls.should == ["SELECT
|
1893
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (select abc from xyz) AS t1 LIMIT 1"]
|
1858
1894
|
end
|
1859
1895
|
|
1860
1896
|
specify "should count properly when using UNION, INTERSECT, or EXCEPT" do
|
1861
1897
|
@dataset.union(@dataset).count.should == 1
|
1862
|
-
@db.sqls.should == ["SELECT
|
1898
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (SELECT * FROM test UNION SELECT * FROM test) AS t1 LIMIT 1"]
|
1863
1899
|
@dataset.intersect(@dataset).count.should == 1
|
1864
|
-
@db.sqls.should == ["SELECT
|
1900
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (SELECT * FROM test INTERSECT SELECT * FROM test) AS t1 LIMIT 1"]
|
1865
1901
|
@dataset.except(@dataset).count.should == 1
|
1866
|
-
@db.sqls.should == ["SELECT
|
1902
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (SELECT * FROM test EXCEPT SELECT * FROM test) AS t1 LIMIT 1"]
|
1867
1903
|
end
|
1868
1904
|
|
1869
1905
|
specify "should return limit if count is greater than it" do
|
1870
1906
|
@dataset.limit(5).count.should == 1
|
1871
|
-
@db.sqls.should == ["SELECT
|
1907
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (SELECT * FROM test LIMIT 5) AS t1 LIMIT 1"]
|
1872
1908
|
end
|
1873
1909
|
|
1874
1910
|
specify "should work correctly with offsets" do
|
1875
1911
|
@dataset.limit(nil, 5).count.should == 1
|
1876
|
-
@db.sqls.should == ["SELECT
|
1912
|
+
@db.sqls.should == ["SELECT count(*) AS count FROM (SELECT * FROM test OFFSET 5) AS t1 LIMIT 1"]
|
1877
1913
|
end
|
1878
1914
|
|
1879
1915
|
it "should work on a graphed_dataset" do
|
1880
1916
|
@dataset.should_receive(:columns).twice.and_return([:a])
|
1881
1917
|
@dataset.graph(@dataset, [:a], :table_alias=>:test2).count.should == 1
|
1882
|
-
@db.sqls.should == ['SELECT
|
1918
|
+
@db.sqls.should == ['SELECT count(*) AS count FROM test LEFT OUTER JOIN test AS test2 USING (a) LIMIT 1']
|
1883
1919
|
end
|
1884
1920
|
|
1885
1921
|
specify "should not cache the columns value" do
|
1886
1922
|
ds = @dataset.from(:blah).columns(:a)
|
1887
1923
|
ds.columns.should == [:a]
|
1888
1924
|
ds.count.should == 1
|
1889
|
-
@db.sqls.should == ['SELECT
|
1925
|
+
@db.sqls.should == ['SELECT count(*) AS count FROM blah LIMIT 1']
|
1890
1926
|
ds.columns.should == [:a]
|
1891
1927
|
end
|
1892
1928
|
end
|
1893
1929
|
|
1894
1930
|
describe "Dataset#group_and_count" do
|
1895
1931
|
before do
|
1896
|
-
@ds = Sequel
|
1932
|
+
@ds = Sequel.mock.dataset.from(:test)
|
1897
1933
|
end
|
1898
1934
|
|
1899
1935
|
specify "should format SQL properly" do
|
@@ -1941,7 +1977,7 @@ end
|
|
1941
1977
|
|
1942
1978
|
describe "Dataset#first_source_alias" do
|
1943
1979
|
before do
|
1944
|
-
@ds = Sequel
|
1980
|
+
@ds = Sequel.mock.dataset
|
1945
1981
|
end
|
1946
1982
|
|
1947
1983
|
specify "should be the entire first source if not aliased" do
|
@@ -1971,7 +2007,7 @@ end
|
|
1971
2007
|
|
1972
2008
|
describe "Dataset#first_source_table" do
|
1973
2009
|
before do
|
1974
|
-
@ds = Sequel
|
2010
|
+
@ds = Sequel.mock.dataset
|
1975
2011
|
end
|
1976
2012
|
|
1977
2013
|
specify "should be the entire first source if not aliased" do
|
@@ -1994,7 +2030,7 @@ end
|
|
1994
2030
|
|
1995
2031
|
describe "Dataset#from_self" do
|
1996
2032
|
before do
|
1997
|
-
@ds = Sequel
|
2033
|
+
@ds = Sequel.mock.dataset.from(:test).select(:name).limit(1)
|
1998
2034
|
end
|
1999
2035
|
|
2000
2036
|
specify "should set up a default alias" do
|
@@ -2019,7 +2055,7 @@ describe "Dataset#from_self" do
|
|
2019
2055
|
end
|
2020
2056
|
|
2021
2057
|
specify "should hoist WITH clauses in current dataset if dataset doesn't support WITH in subselect" do
|
2022
|
-
ds = Sequel
|
2058
|
+
ds = Sequel.mock.dataset
|
2023
2059
|
meta_def(ds, :supports_cte?){true}
|
2024
2060
|
meta_def(ds, :supports_cte_in_subselect?){false}
|
2025
2061
|
ds.from(:a).with(:a, ds.from(:b)).from_self.sql.should == 'WITH a AS (SELECT * FROM b) SELECT * FROM (SELECT * FROM a) AS t1'
|
@@ -2034,7 +2070,7 @@ end
|
|
2034
2070
|
|
2035
2071
|
describe "Dataset#join_table" do
|
2036
2072
|
before do
|
2037
|
-
@d = Sequel
|
2073
|
+
@d = Sequel.mock.dataset.from(:items)
|
2038
2074
|
@d.quote_identifiers = true
|
2039
2075
|
end
|
2040
2076
|
|
@@ -2103,7 +2139,7 @@ describe "Dataset#join_table" do
|
|
2103
2139
|
@d.join(:categories, :category_id=>:id).sql.should == 'SELECT * FROM "items" INNER JOIN "categories" ON ("categories"."category_id" = "items"."id")'
|
2104
2140
|
end
|
2105
2141
|
|
2106
|
-
|
2142
|
+
qspecify "should support aliased tables using the deprecated argument" do
|
2107
2143
|
@d.from('stats').join('players', {:id => :player_id}, 'p').sql.should == 'SELECT * FROM "stats" INNER JOIN "players" AS "p" ON ("p"."id" = "stats"."player_id")'
|
2108
2144
|
end
|
2109
2145
|
|
@@ -2116,7 +2152,7 @@ describe "Dataset#join_table" do
|
|
2116
2152
|
end
|
2117
2153
|
|
2118
2154
|
specify "should support using an alias for the FROM when doing the first join with unqualified condition columns" do
|
2119
|
-
@d.from(:foo
|
2155
|
+
@d.from(Sequel.as(:foo, :f)).join_table(:inner, :bar, :id => :bar_id).sql.should == 'SELECT * FROM "foo" AS "f" INNER JOIN "bar" ON ("bar"."id" = "f"."bar_id")'
|
2120
2156
|
end
|
2121
2157
|
|
2122
2158
|
specify "should support implicit schemas in from table symbols" do
|
@@ -2136,6 +2172,11 @@ describe "Dataset#join_table" do
|
|
2136
2172
|
@d.from('stats').join('players', {:id => :player_id}, :implicit_qualifier=>:p).sql.should == 'SELECT * FROM "stats" INNER JOIN "players" ON ("players"."id" = "p"."player_id")'
|
2137
2173
|
end
|
2138
2174
|
|
2175
|
+
specify "should default :qualify option to default_join_table_qualification" do
|
2176
|
+
def @d.default_join_table_qualification; false; end
|
2177
|
+
@d.from('stats').join(:players, :id => :player_id).sql.should == 'SELECT * FROM "stats" INNER JOIN "players" ON ("id" = "player_id")'
|
2178
|
+
end
|
2179
|
+
|
2139
2180
|
specify "should not qualify if :qualify=>false option is given" do
|
2140
2181
|
@d.from('stats').join(:players, {:id => :player_id}, :qualify=>false).sql.should == 'SELECT * FROM "stats" INNER JOIN "players" ON ("id" = "player_id")'
|
2141
2182
|
end
|
@@ -2152,11 +2193,11 @@ describe "Dataset#join_table" do
|
|
2152
2193
|
end
|
2153
2194
|
|
2154
2195
|
specify "should raise error for a table without a source" do
|
2155
|
-
proc {Sequel
|
2196
|
+
proc {Sequel.mock.dataset.join('players', :id => :player_id)}.should raise_error(Sequel::Error)
|
2156
2197
|
end
|
2157
2198
|
|
2158
2199
|
specify "should support joining datasets" do
|
2159
|
-
ds = Sequel
|
2200
|
+
ds = Sequel.mock.dataset.from(:categories)
|
2160
2201
|
@d.join_table(:left_outer, ds, :item_id => :id).sql.should == 'SELECT * FROM "items" LEFT OUTER JOIN (SELECT * FROM categories) AS "t1" ON ("t1"."item_id" = "items"."id")'
|
2161
2202
|
ds.filter!(:active => true)
|
2162
2203
|
@d.join_table(:left_outer, ds, :item_id => :id).sql.should == 'SELECT * FROM "items" LEFT OUTER JOIN (SELECT * FROM categories WHERE (active IS TRUE)) AS "t1" ON ("t1"."item_id" = "items"."id")'
|
@@ -2164,14 +2205,14 @@ describe "Dataset#join_table" do
|
|
2164
2205
|
end
|
2165
2206
|
|
2166
2207
|
specify "should support joining datasets and aliasing the join" do
|
2167
|
-
ds = Sequel
|
2168
|
-
@d.join_table(:left_outer, ds, {:ds__item_id => :id}, :ds).sql.should == 'SELECT * FROM "items" LEFT OUTER JOIN (SELECT * FROM categories) AS "ds" ON ("ds"."item_id" = "items"."id")'
|
2208
|
+
ds = Sequel.mock.dataset.from(:categories)
|
2209
|
+
@d.join_table(:left_outer, ds, {:ds__item_id => :id}, :table_alias=>:ds).sql.should == 'SELECT * FROM "items" LEFT OUTER JOIN (SELECT * FROM categories) AS "ds" ON ("ds"."item_id" = "items"."id")'
|
2169
2210
|
end
|
2170
2211
|
|
2171
2212
|
specify "should support joining multiple datasets" do
|
2172
|
-
ds = Sequel
|
2173
|
-
ds2 = Sequel
|
2174
|
-
ds3 = Sequel
|
2213
|
+
ds = Sequel.mock.dataset.from(:categories)
|
2214
|
+
ds2 = Sequel.mock.dataset.from(:nodes).select(:name)
|
2215
|
+
ds3 = Sequel.mock.dataset.from(:attributes).filter("name = 'blah'")
|
2175
2216
|
|
2176
2217
|
@d.join_table(:left_outer, ds, :item_id => :id).join_table(:inner, ds2, :node_id=>:id).join_table(:right_outer, ds3, :attribute_id=>:id).sql.should ==
|
2177
2218
|
'SELECT * FROM "items" LEFT OUTER JOIN (SELECT * FROM categories) AS "t1" ON ("t1"."item_id" = "items"."id") ' \
|
@@ -2180,7 +2221,7 @@ describe "Dataset#join_table" do
|
|
2180
2221
|
end
|
2181
2222
|
|
2182
2223
|
specify "should support using an SQL String as the join condition" do
|
2183
|
-
@d.join(:categories, "c.item_id = items.id", :c).sql.should == 'SELECT * FROM "items" INNER JOIN "categories" AS "c" ON (c.item_id = items.id)'
|
2224
|
+
@d.join(:categories, "c.item_id = items.id", :table_alias=>:c).sql.should == 'SELECT * FROM "items" INNER JOIN "categories" AS "c" ON (c.item_id = items.id)'
|
2184
2225
|
end
|
2185
2226
|
|
2186
2227
|
specify "should support using a boolean column as the join condition" do
|
@@ -2194,7 +2235,7 @@ describe "Dataset#join_table" do
|
|
2194
2235
|
specify "should support natural and cross joins" do
|
2195
2236
|
@d.join_table(:natural, :categories).sql.should == 'SELECT * FROM "items" NATURAL JOIN "categories"'
|
2196
2237
|
@d.join_table(:cross, :categories, nil).sql.should == 'SELECT * FROM "items" CROSS JOIN "categories"'
|
2197
|
-
@d.join_table(:natural, :categories, nil, :c).sql.should == 'SELECT * FROM "items" NATURAL JOIN "categories" AS "c"'
|
2238
|
+
@d.join_table(:natural, :categories, nil, :table_alias=>:c).sql.should == 'SELECT * FROM "items" NATURAL JOIN "categories" AS "c"'
|
2198
2239
|
end
|
2199
2240
|
|
2200
2241
|
specify "should support joins with a USING clause if an array of symbols is used" do
|
@@ -2210,7 +2251,7 @@ describe "Dataset#join_table" do
|
|
2210
2251
|
specify "should hoist WITH clauses from subqueries if the dataset doesn't support CTEs in subselects" do
|
2211
2252
|
meta_def(@d, :supports_cte?){true}
|
2212
2253
|
meta_def(@d, :supports_cte_in_subselect?){false}
|
2213
|
-
@d.join(Sequel
|
2254
|
+
@d.join(Sequel.mock.dataset.from(:categories).with(:a, Sequel.mock.dataset.from(:b)), [:id]).sql.should == 'WITH "a" AS (SELECT * FROM b) SELECT * FROM "items" INNER JOIN (SELECT * FROM categories) AS "t1" USING ("id")'
|
2214
2255
|
end
|
2215
2256
|
|
2216
2257
|
specify "should raise an error if using an array of symbols with a block" do
|
@@ -2224,19 +2265,19 @@ describe "Dataset#join_table" do
|
|
2224
2265
|
joins.should == []
|
2225
2266
|
end
|
2226
2267
|
|
2227
|
-
@d.from(:items
|
2268
|
+
@d.from(Sequel.as(:items, :i)).join(:categories, nil, :table_alias=>:c) do |join_alias, last_join_alias, joins|
|
2228
2269
|
join_alias.should == :c
|
2229
2270
|
last_join_alias.should == :i
|
2230
2271
|
joins.should == []
|
2231
2272
|
end
|
2232
2273
|
|
2233
|
-
@d.from(:items___i).join(:categories, nil, :c) do |join_alias, last_join_alias, joins|
|
2274
|
+
@d.from(:items___i).join(:categories, nil, :table_alias=>:c) do |join_alias, last_join_alias, joins|
|
2234
2275
|
join_alias.should == :c
|
2235
2276
|
last_join_alias.should == :i
|
2236
2277
|
joins.should == []
|
2237
2278
|
end
|
2238
2279
|
|
2239
|
-
@d.join(:blah).join(:categories, nil, :c) do |join_alias, last_join_alias, joins|
|
2280
|
+
@d.join(:blah).join(:categories, nil, :table_alias=>:c) do |join_alias, last_join_alias, joins|
|
2240
2281
|
join_alias.should == :c
|
2241
2282
|
last_join_alias.should == :blah
|
2242
2283
|
joins.should be_a_kind_of(Array)
|
@@ -2245,7 +2286,7 @@ describe "Dataset#join_table" do
|
|
2245
2286
|
joins.first.join_type.should == :inner
|
2246
2287
|
end
|
2247
2288
|
|
2248
|
-
@d.join_table(:natural, :blah, nil, :b).join(:categories, nil, :c) do |join_alias, last_join_alias, joins|
|
2289
|
+
@d.join_table(:natural, :blah, nil, :table_alias=>:b).join(:categories, nil, :table_alias=>:c) do |join_alias, last_join_alias, joins|
|
2249
2290
|
join_alias.should == :c
|
2250
2291
|
last_join_alias.should == :b
|
2251
2292
|
joins.should be_a_kind_of(Array)
|
@@ -2296,7 +2337,7 @@ describe "Dataset#join_table" do
|
|
2296
2337
|
end
|
2297
2338
|
|
2298
2339
|
describe "Dataset#[]=" do
|
2299
|
-
|
2340
|
+
qspecify "should perform an update on the specified filter" do
|
2300
2341
|
db = Sequel.mock
|
2301
2342
|
ds = db[:items]
|
2302
2343
|
ds[:a => 1] = {:x => 3}
|
@@ -2305,7 +2346,7 @@ describe "Dataset#[]=" do
|
|
2305
2346
|
end
|
2306
2347
|
|
2307
2348
|
describe "Dataset#set" do
|
2308
|
-
|
2349
|
+
qspecify "should act as alias to #update" do
|
2309
2350
|
db = Sequel.mock
|
2310
2351
|
ds = db[:items]
|
2311
2352
|
ds.set({:x => 3})
|
@@ -2319,7 +2360,7 @@ describe "Dataset#insert_multiple" do
|
|
2319
2360
|
@ds = @db[:items]
|
2320
2361
|
end
|
2321
2362
|
|
2322
|
-
|
2363
|
+
qspecify "should insert all items in the supplied array" do
|
2323
2364
|
@ds.insert_multiple(['aa', 5, 3, {:a => 2}])
|
2324
2365
|
@db.sqls.should == ["INSERT INTO items VALUES ('aa')",
|
2325
2366
|
"INSERT INTO items VALUES (5)",
|
@@ -2327,18 +2368,18 @@ describe "Dataset#insert_multiple" do
|
|
2327
2368
|
"INSERT INTO items (a) VALUES (2)"]
|
2328
2369
|
end
|
2329
2370
|
|
2330
|
-
|
2371
|
+
qspecify "should pass array items through the supplied block if given" do
|
2331
2372
|
@ds.insert_multiple(["inevitable", "hello", "the ticking clock"]){|i| i.gsub('l', 'r')}
|
2332
2373
|
@db.sqls.should == ["INSERT INTO items VALUES ('inevitabre')",
|
2333
2374
|
"INSERT INTO items VALUES ('herro')",
|
2334
2375
|
"INSERT INTO items VALUES ('the ticking crock')"]
|
2335
2376
|
end
|
2336
2377
|
|
2337
|
-
|
2378
|
+
qspecify "should return array of inserted ids" do
|
2338
2379
|
@ds.insert_multiple(['aa', 5, 3, {:a => 2}]).should == [2, 3, 4, 5]
|
2339
2380
|
end
|
2340
2381
|
|
2341
|
-
|
2382
|
+
qspecify "should work exactly like in metioned in the example" do
|
2342
2383
|
@ds.insert_multiple([{:x=>1}, {:x=>2}]){|row| row[:y] = row[:x] * 2 ; row }
|
2343
2384
|
sqls = @db.sqls
|
2344
2385
|
["INSERT INTO items (x, y) VALUES (1, 2)", "INSERT INTO items (y, x) VALUES (2, 1)"].should include(sqls[0])
|
@@ -2352,38 +2393,38 @@ describe "Dataset aggregate methods" do
|
|
2352
2393
|
end
|
2353
2394
|
|
2354
2395
|
specify "should include min" do
|
2355
|
-
@d.min(:a).should == 'SELECT min(a) FROM test LIMIT 1'
|
2396
|
+
@d.min(:a).should == 'SELECT min(a) AS min FROM test LIMIT 1'
|
2356
2397
|
end
|
2357
2398
|
|
2358
2399
|
specify "should include max" do
|
2359
|
-
@d.max(:b).should == 'SELECT max(b) FROM test LIMIT 1'
|
2400
|
+
@d.max(:b).should == 'SELECT max(b) AS max FROM test LIMIT 1'
|
2360
2401
|
end
|
2361
2402
|
|
2362
2403
|
specify "should include sum" do
|
2363
|
-
@d.sum(:c).should == 'SELECT sum(c) FROM test LIMIT 1'
|
2404
|
+
@d.sum(:c).should == 'SELECT sum(c) AS sum FROM test LIMIT 1'
|
2364
2405
|
end
|
2365
2406
|
|
2366
2407
|
specify "should include avg" do
|
2367
|
-
@d.avg(:d).should == 'SELECT avg(d) FROM test LIMIT 1'
|
2408
|
+
@d.avg(:d).should == 'SELECT avg(d) AS avg FROM test LIMIT 1'
|
2368
2409
|
end
|
2369
2410
|
|
2370
2411
|
specify "should accept qualified columns" do
|
2371
|
-
@d.avg(:test__bc).should == 'SELECT avg(test.bc) FROM test LIMIT 1'
|
2412
|
+
@d.avg(:test__bc).should == 'SELECT avg(test.bc) AS avg FROM test LIMIT 1'
|
2372
2413
|
end
|
2373
2414
|
|
2374
2415
|
specify "should use a subselect for the same conditions as count" do
|
2375
2416
|
d = @d.order(:a).limit(5)
|
2376
|
-
d.avg(:a).should == 'SELECT avg(a) FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2377
|
-
d.sum(:a).should == 'SELECT sum(a) FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2378
|
-
d.min(:a).should == 'SELECT min(a) FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2379
|
-
d.max(:a).should == 'SELECT max(a) FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2417
|
+
d.avg(:a).should == 'SELECT avg(a) AS avg FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2418
|
+
d.sum(:a).should == 'SELECT sum(a) AS sum FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2419
|
+
d.min(:a).should == 'SELECT min(a) AS min FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2420
|
+
d.max(:a).should == 'SELECT max(a) AS max FROM (SELECT * FROM test ORDER BY a LIMIT 5) AS t1 LIMIT 1'
|
2380
2421
|
end
|
2381
2422
|
|
2382
2423
|
specify "should accept virtual row blocks" do
|
2383
|
-
@d.avg{a(b)}.should == 'SELECT avg(a(b)) FROM test LIMIT 1'
|
2384
|
-
@d.sum{a(b)}.should == 'SELECT sum(a(b)) FROM test LIMIT 1'
|
2385
|
-
@d.min{a(b)}.should == 'SELECT min(a(b)) FROM test LIMIT 1'
|
2386
|
-
@d.max{a(b)}.should == 'SELECT max(a(b)) FROM test LIMIT 1'
|
2424
|
+
@d.avg{a(b)}.should == 'SELECT avg(a(b)) AS avg FROM test LIMIT 1'
|
2425
|
+
@d.sum{a(b)}.should == 'SELECT sum(a(b)) AS sum FROM test LIMIT 1'
|
2426
|
+
@d.min{a(b)}.should == 'SELECT min(a(b)) AS min FROM test LIMIT 1'
|
2427
|
+
@d.max{a(b)}.should == 'SELECT max(a(b)) AS max FROM test LIMIT 1'
|
2387
2428
|
end
|
2388
2429
|
end
|
2389
2430
|
|
@@ -2424,20 +2465,20 @@ describe "Dataset#interval" do
|
|
2424
2465
|
|
2425
2466
|
specify "should generate the correct SQL statement" do
|
2426
2467
|
@ds.interval(:stamp)
|
2427
|
-
@db.sqls.should == ["SELECT (max(stamp) - min(stamp)) FROM test LIMIT 1"]
|
2468
|
+
@db.sqls.should == ["SELECT (max(stamp) - min(stamp)) AS interval FROM test LIMIT 1"]
|
2428
2469
|
|
2429
2470
|
@ds.filter(Sequel.expr(:price) > 100).interval(:stamp)
|
2430
|
-
@db.sqls.should == ["SELECT (max(stamp) - min(stamp)) FROM test WHERE (price > 100) LIMIT 1"]
|
2471
|
+
@db.sqls.should == ["SELECT (max(stamp) - min(stamp)) AS interval FROM test WHERE (price > 100) LIMIT 1"]
|
2431
2472
|
end
|
2432
2473
|
|
2433
2474
|
specify "should use a subselect for the same conditions as count" do
|
2434
2475
|
@ds.order(:stamp).limit(5).interval(:stamp).should == 1234
|
2435
|
-
@db.sqls.should == ['SELECT (max(stamp) - min(stamp)) FROM (SELECT * FROM test ORDER BY stamp LIMIT 5) AS t1 LIMIT 1']
|
2476
|
+
@db.sqls.should == ['SELECT (max(stamp) - min(stamp)) AS interval FROM (SELECT * FROM test ORDER BY stamp LIMIT 5) AS t1 LIMIT 1']
|
2436
2477
|
end
|
2437
2478
|
|
2438
2479
|
specify "should accept virtual row blocks" do
|
2439
2480
|
@ds.interval{a(b)}
|
2440
|
-
@db.sqls.should == ["SELECT (max(a(b)) - min(a(b))) FROM test LIMIT 1"]
|
2481
|
+
@db.sqls.should == ["SELECT (max(a(b)) - min(a(b))) AS interval FROM test LIMIT 1"]
|
2441
2482
|
end
|
2442
2483
|
end
|
2443
2484
|
|
@@ -2541,25 +2582,28 @@ end
|
|
2541
2582
|
|
2542
2583
|
describe "Dataset compound operations" do
|
2543
2584
|
before do
|
2544
|
-
@a = Sequel
|
2545
|
-
@b = Sequel
|
2585
|
+
@a = Sequel.mock.dataset.from(:a).filter(:z => 1)
|
2586
|
+
@b = Sequel.mock.dataset.from(:b).filter(:z => 2)
|
2546
2587
|
end
|
2547
2588
|
|
2589
|
+
qspecify "should support ALL with deprecated 2nd argument" do
|
2590
|
+
@b.union(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2591
|
+
@b.intersect(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) INTERSECT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2592
|
+
@b.except(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2593
|
+
end
|
2594
|
+
|
2548
2595
|
specify "should support UNION and UNION ALL" do
|
2549
2596
|
@a.union(@b).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM b WHERE (z = 2)) AS t1"
|
2550
|
-
@b.union(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2551
2597
|
@b.union(@a, :all=>true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2552
2598
|
end
|
2553
2599
|
|
2554
2600
|
specify "should support INTERSECT and INTERSECT ALL" do
|
2555
2601
|
@a.intersect(@b).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT SELECT * FROM b WHERE (z = 2)) AS t1"
|
2556
|
-
@b.intersect(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) INTERSECT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2557
2602
|
@b.intersect(@a, :all=>true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) INTERSECT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2558
2603
|
end
|
2559
2604
|
|
2560
2605
|
specify "should support EXCEPT and EXCEPT ALL" do
|
2561
2606
|
@a.except(@b).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM b WHERE (z = 2)) AS t1"
|
2562
|
-
@b.except(@a, true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2563
2607
|
@b.except(@a, :all=>true).sql.should == "SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2564
2608
|
end
|
2565
2609
|
|
@@ -2582,33 +2626,33 @@ describe "Dataset compound operations" do
|
|
2582
2626
|
specify "should raise an InvalidOperation if INTERSECT or EXCEPT is used and they are not supported" do
|
2583
2627
|
meta_def(@a, :supports_intersect_except?){false}
|
2584
2628
|
proc{@a.intersect(@b)}.should raise_error(Sequel::InvalidOperation)
|
2585
|
-
proc{@a.intersect(@b
|
2629
|
+
proc{@a.intersect(@b,:all=> true)}.should raise_error(Sequel::InvalidOperation)
|
2586
2630
|
proc{@a.except(@b)}.should raise_error(Sequel::InvalidOperation)
|
2587
|
-
proc{@a.except(@b, true)}.should raise_error(Sequel::InvalidOperation)
|
2631
|
+
proc{@a.except(@b, :all=>true)}.should raise_error(Sequel::InvalidOperation)
|
2588
2632
|
end
|
2589
2633
|
|
2590
2634
|
specify "should raise an InvalidOperation if INTERSECT ALL or EXCEPT ALL is used and they are not supported" do
|
2591
2635
|
meta_def(@a, :supports_intersect_except_all?){false}
|
2592
2636
|
proc{@a.intersect(@b)}.should_not raise_error
|
2593
|
-
proc{@a.intersect(@b, true)}.should raise_error(Sequel::InvalidOperation)
|
2637
|
+
proc{@a.intersect(@b, :all=>true)}.should raise_error(Sequel::InvalidOperation)
|
2594
2638
|
proc{@a.except(@b)}.should_not raise_error
|
2595
|
-
proc{@a.except(@b, true)}.should raise_error(Sequel::InvalidOperation)
|
2639
|
+
proc{@a.except(@b, :all=>true)}.should raise_error(Sequel::InvalidOperation)
|
2596
2640
|
end
|
2597
2641
|
|
2598
2642
|
specify "should handle chained compound operations" do
|
2599
|
-
@a.union(@b).union(@a, true).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM b WHERE (z = 2)) AS t1 UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2600
|
-
@a.intersect(@b, true).intersect(@a).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT ALL SELECT * FROM b WHERE (z = 2)) AS t1 INTERSECT SELECT * FROM a WHERE (z = 1)) AS t1"
|
2601
|
-
@a.except(@b).except(@a, true).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM b WHERE (z = 2)) AS t1 EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2643
|
+
@a.union(@b).union(@a, :all=>true).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM b WHERE (z = 2)) AS t1 UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2644
|
+
@a.intersect(@b, :all=>true).intersect(@a).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT ALL SELECT * FROM b WHERE (z = 2)) AS t1 INTERSECT SELECT * FROM a WHERE (z = 1)) AS t1"
|
2645
|
+
@a.except(@b).except(@a, :all=>true).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM b WHERE (z = 2)) AS t1 EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1"
|
2602
2646
|
end
|
2603
2647
|
|
2604
2648
|
specify "should use a subselect when using a compound operation with a dataset that already has a compound operation" do
|
2605
|
-
@a.union(@b.union(@a, true)).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM (SELECT * FROM b WHERE (z = 2) UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2606
|
-
@a.intersect(@b.intersect(@a), true).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT ALL SELECT * FROM (SELECT * FROM b WHERE (z = 2) INTERSECT SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2607
|
-
@a.except(@b.except(@a, true)).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2649
|
+
@a.union(@b.union(@a, :all=>true)).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) UNION SELECT * FROM (SELECT * FROM b WHERE (z = 2) UNION ALL SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2650
|
+
@a.intersect(@b.intersect(@a), :all=>true).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) INTERSECT ALL SELECT * FROM (SELECT * FROM b WHERE (z = 2) INTERSECT SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2651
|
+
@a.except(@b.except(@a, :all=>true)).sql.should == "SELECT * FROM (SELECT * FROM a WHERE (z = 1) EXCEPT SELECT * FROM (SELECT * FROM b WHERE (z = 2) EXCEPT ALL SELECT * FROM a WHERE (z = 1)) AS t1) AS t1"
|
2608
2652
|
end
|
2609
2653
|
|
2610
2654
|
specify "should order and limit properly when using UNION, INTERSECT, or EXCEPT" do
|
2611
|
-
@dataset = Sequel
|
2655
|
+
@dataset = Sequel.mock.dataset.from(:test)
|
2612
2656
|
@dataset.union(@dataset).limit(2).sql.should == "SELECT * FROM (SELECT * FROM test UNION SELECT * FROM test) AS t1 LIMIT 2"
|
2613
2657
|
@dataset.limit(2).intersect(@dataset).sql.should == "SELECT * FROM (SELECT * FROM (SELECT * FROM test LIMIT 2) AS t1 INTERSECT SELECT * FROM test) AS t1"
|
2614
2658
|
@dataset.except(@dataset.limit(2)).sql.should == "SELECT * FROM (SELECT * FROM test EXCEPT SELECT * FROM (SELECT * FROM test LIMIT 2) AS t1) AS t1"
|
@@ -2622,7 +2666,7 @@ describe "Dataset compound operations" do
|
|
2622
2666
|
end
|
2623
2667
|
|
2624
2668
|
specify "should hoist WITH clauses in given dataset if dataset doesn't support WITH in subselect" do
|
2625
|
-
ds = Sequel
|
2669
|
+
ds = Sequel.mock.dataset
|
2626
2670
|
meta_def(ds, :supports_cte?){true}
|
2627
2671
|
meta_def(ds, :supports_cte_in_subselect?){false}
|
2628
2672
|
ds.from(:a).union(ds.from(:c).with(:c, ds.from(:d)), :from_self=>false).sql.should == 'WITH c AS (SELECT * FROM d) SELECT * FROM a UNION SELECT * FROM c'
|
@@ -3038,7 +3082,7 @@ end
|
|
3038
3082
|
|
3039
3083
|
describe "Dataset" do
|
3040
3084
|
before do
|
3041
|
-
@d = Sequel
|
3085
|
+
@d = Sequel.mock.dataset.from(:x)
|
3042
3086
|
end
|
3043
3087
|
|
3044
3088
|
specify "should support self-changing select!" do
|
@@ -3086,18 +3130,18 @@ describe "Dataset#to_csv" do
|
|
3086
3130
|
@ds = Sequel.mock(:fetch=>[{:a=>1, :b=>2, :c=>3}, {:a=>4, :b=>5, :c=>6}, {:a=>7, :b=>8, :c=>9}])[:items].columns(:a, :b, :c)
|
3087
3131
|
end
|
3088
3132
|
|
3089
|
-
|
3133
|
+
qspecify "should format a CSV representation of the records" do
|
3090
3134
|
@ds.to_csv.should == "a, b, c\r\n1, 2, 3\r\n4, 5, 6\r\n7, 8, 9\r\n"
|
3091
3135
|
end
|
3092
3136
|
|
3093
|
-
|
3137
|
+
qspecify "should exclude column titles if so specified" do
|
3094
3138
|
@ds.to_csv(false).should == "1, 2, 3\r\n4, 5, 6\r\n7, 8, 9\r\n"
|
3095
3139
|
end
|
3096
3140
|
end
|
3097
3141
|
|
3098
3142
|
describe "Dataset#update_sql" do
|
3099
3143
|
before do
|
3100
|
-
@ds = Sequel
|
3144
|
+
@ds = Sequel.mock.dataset.from(:items)
|
3101
3145
|
end
|
3102
3146
|
|
3103
3147
|
specify "should accept strings" do
|
@@ -3119,7 +3163,7 @@ end
|
|
3119
3163
|
|
3120
3164
|
describe "Dataset#insert_sql" do
|
3121
3165
|
before do
|
3122
|
-
@ds = Sequel
|
3166
|
+
@ds = Sequel.mock.dataset.from(:items)
|
3123
3167
|
end
|
3124
3168
|
|
3125
3169
|
specify "should accept hash with symbol keys" do
|
@@ -3191,13 +3235,13 @@ describe "Dataset#inspect" do
|
|
3191
3235
|
end
|
3192
3236
|
|
3193
3237
|
specify "should include the class name and the corresponding SQL statement" do
|
3194
|
-
Sequel::Dataset.new(
|
3195
|
-
InspectDataset.new(
|
3238
|
+
Sequel::Dataset.new(Sequel.mock).from(:blah).inspect.should == '#<Sequel::Dataset: "SELECT * FROM blah">'
|
3239
|
+
InspectDataset.new(Sequel.mock).from(:blah).inspect.should == '#<InspectDataset: "SELECT * FROM blah">'
|
3196
3240
|
end
|
3197
3241
|
|
3198
3242
|
specify "should skip anonymous classes" do
|
3199
|
-
Class.new(Class.new(Sequel::Dataset)).new(
|
3200
|
-
Class.new(InspectDataset).new(
|
3243
|
+
Class.new(Class.new(Sequel::Dataset)).new(Sequel.mock).from(:blah).inspect.should == '#<Sequel::Dataset: "SELECT * FROM blah">'
|
3244
|
+
Class.new(InspectDataset).new(Sequel.mock).from(:blah).inspect.should == '#<InspectDataset: "SELECT * FROM blah">'
|
3201
3245
|
end
|
3202
3246
|
end
|
3203
3247
|
|
@@ -3290,7 +3334,7 @@ describe "Dataset default #fetch_rows, #insert, #update, #delete, #with_sql_dele
|
|
3290
3334
|
@ds = @db[:items]
|
3291
3335
|
end
|
3292
3336
|
|
3293
|
-
|
3337
|
+
qspecify "#fetch_rows should raise a Sequel::NotImplemented" do
|
3294
3338
|
proc{@ds.fetch_rows(''){}}.should raise_error(Sequel::NotImplemented)
|
3295
3339
|
end
|
3296
3340
|
|
@@ -3535,10 +3579,11 @@ end
|
|
3535
3579
|
|
3536
3580
|
describe "Sequel::Dataset #set_defaults" do
|
3537
3581
|
before do
|
3538
|
-
@ds = Sequel
|
3582
|
+
@ds = Sequel.mock.dataset.from(:items)
|
3539
3583
|
end
|
3540
3584
|
|
3541
|
-
|
3585
|
+
qspecify "should set the default values for inserts" do
|
3586
|
+
@ds = @ds.set_defaults(:x=>1)
|
3542
3587
|
@ds.insert_sql.should == "INSERT INTO items (x) VALUES (1)"
|
3543
3588
|
@ds.insert_sql(:x=>2).should == "INSERT INTO items (x) VALUES (2)"
|
3544
3589
|
@ds.insert_sql(:y=>2).should =~ /INSERT INTO items \([xy], [xy]\) VALUES \([21], [21]\)/
|
@@ -3546,7 +3591,8 @@ describe "Sequel::Dataset #set_defaults" do
|
|
3546
3591
|
@ds.set_defaults(:x=>2).insert_sql.should == "INSERT INTO items (x) VALUES (2)"
|
3547
3592
|
end
|
3548
3593
|
|
3549
|
-
|
3594
|
+
qspecify "should set the default values for updates" do
|
3595
|
+
@ds = @ds.set_defaults(:x=>1)
|
3550
3596
|
@ds.update_sql.should == "UPDATE items SET x = 1"
|
3551
3597
|
@ds.update_sql(:x=>2).should == "UPDATE items SET x = 2"
|
3552
3598
|
@ds.update_sql(:y=>2).should =~ /UPDATE items SET (x = 1|y = 2), (x = 1|y = 2)/
|
@@ -3557,10 +3603,11 @@ end
|
|
3557
3603
|
|
3558
3604
|
describe "Sequel::Dataset #set_overrides" do
|
3559
3605
|
before do
|
3560
|
-
@ds = Sequel
|
3606
|
+
@ds = Sequel.mock.dataset.from(:items)
|
3561
3607
|
end
|
3562
3608
|
|
3563
|
-
|
3609
|
+
qspecify "should override the given values for inserts" do
|
3610
|
+
@ds = @ds.set_overrides(:x=>1)
|
3564
3611
|
@ds.insert_sql.should == "INSERT INTO items (x) VALUES (1)"
|
3565
3612
|
@ds.insert_sql(:x=>2).should == "INSERT INTO items (x) VALUES (1)"
|
3566
3613
|
@ds.insert_sql(:y=>2).should =~ /INSERT INTO items \([xy], [xy]\) VALUES \([21], [21]\)/
|
@@ -3568,7 +3615,8 @@ describe "Sequel::Dataset #set_overrides" do
|
|
3568
3615
|
@ds.set_overrides(:x=>2).insert_sql.should == "INSERT INTO items (x) VALUES (1)"
|
3569
3616
|
end
|
3570
3617
|
|
3571
|
-
|
3618
|
+
qspecify "should override the given values for updates" do
|
3619
|
+
@ds = @ds.set_overrides(:x=>1)
|
3572
3620
|
@ds.update_sql.should == "UPDATE items SET x = 1"
|
3573
3621
|
@ds.update_sql(:x=>2).should == "UPDATE items SET x = 1"
|
3574
3622
|
@ds.update_sql(:y=>2).should =~ /UPDATE items SET (x = 1|y = 2), (x = 1|y = 2)/
|
@@ -3577,112 +3625,106 @@ describe "Sequel::Dataset #set_overrides" do
|
|
3577
3625
|
end
|
3578
3626
|
end
|
3579
3627
|
|
3580
|
-
describe "Sequel::Dataset#
|
3581
|
-
|
3582
|
-
Sequel
|
3583
|
-
end
|
3584
|
-
|
3585
|
-
specify "should qualify to the first source if no table if given" do
|
3586
|
-
Sequel::Dataset.new(nil).from(:t).filter{a<b}.qualify.sql.should == 'SELECT t.* FROM t WHERE (t.a < t.b)'
|
3628
|
+
describe "Sequel::Dataset#qualify_to_first_source" do
|
3629
|
+
qspecify "should qualify to the first source" do
|
3630
|
+
Sequel.mock.dataset.from(:t).filter{a<b}.qualify_to_first_source.sql.should == 'SELECT t.* FROM t WHERE (t.a < t.b)'
|
3587
3631
|
end
|
3588
3632
|
end
|
3589
3633
|
|
3590
3634
|
describe "Sequel::Dataset#qualify_to" do
|
3591
|
-
|
3592
|
-
Sequel
|
3635
|
+
qspecify "should qualify to the given table" do
|
3636
|
+
Sequel.mock.dataset.from(:t).filter{a<b}.qualify_to(:e).sql.should == 'SELECT e.* FROM t WHERE (e.a < e.b)'
|
3593
3637
|
end
|
3594
3638
|
end
|
3595
3639
|
|
3596
|
-
describe "Sequel::Dataset#
|
3640
|
+
describe "Sequel::Dataset#qualify" do
|
3597
3641
|
before do
|
3598
3642
|
@ds = Sequel::Database.new[:t]
|
3599
3643
|
end
|
3600
3644
|
|
3601
|
-
specify "should
|
3602
|
-
@ds.
|
3603
|
-
@ds.should_receive(:qualify_to).with(:t).once
|
3604
|
-
@ds.qualify_to_first_source
|
3645
|
+
specify "should qualify to the table if one is given" do
|
3646
|
+
@ds.filter{a<b}.qualify(:e).sql.should == 'SELECT e.* FROM t WHERE (e.a < e.b)'
|
3605
3647
|
end
|
3606
3648
|
|
3607
3649
|
specify "should handle the select, order, where, having, and group options/clauses" do
|
3608
|
-
@ds.select(:a).filter(:a=>1).order(:a).group(:a).having(:a).
|
3650
|
+
@ds.select(:a).filter(:a=>1).order(:a).group(:a).having(:a).qualify.sql.should == 'SELECT t.a FROM t WHERE (t.a = 1) GROUP BY t.a HAVING t.a ORDER BY t.a'
|
3609
3651
|
end
|
3610
3652
|
|
3611
3653
|
specify "should handle the select using a table.* if all columns are currently selected" do
|
3612
|
-
@ds.filter(:a=>1).order(:a).group(:a).having(:a).
|
3654
|
+
@ds.filter(:a=>1).order(:a).group(:a).having(:a).qualify.sql.should == 'SELECT t.* FROM t WHERE (t.a = 1) GROUP BY t.a HAVING t.a ORDER BY t.a'
|
3613
3655
|
end
|
3614
3656
|
|
3615
|
-
|
3616
|
-
@ds.select(:a=>:b).
|
3657
|
+
qspecify "should handle hashes in select option" do
|
3658
|
+
@ds.select(:a=>:b).qualify.sql.should == 'SELECT t.a AS b FROM t'
|
3617
3659
|
end
|
3618
3660
|
|
3619
3661
|
specify "should handle symbols" do
|
3620
|
-
@ds.select(:a, :b__c, :d___e, :f__g___h).
|
3662
|
+
@ds.select(:a, :b__c, :d___e, :f__g___h).qualify.sql.should == 'SELECT t.a, b.c, t.d AS e, f.g AS h FROM t'
|
3621
3663
|
end
|
3622
3664
|
|
3623
3665
|
specify "should handle arrays" do
|
3624
|
-
@ds.filter(:a=>[:b, :c]).
|
3666
|
+
@ds.filter(:a=>[:b, :c]).qualify.sql.should == 'SELECT t.* FROM t WHERE (t.a IN (t.b, t.c))'
|
3625
3667
|
end
|
3626
3668
|
|
3627
3669
|
specify "should handle hashes" do
|
3628
|
-
@ds.select(Sequel.case({:b=>{:c=>1}}, false)).
|
3670
|
+
@ds.select(Sequel.case({:b=>{:c=>1}}, false)).qualify.sql.should == "SELECT (CASE WHEN t.b THEN (t.c = 1) ELSE 'f' END) FROM t"
|
3629
3671
|
end
|
3630
3672
|
|
3631
3673
|
specify "should handle SQL::Identifiers" do
|
3632
|
-
@ds.select{a}.
|
3674
|
+
@ds.select{a}.qualify.sql.should == 'SELECT t.a FROM t'
|
3633
3675
|
end
|
3634
3676
|
|
3635
3677
|
specify "should handle SQL::OrderedExpressions" do
|
3636
|
-
@ds.order(Sequel.desc(:a), Sequel.asc(:b)).
|
3678
|
+
@ds.order(Sequel.desc(:a), Sequel.asc(:b)).qualify.sql.should == 'SELECT t.* FROM t ORDER BY t.a DESC, t.b ASC'
|
3637
3679
|
end
|
3638
3680
|
|
3639
3681
|
specify "should handle SQL::AliasedExpressions" do
|
3640
|
-
@ds.select(Sequel.expr(:a).as(:b)).
|
3682
|
+
@ds.select(Sequel.expr(:a).as(:b)).qualify.sql.should == 'SELECT t.a AS b FROM t'
|
3641
3683
|
end
|
3642
3684
|
|
3643
3685
|
specify "should handle SQL::CaseExpressions" do
|
3644
|
-
@ds.filter{Sequel.case({a=>b}, c, d)}.
|
3686
|
+
@ds.filter{Sequel.case({a=>b}, c, d)}.qualify.sql.should == 'SELECT t.* FROM t WHERE (CASE t.d WHEN t.a THEN t.b ELSE t.c END)'
|
3645
3687
|
end
|
3646
3688
|
|
3647
3689
|
specify "should handle SQL:Casts" do
|
3648
|
-
@ds.filter{a.cast(:boolean)}.
|
3690
|
+
@ds.filter{a.cast(:boolean)}.qualify.sql.should == 'SELECT t.* FROM t WHERE CAST(t.a AS boolean)'
|
3649
3691
|
end
|
3650
3692
|
|
3651
3693
|
specify "should handle SQL::Functions" do
|
3652
|
-
@ds.filter{a(b, 1)}.
|
3694
|
+
@ds.filter{a(b, 1)}.qualify.sql.should == 'SELECT t.* FROM t WHERE a(t.b, 1)'
|
3653
3695
|
end
|
3654
3696
|
|
3655
3697
|
specify "should handle SQL::ComplexExpressions" do
|
3656
|
-
@ds.filter{(a+b)<(c-3)}.
|
3698
|
+
@ds.filter{(a+b)<(c-3)}.qualify.sql.should == 'SELECT t.* FROM t WHERE ((t.a + t.b) < (t.c - 3))'
|
3657
3699
|
end
|
3658
3700
|
|
3659
3701
|
specify "should handle SQL::ValueLists" do
|
3660
|
-
@ds.filter(:a=>Sequel.value_list([:b, :c])).
|
3702
|
+
@ds.filter(:a=>Sequel.value_list([:b, :c])).qualify.sql.should == 'SELECT t.* FROM t WHERE (t.a IN (t.b, t.c))'
|
3661
3703
|
end
|
3662
3704
|
|
3663
3705
|
specify "should handle SQL::Subscripts" do
|
3664
|
-
@ds.filter{a.sql_subscript(b,3)}.
|
3706
|
+
@ds.filter{a.sql_subscript(b,3)}.qualify.sql.should == 'SELECT t.* FROM t WHERE t.a[t.b, 3]'
|
3665
3707
|
end
|
3666
3708
|
|
3667
3709
|
specify "should handle SQL::PlaceholderLiteralStrings" do
|
3668
|
-
@ds.filter('? > ?', :a, 1).
|
3710
|
+
@ds.filter('? > ?', :a, 1).qualify.sql.should == 'SELECT t.* FROM t WHERE (t.a > 1)'
|
3669
3711
|
end
|
3670
3712
|
|
3671
3713
|
specify "should handle SQL::PlaceholderLiteralStrings with named placeholders" do
|
3672
|
-
@ds.filter(':a > :b', :a=>:c, :b=>1).
|
3714
|
+
@ds.filter(':a > :b', :a=>:c, :b=>1).qualify.sql.should == 'SELECT t.* FROM t WHERE (t.c > 1)'
|
3673
3715
|
end
|
3674
3716
|
|
3675
3717
|
specify "should handle SQL::Wrappers" do
|
3676
|
-
@ds.filter(Sequel::SQL::Wrapper.new(:a)).
|
3718
|
+
@ds.filter(Sequel::SQL::Wrapper.new(:a)).qualify.sql.should == 'SELECT t.* FROM t WHERE t.a'
|
3677
3719
|
end
|
3678
3720
|
|
3679
3721
|
specify "should handle SQL::WindowFunctions" do
|
3680
3722
|
meta_def(@ds, :supports_window_functions?){true}
|
3681
|
-
@ds.select{sum(:over, :args=>:a, :partition=>:b, :order=>:c){}}.
|
3723
|
+
@ds.select{sum(:over, :args=>:a, :partition=>:b, :order=>:c){}}.qualify.sql.should == 'SELECT sum(t.a) OVER (PARTITION BY t.b ORDER BY t.c) FROM t'
|
3682
3724
|
end
|
3683
3725
|
|
3684
3726
|
specify "should handle all other objects by returning them unchanged" do
|
3685
|
-
@ds.select("a").filter{a(3)}.filter('blah').order(Sequel.lit('true')).group(Sequel.lit('a > ?', 1)).having(false).
|
3727
|
+
@ds.select("a").filter{a(3)}.filter('blah').order(Sequel.lit('true')).group(Sequel.lit('a > ?', 1)).having(false).qualify.sql.should == "SELECT 'a' FROM t WHERE (a(3) AND (blah)) GROUP BY a > 1 HAVING 'f' ORDER BY true"
|
3686
3728
|
end
|
3687
3729
|
end
|
3688
3730
|
|
@@ -4073,11 +4115,16 @@ describe "Sequel::Dataset#select_map" do
|
|
4073
4115
|
@ds.db.sqls.should == []
|
4074
4116
|
end
|
4075
4117
|
|
4076
|
-
specify "should
|
4118
|
+
specify "should handle an expression without a determinable alias" do
|
4077
4119
|
@ds.select_map{a(t__c)}.should == [1, 2]
|
4078
4120
|
@ds.db.sqls.should == ['SELECT a(t.c) FROM t']
|
4079
4121
|
end
|
4080
4122
|
|
4123
|
+
specify "should accept a block" do
|
4124
|
+
@ds.select_map{a(t__c).as(b)}.should == [1, 2]
|
4125
|
+
@ds.db.sqls.should == ['SELECT a(t.c) AS b FROM t']
|
4126
|
+
end
|
4127
|
+
|
4081
4128
|
specify "should accept a block with an array of columns" do
|
4082
4129
|
@ds.select_map{[a(t__c).as(c), a(t__c).as(c)]}.should == [[1, 1], [2, 2]]
|
4083
4130
|
@ds.db.sqls.should == ['SELECT a(t.c) AS c, a(t.c) AS c FROM t']
|
@@ -4147,11 +4194,16 @@ describe "Sequel::Dataset#select_order_map" do
|
|
4147
4194
|
@ds.db.sqls.should == ['SELECT a FROM t ORDER BY a DESC']
|
4148
4195
|
end
|
4149
4196
|
|
4150
|
-
specify "should
|
4197
|
+
specify "should handle an expression without a determinable alias" do
|
4151
4198
|
@ds.select_order_map{a(t__c)}.should == [1, 2]
|
4152
4199
|
@ds.db.sqls.should == ['SELECT a(t.c) FROM t ORDER BY a(t.c)']
|
4153
4200
|
end
|
4154
4201
|
|
4202
|
+
specify "should accept a block" do
|
4203
|
+
@ds.select_order_map{a(t__c).as(b)}.should == [1, 2]
|
4204
|
+
@ds.db.sqls.should == ['SELECT a(t.c) AS b FROM t ORDER BY a(t.c)']
|
4205
|
+
end
|
4206
|
+
|
4155
4207
|
specify "should accept a block with an array of columns" do
|
4156
4208
|
@ds.select_order_map{[c.desc, a(t__c).as(c)]}.should == [[1, 1], [2, 2]]
|
4157
4209
|
@ds.db.sqls.should == ['SELECT c, a(t.c) AS c FROM t ORDER BY c DESC, a(t.c)']
|
@@ -4311,7 +4363,7 @@ end
|
|
4311
4363
|
|
4312
4364
|
describe "Dataset#lock_style and for_update" do
|
4313
4365
|
before do
|
4314
|
-
@ds = Sequel
|
4366
|
+
@ds = Sequel.mock.dataset.from(:t)
|
4315
4367
|
end
|
4316
4368
|
|
4317
4369
|
specify "#for_update should use FOR UPDATE" do
|
@@ -4334,7 +4386,7 @@ describe "Custom ASTTransformer" do
|
|
4334
4386
|
(s.is_a?(Symbol) || s.is_a?(String)) ? :"#{s}#{s}" : super
|
4335
4387
|
end
|
4336
4388
|
end.new
|
4337
|
-
ds = Sequel
|
4389
|
+
ds = Sequel.mock.dataset.from(:t).cross_join(:a___g).join(:b___h, [:c]).join(:d___i, :e=>:f)
|
4338
4390
|
ds.sql.should == 'SELECT * FROM t CROSS JOIN a AS g INNER JOIN b AS h USING (c) INNER JOIN d AS i ON (i.e = h.f)'
|
4339
4391
|
ds.clone(:from=>c.transform(ds.opts[:from]), :join=>c.transform(ds.opts[:join])).sql.should == 'SELECT * FROM tt CROSS JOIN aa AS gg INNER JOIN bb AS hh USING (cc) INNER JOIN dd AS ii ON (ii.ee = hh.ff)'
|
4340
4392
|
end
|
@@ -4425,7 +4477,7 @@ describe "Dataset extensions" do
|
|
4425
4477
|
end
|
4426
4478
|
end
|
4427
4479
|
before do
|
4428
|
-
@ds = Sequel
|
4480
|
+
@ds = Sequel.mock.dataset
|
4429
4481
|
end
|
4430
4482
|
|
4431
4483
|
specify "should be able to register an extension with a module Database#extension extend the module" do
|
@@ -4524,7 +4576,7 @@ describe "Dataset#schema_and_table" do
|
|
4524
4576
|
@ds.schema_and_table(Sequel.qualify(:t, :s)).should == ['t', 's']
|
4525
4577
|
end
|
4526
4578
|
|
4527
|
-
|
4579
|
+
qspecify "should respect default_schema" do
|
4528
4580
|
@ds.db.default_schema = :foo
|
4529
4581
|
@ds.schema_and_table(:s).should == ['foo', 's']
|
4530
4582
|
@ds.schema_and_table(:s, nil).should == [nil, 's']
|
@@ -4564,7 +4616,7 @@ describe "Dataset#split_qualifiers" do
|
|
4564
4616
|
@ds.split_qualifiers(Sequel.qualify(Sequel.qualify(:d, :t), Sequel.qualify(:s, :s2))).should == ['d', 't', 's', 's2']
|
4565
4617
|
end
|
4566
4618
|
|
4567
|
-
|
4619
|
+
qspecify "should respect default_schema" do
|
4568
4620
|
@ds.db.default_schema = :foo
|
4569
4621
|
@ds.split_qualifiers(:s).should == ['foo', 's']
|
4570
4622
|
@ds.split_qualifiers(:s, nil).should == ['s']
|