sequel 2.12.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/CHANGELOG +62 -0
  2. data/README.rdoc +3 -3
  3. data/Rakefile +7 -0
  4. data/doc/advanced_associations.rdoc +44 -0
  5. data/doc/release_notes/3.0.0.txt +221 -0
  6. data/lib/sequel/adapters/amalgalite.rb +208 -0
  7. data/lib/sequel/adapters/db2.rb +3 -0
  8. data/lib/sequel/adapters/dbi.rb +9 -0
  9. data/lib/sequel/adapters/do.rb +0 -4
  10. data/lib/sequel/adapters/firebird.rb +16 -18
  11. data/lib/sequel/adapters/informix.rb +5 -3
  12. data/lib/sequel/adapters/jdbc.rb +24 -20
  13. data/lib/sequel/adapters/jdbc/h2.rb +15 -4
  14. data/lib/sequel/adapters/mysql.rb +4 -8
  15. data/lib/sequel/adapters/odbc.rb +0 -4
  16. data/lib/sequel/adapters/oracle.rb +0 -4
  17. data/lib/sequel/adapters/shared/mssql.rb +16 -5
  18. data/lib/sequel/adapters/shared/mysql.rb +87 -86
  19. data/lib/sequel/adapters/shared/oracle.rb +92 -3
  20. data/lib/sequel/adapters/shared/postgres.rb +85 -29
  21. data/lib/sequel/adapters/shared/progress.rb +8 -3
  22. data/lib/sequel/adapters/shared/sqlite.rb +53 -23
  23. data/lib/sequel/adapters/sqlite.rb +4 -7
  24. data/lib/sequel/adapters/utils/unsupported.rb +3 -3
  25. data/lib/sequel/connection_pool.rb +18 -25
  26. data/lib/sequel/core.rb +2 -21
  27. data/lib/sequel/database.rb +60 -44
  28. data/lib/sequel/database/schema_generator.rb +26 -31
  29. data/lib/sequel/database/schema_methods.rb +8 -3
  30. data/lib/sequel/database/schema_sql.rb +114 -28
  31. data/lib/sequel/dataset.rb +14 -41
  32. data/lib/sequel/dataset/convenience.rb +31 -54
  33. data/lib/sequel/dataset/graph.rb +7 -13
  34. data/lib/sequel/dataset/sql.rb +43 -54
  35. data/lib/sequel/extensions/inflector.rb +0 -5
  36. data/lib/sequel/extensions/schema_dumper.rb +238 -0
  37. data/lib/sequel/metaprogramming.rb +0 -20
  38. data/lib/sequel/model.rb +1 -2
  39. data/lib/sequel/model/base.rb +18 -16
  40. data/lib/sequel/model/inflections.rb +6 -9
  41. data/lib/sequel/plugins/caching.rb +0 -6
  42. data/lib/sequel/plugins/hook_class_methods.rb +1 -1
  43. data/lib/sequel/sql.rb +2 -0
  44. data/lib/sequel/version.rb +2 -2
  45. data/spec/adapters/firebird_spec.rb +35 -8
  46. data/spec/adapters/mysql_spec.rb +173 -266
  47. data/spec/adapters/oracle_spec.rb +13 -0
  48. data/spec/adapters/postgres_spec.rb +127 -227
  49. data/spec/adapters/sqlite_spec.rb +13 -171
  50. data/spec/core/connection_pool_spec.rb +15 -4
  51. data/spec/core/core_sql_spec.rb +14 -170
  52. data/spec/core/database_spec.rb +50 -132
  53. data/spec/core/dataset_spec.rb +47 -930
  54. data/spec/core/expression_filters_spec.rb +12 -0
  55. data/spec/core/schema_generator_spec.rb +37 -45
  56. data/spec/core/schema_spec.rb +26 -16
  57. data/spec/core/spec_helper.rb +0 -25
  58. data/spec/extensions/inflector_spec.rb +0 -3
  59. data/spec/extensions/schema_dumper_spec.rb +292 -0
  60. data/spec/extensions/serialization_spec.rb +9 -0
  61. data/spec/extensions/single_table_inheritance_spec.rb +6 -1
  62. data/spec/extensions/spec_helper.rb +1 -3
  63. data/spec/extensions/validation_helpers_spec.rb +4 -4
  64. data/spec/integration/database_test.rb +18 -0
  65. data/spec/integration/dataset_test.rb +112 -1
  66. data/spec/integration/eager_loader_test.rb +70 -9
  67. data/spec/integration/prepared_statement_test.rb +2 -2
  68. data/spec/integration/schema_test.rb +76 -27
  69. data/spec/integration/spec_helper.rb +0 -14
  70. data/spec/integration/transaction_test.rb +27 -0
  71. data/spec/model/associations_spec.rb +0 -36
  72. data/spec/model/base_spec.rb +18 -123
  73. data/spec/model/hooks_spec.rb +2 -235
  74. data/spec/model/inflector_spec.rb +15 -115
  75. data/spec/model/model_spec.rb +0 -120
  76. data/spec/model/plugins_spec.rb +0 -70
  77. data/spec/model/record_spec.rb +35 -93
  78. data/spec/model/spec_helper.rb +0 -27
  79. data/spec/model/validations_spec.rb +0 -931
  80. metadata +9 -14
  81. data/lib/sequel/deprecated.rb +0 -593
  82. data/lib/sequel/deprecated_migration.rb +0 -91
  83. data/lib/sequel/model/deprecated.rb +0 -204
  84. data/lib/sequel/model/deprecated_hooks.rb +0 -103
  85. data/lib/sequel/model/deprecated_inflector.rb +0 -335
  86. data/lib/sequel/model/deprecated_validations.rb +0 -388
  87. data/spec/core/core_ext_spec.rb +0 -156
  88. data/spec/core/migration_spec.rb +0 -263
  89. data/spec/core/pretty_table_spec.rb +0 -58
  90. data/spec/model/caching_spec.rb +0 -217
  91. data/spec/model/schema_spec.rb +0 -92
@@ -56,27 +56,6 @@ context "A new Database" do
56
56
  db.send(:identifier_output_method_default).should == :downcase
57
57
  end
58
58
 
59
- deprec_specify "should respect the :upcase_identifiers option" do
60
- Sequel.upcase_identifiers = false
61
- db = Sequel::Database.new(:upcase_identifiers=>false)
62
- db.upcase_identifiers?.should == false
63
- db.upcase_identifiers = true
64
- db.upcase_identifiers?.should == true
65
- db = Sequel::Database.new(:upcase_identifiers=>true)
66
- db.upcase_identifiers?.should == true
67
- db.upcase_identifiers = false
68
- db.upcase_identifiers?.should == false
69
- Sequel.upcase_identifiers = true
70
- db = Sequel::Database.new(:upcase_identifiers=>false)
71
- db.upcase_identifiers?.should == false
72
- db.upcase_identifiers = true
73
- db.upcase_identifiers?.should == true
74
- db = Sequel::Database.new(:upcase_identifiers=>true)
75
- db.upcase_identifiers?.should == true
76
- db.upcase_identifiers = false
77
- db.upcase_identifiers?.should == false
78
- end
79
-
80
59
  specify "should respect the :identifier_input_method option" do
81
60
  Sequel.identifier_input_method = nil
82
61
  Sequel::Database.identifier_input_method.should == ""
@@ -134,17 +113,6 @@ context "A new Database" do
134
113
  Sequel::Database.new({}).quote_identifiers?.should == false
135
114
  end
136
115
 
137
- deprec_specify "should use the default Sequel.upcase_identifiers value" do
138
- Sequel.upcase_identifiers = true
139
- Sequel::Database.new({}).upcase_identifiers?.should == true
140
- Sequel.upcase_identifiers = false
141
- Sequel::Database.new({}).upcase_identifiers?.should == false
142
- Sequel::Database.upcase_identifiers = true
143
- Sequel::Database.new({}).upcase_identifiers?.should == true
144
- Sequel::Database.upcase_identifiers = false
145
- Sequel::Database.new({}).upcase_identifiers?.should == false
146
- end
147
-
148
116
  specify "should use the default Sequel.identifier_input_method value" do
149
117
  Sequel.identifier_input_method = :downcase
150
118
  Sequel::Database.new({}).identifier_input_method.should == :downcase
@@ -316,28 +284,6 @@ context "Database#<<" do
316
284
  (@db << "DELETE FROM items").should == "DELETE FROM items"
317
285
  end
318
286
 
319
- deprec_specify "should accept an array and convert it to SQL" do
320
- a = %[
321
- --
322
- CREATE TABLE items (a integer, /*b integer*/
323
- b text, c integer);
324
- DROP TABLE old_items;
325
- ].split($/)
326
- (@db << a).should ==
327
- "CREATE TABLE items (a integer, b text, c integer); DROP TABLE old_items;"
328
- end
329
-
330
- deprec_specify "should remove comments and whitespace from arrays" do
331
- s = %[
332
- --
333
- CREATE TABLE items (a integer, /*b integer*/
334
- b text, c integer); \r\n
335
- DROP TABLE old_items;
336
- ].split($/)
337
- (@db << s).should ==
338
- "CREATE TABLE items (a integer, b text, c integer); DROP TABLE old_items;"
339
- end
340
-
341
287
  specify "should not remove comments and whitespace from strings" do
