sequel 4.9.0 → 4.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +79 -1
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +1 -1
  5. data/Rakefile +2 -12
  6. data/bin/sequel +1 -0
  7. data/doc/advanced_associations.rdoc +82 -25
  8. data/doc/association_basics.rdoc +21 -22
  9. data/doc/core_extensions.rdoc +1 -1
  10. data/doc/opening_databases.rdoc +7 -0
  11. data/doc/release_notes/4.10.0.txt +226 -0
  12. data/doc/security.rdoc +1 -0
  13. data/doc/testing.rdoc +7 -7
  14. data/doc/transactions.rdoc +8 -0
  15. data/lib/sequel/adapters/jdbc.rb +160 -168
  16. data/lib/sequel/adapters/jdbc/db2.rb +17 -18
  17. data/lib/sequel/adapters/jdbc/derby.rb +5 -28
  18. data/lib/sequel/adapters/jdbc/h2.rb +11 -22
  19. data/lib/sequel/adapters/jdbc/hsqldb.rb +31 -18
  20. data/lib/sequel/adapters/jdbc/jtds.rb +0 -15
  21. data/lib/sequel/adapters/jdbc/oracle.rb +36 -35
  22. data/lib/sequel/adapters/jdbc/postgresql.rb +72 -90
  23. data/lib/sequel/adapters/jdbc/sqlanywhere.rb +18 -16
  24. data/lib/sequel/adapters/jdbc/sqlite.rb +7 -0
  25. data/lib/sequel/adapters/jdbc/sqlserver.rb +10 -30
  26. data/lib/sequel/adapters/jdbc/transactions.rb +5 -6
  27. data/lib/sequel/adapters/openbase.rb +1 -7
  28. data/lib/sequel/adapters/postgres.rb +1 -1
  29. data/lib/sequel/adapters/shared/access.rb +3 -6
  30. data/lib/sequel/adapters/shared/cubrid.rb +24 -9
  31. data/lib/sequel/adapters/shared/db2.rb +13 -5
  32. data/lib/sequel/adapters/shared/firebird.rb +16 -16
  33. data/lib/sequel/adapters/shared/informix.rb +2 -5
  34. data/lib/sequel/adapters/shared/mssql.rb +72 -63
  35. data/lib/sequel/adapters/shared/mysql.rb +72 -40
  36. data/lib/sequel/adapters/shared/oracle.rb +27 -15
  37. data/lib/sequel/adapters/shared/postgres.rb +24 -44
  38. data/lib/sequel/adapters/shared/progress.rb +1 -5
  39. data/lib/sequel/adapters/shared/sqlanywhere.rb +26 -18
  40. data/lib/sequel/adapters/shared/sqlite.rb +21 -6
  41. data/lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb +8 -1
  42. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +8 -2
  43. data/lib/sequel/adapters/utils/split_alter_table.rb +8 -0
  44. data/lib/sequel/core.rb +14 -9
  45. data/lib/sequel/database/dataset_defaults.rb +1 -0
  46. data/lib/sequel/database/misc.rb +12 -0
  47. data/lib/sequel/database/query.rb +4 -1
  48. data/lib/sequel/database/schema_methods.rb +3 -2
  49. data/lib/sequel/database/transactions.rb +47 -17
  50. data/lib/sequel/dataset/features.rb +12 -2
  51. data/lib/sequel/dataset/mutation.rb +2 -0
  52. data/lib/sequel/dataset/placeholder_literalizer.rb +12 -4
  53. data/lib/sequel/dataset/prepared_statements.rb +6 -0
  54. data/lib/sequel/dataset/query.rb +1 -1
  55. data/lib/sequel/dataset/sql.rb +132 -70
  56. data/lib/sequel/extensions/columns_introspection.rb +1 -1
  57. data/lib/sequel/extensions/null_dataset.rb +8 -4
  58. data/lib/sequel/extensions/pg_array.rb +4 -4
  59. data/lib/sequel/extensions/pg_row.rb +1 -0
  60. data/lib/sequel/model/associations.rb +468 -188
  61. data/lib/sequel/model/base.rb +88 -13
  62. data/lib/sequel/plugins/association_pks.rb +23 -64
  63. data/lib/sequel/plugins/auto_validations.rb +3 -2
  64. data/lib/sequel/plugins/dataset_associations.rb +1 -3
  65. data/lib/sequel/plugins/many_through_many.rb +18 -65
  66. data/lib/sequel/plugins/pg_array_associations.rb +97 -86
  67. data/lib/sequel/plugins/prepared_statements.rb +2 -1
  68. data/lib/sequel/plugins/prepared_statements_associations.rb +36 -27
  69. data/lib/sequel/plugins/rcte_tree.rb +12 -16
  70. data/lib/sequel/plugins/sharding.rb +21 -3
  71. data/lib/sequel/plugins/single_table_inheritance.rb +2 -1
  72. data/lib/sequel/plugins/subclasses.rb +1 -9
  73. data/lib/sequel/plugins/tactical_eager_loading.rb +9 -0
  74. data/lib/sequel/plugins/tree.rb +2 -2
  75. data/lib/sequel/plugins/validation_class_methods.rb +1 -1
  76. data/lib/sequel/version.rb +1 -1
  77. data/spec/adapters/mssql_spec.rb +57 -15
  78. data/spec/adapters/mysql_spec.rb +11 -0
  79. data/spec/bin_spec.rb +2 -2
  80. data/spec/core/database_spec.rb +38 -4
  81. data/spec/core/dataset_spec.rb +45 -7
  82. data/spec/core/placeholder_literalizer_spec.rb +17 -0
  83. data/spec/core/schema_spec.rb +6 -1
  84. data/spec/extensions/active_model_spec.rb +18 -9
  85. data/spec/extensions/association_pks_spec.rb +20 -18
  86. data/spec/extensions/association_proxies_spec.rb +9 -9
  87. data/spec/extensions/auto_validations_spec.rb +6 -0
  88. data/spec/extensions/columns_introspection_spec.rb +1 -0
  89. data/spec/extensions/constraint_validations_spec.rb +3 -1
  90. data/spec/extensions/many_through_many_spec.rb +191 -111
  91. data/spec/extensions/pg_array_associations_spec.rb +133 -103
  92. data/spec/extensions/prepared_statements_associations_spec.rb +23 -4
  93. data/spec/extensions/rcte_tree_spec.rb +35 -27
  94. data/spec/extensions/sequel_3_dataset_methods_spec.rb +0 -1
  95. data/spec/extensions/sharding_spec.rb +2 -2
  96. data/spec/extensions/tactical_eager_loading_spec.rb +4 -0
  97. data/spec/extensions/to_dot_spec.rb +1 -0
  98. data/spec/extensions/touch_spec.rb +2 -2
  99. data/spec/integration/associations_test.rb +130 -37
  100. data/spec/integration/dataset_test.rb +17 -0
  101. data/spec/integration/model_test.rb +17 -0
  102. data/spec/integration/schema_test.rb +14 -0
  103. data/spec/integration/transaction_test.rb +25 -1
  104. data/spec/model/association_reflection_spec.rb +63 -24
  105. data/spec/model/associations_spec.rb +104 -57
  106. data/spec/model/base_spec.rb +14 -1
  107. data/spec/model/class_dataset_methods_spec.rb +1 -0
  108. data/spec/model/eager_loading_spec.rb +221 -74
  109. data/spec/model/model_spec.rb +119 -1
  110. metadata +4 -2
@@ -2,14 +2,15 @@ require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
2
2
 
3
3
  describe Sequel::Model, "pg_array_associations" do
4
4
  before do
5
- class ::Artist < Sequel::Model
5
+ @db = Sequel.mock(:numrows=>1)
6
+ class ::Artist < Sequel::Model(@db)
6
7
  attr_accessor :yyy
7
8
  columns :id, :tag_ids
8
9
  plugin :pg_array_associations
9
10
  pg_array_to_many :tags
10
11
  pg_array_to_many :a_tags, :clone=>:tags, :conditions=>{:name=>'A'}, :key=>:tag_ids
11
12
  end
12
- class ::Tag < Sequel::Model
13
+ class ::Tag < Sequel::Model(@db)
13
14
  columns :id
14
15
  plugin :pg_array_associations
15
16
  many_to_pg_array :artists
@@ -26,7 +27,7 @@ describe Sequel::Model, "pg_array_associations" do
26
27
  @o2 = @c2.first
27
28
  @n1 = @c1.new
28
29
  @n2 = @c2.new
29
- DB.reset
30
+ @db.sqls
30
31
  end
31
32
  after do
32
33
  Object.send(:remove_const, :Artist)
@@ -47,24 +48,24 @@ describe Sequel::Model, "pg_array_associations" do
47
48
  @n1.tags.should == []
48
49
  @c1.load(:tag_ids=>[]).tags.should == []
49
50
  @n2.artists.should == []
50
- DB.sqls.should == []
51
+ @db.sqls.should == []
51
52
  end
52
53
 
53
54
  it "should use correct SQL when loading associations lazily" do
54
55
  @o1.tags.should == [@o2]
55
56
  @o2.artists.should == [@o1]
56
- DB.sqls.should == ["SELECT * FROM tags WHERE (tags.id IN (1, 2, 3))", "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2])"]
57
+ @db.sqls.should == ["SELECT * FROM tags WHERE (tags.id IN (1, 2, 3))", "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]::integer[])"]
57
58
  end
58
59
 
59
60
  it "should accept :primary_key option for primary keys to use in current and associated table" do
60
61
  @c1.pg_array_to_many :tags, :clone=>:tags, :primary_key=>Sequel./(:id, 3)
61
62
  @c2.many_to_pg_array :artists, :clone=>:artists, :primary_key=>:id3
62
63
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE ((tags.id / 3) IN (1, 2, 3))"
63
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[6])"
64
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[6]::integer[])"
64
65
  end
65
66
 
66
67
  it "should allowing filtering by associations" do
67
- @c1.filter(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2])"
68
+ @c1.filter(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]::integer[])"
68
69
  @c2.filter(:artists=>@o1).sql.should == "SELECT * FROM tags WHERE (tags.id IN (1, 2, 3))"
69
70
  end
70
71
 
@@ -74,7 +75,7 @@ describe Sequel::Model, "pg_array_associations" do
74
75
  end
75
76
 
76
77
  it "should allowing excluding by associations" do