342
288
  s = "INSERT INTO items VALUES ('---abc')"
343
289
  (@db << s).should == s
@@ -423,6 +369,19 @@ context "Database#create_table" do
423
369
  'CREATE UNIQUE INDEX test_name_index ON test (name)'
424
370
  ]
425
371
  end
372
+
373
+ specify "should create a temporary table" do
374
+ @db.create_table :test_tmp, :temp => true do
375
+ primary_key :id, :integer, :null => false
376
+ column :name, :text
377
+ index :name, :unique => true
378
+ end
379
+
380
+ @db.sqls.should == [
381
+ 'CREATE TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
382
+ 'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)'
383
+ ]
384
+ end
426
385
  end
427
386
 
428
387
  context "Database#alter_table" do
@@ -695,6 +654,10 @@ context "A Database adapter with a scheme" do
695
654
  Sequel::ADAPTER_MAP[:ccc].should == CCC
696
655
  end
697
656
 
657
+ specify "should give the database_type as the adapter scheme by default" do
658
+ CCC.new.database_type.should == :ccc
659
+ end
660
+
698
661
  specify "should be instantiated when its scheme is specified" do
699
662
  c = Sequel::Database.connect('ccc://localhost/db')
700
663
  c.should be_a_kind_of(CCC)
@@ -733,13 +696,6 @@ context "A Database adapter with a scheme" do
733
696
  CCC::DISCONNECTS.should == [z, y, x]
734
697
  end
735
698
 
736
- deprec_specify "should be accessible through Sequel.open" do
737
- c = Sequel.open 'ccc://localhost/db'
738
- c.should be_a_kind_of(CCC)
739
- c.opts[:host].should == 'localhost'
740
- c.opts[:database].should == 'db'
741
- end
742
-
743
699
  specify "should be accessible through Sequel.<adapter>" do
744
700
  Sequel.send(:def_adapter_method, :ccc)
745
701
 
@@ -863,25 +819,6 @@ context "A database" do
863
819
  Sequel::Database.single_threaded = false
864
820
  end
865
821
 
866
- deprec_specify "should have a multi_threaded? method" do
867
- db = Sequel::Database.new(:single_threaded => true)
868
- db.should_not be_multi_threaded
869
-
870
- db = Sequel::Database.new(:max_options => 1)
871
- db.should be_multi_threaded
872
-
873
- db = Sequel::Database.new
874
- db.should be_multi_threaded
875
-
876
- Sequel::Database.single_threaded = true
877
-
878
- db = Sequel::Database.new
879
- db.should_not be_multi_threaded
880
-
881
- db = Sequel::Database.new(:max_options => 4)
882
- db.should_not be_multi_threaded
883
- end
884
-
885
822
  specify "should have single_threaded? respond to true if in single threaded mode" do
886
823
  db = Sequel::Database.new(:single_threaded => true)
887
824
  db.should be_single_threaded
@@ -901,20 +838,6 @@ context "A database" do
901
838
  db.should be_single_threaded
902
839
  end
903
840
 
904
- deprec_specify "should have a logger method" do
905
- db = Sequel::Database.new
906
- s = "I'm a logger"
907
- db.logger = s
908
- db.logger.should == s
909
- db.logger = nil
910
- db.logger.should == nil
911
- db.loggers = []
912
- db.logger.should == nil
913
- t = "I'm also a logger"
914
- db.loggers = [s, t]
915
- db.logger.should == s
916
- end
917
-
918
841
  specify "should be able to set loggers via the logger= and loggers= methods" do
919
842
  db = Sequel::Database.new
920
843
  s = "I'm a logger"
@@ -934,18 +857,6 @@ context "A database" do
934
857
  end
935
858
  end
936
859
 
937
- context "Database#dataset" do
938
- before do
939
- @db = Sequel::Database.new
940
- end
941
-
942
- deprec_specify "should delegate to Dataset#query if block is provided" do
943
- @d = @db.query {select :x; from :y}
944
- @d.should be_a_kind_of(Sequel::Dataset)
945
- @d.sql.should == "SELECT x FROM y"
946
- end
947
- end
948
-
949
860
  context "Database#fetch" do
950
861
  before do
951
862
  @db = Sequel::Database.new
@@ -1084,32 +995,6 @@ context "Database#alter_table_sql" do
1084
995
  end
1085
996
  end
1086
997
 
1087
- context "Database.connect" do
1088
- EEE_YAML = "development:\r\n adapter: eee\r\n username: mau\r\n password: tau\r\n host: alfonso\r\n database: mydb\r\n"
1089
-
1090
- before do
1091
- class EEE < Sequel::Database
1092
- set_adapter_scheme :eee
1093
- end
1094
-
1095
- @fn = File.join(File.dirname(__FILE__), 'eee.yaml')
1096
- File.open(@fn, 'wb') {|f| f << EEE_YAML}
1097
- end
1098
-
1099
- after do
1100
- File.delete(@fn)
1101
- end
1102
-
1103
- specify "should accept hashes loaded from YAML files" do
1104
- db = Sequel.connect(YAML.load_file(@fn)['development'])
1105
- db.class.should == EEE
1106
- db.opts[:database].should == 'mydb'
1107
- db.opts[:user].should == 'mau'
1108
- db.opts[:password].should == 'tau'
1109
- db.opts[:host].should == 'alfonso'
1110
- end
1111
- end
1112
-
1113
998
  context "Database#inspect" do
1114
999
  before do
1115
1000
  @db = DummyDatabase.new
@@ -1222,3 +1107,36 @@ context "Database#typecast_value" do
1222
1107
  proc{@db.typecast_value(:datetime, 4)}.should raise_error(Sequel::InvalidValue)
1223
1108
  end
1224
1109
  end
1110
+
1111
+ context "Database#blank_object?" do
1112
+ specify "should return whether the object is considered blank" do
1113
+ db = Sequel::Database.new
1114
+ c = lambda{|meth, value| Class.new{define_method(meth){value}}.new}
1115
+
1116
+ db.send(:blank_object?, "").should == true
1117
+ db.send(:blank_object?, " ").should == true
1118
+ db.send(:blank_object?, nil).should == true
1119
+ db.send(:blank_object?, false).should == true
1120
+ db.send(:blank_object?, []).should == true
1121
+ db.send(:blank_object?, {}).should == true
1122
+ db.send(:blank_object?, c[:empty?, true]).should == true
1123
+ db.send(:blank_object?, c[:blank?, true]).should == true
1124
+
1125
+ db.send(:blank_object?, " a ").should == false
1126
+ db.send(:blank_object?, 1).should == false
1127
+ db.send(:blank_object?, 1.0).should == false
1128
+ db.send(:blank_object?, true).should == false
1129
+ db.send(:blank_object?, [1]).should == false
1130
+ db.send(:blank_object?, {1.0=>2.0}).should == false
1131
+ db.send(:blank_object?, c[:empty?, false]).should == false
1132
+ db.send(:blank_object?, c[:blank?, false]).should == false
1133
+ end
1134
+ end
1135
+
1136
+ context "Database#schema_autoincrementing_primary_key?" do
1137
+ specify "should whether the parsed schema row indicates a primary key" do
1138
+ m = Sequel::Database.new.method(:schema_autoincrementing_primary_key?)
1139
+ m.call(:primary_key=>true).should == true
1140
+ m.call(:primary_key=>false).should == false
1141
+ end
1142
+ end
@@ -74,20 +74,6 @@ context "Dataset" do
74
74
  @dataset.literal(:a).should == 'a'
75
75
  end
76
76
 
77
- deprec_specify "should have upcase_identifiers= method which changes literalization of identifiers" do
78
- @dataset.upcase_identifiers = true
79
- @dataset.literal(:a).should == 'A'
80
- @dataset.upcase_identifiers = false
81
- @dataset.literal(:a).should == 'a'
82
- end
83
-
84
- deprec_specify "should have upcase_identifiers? method which returns whether identifiers are currently upcased" do
85
- @dataset.upcase_identifiers = true
86
- @dataset.upcase_identifiers?.should == true
87
- @dataset.upcase_identifiers = false
88
- @dataset.upcase_identifiers?.should == false
89
- end
90
-
91
77
  specify "should have identifier_input_method= method which changes literalization of identifiers" do
92
78
  @dataset.identifier_input_method = :upcase
93
79
  @dataset.literal(:a).should == 'A'
@@ -431,6 +417,15 @@ context "Dataset#where" do
431
417
  "SELECT * FROM test WHERE 'f'"
432
418
  end
433
419
 
420
+ specify "should allow the use of multiple arguments" do
421
+ @dataset.filter(:a, :b).sql.should ==
422
+ 'SELECT * FROM test WHERE (a AND b)'
423
+ @dataset.filter(:a, :b=>1).sql.should ==
424
+ 'SELECT * FROM test WHERE (a AND (b = 1))'
425
+ @dataset.filter(:a, :c.sql_number > 3, :b=>1).sql.should ==
426
+ 'SELECT * FROM test WHERE (a AND (c > 3) AND (b = 1))'
427
+ end
428
+
434
429
  specify "should allow the use of blocks and arguments simultaneously" do
435
430
  @dataset.filter(:zz.sql_number < 3){:yy.sql_number > 3}.sql.should ==
436
431
  'SELECT * FROM test WHERE ((zz < 3) AND (yy > 3))'
@@ -548,15 +543,15 @@ context "Dataset#exclude" do
548
543
  @dataset = Sequel::Dataset.new(nil).from(:test)
549
544
  end
550
545
 
551
- specify "should correctly include the NOT operator when one condition is given" do
546
+ specify "should correctly negate the expression when one condition is given" do
552
547
  @dataset.exclude(:region=>'Asia').select_sql.should ==
553
548
  "SELECT * FROM test WHERE (region != 'Asia')"
554
549
  end
555
550
 
556
551
  specify "should take multiple conditions as a hash and express the logic correctly in SQL" do
557
552
  @dataset.exclude(:region => 'Asia', :name => 'Japan').select_sql.
558
- should match(Regexp.union(/WHERE \(\(region != 'Asia'\) AND \(name != 'Japan'\)\)/,
559
- /WHERE \(\(name != 'Japan'\) AND \(region != 'Asia'\)\)/))
553
+ should match(Regexp.union(/WHERE \(\(region != 'Asia'\) OR \(name != 'Japan'\)\)/,
554
+ /WHERE \(\(name != 'Japan'\) OR \(region != 'Asia'\)\)/))
560
555
  end
561
556
 
562
557
  specify "should parenthesize a single string condition correctly" do
@@ -582,6 +577,8 @@ context "Dataset#exclude" do
582
577
  specify "should allow the use of blocks and arguments simultaneously" do
583
578
  @dataset.exclude(:id => (7..11)){:id.sql_number < 6}.sql.should ==
584
579
  'SELECT * FROM test WHERE (((id < 7) OR (id > 11)) OR (id >= 6))'
580
+ @dataset.exclude([:id, 1], [:x, 3]){:id.sql_number < 6}.sql.should ==
581
+ 'SELECT * FROM test WHERE (((id != 1) OR (x != 3)) OR (id >= 6))'
585
582
  end
586
583
  end
587
584
 
@@ -653,7 +650,7 @@ context "a grouped dataset" do
653
650
  specify "should format the right statement for counting (as a subquery)" do
654
651
  db = MockDatabase.new
655
652
  db[:test].select(:name).group(:name).count
656
- db.sqls.should == ["SELECT COUNT(*) FROM (SELECT name FROM test GROUP BY name) AS t1 LIMIT 1"]
653
+ db.sqls.should == ["SELECT COUNT(*) AS count FROM (SELECT name FROM test GROUP BY name) AS t1 LIMIT 1"]
657
654
  end
658
655
  end
659
656
 
@@ -822,8 +819,8 @@ context "Dataset#from" do
822
819
  "SELECT * FROM (SELECT * FROM a) AS t1"
823
820
  end
824
821
 
825
- specify "should raise if no source is given" do
826
- proc {@dataset.from(@dataset.from).select_sql}.should raise_error(Sequel::Error)
822
+ specify "should remove all FROM tables if called with no arguments" do
823
+ @dataset.from.sql.should == 'SELECT *'
827
824
  end
828
825
 
829
826
  specify "should accept sql functions" do
@@ -1252,10 +1249,6 @@ context "Dataset#distinct" do
1252
1249
  @dataset.distinct.sql.should == 'SELECT DISTINCT name FROM test'
1253
1250
  end
1254
1251
 
1255
- deprec_specify "should be aliased by Dataset#uniq" do
1256
- @dataset.uniq.sql.should == 'SELECT DISTINCT name FROM test'
1257
- end
1258
-
1259
1252
  specify "should accept an expression list" do
1260
1253
  @dataset.distinct(:a, :b).sql.should == 'SELECT DISTINCT ON (a, b) name FROM test'
1261
1254
  @dataset.distinct(:stamp.cast(:integer), :node_id=>nil).sql.should == 'SELECT DISTINCT ON (CAST(stamp AS integer), (node_id IS NULL)) name FROM test'
@@ -1263,7 +1256,7 @@ context "Dataset#distinct" do
1263
1256
 
1264
1257
  specify "should do a subselect for count" do
1265
1258
  @dataset.distinct.count
1266
- @db.sqls.should == ['SELECT COUNT(*) FROM (SELECT DISTINCT name FROM test) AS t1 LIMIT 1']
1259
+ @db.sqls.should == ['SELECT COUNT(*) AS count FROM (SELECT DISTINCT name FROM test) AS t1 LIMIT 1']
1267
1260
  end
1268
1261
  end
1269
1262
 
@@ -1285,49 +1278,45 @@ context "Dataset#count" do
1285
1278
 
1286
1279
  specify "should format SQL properly" do
1287
1280
  @dataset.count.should == 1
1288
- @c.sql.should == 'SELECT COUNT(*) FROM test LIMIT 1'
1289
- end
1290
-
1291
- deprec_specify "should be aliased by #size" do
1292
- @dataset.size.should == 1
1281
+ @c.sql.should == 'SELECT COUNT(*) AS count FROM test LIMIT 1'
1293
1282
  end
1294
1283
 
1295
1284
  specify "should include the where clause if it's there" do
1296
1285
  @dataset.filter(:abc.sql_number < 30).count.should == 1
1297
- @c.sql.should == 'SELECT COUNT(*) FROM test WHERE (abc < 30) LIMIT 1'
1286
+ @c.sql.should == 'SELECT COUNT(*) AS count FROM test WHERE (abc < 30) LIMIT 1'
1298
1287
  end
1299
1288
 
1300
1289
  specify "should count properly for datasets with fixed sql" do
1301
1290
  @dataset.opts[:sql] = "select abc from xyz"
1302
1291
  @dataset.count.should == 1
1303
- @c.sql.should == "SELECT COUNT(*) FROM (select abc from xyz) AS t1 LIMIT 1"
1292
+ @c.sql.should == "SELECT COUNT(*) AS count FROM (select abc from xyz) AS t1 LIMIT 1"
1304
1293
  end
1305
1294
 
1306
1295
  specify "should count properly when using UNION, INTERSECT, or EXCEPT" do
1307
1296
  @dataset.union(@dataset).count.should == 1
1308
- @c.sql.should == "SELECT COUNT(*) FROM (SELECT * FROM test UNION SELECT * FROM test) AS t1 LIMIT 1"
1297
+ @c.sql.should == "SELECT COUNT(*) AS count FROM (SELECT * FROM test UNION SELECT * FROM test) AS t1 LIMIT 1"
1309
1298
  @dataset.intersect(@dataset).count.should == 1
1310
- @c.sql.should == "SELECT COUNT(*) FROM (SELECT * FROM test INTERSECT SELECT * FROM test) AS t1 LIMIT 1"
1299
+ @c.sql.should == "SELECT COUNT(*) AS count FROM (SELECT * FROM test INTERSECT SELECT * FROM test) AS t1 LIMIT 1"
1311
1300
  @dataset.except(@dataset).count.should == 1
1312
- @c.sql.should == "SELECT COUNT(*) FROM (SELECT * FROM test EXCEPT SELECT * FROM test) AS t1 LIMIT 1"
1301
+ @c.sql.should == "SELECT COUNT(*) AS count FROM (SELECT * FROM test EXCEPT SELECT * FROM test) AS t1 LIMIT 1"
1313
1302
  end
1314
1303
 
1315
1304
  specify "should return limit if count is greater than it" do
1316
1305
  @dataset.limit(5).count.should == 1
1317
- @c.sql.should == "SELECT COUNT(*) FROM (SELECT * FROM test LIMIT 5) AS t1 LIMIT 1"
1306
+ @c.sql.should == "SELECT COUNT(*) AS count FROM (SELECT * FROM test LIMIT 5) AS t1 LIMIT 1"
1318
1307
  end
1319
1308
 
1320
1309
  it "should work on a graphed_dataset" do
1321
1310
  @dataset.should_receive(:columns).twice.and_return([:a])
1322
1311
  @dataset.graph(@dataset, [:a], :table_alias=>:test2).count.should == 1
1323
- @c.sql.should == 'SELECT COUNT(*) FROM test LEFT OUTER JOIN test AS test2 USING (a) LIMIT 1'
1312
+ @c.sql.should == 'SELECT COUNT(*) AS count FROM test LEFT OUTER JOIN test AS test2 USING (a) LIMIT 1'
1324
1313
  end
1325
1314
 
1326
1315
  specify "should not cache the columns value" do
1327
1316
  ds = @dataset.from(:blah)
1328
1317
  ds.columns.should == [:a]