77
- @c1.exclude(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (NOT (artists.tag_ids @> ARRAY[2]) OR (artists.tag_ids IS NULL))"
78
+ @c1.exclude(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (NOT (artists.tag_ids @> ARRAY[2]::integer[]) OR (artists.tag_ids IS NULL))"
78
79
  @c2.exclude(:artists=>@o1).sql.should == "SELECT * FROM tags WHERE ((tags.id NOT IN (1, 2, 3)) OR (tags.id IS NULL))"
79
80
  end
80
81
 
@@ -84,7 +85,7 @@ describe Sequel::Model, "pg_array_associations" do
84
85
  end
85
86
 
86
87
  it "should allowing filtering by multiple associations" do
87
- @c1.filter(:tags=>[@c2.load(:id=>1), @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[1,2])"
88
+ @c1.filter(:tags=>[@c2.load(:id=>1), @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[1,2]::integer[])"
88
89
  @c2.filter(:artists=>[@c1.load(:tag_ids=>Sequel.pg_array([3, 4])), @c1.load(:tag_ids=>Sequel.pg_array([4, 5]))]).sql.should == "SELECT * FROM tags WHERE (tags.id IN (3, 4, 5))"
89
90
  end
90
91
 
@@ -94,7 +95,7 @@ describe Sequel::Model, "pg_array_associations" do
94
95
  end
95
96
 
96
97
  it "should allowing excluding by multiple associations" do
97
- @c1.exclude(:tags=>[@c2.load(:id=>1), @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (NOT (artists.tag_ids && ARRAY[1,2]) OR (artists.tag_ids IS NULL))"
98
+ @c1.exclude(:tags=>[@c2.load(:id=>1), @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (NOT (artists.tag_ids && ARRAY[1,2]::integer[]) OR (artists.tag_ids IS NULL))"
98
99
  @c2.exclude(:artists=>[@c1.load(:tag_ids=>Sequel.pg_array([3, 4])), @c1.load(:tag_ids=>Sequel.pg_array([4, 5]))]).sql.should == "SELECT * FROM tags WHERE ((tags.id NOT IN (3, 4, 5)) OR (tags.id IS NULL))"
99
100
  end
100
101
 
@@ -112,7 +113,7 @@ describe Sequel::Model, "pg_array_associations" do
112
113
  @c2.filter(:artists=>@c1.load(:tag_ids=>[])).sql.should == 'SELECT * FROM tags WHERE \'f\''
113
114
  @c2.exclude(:artists=>@c1.load(:tag_ids=>[])).sql.should == 'SELECT * FROM tags WHERE \'t\''
114
115
 
115
- @c1.filter(:tags=>[@c2.new, @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"
116
+ @c1.filter(:tags=>[@c2.new, @c2.load(:id=>2)]).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"
116
117
  @c2.filter(:artists=>[@c1.load(:tag_ids=>Sequel.pg_array([3, 4])), @c1.new]).sql.should == "SELECT * FROM tags WHERE (tags.id IN (3, 4))"
117
118
  end
118
119
 
@@ -141,7 +142,7 @@ describe Sequel::Model, "pg_array_associations" do
141
142
  @c1.pg_array_to_many :tags, :clone=>:tags, :primary_key=>Sequel.*(:id, 3), :primary_key_method=>:id3, :key=>:tag3_ids, :key_column=>Sequel.pg_array(:tag_ids)[1..2]
142
143
  @c2.many_to_pg_array :artists, :clone=>:artists, :primary_key=>Sequel.*(:id, 3), :primary_key_method=>:id3, :key=>:tag3_ids, :key_column=>Sequel.pg_array(:tag_ids)[1..2]
143
144
 
144
- @c1.filter(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids[1:2] @> ARRAY[6])"
145
+ @c1.filter(:tags=>@o2).sql.should == "SELECT * FROM artists WHERE (artists.tag_ids[1:2] @> ARRAY[6]::integer[])"
145
146
  @c2.filter(:artists=>@o1).sql.should == "SELECT * FROM tags WHERE ((tags.id * 3) IN (3, 6, 9))"
146
147
  @c1.filter(:tags=>@c2.where(:id=>1)).sql.should == "SELECT * FROM artists WHERE coalesce((artists.tag_ids[1:2] && (SELECT array_agg((tags.id * 3)) FROM tags WHERE (id = 1))), 'f')"
147
148
  @c2.filter(:artists=>@c1.where(:id=>1)).sql.should == "SELECT * FROM tags WHERE ((tags.id * 3) IN (SELECT unnest(artists.tag_ids[1:2]) FROM artists WHERE (id = 1)))"
@@ -152,12 +153,12 @@ describe Sequel::Model, "pg_array_associations" do
152
153
  @c2.many_to_pg_array :artists, :clone=>:artists, :key=>:tag2_ids
153
154
  @c1.class_eval{def tag2_ids; tag_ids.map{|x| x * 2} end}
154
155
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE (tags.id IN (2, 4, 6))"
155
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag2_ids @> ARRAY[2])"
156
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag2_ids @> ARRAY[2]::integer[])"
156
157
  end
157
158
 
158
159
  it "should support a :key_column option" do
159
160
  @c2.many_to_pg_array :artists, :clone=>:artists, :key_column=>Sequel.pg_array(:tag_ids)[1..2], :key=>:tag2_ids
160
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids[1:2] @> ARRAY[2])"
161
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids[1:2] @> ARRAY[2]::integer[])"
161
162
  end
162
163
 
163
164
  it "should support a :primary_key option" do
@@ -165,35 +166,35 @@ describe Sequel::Model, "pg_array_associations" do
165
166
  @c2.many_to_pg_array :artists, :clone=>:artists, :primary_key=>:id2
166
167
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE (tags.id2 IN (1, 2, 3))"
167
168
  @c2.class_eval{def id2; id*2 end}
168
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[4])"
169
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[4]::integer[])"
169
170
  end
170
171
 
171
172
  it "should support a :conditions option" do
172
173
  @c1.pg_array_to_many :tags, :clone=>:tags, :conditions=>{:a=>1}
173
174
  @c2.many_to_pg_array :artists, :clone=>:artists, :conditions=>{:a=>1}
174
175
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE ((a = 1) AND (tags.id IN (1, 2, 3)))"
175
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE ((a = 1) AND (artists.tag_ids @> ARRAY[2]))"
176
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE ((a = 1) AND (artists.tag_ids @> ARRAY[2]::integer[]))"
176
177
  end
177
178
 
178
179
  it "should support an :order option" do
179
180
  @c1.pg_array_to_many :tags, :clone=>:tags, :order=>[:a, :b]
180
181
  @c2.many_to_pg_array :artists, :clone=>:artists, :order=>[:a, :b]
181
182
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE (tags.id IN (1, 2, 3)) ORDER BY a, b"
182
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]) ORDER BY a, b"
183
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]::integer[]) ORDER BY a, b"
183
184
  end