1329
1318
  ds.count.should == 1
1330
- @c.sql.should == 'SELECT COUNT(*) FROM blah LIMIT 1'
1319
+ @c.sql.should == 'SELECT COUNT(*) AS count FROM blah LIMIT 1'
1331
1320
  ds.columns.should == [:a]
1332
1321
  end
1333
1322
  end
@@ -1357,11 +1346,6 @@ context "Dataset#group_and_count" do
1357
1346
  @ds.group_and_count(:a, :b).sql.should ==
1358
1347
  "SELECT a, b, count(*) AS count FROM test GROUP BY a, b ORDER BY count"
1359
1348
  end
1360
-
1361
- deprec_specify "should work within query block" do
1362
- @ds.query{group_and_count(:a, :b)}.sql.should ==
1363
- "SELECT a, b, count(*) AS count FROM test GROUP BY a, b ORDER BY count"
1364
- end
1365
1349
  end
1366
1350
 
1367
1351
  context "Dataset#empty?" do
@@ -1961,14 +1945,6 @@ context "Dataset#single_record" do
1961
1945
  @d.single_record.should == 'SELECT * FROM test LIMIT 1'
1962
1946
  end
1963
1947
 
1964
- deprec_specify "should pass opts to each" do
1965
- @d.single_record(:order => [:name]).should == 'SELECT * FROM test ORDER BY name LIMIT 1'
1966
- end
1967
-
1968
- deprec_specify "should override the limit if passed as an option" do
1969
- @d.single_record(:limit => 3).should == 'SELECT * FROM test LIMIT 1'
1970
- end
1971
-
1972
1948
  specify "should return nil if no record is present" do
1973
1949
  @e.single_record.should be_nil
1974
1950
  end
@@ -1993,10 +1969,6 @@ context "Dataset#single_value" do
1993
1969
  @d.single_value.should == 'SELECT * FROM test LIMIT 1'
1994
1970
  end
1995
1971
 
1996
- deprec_specify "should pass opts to each" do
1997
- @d.single_value(:from => [:blah]).should == 'SELECT * FROM blah LIMIT 1'
1998
- end
1999
-
2000
1972
  specify "should return nil if no records" do
2001
1973
  @e.single_value.should be_nil
2002
1974
  end
@@ -2071,251 +2043,6 @@ context "Dataset#set_row_proc" do
2071
2043
  end
2072
2044
  end
2073
2045
 
2074
- context "Dataset#set_model" do
2075
- before do
2076
- @c = Class.new(Sequel::Dataset) do
2077
- def fetch_rows(sql, &block)
2078
- # yield a hash with kind as the 1 bit of a number
2079
- (1..10).each {|i| block.call({:kind => i[0]})}
2080
- end
2081
- end
2082
- @dataset = @c.new(nil).from(:items)
2083
- @m = Class.new do
2084
- attr_accessor :c, :args
2085
- def initialize(c, *args); @c = c; @args = args; end
2086
- def ==(o); (@c == o.c) && (@args = o.args); end
2087
- end
2088
- end
2089
-
2090
- deprec_specify "should clear the models hash and restore the stock #each if nil is specified" do
2091
- @dataset.set_model(@m)
2092
- @dataset.set_model(nil)
2093
- @dataset.first.should == {:kind => 1}
2094
- @dataset.model_classes.should be_nil
2095
- end
2096
-
2097
- deprec_specify "should clear the models hash and restore the stock #each if nothing is specified" do
2098
- @dataset.set_model(@m)
2099
- @dataset.set_model(nil)
2100
- @dataset.first.should == {:kind => 1}
2101
- @dataset.model_classes.should be_nil
2102
- end
2103
-
2104
- deprec_specify "should alter #each to provide model instances" do
2105
- @dataset.first.should == {:kind => 1}
2106
- @dataset.set_model(@m)
2107
- @dataset.first.should == @m.new({:kind => 1})
2108
- end
2109
-
2110
- deprec_specify "should set opts[:naked] to nil" do
2111
- @dataset.opts[:naked] = true
2112
- @dataset.set_model(@m)
2113
- @dataset.opts[:naked].should be_nil
2114
- end
2115
-
2116
- deprec_specify "should send additional arguments to the models' initialize method" do
2117
- @dataset.set_model(@m, 7, 6, 5)
2118
- @dataset.first.should == @m.new({:kind => 1}, 7, 6, 5)
2119
- end
2120
-
2121
- deprec_specify "should provide support for polymorphic model instantiation" do
2122
- @m1 = Class.new(@m)
2123
- @m2 = Class.new(@m)
2124
- @dataset.set_model(:kind, 0 => @m1, 1 => @m2)
2125
- @dataset.opts[:polymorphic_key].should == :kind
2126
- all = @dataset.all
2127
- all[0].class.should == @m2
2128
- all[1].class.should == @m1
2129
- all[2].class.should == @m2
2130
- all[3].class.should == @m1
2131
- #...
2132
-
2133
- # denude model
2134
- @dataset.set_model(nil)
2135
- @dataset.first.should == {:kind => 1}
2136
- end
2137
-
2138
- deprec_specify "should send additional arguments for polymorphic models as well" do
2139
- @m1 = Class.new(@m)
2140
- @m2 = Class.new(@m)
2141
- @dataset.set_model(:kind, {0 => @m1, 1 => @m2}, :hey => :wow)
2142
- all = @dataset.all
2143
- all[0].class.should == @m2; all[0].args.should == [{:hey => :wow}]
2144
- all[1].class.should == @m1; all[1].args.should == [{:hey => :wow}]
2145
- all[2].class.should == @m2; all[2].args.should == [{:hey => :wow}]
2146
- all[3].class.should == @m1; all[3].args.should == [{:hey => :wow}]
2147
- end
2148
-
2149
- deprec_specify "should raise for invalid parameters" do
2150
- proc {@dataset.set_model('kind')}.should raise_error(ArgumentError)
2151
- proc {@dataset.set_model(0)}.should raise_error(ArgumentError)
2152
- proc {@dataset.set_model(:kind)}.should raise_error(ArgumentError) # no hash given
2153
- end
2154
- end
2155
-
2156
- context "Dataset#model_classes" do
2157
- before do
2158
- @c = Class.new(Sequel::Dataset) do
2159
- # # We don't need that for now
2160
- # def fetch_rows(sql, &block)
2161
- # (1..10).each(&block)
2162
- # end
2163
- end
2164
- @dataset = @c.new(nil).from(:items)
2165
- @m = Class.new do
2166
- attr_accessor :c
2167
- def initialize(c); @c = c; end
2168
- def ==(o); @c == o.c; end
2169
- end
2170
- end
2171
-
2172
- deprec_specify "should return nil for a naked dataset" do
2173
- @dataset.model_classes.should == nil
2174
- end
2175
-
2176
- deprec_specify "should return a {nil => model_class} hash for a model dataset" do
2177
- @dataset.set_model(@m)
2178
- @dataset.model_classes.should == {nil => @m}
2179
- end
2180
-
2181
- deprec_specify "should return the polymorphic hash for a polymorphic model dataset" do
2182
- @m1 = Class.new(@m)
2183
- @m2 = Class.new(@m)
2184
- @dataset.set_model(:key, 0 => @m1, 1 => @m2)
2185
- @dataset.model_classes.should == {0 => @m1, 1 => @m2}
2186
- end
2187
- end
2188
-
2189
- context "Dataset#polymorphic_key" do
2190
- before do
2191
- @c = Class.new(Sequel::Dataset) do
2192
- # # We don't need this for now
2193
- # def fetch_rows(sql, &block)
2194
- # (1..10).each(&block)
2195
- # end
2196
- end
2197
- @dataset = @c.new(nil).from(:items)
2198
- @m = Class.new do
2199
- attr_accessor :c
2200
- def initialize(c); @c = c; end
2201
- def ==(o); @c == o.c; end
2202
- end
2203
- end
2204
-
2205
- deprec_specify "should return nil for a naked dataset" do
2206
- @dataset.polymorphic_key.should be_nil
2207
- end
2208
-
2209
- deprec_specify "should return the polymorphic key" do
2210
- @dataset.set_model(:id, nil => @m)
2211
- @dataset.polymorphic_key.should == :id
2212
- end
2213
- end
2214
-
2215
- context "A model dataset" do
2216
- before do
2217
- @c = Class.new(Sequel::Dataset) do
2218
- def fetch_rows(sql, &block)
2219
- (1..10).each(&block)
2220
- end
2221
- end
2222
- @dataset = @c.new(nil).from(:items)
2223
- @m = Class.new do
2224
- attr_accessor :c
2225
- def initialize(c); @c = c; end
2226
- def ==(o); @c == o.c; end
2227
- end
2228
- @dataset.row_proc = Proc.new{|r| @m.new(r)}
2229
- end
2230
-
2231
- deprec_specify "should supply naked records if the naked option is specified" do
2232
- @dataset.each {|r| r.class.should == @m}
2233
- @dataset.naked.each(:naked => true) {|r| r.class.should == Fixnum}
2234
- end
2235
- end
2236
-
2237
- context "A polymorphic model dataset" do
2238
- before do
2239
- @c = Class.new(Sequel::Dataset) do
2240
- def fetch_rows(sql, &block)
2241
- (1..10).each {|i| block.call(:bit => i[0])}
2242
- end
2243
- end
2244
- @dataset = @c.new(nil).from(:items)
2245
- @m = Class.new do
2246
- attr_accessor :c
2247
- def initialize(c); @c = c; end
2248
- def ==(o); @c == o.c; end
2249
- end
2250
- end
2251
-
2252
- deprec_specify "should use a nil key in the polymorphic hash to specify the default model class" do
2253
- @m2 = Class.new(@m)
2254
- @dataset.set_model(:bit, nil => @m, 1 => @m2)
2255
- all = @dataset.all
2256
- all[0].class.should == @m2
2257
- all[1].class.should == @m
2258
- all[2].class.should == @m2
2259
- all[3].class.should == @m
2260
- #...
2261
- end
2262
-
2263
- deprec_specify "should raise Sequel::Error if no suitable class is found in the polymorphic hash" do
2264
- @m2 = Class.new(@m)
2265
- @dataset.set_model(:bit, 1 => @m2)
2266
- proc {@dataset.all}.should raise_error(Sequel::Error)
2267
- end
2268
-
2269
- deprec_specify "should supply naked records if the naked option is specified" do
2270
- @dataset.set_model(:bit, nil => @m)
2271
- @dataset.each(:naked => true) {|r| r.class.should == Hash}
2272
- end
2273
- end
2274
-
2275
- context "A dataset with associated model class(es)" do
2276
- before do
2277
- @c = Class.new(Sequel::Dataset) do
2278
- def fetch_rows(sql, &block)
2279
- block.call({:x => 1, :y => 2})
2280
- end
2281
- end
2282
- @dataset = @c.new(nil).from(:items)
2283
- @m1 = Class.new do
2284
- attr_accessor :v
2285
- def initialize(v); @v = v; end
2286
- end
2287
- @m2 = Class.new do
2288
- attr_accessor :v, :vv
2289
- def initialize(v = nil); @v = v; end
2290
- def self.load(v); o = new(nil); o.vv = v; o; end
2291
- end
2292
- @m3 = Class.new(@m2)
2293
- end
2294
-
2295
- deprec_specify "should instantiate an instance by passing the record hash as argument" do
2296
- @dataset.set_model(@m1)
2297
- o = @dataset.first
2298
- o.class.should == @m1
2299
- o.v.should == {:x => 1, :y => 2}
2300
- end
2301
-
2302
- deprec_specify "should use the .load constructor if available" do
2303
- @dataset.set_model(@m2)
2304
- o = @dataset.first
2305
- o.class.should == @m2
2306
- o.v.should == nil
2307
- o.vv.should == {:x => 1, :y => 2}
2308
- end
2309
-
2310
- deprec_specify "should use the .load constructor also for polymorphic datasets" do
2311
- @dataset.set_model(:y, 1 => @m2, 2 => @m3)
2312
- o = @dataset.first
2313
- o.class.should == @m3
2314
- o.v.should == nil
2315
- o.vv.should == {:x => 1, :y => 2}
2316
- end
2317
- end
2318
-
2319
2046
  context "Dataset#<<" do
2320
2047
  before do
2321
2048
  @d = Sequel::Dataset.new(nil)
@@ -2329,104 +2056,6 @@ context "Dataset#<<" do
2329
2056
  end
2330
2057
  end
2331
2058
 
2332
- context "A paginated dataset" do
2333
- before do
2334
- @d = Sequel::Dataset.new(nil)
2335
- @d.meta_def(:count) {153}
2336
-
2337
- deprec{@paginated = @d.paginate(1, 20)}
2338
- end
2339
-
2340
- deprec_specify "should raise an error if the dataset already has a limit" do
2341
- proc{@d.limit(10).paginate(1,10)}.should raise_error(Sequel::Error)
2342
- proc{@paginated.paginate(2,20)}.should raise_error(Sequel::Error)
2343
- end
2344
-
2345
- deprec_specify "should set the limit and offset options correctly" do
2346
- @paginated.opts[:limit].should == 20
2347
- @paginated.opts[:offset].should == 0
2348
- end
2349
-
2350
- deprec_specify "should set the page count correctly" do
2351
- @paginated.page_count.should == 8
2352
- @d.paginate(1, 50).page_count.should == 4
2353
- end
2354
-
2355
- deprec_specify "should set the current page number correctly" do
2356
- @paginated.current_page.should == 1
2357
- @d.paginate(3, 50).current_page.should == 3
2358
- end
2359
-
2360
- deprec_specify "should return the next page number or nil if we're on the last" do
2361
- @paginated.next_page.should == 2
2362
- @d.paginate(4, 50).next_page.should be_nil
2363
- end
2364
-
2365
- deprec_specify "should return the previous page number or nil if we're on the last" do
2366
- @paginated.prev_page.should be_nil
2367
- @d.paginate(4, 50).prev_page.should == 3
2368
- end
2369
-
2370
- deprec_specify "should return the page range" do
2371
- @paginated.page_range.should == (1..8)
2372
- @d.paginate(4, 50).page_range.should == (1..4)
2373
- end
2374
-
2375
- deprec_specify "should return the record range for the current page" do
2376
- @paginated.current_page_record_range.should == (1..20)
2377
- @d.paginate(4, 50).current_page_record_range.should == (151..153)
2378
- @d.paginate(5, 50).current_page_record_range.should == (0..0)
2379
- end
2380
-
2381
- deprec_specify "should return the record count for the current page" do
2382
- @paginated.current_page_record_count.should == 20
2383
- @d.paginate(3, 50).current_page_record_count.should == 50
2384
- @d.paginate(4, 50).current_page_record_count.should == 3
2385
- @d.paginate(5, 50).current_page_record_count.should == 0
2386
- end
2387
-
2388
- deprec_specify "should know if current page is last page" do
2389
- @paginated.last_page?.should be_false
2390
- @d.paginate(2, 20).last_page?.should be_false
2391
- @d.paginate(5, 30).last_page?.should be_false
2392
- @d.paginate(6, 30).last_page?.should be_true
2393
- end
2394
-
2395
- deprec_specify "should know if current page is first page" do
2396
- @paginated.first_page?.should be_true
2397
- @d.paginate(1, 20).first_page?.should be_true
2398
- @d.paginate(2, 20).first_page?.should be_false
2399
- end
2400
-
2401
- deprec_specify "should work with fixed sql" do
2402
- ds = @d.clone(:sql => 'select * from blah')
2403
- ds.meta_def(:count) {150}
2404
- ds.paginate(2, 50).sql.should == 'SELECT * FROM (select * from blah) AS t1 LIMIT 50 OFFSET 50'
2405
- end
2406
- end
2407
-
2408
- context "Dataset#each_page" do
2409
- before do
2410
- @d = Sequel::Dataset.new(nil).from(:items)
2411
- @d.meta_def(:count) {153}
2412
- end
2413
-
2414
- deprec_specify "should raise an error if the dataset already has a limit" do
2415
- proc{@d.limit(10).each_page(10){}}.should raise_error(Sequel::Error)
2416
- end
2417
-
2418
- deprec_specify "should iterate over each page in the resultset as a paginated dataset" do
2419
- a = []
2420
- @d.each_page(50) {|p| a << p}
2421
- a.map {|p| p.sql}.should == [
2422
- 'SELECT * FROM items LIMIT 50 OFFSET 0',
2423
- 'SELECT * FROM items LIMIT 50 OFFSET 50',
2424
- 'SELECT * FROM items LIMIT 50 OFFSET 100',
2425
- 'SELECT * FROM items LIMIT 50 OFFSET 150',
2426
- ]
2427
- end
2428
- end
2429
-
2430
2059
  context "Dataset#columns" do
2431
2060
  before do
2432
2061
  @dataset = DummyDataset.new(nil).from(:items)
@@ -2469,39 +2098,9 @@ context "Dataset#columns!" do
2469
2098
  end
2470
2099
  end
2471
2100
 
2472
- require 'stringio'
2473
-
2474
- context "Dataset#print" do
2475
- before do
2476
- @output = StringIO.new
2477
- @orig_stdout = $stdout
2478
- $stdout = @output
2479
- @dataset = DummyDataset.new(nil).from(:items)
2480
- end
2481
-
2482
- after do
2483
- $stdout = @orig_stdout
2484
- end
2485
-
2486
- deprec_specify "should print out a table with the values" do
2487
- @dataset.print(:a, :b)
2488
- @output.rewind
2489
- @output.read.should == \
2490
- "+-+-+\n|a|b|\n+-+-+\n|1|2|\n|3|4|\n|5|6|\n+-+-+\n"
2491
- end
2492
-
2493
- deprec_specify "should default to the dataset's columns" do
2494
- @dataset.meta_def(:columns) {[:a, :b]}
2495
- @dataset.print
2496
- @output.rewind
2497
- @output.read.should == \
2498
- "+-+-+\n|a|b|\n+-+-+\n|1|2|\n|3|4|\n|5|6|\n+-+-+\n"
2499
- end
2500
- end
2501
-
2502
2101
  context "Dataset#import" do