184
185
 
185
186
  it "should support a select option" do
186
187
  @c1.pg_array_to_many :tags, :clone=>:tags, :select=>[:a, :b]
187
188
  @c2.many_to_pg_array :artists, :clone=>:artists, :select=>[:a, :b]
188
189
  @c1.load(:tag_ids=>Sequel.pg_array([1,2,3])).tags_dataset.sql.should == "SELECT a, b FROM tags WHERE (tags.id IN (1, 2, 3))"
189
- @c2.load(:id=>1).artists_dataset.sql.should == "SELECT a, b FROM artists WHERE (artists.tag_ids @> ARRAY[1])"
190
+ @c2.load(:id=>1).artists_dataset.sql.should == "SELECT a, b FROM artists WHERE (artists.tag_ids @> ARRAY[1]::integer[])"
190
191
  end
191
192
 
192
193
  it "should accept a block" do
193
194
  @c1.pg_array_to_many :tags, :clone=>:tags do |ds| ds.filter(:yyy=>@yyy) end
194
195
  @c2.many_to_pg_array :artists, :clone=>:artists do |ds| ds.filter(:a=>1) end
195
196
  @c1.new(:yyy=>6, :tag_ids=>Sequel.pg_array([1,2,3])).tags_dataset.sql.should == "SELECT * FROM tags WHERE ((tags.id IN (1, 2, 3)) AND (yyy = 6))"
196
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE ((artists.tag_ids @> ARRAY[2]) AND (a = 1))"
197
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE ((artists.tag_ids @> ARRAY[2]::integer[]) AND (a = 1))"
197
198
  end
198
199
 
199
200
  it "should support a :dataset option that is used instead of the default" do
@@ -207,7 +208,7 @@ describe Sequel::Model, "pg_array_associations" do
207
208
  @c1.pg_array_to_many :tags, :clone=>:tags, :limit=>[2, 3]
208
209
  @c2.many_to_pg_array :artists, :clone=>:artists, :limit=>[3, 2]
209
210
  @o1.tags_dataset.sql.should == "SELECT * FROM tags WHERE (tags.id IN (1, 2, 3)) LIMIT 2 OFFSET 3"
210
- @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]) LIMIT 3 OFFSET 2"
211
+ @o2.artists_dataset.sql.should == "SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]::integer[]) LIMIT 3 OFFSET 2"
211
212
  end
212
213
 
213
214
  it "should support a :uniq option that removes duplicates from the association" do
@@ -237,17 +238,17 @@ describe Sequel::Model, "pg_array_associations" do
237
238
  it "should eagerly load correctly" do
238
239
  a = @c1.eager(:tags).all
239
240
  a.should == [@o1]
240
- sqls = DB.sqls
241
+ sqls = @db.sqls
241
242
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
242
243
  sqls.should == ["SELECT * FROM artists"]
243
244
  a.first.tags.should == [@o2]
244
- DB.sqls.should == []
245
+ @db.sqls.should == []
245
246
 
246
247
  a = @c2.eager(:artists).all
247
248
  a.should == [@o2]
248
- DB.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
249
+ @db.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
249
250
  a.first.artists.should == [@o1]
250
- DB.sqls.should == []
251
+ @db.sqls.should == []
251
252
  end
252
253
 
253
254
  it "should support using custom key options when eager loading associations" do
@@ -257,28 +258,28 @@ describe Sequel::Model, "pg_array_associations" do
257
258
 
258
259
  a = @c1.eager(:tags).all
259
260
  a.should == [@o1]
260
- sqls = DB.sqls
261
+ sqls = @db.sqls
261
262
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(\(tags\.id \* 3\) IN \([369], [369], [369]\)\)/
262
263
  sqls.should == ["SELECT * FROM artists"]
263
264
  a.first.tags.should == [@o2]
264
- DB.sqls.should == []
265
+ @db.sqls.should == []
265
266
 
266
267
  a = @c2.eager(:artists).all
267
268
  a.should == [@o2]
268
- DB.sqls.should == ["SELECT * FROM tags", "SELECT * FROM artists WHERE (artists.tag_ids[1:2] && ARRAY[6])"]
269
+ @db.sqls.should == ["SELECT * FROM tags", "SELECT * FROM artists WHERE (artists.tag_ids[1:2] && ARRAY[6]::integer[])"]
269
270
  a.first.artists.should == [@o1]
270
- DB.sqls.should == []
271
+ @db.sqls.should == []
271
272
  end
272
273
 
273
274
  it "should allow cascading of eager loading for associations of associated models" do
274
275
  a = @c1.eager(:tags=>:artists).all
275
276
  a.should == [@o1]
276
- sqls = DB.sqls
277
+ sqls = @db.sqls
277
278
  sqls.slice!(1).should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