2503
2102
  before do
2504
- @dbc = Class.new do
2103
+ @dbc = Class.new(Sequel::Database) do
2505
2104
  attr_reader :sqls
2506
2105
 
2507
2106
  def execute(sql, opts={})
@@ -2524,76 +2123,6 @@ context "Dataset#import" do
2524
2123
  @list = [{:name => 'abc'}, {:name => 'def'}, {:name => 'ghi'}]
2525
2124
  end
2526
2125
 
2527
- deprec_specify "should issue multiple inserts inside a transaction" do
2528
- @ds.import(@list)
2529
- @db.sqls.should == [
2530
- 'BEGIN',
2531
- "INSERT INTO items (name) VALUES ('abc')",
2532
- "INSERT INTO items (name) VALUES ('def')",
2533
- "INSERT INTO items (name) VALUES ('ghi')",
2534
- 'COMMIT'
2535
- ]
2536
- end
2537
-
2538
- deprec_specify "should handle different formats for tables" do
2539
- @ds = @ds.from(:sch__tab)
2540
- @ds.import(@list)
2541
- @db.sqls.should == [
2542
- 'BEGIN',
2543
- "INSERT INTO sch.tab (name) VALUES ('abc')",
2544
- "INSERT INTO sch.tab (name) VALUES ('def')",
2545
- "INSERT INTO sch.tab (name) VALUES ('ghi')",
2546
- 'COMMIT'
2547
- ]
2548
- @db.sqls.clear
2549
-
2550
- @ds = @ds.from(:tab.qualify(:sch))
2551
- @ds.import(@list)
2552
- @db.sqls.should == [
2553
- 'BEGIN',
2554
- "INSERT INTO sch.tab (name) VALUES ('abc')",
2555
- "INSERT INTO sch.tab (name) VALUES ('def')",
2556
- "INSERT INTO sch.tab (name) VALUES ('ghi')",
2557
- 'COMMIT'
2558
- ]
2559
- @db.sqls.clear
2560
- @ds = @ds.from(:sch__tab.identifier)
2561
- @ds.import(@list)
2562
- @db.sqls.should == [
2563
- 'BEGIN',
2564
- "INSERT INTO sch__tab (name) VALUES ('abc')",
2565
- "INSERT INTO sch__tab (name) VALUES ('def')",
2566
- "INSERT INTO sch__tab (name) VALUES ('ghi')",
2567
- 'COMMIT'
2568
- ]
2569
- end
2570
-
2571
- deprec_specify "should accept the :commit_every option for committing every x records" do
2572
- @ds.import(@list, :commit_every => 2)
2573
- @db.sqls.should == [
2574
- 'BEGIN',
2575
- "INSERT INTO items (name) VALUES ('abc')",
2576
- "INSERT INTO items (name) VALUES ('def')",
2577
- 'COMMIT',
2578
- 'BEGIN',
2579
- "INSERT INTO items (name) VALUES ('ghi')",
2580
- 'COMMIT'
2581
- ]
2582
- end
2583
-
2584
- deprec_specify "should accept the :slice option for committing every x records" do
2585
- @ds.import(@list, :slice => 2)
2586
- @db.sqls.should == [
2587
- 'BEGIN',
2588
- "INSERT INTO items (name) VALUES ('abc')",
2589
- "INSERT INTO items (name) VALUES ('def')",
2590
- 'COMMIT',
2591
- 'BEGIN',
2592
- "INSERT INTO items (name) VALUES ('ghi')",
2593
- 'COMMIT'
2594
- ]
2595
- end
2596
-
2597
2126
  specify "should accept string keys as column names" do
2598
2127
  @ds.import(['x', 'y'], [[1, 2], [3, 4]])
2599
2128
  @db.sqls.should == [
@@ -2625,6 +2154,16 @@ context "Dataset#import" do
2625
2154
  ]
2626
2155
  end
2627
2156
 
2157
+ specify "should accept a columns array and a values array with :commit_every option" do
2158
+ @ds.import([:x, :y], [[1, 2], [3, 4], [5, 6]], :commit_every => 3)
2159
+ @db.sqls.should == [
2160
+ 'BEGIN',
2161
+ "INSERT INTO items (x, y) VALUES (1, 2)",
2162
+ "INSERT INTO items (x, y) VALUES (3, 4)",
2163
+ "INSERT INTO items (x, y) VALUES (5, 6)",
2164
+ 'COMMIT',
2165
+ ]
2166
+ end
2628
2167
  specify "should accept a columns array and a values array with slice option" do
2629
2168
  @ds.import([:x, :y], [[1, 2], [3, 4], [5, 6]], :slice => 2)
2630
2169
  @db.sqls.should == [
@@ -2637,23 +2176,6 @@ context "Dataset#import" do
2637
2176
  'COMMIT'
2638
2177
  ]
2639
2178
  end
2640
-
2641
- deprec_specify "should not do anything if no columns or values are given" do
2642
- @ds.import
2643
- @db.sqls.should be_nil
2644
-
2645
- @ds.import([])
2646
- @db.sqls.should be_nil
2647
-
2648
- @ds.import([], [])
2649
- @db.sqls.should be_nil
2650
-
2651
- @ds.import([{}, {}])
2652
- @db.sqls.should be_nil
2653
-
2654
- @ds.import([:a, :b], [])
2655
- @db.sqls.should be_nil
2656
- end
2657
2179
  end
2658
2180
 
2659
2181
  context "Dataset#multi_insert" do
@@ -2726,10 +2248,12 @@ context "Dataset#multi_insert" do
2726
2248
  end
2727
2249
 
2728
2250
  specify "should accept the :commit_every option for committing every x records" do
2729
- @ds.multi_insert(@list, :commit_every => 2)
2251
+ @ds.multi_insert(@list, :commit_every => 1)
2730
2252
  @db.sqls.should == [
2731
2253
  'BEGIN',
2732
2254
  "INSERT INTO items (name) VALUES ('abc')",
2255
+ 'COMMIT',
2256
+ 'BEGIN',
2733
2257
  "INSERT INTO items (name) VALUES ('def')",
2734
2258
  'COMMIT',
2735
2259
  'BEGIN',
@@ -2761,118 +2285,12 @@ context "Dataset#multi_insert" do
2761
2285
  ]
2762
2286
  end
2763
2287
 
2764
- deprec_specify "should accept a columns array and a values array" do
2765
- @ds.multi_insert([:x, :y], [[1, 2], [3, 4]])
2766
- @db.sqls.should == [
2767
- 'BEGIN',
2768
- "INSERT INTO items (x, y) VALUES (1, 2)",
2769
- "INSERT INTO items (x, y) VALUES (3, 4)",
2770
- 'COMMIT'
2771
- ]
2772
- end
2773
-
2774
- deprec_specify "should accept a columns array and a dataset" do
2775
- @ds2 = Sequel::Dataset.new(@db).from(:cats).filter(:purr => true).select(:a, :b)
2776
-
2777
- @ds.multi_insert([:x, :y], @ds2)
2778
- @db.sqls.should == [
2779
- 'BEGIN',
2780
- "INSERT INTO items (x, y) VALUES (SELECT a, b FROM cats WHERE (purr IS TRUE))",
2781
- 'COMMIT'
2782
- ]
2783
- end
2784
-
2785
- deprec_specify "should accept a columns array and a values array with slice option" do
2786
- @ds.multi_insert([:x, :y], [[1, 2], [3, 4], [5, 6]], :slice => 2)
2787
- @db.sqls.should == [
2788
- 'BEGIN',
2789
- "INSERT INTO items (x, y) VALUES (1, 2)",
2790
- "INSERT INTO items (x, y) VALUES (3, 4)",
2791
- 'COMMIT',
2792
- 'BEGIN',
2793
- "INSERT INTO items (x, y) VALUES (5, 6)",
2794
- 'COMMIT'
2795
- ]
2796
- end
2797
-
2798
- deprec_specify "should not do anything if no hashes are provided" do
2799
- @ds.multi_insert
2288
+ specify "should not do anything if no hashes are provided" do
2289
+ @ds.multi_insert([])
2800
2290
  @db.sqls.should be_nil
2801
2291
  end
2802
2292
  end
2803
2293
 