278
- sqls.should == ['SELECT * FROM artists', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
279
+ sqls.should == ['SELECT * FROM artists', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
279
280
  a.first.tags.should == [@o2]
280
281
  a.first.tags.first.artists.should == [@o1]
281
- DB.sqls.should == []
282
+ @db.sqls.should == []
282
283
  end
283
284
 
284
285
  it "should respect :eager when lazily loading an association" do
@@ -286,16 +287,16 @@ describe Sequel::Model, "pg_array_associations" do
286
287
  @c2.many_to_pg_array :artists2, :clone=>:artists, :eager=>:tags
287
288
 
288
289
  @o1.tags2.should == [@o2]
289
- DB.sqls.should == ["SELECT * FROM tags WHERE (tags.id IN (1, 2, 3))", "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
290
+ @db.sqls.should == ["SELECT * FROM tags WHERE (tags.id IN (1, 2, 3))", "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
290
291
  @o1.tags2.first.artists.should == [@o1]
291
- DB.sqls.should == []
292
+ @db.sqls.should == []
292
293
 
293
294
  @o2.artists2.should == [@o1]
294
- sqls = DB.sqls
295
+ sqls = @db.sqls
295
296
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
296
- sqls.should == ["SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2])"]
297
+ sqls.should == ["SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[2]::integer[])"]
297
298
  @o2.artists2.first.tags.should == [@o2]
298
- DB.sqls.should == []
299
+ @db.sqls.should == []
299
300
  end
300
301
 
301
302
  it "should cascade eagerly loading when the :eager_graph association option is used" do
@@ -306,36 +307,36 @@ describe Sequel::Model, "pg_array_associations" do
306
307
  @c1.dataset._fetch = {:id=>1, :tags_id=>2, :tag_ids=>Sequel.pg_array([1,2,3])}
307
308
 
308
309
  @o1.tags2.should == [@o2]
309
- DB.sqls.first.should =~ /SELECT tags\.id, artists\.id AS artists_id, artists\.tag_ids FROM tags LEFT OUTER JOIN artists ON \(artists.tag_ids @> ARRAY\[tags.id\]\) WHERE \(tags\.id IN \([123], [123], [123]\)\)/
310
+ @db.sqls.first.should =~ /SELECT tags\.id, artists\.id AS artists_id, artists\.tag_ids FROM tags LEFT OUTER JOIN artists ON \(artists.tag_ids @> ARRAY\[tags.id\]\) WHERE \(tags\.id IN \([123], [123], [123]\)\)/
310
311
  @o1.tags2.first.artists.should == [@o1]
311
- DB.sqls.should == []
312
+ @db.sqls.should == []
312
313
 
313
314
  @o2.artists2.should == [@o1]
314
- DB.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) WHERE (artists.tag_ids @> ARRAY[2])"]
315
+ @db.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) WHERE (artists.tag_ids @> ARRAY[2]::integer[])"]
315
316
  @o2.artists2.first.tags.should == [@o2]
316
- DB.sqls.should == []
317
+ @db.sqls.should == []
317
318
 
318
319
  @c2.dataset._fetch = {:id=>2, :artists_id=>1, :tag_ids=>Sequel.pg_array([1,2,3])}
319
320
  @c1.dataset._fetch = {:id=>1, :tag_ids=>Sequel.pg_array([1,2,3])}
320
321
 
321
322
  a = @c1.eager(:tags2).all
322
- sqls = DB.sqls
323
+ sqls = @db.sqls
323
324
  sqls.pop.should =~ /SELECT tags\.id, artists\.id AS artists_id, artists\.tag_ids FROM tags LEFT OUTER JOIN artists ON \(artists.tag_ids @> ARRAY\[tags.id\]\) WHERE \(tags\.id IN \([123], [123], [123]\)\)/
324
325
  sqls.should == ["SELECT * FROM artists"]
325
326
  a.should == [@o1]
326
327
  a.first.tags2.should == [@o2]
327
328
  a.first.tags2.first.artists.should == [@o1]
328
- DB.sqls.should == []
329
+ @db.sqls.should == []
329
330
 
330
331
  @c2.dataset._fetch = {:id=>2}
331
332
  @c1.dataset._fetch = {:id=>1, :tags_id=>2, :tag_ids=>Sequel.pg_array([1,2,3])}
332
333
 
333
334
  a = @c2.eager(:artists2).all
334
- DB.sqls.should == ["SELECT * FROM tags", "SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) WHERE (artists.tag_ids && ARRAY[2])"]
335
+ @db.sqls.should == ["SELECT * FROM tags", "SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
335
336
  a.should == [@o2]
336
337
  a.first.artists2.should == [@o1]
337
338
  a.first.artists2.first.tags.should == [@o2]
338
- DB.sqls.should == []
339
+ @db.sqls.should == []
339
340
  end
340
341
 
341
342
  it "should respect the :limit option when eager loading" do
@@ -344,29 +345,29 @@ describe Sequel::Model, "pg_array_associations" do
344
345
  @c1.pg_array_to_many :tags, :clone=>:tags, :limit=>2
345
346
  a = @c1.eager(:tags).all
346
347
  a.should == [@o1]
347
- sqls = DB.sqls
348
+ sqls = @db.sqls
348
349
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
349
350
  sqls.should == ["SELECT * FROM artists"]
350
351
  a.first.tags.should == [@c2.load(:id=>1), @c2.load(:id=>2)]
351
- DB.sqls.should == []
352
+ @db.sqls.should == []
352
353
 
353
354
  @c1.pg_array_to_many :tags, :clone=>:tags, :limit=>[1, 1]
354
355
  a = @c1.eager(:tags).all
355
356
  a.should == [@o1]
356
- sqls = DB.sqls
357
+ sqls = @db.sqls
357
358
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
358
359
  sqls.should == ["SELECT * FROM artists"]
359
360
  a.first.tags.should == [@c2.load(:id=>2)]
360
- DB.sqls.should == []
361
+ @db.sqls.should == []
361
362
 
362
363
  @c1.pg_array_to_many :tags, :clone=>:tags, :limit=>[nil, 1]
363
364
  a = @c1.eager(:tags).all
364
365
  a.should == [@o1]
365
- sqls = DB.sqls
366
+ sqls = @db.sqls
366
367
  sqls.pop.should =~ /SELECT \* FROM tags WHERE \(tags\.id IN \([123], [123], [123]\)\)/
367
368
  sqls.should == ["SELECT * FROM artists"]
368
369
  a.first.tags.should == [@c2.load(:id=>2), @c2.load(:id=>3)]
369
- DB.sqls.length.should == 0
370
+ @db.sqls.length.should == 0
370
371
 
371
372
  @c2.dataset._fetch = [{:id=>2}]
372
373
  @c1.dataset._fetch = [{:id=>5, :tag_ids=>Sequel.pg_array([1,2,3])},{:id=>6, :tag_ids=>Sequel.pg_array([2,3])}, {:id=>7, :tag_ids=>Sequel.pg_array([1,2])}]
@@ -374,23 +375,23 @@ describe Sequel::Model, "pg_array_associations" do
374
375
  @c2.many_to_pg_array :artists, :clone=>:artists, :limit=>2
375
376
  a = @c2.eager(:artists).all
376
377
  a.should == [@o2]
377
- DB.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
378
+ @db.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
378
379
  a.first.artists.should == [@c1.load(:id=>5, :tag_ids=>Sequel.pg_array([1,2,3])), @c1.load(:id=>6, :tag_ids=>Sequel.pg_array([2,3]))]
379
- DB.sqls.should == []
380
+ @db.sqls.should == []
380
381
 
381
382
  @c2.many_to_pg_array :artists, :clone=>:artists, :limit=>[1, 1]
382
383
  a = @c2.eager(:artists).all
383
384
  a.should == [@o2]
384
- DB.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
385
+ @db.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
385
386
  a.first.artists.should == [@c1.load(:id=>6, :tag_ids=>Sequel.pg_array([2,3]))]
386
- DB.sqls.should == []
387
+ @db.sqls.should == []
387
388
 
388
389
  @c2.many_to_pg_array :artists, :clone=>:artists, :limit=>[nil, 1]
389
390
  a = @c2.eager(:artists).all
390
391
  a.should == [@o2]
391
- DB.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2])"]
392
+ @db.sqls.should == ['SELECT * FROM tags', "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::integer[])"]
392
393
  a.first.artists.should == [@c1.load(:id=>6, :tag_ids=>Sequel.pg_array([2,3])), @c1.load(:id=>7, :tag_ids=>Sequel.pg_array([1,2]))]
393
- DB.sqls.should == []
394
+ @db.sqls.should == []
394
395
  end
395
396
 
396
397
  it "should support association_join" do
@@ -403,16 +404,16 @@ describe Sequel::Model, "pg_array_associations" do
403
404
  @c1.dataset._fetch = {:id=>1, :tags_id=>2, :tag_ids=>Sequel.pg_array([1,2,3])}
404
405
 
405
406
  a = @c1.eager_graph(:tags).all
406
- DB.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id])"]
407
+ @db.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id])"]
407
408
  a.should == [@o1]
408
409
  a.first.tags.should == [@o2]
409
- DB.sqls.should == []
410
+ @db.sqls.should == []
410
411
 
411
412
  a = @c2.eager_graph(:artists).all
412
- DB.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id])"]
413
+ @db.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id])"]
413
414
  a.should == [@o2]
414
415
  a.first.artists.should == [@o1]
415
- DB.sqls.should == []
416
+ @db.sqls.should == []
416
417
  end
417
418
 
418
419
  it "should allow cascading of eager graphing for associations of associated models" do
@@ -420,18 +421,18 @@ describe Sequel::Model, "pg_array_associations" do
420
421
  @c1.dataset._fetch = {:id=>1, :tags_id=>2, :tag_ids=>Sequel.pg_array([1,2,3]), :artists_0_id=>1, :artists_0_tag_ids=>Sequel.pg_array([1,2,3])}
421
422
 
422
423
  a = @c1.eager_graph(:tags=>:artists).all
423
- DB.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id, artists_0.id AS artists_0_id, artists_0.tag_ids AS artists_0_tag_ids FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.tag_ids @> ARRAY[tags.id])"]
424
+ @db.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id, artists_0.id AS artists_0_id, artists_0.tag_ids AS artists_0_tag_ids FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id]) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.tag_ids @> ARRAY[tags.id])"]
424
425
  a.should == [@o1]
425
426
  a.first.tags.should == [@o2]
426
427
  a.first.tags.first.artists.should == [@o1]
427
- DB.sqls.should == []
428
+ @db.sqls.should == []
428
429
 
429
430
  a = @c2.eager_graph(:artists=>:tags).all
430
- DB.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids, tags_0.id AS tags_0_id FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id]) LEFT OUTER JOIN tags AS tags_0 ON (artists.tag_ids @> ARRAY[tags_0.id])"]
431
+ @db.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids, tags_0.id AS tags_0_id FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id]) LEFT OUTER JOIN tags AS tags_0 ON (artists.tag_ids @> ARRAY[tags_0.id])"]
431
432
  a.should == [@o2]
432
433
  a.first.artists.should == [@o1]
433
434
  a.first.artists.first.tags.should == [@o2]
434
- DB.sqls.should == []
435
+ @db.sqls.should == []
435
436
  end
436
437
 
437
438
  it "eager graphing should respect key options" do
@@ -444,15 +445,15 @@ describe Sequel::Model, "pg_array_associations" do
444
445
 
445
446
  a = @c1.eager_graph(:tags).all
446
447
  a.should == [@o1]
447
- DB.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids[1:2] @> ARRAY[(tags.id * 3)])"]
448
+ @db.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id AS tags_id FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids[1:2] @> ARRAY[(tags.id * 3)])"]
448
449
  a.first.tags.should == [@o2]