2804
- context "Dataset#query" do
2805
- before do
2806
- @d = Sequel::Dataset.new(nil)
2807
- end
2808
-
2809
- deprec_specify "should support #from" do
2810
- q = @d.query {from :xxx}
2811
- q.class.should == @d.class
2812
- q.sql.should == "SELECT * FROM xxx"
2813
- end
2814
-
2815
- deprec_specify "should support #select" do
2816
- q = @d.query do
2817
- select :a, :b___mongo
2818
- from :yyy
2819
- end
2820
- q.class.should == @d.class
2821
- q.sql.should == "SELECT a, b AS mongo FROM yyy"
2822
- end
2823
-
2824
- deprec_specify "should support #where" do
2825
- q = @d.query do
2826
- from :zzz
2827
- where(:x + 2 > :y + 3)
2828
- end
2829
- q.class.should == @d.class
2830
- q.sql.should == "SELECT * FROM zzz WHERE ((x + 2) > (y + 3))"
2831
-
2832
- q = @d.from(:zzz).query do
2833
- where((:x.sql_number > 1) & (:y.sql_number > 2))
2834
- end
2835
- q.class.should == @d.class
2836
- q.sql.should == "SELECT * FROM zzz WHERE ((x > 1) AND (y > 2))"
2837
-
2838
- q = @d.from(:zzz).query do
2839
- where :x => 33
2840
- end
2841
- q.class.should == @d.class
2842
- q.sql.should == "SELECT * FROM zzz WHERE (x = 33)"
2843
- end
2844
-
2845
- deprec_specify "should support #group_by and #having" do
2846
- q = @d.query do
2847
- from :abc
2848
- group_by :id
2849
- having(:x.sql_number >= 2)
2850
- end
2851
- q.class.should == @d.class
2852
- q.sql.should == "SELECT * FROM abc GROUP BY id HAVING (x >= 2)"
2853
- end
2854
-
2855
- deprec_specify "should support #order, #order_by" do
2856
- q = @d.query do
2857
- from :xyz
2858
- order_by :stamp
2859
- end
2860
- q.class.should == @d.class
2861
- q.sql.should == "SELECT * FROM xyz ORDER BY stamp"
2862
- end
2863
-
2864
- deprec_specify "should raise on non-chainable method calls" do
2865
- proc {@d.query {first_source}}.should raise_error(Sequel::Error)
2866
- end
2867
-
2868
- deprec_specify "should raise on each, insert, update, delete" do
2869
- proc {@d.query {each}}.should raise_error(Sequel::Error)
2870
- proc {@d.query {insert(:x => 1)}}.should raise_error(Sequel::Error)
2871
- proc {@d.query {update(:x => 1)}}.should raise_error(Sequel::Error)
2872
- proc {@d.query {delete}}.should raise_error(Sequel::Error)
2873
- end
2874
- end
2875
-
2876
2294
  context "Dataset" do
2877
2295
  before do
2878
2296
  @d = Sequel::Dataset.new(nil).from(:x)
@@ -2920,191 +2338,6 @@ context "Dataset" do
2920
2338
  end
2921
2339
  end
2922
2340
 
2923
- context "Dataset#transform" do
2924
- before do
2925
- @c = Class.new(Sequel::Dataset) do
2926
- attr_accessor :raw
2927
- attr_accessor :sql
2928
-
2929
- def fetch_rows(sql, &block)
2930
- block[@raw]
2931
- end
2932
-
2933
- def insert(v)
2934
- @sql = insert_sql(v)
2935
- end
2936
-
2937
- def update(v)
2938
- @sql = update_sql(v)
2939
- end
2940
- end
2941
-
2942
- @ds = @c.new(nil).from(:items)
2943
- deprec do
2944
- @ds.transform(:x => [
2945
- proc {|v| Marshal.load(v)},
2946
- proc {|v| Marshal.dump(v)}
2947
- ])
2948
- end
2949
- end
2950
-
2951
- deprec_specify "should change the dataset to transform values loaded from the database" do
2952
- @ds.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
2953
- @ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
2954
- @ds.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
2955
- @ds.all.should == [{:x => [1, 2, 3], :y => 'hello'}]
2956
- end
2957
-
2958
- deprec_specify "should change the dataset to transform values saved to the database" do
2959
- @ds.insert(:x => :toast)
2960
- @ds.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
2961
-
2962
- @ds.insert(:y => 'butter')
2963
- @ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
2964
-
2965
- @ds.update(:x => ['dream'])
2966
- @ds.sql.should == "UPDATE items SET x = '#{Marshal.dump(['dream'])}'"
2967
- end
2968
-
2969
- deprec_specify "should be transferred to cloned datasets" do
2970
- @ds2 = @ds.filter(:a => 1)
2971
-
2972
- @ds2.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
2973
- @ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
2974
-
2975
- @ds2.insert(:x => :toast)
2976
- @ds2.sql.should == "INSERT INTO items (x) VALUES ('#{Marshal.dump(:toast)}')"
2977
- end
2978
-
2979
- deprec_specify "should work correctly together with set_row_proc" do
2980
- @ds.row_proc = proc{|r| r[:z] = r[:x] * 2; r}
2981
- @ds.raw = {:x => Marshal.dump("wow"), :y => 'hello'}
2982
- @ds.first.should == {:x => "wow", :y => 'hello', :z => "wowwow"}
2983
-
2984
- f = nil
2985
- @ds.raw = {:x => Marshal.dump("wow"), :y => 'hello'}
2986
- @ds.naked.each{|r| f = r}
2987
- f.should == {:x => "wow", :y => 'hello'}
2988
- end
2989
-
2990
- deprec_specify "should leave the supplied values intact" do
2991
- h = {:x => :toast}
2992
- @ds.insert(h)
2993
- h.should == {:x => :toast}
2994
- end
2995
- end
2996
-
2997
- context "Dataset#transform" do
2998
- before do
2999
- @c = Class.new(Sequel::Dataset) do
3000
- attr_accessor :raw
3001
- attr_accessor :sql
3002
-
3003
- def fetch_rows(sql, &block)
3004
- block[@raw]
3005
- end
3006
-
3007
- def insert(v)
3008
- @sql = insert_sql(v)
3009
- end
3010
-
3011
- def update(v)
3012
- @sql = update_sql(v)
3013
- end
3014
- end
3015
-
3016
- @ds = @c.new(nil).from(:items)
3017
- end
3018
-
3019
- deprec_specify "should raise Sequel::Error for invalid transformations" do
3020
- proc {@ds.transform(:x => 'mau')}.should raise_error(Sequel::Error::InvalidTransform)
3021
- proc {@ds.transform(:x => :mau)}.should raise_error(Sequel::Error::InvalidTransform)
3022
- proc {@ds.transform(:x => [])}.should raise_error(Sequel::Error::InvalidTransform)
3023
- proc {@ds.transform(:x => ['mau'])}.should raise_error(Sequel::Error::InvalidTransform)
3024
- proc {@ds.transform(:x => [proc {|v|}, proc {|v|}])}.should_not raise_error(Sequel::Error::InvalidTransform)
3025
- end
3026
-
3027
- deprec_specify "should support stock YAML transformation" do
3028
- @ds.transform(:x => :yaml)
3029
-
3030
- @ds.raw = {:x => [1, 2, 3].to_yaml, :y => 'hello'}
3031
- @ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
3032
-
3033
- @ds.insert(:x => :toast)
3034
- @ds.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')"
3035
- @ds.insert(:y => 'butter')
3036
- @ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
3037
- @ds.update(:x => ['dream'])
3038
- @ds.sql.should == "UPDATE items SET x = '#{['dream'].to_yaml}'"
3039
-
3040
- @ds2 = @ds.filter(:a => 1)
3041
- @ds2.raw = {:x => [1, 2, 3].to_yaml, :y => 'hello'}
3042
- @ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
3043
- @ds2.insert(:x => :toast)
3044
- @ds2.sql.should == "INSERT INTO items (x) VALUES ('#{:toast.to_yaml}')"
3045
-
3046
- @ds.row_proc = proc{|r| r[:z] = r[:x] * 2; r}
3047
- @ds.raw = {:x => "wow".to_yaml, :y => 'hello'}
3048
- @ds.first.should == {:x => "wow", :y => 'hello', :z => "wowwow"}
3049
- f = nil
3050
- @ds.raw = {:x => "wow".to_yaml, :y => 'hello'}
3051
- @ds.naked.each{|r| f = r}
3052
- f.should == {:x => "wow", :y => 'hello'}
3053
- end
3054
-
3055
- deprec_specify "should support stock Marshal transformation with Base64 encoding" do
3056
- @ds.transform(:x => :marshal)
3057
-
3058
- @ds.raw = {:x => [Marshal.dump([1, 2, 3])].pack('m'), :y => 'hello'}
3059
- @ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
3060
-
3061
- @ds.insert(:x => :toast)
3062
- @ds.sql.should == "INSERT INTO items (x) VALUES ('#{[Marshal.dump(:toast)].pack('m')}')"
3063
- @ds.insert(:y => 'butter')
3064
- @ds.sql.should == "INSERT INTO items (y) VALUES ('butter')"
3065
- @ds.update(:x => ['dream'])
3066
- @ds.sql.should == "UPDATE items SET x = '#{[Marshal.dump(['dream'])].pack('m')}'"
3067
-
3068
- @ds2 = @ds.filter(:a => 1)
3069
- @ds2.raw = {:x => [Marshal.dump([1, 2, 3])].pack('m'), :y => 'hello'}
3070
- @ds2.first.should == {:x => [1, 2, 3], :y => 'hello'}
3071
- @ds2.insert(:x => :toast)
3072
- @ds2.sql.should == "INSERT INTO items (x) VALUES ('#{[Marshal.dump(:toast)].pack('m')}')"
3073
-
3074
- @ds.row_proc = proc{|r| r[:z] = r[:x] * 2; r}
3075
- @ds.raw = {:x => [Marshal.dump("wow")].pack('m'), :y => 'hello'}
3076
- @ds.first.should == {:x => "wow", :y => 'hello', :z => "wowwow"}
3077
- f = nil
3078
- @ds.raw = {:x => [Marshal.dump("wow")].pack('m'), :y => 'hello'}
3079
- @ds.naked.each{|r| f = r}
3080
- f.should == {:x => "wow", :y => 'hello'}
3081
- end
3082
-
3083
- deprec_specify "should support loading of Marshalled values without Base64 encoding" do
3084
- @ds.transform(:x => :marshal)
3085
-
3086
- @ds.raw = {:x => Marshal.dump([1,2,3]), :y => nil}
3087
- @ds.first.should == {:x => [1,2,3], :y => nil}
3088
- end
3089
-
3090
- deprec_specify "should return self" do
3091
- @ds.transform(:x => :marshal).should be(@ds)
3092
- end
3093
- end
3094
-
3095
- context "A dataset with a transform" do
3096
- before do
3097
- @ds = Sequel::Dataset.new(nil).from(:items)
3098
- deprec{@ds.transform(:x => :marshal)}
3099
- end
3100
-
3101
- deprec_specify "should automatically transform hash filters" do
3102
- @ds.filter(:y => 2).sql.should == 'SELECT * FROM items WHERE (y = 2)'
3103
-
3104
- @ds.filter(:x => 2).sql.should == "SELECT * FROM items WHERE (x = '#{[Marshal.dump(2)].pack('m')}')"
3105
- end
3106
- end
3107
-
3108
2341
  context "Dataset#to_csv" do