449
- DB.sqls.should == []
450
+ @db.sqls.should == []
450
451
 
451
452
  a = @c2.eager_graph(:artists).all
452
453
  a.should == [@o2]
453
- DB.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids[1:2] @> ARRAY[tags.id3])"]
454
+ @db.sqls.should == ["SELECT tags.id, artists.id AS artists_id, artists.tag_ids FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids[1:2] @> ARRAY[tags.id3])"]
454
455
  a.first.artists.should == [@o1]
455
- DB.sqls.should == []
456
+ @db.sqls.should == []
456
457
  end
457
458
 
458
459
  it "should respect the association's :graph_select option" do
@@ -463,16 +464,16 @@ describe Sequel::Model, "pg_array_associations" do
463
464
  @c1.dataset._fetch = {:id=>1, :id2=>2, :tag_ids=>Sequel.pg_array([1,2,3])}
464
465
 
465
466
  a = @c1.eager_graph(:tags).all
466
- DB.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id2 FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id])"]
467
+ @db.sqls.should == ["SELECT artists.id, artists.tag_ids, tags.id2 FROM artists LEFT OUTER JOIN tags ON (artists.tag_ids @> ARRAY[tags.id])"]
467
468
  a.should == [@o1]
468
469
  a.first.tags.should == [@c2.load(:id2=>2)]
469
- DB.sqls.should == []
470
+ @db.sqls.should == []
470
471
 
471
472
  a = @c2.eager_graph(:artists).all
472
- DB.sqls.should == ["SELECT tags.id, artists.id AS artists_id FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id])"]
473
+ @db.sqls.should == ["SELECT tags.id, artists.id AS artists_id FROM tags LEFT OUTER JOIN artists ON (artists.tag_ids @> ARRAY[tags.id])"]
473
474
  a.should == [@o2]
474
475
  a.first.artists.should == [@c1.load(:id=>1)]
475
- DB.sqls.should == []
476
+ @db.sqls.should == []
476
477
  end
477
478
 
478
479
  it "should respect the association's :graph_join_type option" do
@@ -520,34 +521,34 @@ describe Sequel::Model, "pg_array_associations" do
520
521
  it "should define an add_ method for adding associated objects" do
521
522
  @o1.add_tag(@c2.load(:id=>4))
522
523
  @o1.tag_ids.should == [1,2,3,4]
523
- DB.sqls.should == []
524
+ @db.sqls.should == []
524
525
  @o1.save_changes
525
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,2,3,4] WHERE (id = 1)"]
526
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,2,3,4] WHERE (id = 1)"]
526
527
 
527
528
  @o2.add_artist(@c1.load(:id=>1, :tag_ids=>Sequel.pg_array([4])))
528
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4,2] WHERE (id = 1)"]
529
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4,2] WHERE (id = 1)"]
529
530
  end
530
531
 
531
532
  it "should define a remove_ method for removing associated objects" do
532
533
  @o1.remove_tag(@o2)
533
534
  @o1.tag_ids.should == [1,3]
534
- DB.sqls.should == []
535
+ @db.sqls.should == []
535
536
  @o1.save_changes
536
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3] WHERE (id = 1)"]
537
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3] WHERE (id = 1)"]
537
538
 
538
539
  @o2.remove_artist(@c1.load(:id=>1, :tag_ids=>Sequel.pg_array([1,2,3,4])))
539
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3,4] WHERE (id = 1)"]
540
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3,4] WHERE (id = 1)"]
540
541
  end
541
542
 
542
543
  it "should define a remove_all_ method for removing all associated objects" do
543
544
  @o1.remove_all_tags
544
545
  @o1.tag_ids.should == []
545
- DB.sqls.should == []
546
+ @db.sqls.should == []
546
547
  @o1.save_changes
547
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[] WHERE (id = 1)"]
548
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[] WHERE (id = 1)"]
548
549
 
549
550
  @o2.remove_all_artists
550
- DB.sqls.should == ["UPDATE artists SET tag_ids = array_remove(tag_ids, 2) WHERE (tag_ids @> ARRAY[2])"]
551
+ @db.sqls.should == ["UPDATE artists SET tag_ids = array_remove(tag_ids, 2) WHERE (tag_ids @> ARRAY[2])"]
551
552
  end
552
553
 
553
554
  it "should allow calling add_ and remove_ methods on new objects for pg_array_to_many associations" do
@@ -567,73 +568,73 @@ describe Sequel::Model, "pg_array_associations" do
567
568
 
568
569
  @o1.add_tag(@c2.load(:id=>4))
569
570
  @o1.tag_ids.should == [1,2,3,4]
570
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,2,3,4] WHERE (id = 1)"]
571
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,2,3,4] WHERE (id = 1)"]
571
572
 
572
573
  @o1.remove_tag(@o2)
573
574
  @o1.tag_ids.should == [1,3,4]
574
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3,4] WHERE (id = 1)"]
575
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1,3,4] WHERE (id = 1)"]
575
576
 
576
577
  @o1.remove_all_tags
577
578
  @o1.tag_ids.should == []
578
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[] WHERE (id = 1)"]
579
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[] WHERE (id = 1)"]
579
580
  end
580
581
 
581
582
  it "should have association modification methods deal with nil values" do
582
583
  v = @c1.load(:id=>1)
583
584
  v.add_tag(@c2.load(:id=>4))
584
585
  v.tag_ids.should == [4]
585
- DB.sqls.should == []
586
+ @db.sqls.should == []
586
587
  v.save_changes