3109
2342
  before do
3110
2343
  @c = Class.new(Sequel::Dataset) do
@@ -3137,48 +2370,6 @@ context "Dataset#to_csv" do
3137
2370
  end
3138
2371
  end
3139
2372
 
3140
- context "Dataset#create_view" do
3141
- before do
3142
- @dbc = Class.new(Sequel::Database) do
3143
- attr_reader :sqls
3144
-
3145
- def execute(sql, opts={})
3146
- @sqls ||= []
3147
- @sqls << sql
3148
- end
3149
- end
3150
- @db = @dbc.new
3151
-
3152
- @ds = @db[:items].order(:abc).filter(:category => 'ruby')
3153
- end
3154
-
3155
- deprec_specify "should create a view with the dataset's sql" do
3156
- @ds.create_view(:xyz)
3157
- @db.sqls.should == ["CREATE VIEW xyz AS #{@ds.sql}"]
3158
- end
3159
- end
3160
-
3161
- context "Dataset#create_or_replace_view" do
3162
- before do
3163
- @dbc = Class.new(Sequel::Database) do
3164
- attr_reader :sqls
3165
-
3166
- def execute(sql, opts={})
3167
- @sqls ||= []
3168
- @sqls << sql
3169
- end
3170
- end
3171
- @db = @dbc.new
3172
-
3173
- @ds = @db[:items].order(:abc).filter(:category => 'ruby')
3174
- end
3175
-
3176
- deprec_specify "should create a view with the dataset's sql" do
3177
- @ds.create_or_replace_view(:xyz)
3178
- @db.sqls.should == ["CREATE OR REPLACE VIEW xyz AS #{@ds.sql}"]
3179
- end
3180
- end
3181
-
3182
2373
  context "Dataset#update_sql" do
3183
2374
  before do
3184
2375
  @ds = Sequel::Dataset.new(nil).from(:items)
@@ -3296,14 +2487,6 @@ context "Dataset#all" do
3296
2487
  ]
3297
2488
  end
3298
2489
 
3299
- deprec_specify "should accept options and pass them to #each" do
3300
- @dataset.all(:limit => 33).should == [
3301
- {:x => 1, :y => 2},
3302
- {:x => 3, :y => 4},
3303
- "SELECT * FROM items LIMIT 33"
3304
- ]
3305
- end
3306
-
3307
2490
  specify "should iterate over the array if a block is given" do
3308
2491
  a = []
3309
2492
 
@@ -3354,30 +2537,6 @@ context "Dataset#grep" do
3354
2537
  end
3355
2538
  end
3356
2539
 
3357
- context "Sequel.use_parse_tree" do
3358
- deprec_specify "be false" do
3359
- Sequel.use_parse_tree.should == false
3360
- end
3361
- end
3362
-
3363
- context "Sequel.use_parse_tree=" do
3364
- deprec_specify "raise an error if true" do
3365
- proc{Sequel.use_parse_tree = true}.should raise_error(Sequel::Error)
3366
- end
3367
-
3368
- deprec_specify "do nothing if false" do
3369
- proc{Sequel.use_parse_tree = false}.should_not raise_error
3370
- end
3371
- end
3372
-
3373
- context "Dataset.dataset_classes" do
3374
- deprec_specify "should be an array of dataset subclasses" do
3375
- ds_class = Class.new(Sequel::Dataset)
3376
- Sequel::Dataset.dataset_classes.should be_a_kind_of(Array)
3377
- Sequel::Dataset.dataset_classes.should include(ds_class)
3378
- end
3379
- end
3380
-
3381
2540
  context "Dataset default #fetch_rows, #insert, #update, and #delete, #execute" do
3382
2541
  before do
3383
2542
  @db = Sequel::Database.new
@@ -3412,10 +2571,9 @@ end
3412
2571
  context "Dataset prepared statements and bound variables " do
3413
2572
  before do
3414
2573
  @db = Sequel::Database.new
3415
- @db.send :metaattr_accessor, :sqls
3416
- @db.sqls = []
2574
+ @db.meta_def(:sqls){@sqls||=[]}
3417
2575
  def @db.execute(sql, opts={})
3418
- @sqls << sql
2576
+ sqls << sql
3419
2577
  end
3420
2578
  def @db.dataset
3421
2579
  ds = super()
@@ -3500,10 +2658,9 @@ end
3500
2658
  context Sequel::Dataset::UnnumberedArgumentMapper do
3501
2659
  before do
3502
2660
  @db = Sequel::Database.new
3503
- @db.send :metaattr_accessor, :sqls
3504
- @db.sqls = []
2661
+ @db.meta_def(:sqls){@sqls||=[]}
3505
2662
  def @db.execute(sql, opts={})
3506
- @sqls << [sql, *opts[:arguments]]
2663
+ sqls << [sql, *opts[:arguments]]
3507
2664
  end
3508
2665
  @ds = @db[:items].filter(:num=>:$n)
3509
2666
  def @ds.fetch_rows(sql, &block)
@@ -3591,43 +2748,3 @@ context "Sequel::Dataset #set_overrides" do
3591
2748
  @ds.set_overrides(:x=>2).update_sql.should == "UPDATE items SET x = 1"
3592
2749
  end
3593
2750
  end
3594
-
3595
- context "Sequel::Dataset#each" do
3596
- before do
3597
- @ds = Sequel::Dataset.new(nil).from(:items)
3598
- def @ds.fetch_rows(sql)
3599
- @columns = /count/i.match(sql) ? [:count] : [:a]
3600
- yield({@columns.first=>sql})
3601
- end
3602
- end
3603
-
3604
- deprec_specify "should not set the columns if passing an option that modifies them" do
3605
- @ds.each(:select=>[:count]){}
3606
- @ds.columns.should == [:a]
3607
- @ds.each(:from=>[:count]){}
3608
- @ds.columns.should == [:a]
3609
- @ds.each(:join=>[Sequel::SQL::JoinClause.new(:natural, :count)]){}
3610
- @ds.columns.should == [:a]
3611
- @ds.each(:sql=>'SELECT COUNT'){}
3612
- @ds.columns.should == [:a]
3613
- end
3614
-
3615
- deprec_specify "should have the correct columns inside the block regardless" do
3616
- @ds.each(:select=>[:count]) do |x|
3617
- x[:count].should == 'SELECT count FROM items'
3618
- @ds.columns.should == [:count]
3619
- end
3620
- @ds.each(:from=>[:count]) do |x|
3621
- x[:count].should == 'SELECT * FROM count'
3622
- @ds.columns.should == [:count]
3623
- end
3624
- @ds.each(:join=>[Sequel::SQL::JoinClause.new(:natural, :count)]) do |x|
3625
- x[:count].should == 'SELECT * FROM items NATURAL JOIN count'
3626
- @ds.columns.should == [:count]
3627
- end
3628
- @ds.each(:sql=>'SELECT COUNT') do |x|
3629
- x[:count].should == 'SELECT COUNT'
3630
- @ds.columns.should == [:count]
3631
- end
3632
- end
3633
- end