587
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4]::integer[] WHERE (id = 1)"]
588
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4]::integer[] WHERE (id = 1)"]
588
589
 
589
590
  @o2.add_artist(@c1.load(:id=>1))
590
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2]::integer[] WHERE (id = 1)"]
591
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2]::integer[] WHERE (id = 1)"]
591
592
 
592
593
  v = @c1.load(:id=>1)
593
594
  v.remove_tag(@c2.load(:id=>4))
594
595
  v.tag_ids.should == nil
595
- DB.sqls.should == []
596
+ @db.sqls.should == []
596
597
  v.save_changes
597
- DB.sqls.should == []
598
+ @db.sqls.should == []
598
599
 
599
600
  @o2.remove_artist(@c1.load(:id=>1))
600
- DB.sqls.should == []
601
+ @db.sqls.should == []
601
602
 
602
603
  v = @c1.load(:id=>1)
603
604
  v.remove_all_tags
604
605
  v.tag_ids.should == nil
605
- DB.sqls.should == []
606
+ @db.sqls.should == []
606
607
  v.save_changes
607
- DB.sqls.should == []
608
+ @db.sqls.should == []
608
609
  end
609
610
 
610
611
  it "should have association modification methods deal with empty arrays values" do
611
612
  v = @c1.load(:id=>1, :tag_ids=>Sequel.pg_array([]))
612
613
  v.add_tag(@c2.load(:id=>4))
613
614
  v.tag_ids.should == [4]
614
- DB.sqls.should == []
615
+ @db.sqls.should == []
615
616
  v.save_changes
616
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4] WHERE (id = 1)"]
617
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4] WHERE (id = 1)"]
617
618
 
618
619
  @o2.add_artist(@c1.load(:id=>1, :tag_ids=>Sequel.pg_array([])))
619
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2] WHERE (id = 1)"]
620
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2] WHERE (id = 1)"]
620
621
 
621
622
  v = @c1.load(:id=>1, :tag_ids=>Sequel.pg_array([]))
622
623
  v.remove_tag(@c2.load(:id=>4))
623
624
  v.tag_ids.should == []
624
- DB.sqls.should == []
625
+ @db.sqls.should == []
625
626
  v.save_changes
626
- DB.sqls.should == []
627
+ @db.sqls.should == []
627
628
 
628
629
  @o2.remove_artist(@c1.load(:id=>1, :tag_ids=>Sequel.pg_array([])))
629
- DB.sqls.should == []
630
+ @db.sqls.should == []
630
631
 
631
632
  v = @c1.load(:id=>1, :tag_ids=>Sequel.pg_array([]))
632
633
  v.remove_all_tags
633
634
  v.tag_ids.should == []
634
- DB.sqls.should == []
635
+ @db.sqls.should == []
635
636
  v.save_changes
636
- DB.sqls.should == []
637
+ @db.sqls.should == []
637
638
  end
638
639
 
639
640
  it "should respect the :array_type option when manually creating arrays" do
@@ -642,12 +643,41 @@ describe Sequel::Model, "pg_array_associations" do
642
643
  v = @c1.load(:id=>1)
643
644
  v.add_tag(@c2.load(:id=>4))
644
645
  v.tag_ids.should == [4]
645
- DB.sqls.should == []
646
+ @db.sqls.should == []
646
647
  v.save_changes
647
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4]::int8[] WHERE (id = 1)"]
648
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[4]::int8[] WHERE (id = 1)"]
648
649
 
649
650
  @o2.add_artist(@c1.load(:id=>1))
650
- DB.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2]::int8[] WHERE (id = 1)"]
651
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[2]::int8[] WHERE (id = 1)"]
652
+ end
653
+
654
+ it "should respect the :array_type option in the associations dataset" do
655
+ @c2.many_to_pg_array :artists, :clone=>:artists, :array_type=>:int8
656
+ @c2.load(:id=>1).artists_dataset.sql.should == 'SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[1]::int8[])'
657
+ end
658
+
659
+ it "should respect the :array_type option when eager loading" do
660
+ @c2.many_to_pg_array :artists, :clone=>:artists, :array_type=>:int8
661
+ @c2.eager(:artists).all
662
+ @db.sqls.should == ["SELECT * FROM tags", "SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[2]::int8[])"]
663
+ end
664
+
665
+ it "should respect the :array_type option when filtering by associations" do
666
+ @c1.pg_array_to_many :tags, :clone=>:tags, :array_type=>:int8
667
+ @c1.where(:tags=>@c2.load(:id=>1)).sql.should == 'SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[1]::int8[])'
668
+ @c1.where(:tags=>[@c2.load(:id=>1), @c2.load(:id=>2)]).sql.should == 'SELECT * FROM artists WHERE (artists.tag_ids && ARRAY[1,2]::int8[])'
669
+ end
670
+
671
+ it "should automatically determine the array type by looking at the schema" do
672
+ @c1.db_schema[:tag_ids][:db_type] = 'int8'
673
+ @c2.many_to_pg_array :artists, :clone=>:artists
674
+ @c1.pg_array_to_many :tags, :clone=>:tags, :save_after_modify=>true
675
+ @c2.load(:id=>1).artists_dataset.sql.should == 'SELECT * FROM artists WHERE (artists.tag_ids @> ARRAY[1]::int8[])'
676
+ @c1.load(:id=>1).add_tag(@c2.load(:id=>1))
677
+ @db.sqls.should == ["UPDATE artists SET tag_ids = ARRAY[1]::int8[] WHERE (id = 1)"]
678
+ end
679
+
680
+ it "should automatically determine the array type by looking at the schema" do
651
681
  end
652
682
 
653
683
  it "should not validate the current/associated object in add_ and remove_ if the :validate=>false option is used" do