sequel 3.48.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +114 -0
  3. data/Rakefile +10 -7
  4. data/doc/association_basics.rdoc +25 -23
  5. data/doc/code_order.rdoc +7 -0
  6. data/doc/core_extensions.rdoc +0 -10
  7. data/doc/object_model.rdoc +4 -1
  8. data/doc/querying.rdoc +3 -3
  9. data/doc/release_notes/4.0.0.txt +262 -0
  10. data/doc/security.rdoc +0 -28
  11. data/doc/testing.rdoc +8 -14
  12. data/lib/sequel/adapters/ado.rb +7 -11
  13. data/lib/sequel/adapters/ado/access.rb +8 -8
  14. data/lib/sequel/adapters/ado/mssql.rb +4 -4
  15. data/lib/sequel/adapters/amalgalite.rb +6 -6
  16. data/lib/sequel/adapters/cubrid.rb +7 -7
  17. data/lib/sequel/adapters/db2.rb +5 -9
  18. data/lib/sequel/adapters/dbi.rb +2 -6
  19. data/lib/sequel/adapters/do.rb +4 -4
  20. data/lib/sequel/adapters/firebird.rb +4 -4
  21. data/lib/sequel/adapters/ibmdb.rb +8 -8
  22. data/lib/sequel/adapters/informix.rb +2 -10
  23. data/lib/sequel/adapters/jdbc.rb +17 -17
  24. data/lib/sequel/adapters/jdbc/as400.rb +2 -2
  25. data/lib/sequel/adapters/jdbc/cubrid.rb +1 -1
  26. data/lib/sequel/adapters/jdbc/db2.rb +1 -1
  27. data/lib/sequel/adapters/jdbc/derby.rb +1 -1
  28. data/lib/sequel/adapters/jdbc/h2.rb +2 -2
  29. data/lib/sequel/adapters/jdbc/hsqldb.rb +1 -1
  30. data/lib/sequel/adapters/jdbc/informix.rb +1 -1
  31. data/lib/sequel/adapters/jdbc/mssql.rb +2 -2
  32. data/lib/sequel/adapters/jdbc/mysql.rb +1 -1
  33. data/lib/sequel/adapters/jdbc/oracle.rb +5 -1
  34. data/lib/sequel/adapters/jdbc/postgresql.rb +3 -3
  35. data/lib/sequel/adapters/jdbc/sqlite.rb +3 -3
  36. data/lib/sequel/adapters/jdbc/transactions.rb +3 -3
  37. data/lib/sequel/adapters/mock.rb +7 -7
  38. data/lib/sequel/adapters/mysql.rb +3 -3
  39. data/lib/sequel/adapters/mysql2.rb +4 -4
  40. data/lib/sequel/adapters/odbc.rb +2 -6
  41. data/lib/sequel/adapters/odbc/mssql.rb +1 -1
  42. data/lib/sequel/adapters/openbase.rb +1 -5
  43. data/lib/sequel/adapters/oracle.rb +13 -17
  44. data/lib/sequel/adapters/postgres.rb +20 -25
  45. data/lib/sequel/adapters/shared/cubrid.rb +3 -3
  46. data/lib/sequel/adapters/shared/db2.rb +2 -2
  47. data/lib/sequel/adapters/shared/firebird.rb +7 -7
  48. data/lib/sequel/adapters/shared/mssql.rb +9 -9
  49. data/lib/sequel/adapters/shared/mysql.rb +29 -13
  50. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +7 -7
  51. data/lib/sequel/adapters/shared/oracle.rb +22 -13
  52. data/lib/sequel/adapters/shared/postgres.rb +61 -46
  53. data/lib/sequel/adapters/shared/sqlite.rb +9 -9
  54. data/lib/sequel/adapters/sqlite.rb +17 -11
  55. data/lib/sequel/adapters/swift.rb +3 -3
  56. data/lib/sequel/adapters/swift/mysql.rb +1 -1
  57. data/lib/sequel/adapters/swift/sqlite.rb +1 -1
  58. data/lib/sequel/adapters/tinytds.rb +8 -8
  59. data/lib/sequel/ast_transformer.rb +3 -1
  60. data/lib/sequel/connection_pool.rb +4 -2
  61. data/lib/sequel/connection_pool/sharded_single.rb +2 -2
  62. data/lib/sequel/connection_pool/sharded_threaded.rb +5 -5
  63. data/lib/sequel/connection_pool/threaded.rb +7 -7
  64. data/lib/sequel/core.rb +4 -67
  65. data/lib/sequel/database.rb +1 -0
  66. data/lib/sequel/database/connecting.rb +2 -8
  67. data/lib/sequel/database/dataset.rb +2 -7
  68. data/lib/sequel/database/dataset_defaults.rb +0 -18
  69. data/lib/sequel/database/features.rb +4 -4
  70. data/lib/sequel/database/misc.rb +6 -8
  71. data/lib/sequel/database/query.rb +5 -61
  72. data/lib/sequel/database/schema_generator.rb +22 -20
  73. data/lib/sequel/database/schema_methods.rb +48 -20
  74. data/lib/sequel/database/transactions.rb +7 -17
  75. data/lib/sequel/dataset.rb +2 -0
  76. data/lib/sequel/dataset/actions.rb +23 -91
  77. data/lib/sequel/dataset/features.rb +1 -4
  78. data/lib/sequel/dataset/graph.rb +3 -47
  79. data/lib/sequel/dataset/misc.rb +4 -33
  80. data/lib/sequel/dataset/prepared_statements.rb +3 -1
  81. data/lib/sequel/dataset/query.rb +116 -240
  82. data/lib/sequel/dataset/sql.rb +19 -97
  83. data/lib/sequel/deprecated.rb +0 -16
  84. data/lib/sequel/exceptions.rb +0 -3
  85. data/lib/sequel/extensions/_pretty_table.rb +1 -1
  86. data/lib/sequel/extensions/columns_introspection.rb +1 -12
  87. data/lib/sequel/extensions/constraint_validations.rb +3 -3
  88. data/lib/sequel/extensions/core_extensions.rb +0 -9
  89. data/lib/sequel/extensions/date_arithmetic.rb +1 -2
  90. data/lib/sequel/extensions/graph_each.rb +11 -0
  91. data/lib/sequel/extensions/migration.rb +5 -5
  92. data/lib/sequel/extensions/null_dataset.rb +11 -13
  93. data/lib/sequel/extensions/pagination.rb +3 -6
  94. data/lib/sequel/extensions/pg_array.rb +6 -4
  95. data/lib/sequel/extensions/pg_array_ops.rb +35 -1
  96. data/lib/sequel/extensions/pg_json.rb +12 -2
  97. data/lib/sequel/extensions/pg_json_ops.rb +266 -0
  98. data/lib/sequel/extensions/pg_range.rb +2 -2
  99. data/lib/sequel/extensions/pg_range_ops.rb +0 -8
  100. data/lib/sequel/extensions/pg_row.rb +2 -2
  101. data/lib/sequel/extensions/pretty_table.rb +0 -4
  102. data/lib/sequel/extensions/query.rb +3 -8
  103. data/lib/sequel/extensions/schema_caching.rb +0 -7
  104. data/lib/sequel/extensions/schema_dumper.rb +10 -17
  105. data/lib/sequel/extensions/select_remove.rb +0 -4
  106. data/lib/sequel/extensions/set_overrides.rb +28 -0
  107. data/lib/sequel/extensions/to_dot.rb +6 -10
  108. data/lib/sequel/model.rb +6 -7
  109. data/lib/sequel/model/associations.rb +127 -182
  110. data/lib/sequel/model/base.rb +88 -211
  111. data/lib/sequel/model/errors.rb +0 -13
  112. data/lib/sequel/model/plugins.rb +2 -2
  113. data/lib/sequel/no_core_ext.rb +0 -1
  114. data/lib/sequel/plugins/after_initialize.rb +11 -17
  115. data/lib/sequel/plugins/association_autoreloading.rb +1 -47
  116. data/lib/sequel/plugins/association_dependencies.rb +2 -2
  117. data/lib/sequel/plugins/auto_validations.rb +2 -8
  118. data/lib/sequel/plugins/blacklist_security.rb +32 -2
  119. data/lib/sequel/plugins/caching.rb +1 -1
  120. data/lib/sequel/plugins/class_table_inheritance.rb +2 -2
  121. data/lib/sequel/plugins/composition.rb +10 -8
  122. data/lib/sequel/plugins/constraint_validations.rb +2 -2
  123. data/lib/sequel/plugins/dataset_associations.rb +4 -0
  124. data/lib/sequel/plugins/defaults_setter.rb +8 -6
  125. data/lib/sequel/plugins/dirty.rb +6 -6
  126. data/lib/sequel/plugins/force_encoding.rb +13 -8
  127. data/lib/sequel/plugins/hook_class_methods.rb +1 -7
  128. data/lib/sequel/plugins/json_serializer.rb +13 -74
  129. data/lib/sequel/plugins/lazy_attributes.rb +2 -4
  130. data/lib/sequel/plugins/list.rb +1 -1
  131. data/lib/sequel/plugins/many_through_many.rb +4 -11
  132. data/lib/sequel/plugins/many_to_one_pk_lookup.rb +1 -49
  133. data/lib/sequel/plugins/nested_attributes.rb +1 -1
  134. data/lib/sequel/plugins/optimistic_locking.rb +3 -5
  135. data/lib/sequel/plugins/pg_array_associations.rb +453 -0
  136. data/lib/sequel/plugins/pg_typecast_on_load.rb +23 -7
  137. data/lib/sequel/plugins/prepared_statements.rb +1 -1
  138. data/lib/sequel/plugins/prepared_statements_associations.rb +20 -14
  139. data/lib/sequel/plugins/prepared_statements_safe.rb +2 -2
  140. data/lib/sequel/plugins/rcte_tree.rb +1 -1
  141. data/lib/sequel/plugins/serialization.rb +5 -4
  142. data/lib/sequel/plugins/serialization_modification_detection.rb +1 -1
  143. data/lib/sequel/plugins/sharding.rb +7 -1
  144. data/lib/sequel/plugins/single_table_inheritance.rb +1 -1
  145. data/lib/sequel/plugins/timestamps.rb +1 -1
  146. data/lib/sequel/plugins/touch.rb +2 -2
  147. data/lib/sequel/plugins/tree.rb +1 -1
  148. data/lib/sequel/plugins/typecast_on_load.rb +19 -4
  149. data/lib/sequel/plugins/validation_class_methods.rb +0 -30
  150. data/lib/sequel/plugins/validation_helpers.rb +13 -31
  151. data/lib/sequel/plugins/xml_serializer.rb +18 -57
  152. data/lib/sequel/sql.rb +20 -22
  153. data/lib/sequel/version.rb +2 -2
  154. data/spec/adapters/db2_spec.rb +14 -23
  155. data/spec/adapters/firebird_spec.rb +25 -29
  156. data/spec/adapters/informix_spec.rb +11 -14
  157. data/spec/adapters/mssql_spec.rb +71 -77
  158. data/spec/adapters/mysql_spec.rb +165 -172
  159. data/spec/adapters/oracle_spec.rb +36 -39
  160. data/spec/adapters/postgres_spec.rb +175 -100
  161. data/spec/adapters/spec_helper.rb +13 -11
  162. data/spec/adapters/sqlite_spec.rb +36 -44
  163. data/spec/core/connection_pool_spec.rb +2 -1
  164. data/spec/core/database_spec.rb +55 -55
  165. data/spec/core/dataset_spec.rb +45 -249
  166. data/spec/core/deprecated_spec.rb +0 -8
  167. data/spec/core/expression_filters_spec.rb +23 -5
  168. data/spec/core/object_graph_spec.rb +4 -66
  169. data/spec/core/schema_spec.rb +35 -12
  170. data/spec/core/spec_helper.rb +3 -2
  171. data/spec/core_extensions_spec.rb +17 -19
  172. data/spec/extensions/arbitrary_servers_spec.rb +2 -3
  173. data/spec/extensions/association_dependencies_spec.rb +14 -14
  174. data/spec/extensions/auto_validations_spec.rb +7 -0
  175. data/spec/extensions/blacklist_security_spec.rb +5 -5
  176. data/spec/extensions/blank_spec.rb +2 -0
  177. data/spec/extensions/class_table_inheritance_spec.rb +2 -2
  178. data/spec/extensions/columns_introspection_spec.rb +2 -29
  179. data/spec/extensions/composition_spec.rb +10 -17
  180. data/spec/extensions/core_refinements_spec.rb +5 -1
  181. data/spec/extensions/dataset_associations_spec.rb +18 -0
  182. data/spec/extensions/date_arithmetic_spec.rb +2 -2
  183. data/spec/extensions/defaults_setter_spec.rb +9 -9
  184. data/spec/extensions/dirty_spec.rb +0 -5
  185. data/spec/extensions/eval_inspect_spec.rb +2 -0
  186. data/spec/extensions/force_encoding_spec.rb +2 -18
  187. data/spec/extensions/hash_aliases_spec.rb +8 -0
  188. data/spec/extensions/hook_class_methods_spec.rb +39 -58
  189. data/spec/extensions/inflector_spec.rb +2 -0
  190. data/spec/extensions/instance_filters_spec.rb +8 -8
  191. data/spec/extensions/json_serializer_spec.rb +1 -41
  192. data/spec/extensions/list_spec.rb +1 -1
  193. data/spec/extensions/many_through_many_spec.rb +106 -109
  194. data/spec/extensions/migration_spec.rb +2 -0
  195. data/spec/extensions/named_timezones_spec.rb +1 -0
  196. data/spec/extensions/pg_array_associations_spec.rb +603 -0
  197. data/spec/extensions/pg_array_ops_spec.rb +25 -0
  198. data/spec/extensions/pg_array_spec.rb +9 -1
  199. data/spec/extensions/pg_hstore_ops_spec.rb +13 -0
  200. data/spec/extensions/pg_hstore_spec.rb +1 -0
  201. data/spec/extensions/pg_json_ops_spec.rb +131 -0
  202. data/spec/extensions/pg_json_spec.rb +10 -4
  203. data/spec/extensions/pg_range_ops_spec.rb +2 -5
  204. data/spec/extensions/pg_range_spec.rb +6 -2
  205. data/spec/extensions/pg_row_ops_spec.rb +2 -0
  206. data/spec/extensions/prepared_statements_associations_spec.rb +26 -5
  207. data/spec/extensions/rcte_tree_spec.rb +15 -15
  208. data/spec/extensions/schema_dumper_spec.rb +0 -1
  209. data/spec/extensions/schema_spec.rb +9 -9
  210. data/spec/extensions/serialization_modification_detection_spec.rb +1 -1
  211. data/spec/extensions/serialization_spec.rb +18 -29
  212. data/spec/extensions/set_overrides_spec.rb +4 -0
  213. data/spec/extensions/{many_to_one_pk_lookup_spec.rb → shared_caching_spec.rb} +1 -4
  214. data/spec/extensions/single_table_inheritance_spec.rb +4 -4
  215. data/spec/extensions/spec_helper.rb +8 -9
  216. data/spec/extensions/sql_expr_spec.rb +2 -0
  217. data/spec/extensions/string_date_time_spec.rb +2 -0
  218. data/spec/extensions/string_stripper_spec.rb +2 -0
  219. data/spec/extensions/tactical_eager_loading_spec.rb +12 -12
  220. data/spec/extensions/thread_local_timezones_spec.rb +2 -0
  221. data/spec/extensions/timestamps_spec.rb +1 -1
  222. data/spec/extensions/to_dot_spec.rb +1 -1
  223. data/spec/extensions/touch_spec.rb +24 -24
  224. data/spec/extensions/tree_spec.rb +7 -7
  225. data/spec/extensions/typecast_on_load_spec.rb +8 -1
  226. data/spec/extensions/update_primary_key_spec.rb +10 -10
  227. data/spec/extensions/validation_class_methods_spec.rb +10 -39
  228. data/spec/extensions/validation_helpers_spec.rb +29 -47
  229. data/spec/extensions/xml_serializer_spec.rb +1 -23
  230. data/spec/integration/associations_test.rb +231 -40
  231. data/spec/integration/database_test.rb +1 -1
  232. data/spec/integration/dataset_test.rb +64 -64
  233. data/spec/integration/eager_loader_test.rb +28 -28
  234. data/spec/integration/migrator_test.rb +1 -1
  235. data/spec/integration/model_test.rb +2 -2
  236. data/spec/integration/plugin_test.rb +21 -21
  237. data/spec/integration/prepared_statement_test.rb +7 -7
  238. data/spec/integration/schema_test.rb +115 -110
  239. data/spec/integration/spec_helper.rb +17 -27
  240. data/spec/integration/timezone_test.rb +1 -1
  241. data/spec/integration/transaction_test.rb +10 -10
  242. data/spec/integration/type_test.rb +2 -2
  243. data/spec/model/association_reflection_spec.rb +2 -28
  244. data/spec/model/associations_spec.rb +239 -188
  245. data/spec/model/base_spec.rb +27 -68
  246. data/spec/model/dataset_methods_spec.rb +4 -4
  247. data/spec/model/eager_loading_spec.rb +160 -172
  248. data/spec/model/hooks_spec.rb +62 -79
  249. data/spec/model/model_spec.rb +36 -51
  250. data/spec/model/plugins_spec.rb +5 -19
  251. data/spec/model/record_spec.rb +125 -151
  252. data/spec/model/spec_helper.rb +8 -6
  253. data/spec/model/validations_spec.rb +4 -17
  254. data/spec/spec_config.rb +2 -10
  255. metadata +50 -56
  256. data/lib/sequel/deprecated_core_extensions.rb +0 -135
  257. data/lib/sequel/extensions/pg_auto_parameterize.rb +0 -185
  258. data/lib/sequel/extensions/pg_statement_cache.rb +0 -318
  259. data/lib/sequel/plugins/identity_map.rb +0 -260
  260. data/lib/sequel_core.rb +0 -2
  261. data/lib/sequel_model.rb +0 -2
  262. data/spec/extensions/association_autoreloading_spec.rb +0 -102
  263. data/spec/extensions/identity_map_spec.rb +0 -337
  264. data/spec/extensions/pg_auto_parameterize_spec.rb +0 -70
  265. data/spec/extensions/pg_statement_cache_spec.rb +0 -208
  266. data/spec/rcov.opts +0 -8
  267. data/spec/spec_config.rb.example +0 -10
@@ -1,5 +1,7 @@
1
1
  require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
2
2
 
3
+ Sequel.extension :inflector
4
+
3
5
  describe String do
4
6
  it "#camelize and #camelcase should transform the word to CamelCase" do
5
7
  "egg_and_hams".camelize.should == "EggAndHams"
@@ -7,40 +7,40 @@ describe "instance_filters plugin" do
7
7
  @c.columns :id, :name, :num
8
8
  @c.plugin :instance_filters
9
9
  @p = @c.load(:id=>1, :name=>'John', :num=>1)
10
- MODEL_DB.sqls
10
+ DB.sqls
11
11
  end
12
12
 
13
13
  specify "should raise an error when updating a stale record" do
14
14
  @p.update(:name=>'Bob')
15
- MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
15
+ DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
16
16
  @p.instance_filter(:name=>'Jim')
17
17
  @p.this.numrows = 0
18
18
  proc{@p.update(:name=>'Joe')}.should raise_error(Sequel::Plugins::InstanceFilters::Error)
19
- MODEL_DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Jim'))"]
19
+ DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Jim'))"]
20
20
  end
21
21
 
22
22
  specify "should raise an error when destroying a stale record" do
23
23
  @p.destroy
24
- MODEL_DB.sqls.should == ["DELETE FROM people WHERE id = 1"]
24
+ DB.sqls.should == ["DELETE FROM people WHERE id = 1"]
25
25
  @p.instance_filter(:name=>'Jim')
26
26
  @p.this.numrows = 0
27
27
  proc{@p.destroy}.should raise_error(Sequel::Plugins::InstanceFilters::Error)
28
- MODEL_DB.sqls.should == ["DELETE FROM people WHERE ((id = 1) AND (name = 'Jim'))"]
28
+ DB.sqls.should == ["DELETE FROM people WHERE ((id = 1) AND (name = 'Jim'))"]
29
29
  end
30
30
 
31
31
  specify "should apply all instance filters" do
32
32
  @p.instance_filter(:name=>'Jim')
33
33
  @p.instance_filter{num > 2}
34
34
  @p.update(:name=>'Bob')
35
- MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE ((id = 1) AND (name = 'Jim') AND (num > 2))"]
35
+ DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE ((id = 1) AND (name = 'Jim') AND (num > 2))"]
36
36
  end
37
37
 
38
38
  specify "should drop instance filters after updating" do
39
39
  @p.instance_filter(:name=>'Joe')
40
40
  @p.update(:name=>'Joe')
41
- MODEL_DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Joe'))"]
41
+ DB.sqls.should == ["UPDATE people SET name = 'Joe' WHERE ((id = 1) AND (name = 'Joe'))"]
42
42
  @p.update(:name=>'Bob')
43
- MODEL_DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
43
+ DB.sqls.should == ["UPDATE people SET name = 'Bob' WHERE (id = 1)"]
44
44
  end
45
45
 
46
46
  specify "shouldn't allow instance filters on frozen objects" do
@@ -43,15 +43,6 @@ describe "Sequel::Plugins::JsonSerializer" do
43
43
  Artist.from_json(Artist.load(:name=>Date.today).to_json).should == Artist.load(:name=>Date.today)
44
44
  end
45
45
 
46
- qspecify "should have .json_create method for creating an instance from a hash parsed from JSON" do
47
- JSON.parse(@artist.to_json(:naked=>false), :create_additions=>true).should == @artist
48
- JSON.parse(@artist.to_json(:naked=>false, :include=>{:albums=>{:include=>{:artist=>{:include=>:albums}}}}), :create_additions=>true).albums.map{|a| a.artist.albums}.should == [[@album]]
49
- end
50
-
51
- qspecify "should have .json_create method raise error if not given a hash" do
52
- proc{Artist.json_create([])}.should raise_error(Sequel::Error)
53
- end
54
-
55
46
  it "should handle the :only option" do
56
47
  Artist.from_json(@artist.to_json(:only=>:name)).should == Artist.load(:name=>@artist.name)
57
48
  Album.from_json(@album.to_json(:only=>[:id, :name])).should == Album.load(:id=>@album.id, :name=>@album.name)
@@ -129,10 +120,6 @@ describe "Sequel::Plugins::JsonSerializer" do
129
120
  Album.from_json(@album.to_json(:include=>{:artist=>{:include=>{:albums=>{:only=>:name}}}}), :associations=>{:artist=>{:associations=>:albums}}).artist.albums.should == [Album.load(:name=>@album.name)]
130
121
  end
131
122
 
132
- qspecify "should automatically cascade parsing for all associations if :all_associations is used" do
133
- Artist.from_json(@artist.to_json(:include=>{:albums=>{:include=>:artist}}), :all_associations=>true).albums.map{|a| a.artist}.should == [@artist]
134
- end
135
-
136
123
  it "should handle the :include option cascading with an empty hash" do
137
124
  Album.from_json(@album.to_json(:include=>{:artist=>{}}), :associations=>:artist).artist.should == @artist
138
125
  Album.from_json(@album.to_json(:include=>{:blah=>{}})).blah.should == @album.blah
@@ -228,6 +215,7 @@ describe "Sequel::Plugins::JsonSerializer" do
228
215
  it "should handle the :root=>:collection option to qualify just the collection" do
229
216
  Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
230
217
  Album.dataset.to_json(:root=>:collection, :only => :id).to_s.should == '{"albums":[{"id":1},{"id":1}]}'
218
+ Album.dataset.to_json(:root=>true, :only => :id).to_s.should == '{"albums":[{"id":1},{"id":1}]}'
231
219
  end
232
220
 
233
221
  it "should handle the :root=>:instance option to qualify just the instances" do
@@ -235,11 +223,6 @@ describe "Sequel::Plugins::JsonSerializer" do
235
223
  Album.dataset.to_json(:root=>:instance, :only => :id).to_s.should == '[{"album":{"id":1}},{"album":{"id":1}}]'
236
224
  end
237
225
 
238
- qspecify "should handle the :root=>true option be the same as :root=>:both for backwards compatibility" do
239
- Album.dataset._fetch = [{:id=>1, :name=>'RF'}, {:id=>1, :name=>'RF'}]
240
- Album.dataset.to_json(:root=>true, :only => :id).to_s.should == '{"albums":[{"album":{"id":1}},{"album":{"id":1}}]}'
241
- end
242
-
243
226
  it "should store the default options in json_serializer_opts" do
244
227
  Album.json_serializer_opts.should == {:naked=>true}
245
228
  c = Class.new(Album)
@@ -260,29 +243,6 @@ describe "Sequel::Plugins::JsonSerializer" do
260
243
  Object.send(:remove_const, :Artist3)
261
244
  end
262
245
 
263
- qspecify "should have :associations option take precedence over :all_assocations" do
264
- Artist.from_json(@artist.to_json(:include=>:albums), :associations=>[], :all_associations=>true, :fields=>[]).associations.should == {}
265
- end
266
-
267
- qspecify "should allow overriding of :all_columns options in associated objects" do
268
- Album.restrict_primary_key
269
- Artist.from_json(@artist.to_json(:include=>:albums), :associations=>{:albums=>{:fields=>[:id, :name, :artist_id], :missing=>:raise}}, :all_columns=>true).albums.should == [@album]
270
- end
271
-
272
- qspecify "should handle typecasting if setter columns exist when using :all_columns" do
273
- Artist.restrict_primary_key
274
- Artist.from_json('{"id":"2","name":"YJM","json_class":"Artist"}', :all_columns=>true).should == @artist
275
- end
276
-
277
- qspecify "should allow setting columns that are restricted if :all_columns is used" do
278
- Artist.restrict_primary_key
279
- Artist.from_json(@artist.to_json, :all_columns=>true).should == @artist
280
- end
281
-
282
- qspecify "should raise an error if using :all_columns and non-column is in the JSON" do
283
- proc{Artist.from_json('{"foo":"bar"}', :all_columns=>true)}.should raise_error(Sequel::Error)
284
- end
285
-
286
246
  it "should raise an error if attempting to set a restricted column and :all_columns is not used" do
287
247
  Artist.restrict_primary_key
288
248
  proc{Artist.from_json(@artist.to_json)}.should raise_error(Sequel::Error)
@@ -2,7 +2,7 @@ require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
2
2
 
3
3
  describe "List plugin" do
4
4
  def klass(opts={})
5
- @db = MODEL_DB
5
+ @db = DB
6
6
  c = Class.new(Sequel::Model(@db[:items]))
7
7
  c.class_eval do
8
8
  columns :id, :position, :scope_id, :pos
@@ -14,7 +14,7 @@ describe Sequel::Model, "many_through_many" do
14
14
  @c2 = Tag
15
15
  @dataset = @c2.dataset
16
16
  @dataset._fetch = {:id=>1}
17
- MODEL_DB.reset
17
+ DB.reset
18
18
  end
19
19
  after do
20
20
  Object.send(:remove_const, :Artist)
@@ -41,9 +41,9 @@ describe Sequel::Model, "many_through_many" do
41
41
  @c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:id3
42
42
  a = @c1.eager(:tags).all
43
43
  a.should == [@c1.load(:id => 1)]
44
- MODEL_DB.sqls.should == ['SELECT * FROM artists', "SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (3)))"]
44
+ DB.sqls.should == ['SELECT * FROM artists', "SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (3)))"]
45
45
  a.first.tags.should == [@c2.load(:id=>4)]
46
- MODEL_DB.sqls.should == []
46
+ DB.sqls.should == []
47
47
  end
48
48
 
49
49
  it "should handle a :eager_loading_predicate_key option to change the SQL used in the lookup" do
@@ -52,7 +52,7 @@ describe Sequel::Model, "many_through_many" do
52
52
  @c1.many_through_many :tags, :through=>[[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_loading_predicate_key=>Sequel./(:albums_artists__artist_id, 3)
53
53
  a = @c1.eager(:tags).all
54
54
  a.should == [@c1.load(:id => 1)]
55
- MODEL_DB.sqls.should == ['SELECT * FROM artists', "SELECT tags.*, (albums_artists.artist_id / 3) AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND ((albums_artists.artist_id / 3) IN (1)))"]
55
+ DB.sqls.should == ['SELECT * FROM artists', "SELECT tags.*, (albums_artists.artist_id / 3) AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND ((albums_artists.artist_id / 3) IN (1)))"]
56
56
  a.first.tags.should == [@c2.load(:id=>4)]
57
57
  end
58
58
 
@@ -294,18 +294,18 @@ describe Sequel::Model, "many_through_many" do
294
294
  it "should provide an array with all members of the association" do
295
295
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
296
296
  @c1.load(:id => 1234).tags.should == [@c2.load(:id=>1)]
297
- MODEL_DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
297
+ DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
298
298
  end
299
299
 
300
300
  it "should populate cache when accessed" do
301
301
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
302
302
  n = @c1.load(:id => 1234)
303
303
  n.associations[:tags].should == nil
304
- MODEL_DB.sqls.should == []
304
+ DB.sqls.should == []
305
305
  n.tags.should == [@c2.load(:id=>1)]
306
- MODEL_DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
306
+ DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
307
307
  n.associations[:tags].should == n.tags
308
- MODEL_DB.sqls.length.should == 0
308
+ DB.sqls.length.should == 0
309
309
  end
310
310
 
311
311
  it "should use cache if available" do
@@ -313,18 +313,18 @@ describe Sequel::Model, "many_through_many" do
313
313
  n = @c1.load(:id => 1234)
314
314
  n.associations[:tags] = []
315
315
  n.tags.should == []
316
- MODEL_DB.sqls.should == []
316
+ DB.sqls.should == []
317
317
  end
318
318
 
319
319
  it "should not use cache if asked to reload" do
320
320
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]
321
321
  n = @c1.load(:id => 1234)
322
322
  n.associations[:tags] = []
323
- MODEL_DB.sqls.should == []
323
+ DB.sqls.should == []
324
324
  n.tags(true).should == [@c2.load(:id=>1)]
325
- MODEL_DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
325
+ DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1234))']
326
326
  n.associations[:tags].should == n.tags
327
- MODEL_DB.sqls.length.should == 0
327
+ DB.sqls.length.should == 0
328
328
  end
329
329
 
330
330
  it "should not add associations methods directly to class" do
@@ -368,7 +368,7 @@ describe 'Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection
368
368
  end
369
369
  class ::Tag < Sequel::Model
370
370
  end
371
- MODEL_DB.reset
371
+ DB.reset
372
372
  @ar = Artist.association_reflection(:tags)
373
373
  end
374
374
  after do
@@ -452,7 +452,7 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
452
452
  end
453
453
 
454
454
  @c1 = Artist
455
- MODEL_DB.reset
455
+ DB.reset
456
456
  end
457
457
  after do
458
458
  [:Artist, :Tag, :Album, :Track].each{|x| Object.send(:remove_const, x)}
@@ -461,15 +461,15 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
461
461
  it "should eagerly load a single many_through_many association" do
462
462
  a = @c1.eager(:tags).all
463
463
  a.should == [@c1.load(:id=>1)]
464
- MODEL_DB.sqls.should == ['SELECT * FROM artists', 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
464
+ DB.sqls.should == ['SELECT * FROM artists', 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
465
465
  a.first.tags.should == [Tag.load(:id=>2)]
466
- MODEL_DB.sqls.length.should == 0
466
+ DB.sqls.length.should == 0
467
467
  end
468
468
 
469
469
  it "should eagerly load multiple associations in a single call" do
470
470
  a = @c1.eager(:tags, :albums).all
471
471
  a.should == [@c1.load(:id=>1)]
472
- sqls = MODEL_DB.sqls
472
+ sqls = DB.sqls
473
473
  sqls.length.should == 3
474
474
  sqls[0].should == 'SELECT * FROM artists'
475
475
  sqls[1..-1].should(include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))'))
@@ -477,13 +477,13 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
477
477
  a = a.first
478
478
  a.tags.should == [Tag.load(:id=>2)]
479
479
  a.albums.should == [Album.load(:id=>3)]
480
- MODEL_DB.sqls.length.should == 0
480
+ DB.sqls.length.should == 0
481
481
  end
482
482
 
483
483
  it "should eagerly load multiple associations in separate" do
484
484
  a = @c1.eager(:tags).eager(:albums).all
485
485
  a.should == [@c1.load(:id=>1)]
486
- sqls = MODEL_DB.sqls
486
+ sqls = DB.sqls
487
487
  sqls.length.should == 3
488
488
  sqls[0].should == 'SELECT * FROM artists'
489
489
  sqls[1..-1].should(include('SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))'))
@@ -491,45 +491,45 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
491
491
  a = a.first
492
492
  a.tags.should == [Tag.load(:id=>2)]
493
493
  a.albums.should == [Album.load(:id=>3)]
494
- MODEL_DB.sqls.length.should == 0
494
+ DB.sqls.length.should == 0
495
495
  end
496
496
 
497
497
  it "should allow cascading of eager loading for associations of associated models" do
498
498
  a = @c1.eager(:tags=>:tracks).all
499
499
  a.should == [@c1.load(:id=>1)]
500
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
500
+ DB.sqls.should == ['SELECT * FROM artists',
501
501
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))',
502
502
  'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON ((albums_tags.album_id = albums.id) AND (albums_tags.tag_id IN (2)))']
503
503
  a = a.first
504
504
  a.tags.should == [Tag.load(:id=>2)]
505
505
  a.tags.first.tracks.should == [Track.load(:id=>4)]
506
- MODEL_DB.sqls.length.should == 0
506
+ DB.sqls.length.should == 0
507
507
  end
508
508
 
509
509
  it "should cascade eagerly loading when the :eager association option is used" do
510
510
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:tracks
511
511
  a = @c1.eager(:tags).all
512
512
  a.should == [@c1.load(:id=>1)]
513
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
513
+ DB.sqls.should == ['SELECT * FROM artists',
514
514
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))',
515
515
  'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON ((albums_tags.album_id = albums.id) AND (albums_tags.tag_id IN (2)))']
516
516
  a = a.first
517
517
  a.tags.should == [Tag.load(:id=>2)]
518
518
  a.tags.first.tracks.should == [Track.load(:id=>4)]
519
- MODEL_DB.sqls.length.should == 0
519
+ DB.sqls.length.should == 0
520
520
  end
521
521
 
522
522
  it "should respect :eager when lazily loading an association" do
523
523
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager=>:tracks
524
524
  a = @c1.load(:id=>1)
525
525
  a.tags.should == [Tag.load(:id=>2)]
526
- MODEL_DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1))',
526
+ DB.sqls.should == ['SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1))',
527
527
  'SELECT tracks.*, albums_tags.tag_id AS x_foreign_key_x FROM tracks INNER JOIN albums ON (albums.id = tracks.album_id) INNER JOIN albums_tags ON ((albums_tags.album_id = albums.id) AND (albums_tags.tag_id IN (2)))']
528
528
  a.tags.first.tracks.should == [Track.load(:id=>4)]
529
- MODEL_DB.sqls.length.should == 0
529
+ DB.sqls.length.should == 0
530
530
  end
531
531
 
532
- it "should cascade eagerly loading when the :eager_graph association option is used" do
532
+ it "should raise error if attempting to eagerly load an association using :eager_graph option" do
533
533
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:tracks
534
534
  proc{@c1.eager(:tags).all}.should raise_error(Sequel::Error)
535
535
  end
@@ -544,50 +544,50 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
544
544
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_graph=>:tracks
545
545
  a = @c1.load(:id=>1)
546
546
  a.tags
547
- MODEL_DB.sqls.should == [ 'SELECT tags.id, tracks.id AS tracks_id FROM (SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1))) AS tags LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums ON (albums.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)']
547
+ DB.sqls.should == [ 'SELECT tags.id, tracks.id AS tracks_id FROM (SELECT tags.* FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id = 1))) AS tags LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums ON (albums.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums.id)']
548
548
  a.tags.should == [Tag.load(:id=>2)]
549
549
  a.tags.first.tracks.should == [Track.load(:id=>4)]
550
- MODEL_DB.sqls.length.should == 0
550
+ DB.sqls.length.should == 0
551
551
  end
552
552
 
553
553
  it "should respect :conditions when eagerly loading" do
554
554
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :conditions=>{:a=>32}
555
555
  a = @c1.eager(:tags).all
556
556
  a.should == [@c1.load(:id=>1)]
557
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
557
+ DB.sqls.should == ['SELECT * FROM artists',
558
558
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) WHERE (a = 32)']
559
559
  a.first.tags.should == [Tag.load(:id=>2)]
560
- MODEL_DB.sqls.length.should == 0
560
+ DB.sqls.length.should == 0
561
561
  end
562
562
 
563
563
  it "should respect :order when eagerly loading" do
564
564
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :order=>:blah
565
565
  a = @c1.eager(:tags).all
566
566
  a.should == [@c1.load(:id=>1)]
567
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
567
+ DB.sqls.should == ['SELECT * FROM artists',
568
568
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) ORDER BY blah']
569
569
  a.first.tags.should == [Tag.load(:id=>2)]
570
- MODEL_DB.sqls.length.should == 0
570
+ DB.sqls.length.should == 0
571
571
  end
572
572
 
573
573
  it "should use the association's block when eager loading by default" do
574
574
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]] do |ds| ds.filter(:a) end
575
575
  a = @c1.eager(:tags).all
576
576
  a.should == [@c1.load(:id=>1)]
577
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
577
+ DB.sqls.should == ['SELECT * FROM artists',
578
578
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) WHERE a']
579
579
  a.first.tags.should == [Tag.load(:id=>2)]
580
- MODEL_DB.sqls.length.should == 0
580
+ DB.sqls.length.should == 0
581
581
  end
582
582
 
583
583
  it "should use the :eager_block option when eager loading if given" do
584
584
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :eager_block=>proc{|ds| ds.filter(:b)} do |ds| ds.filter(:a) end
585
585
  a = @c1.eager(:tags).all
586
586
  a.should == [@c1.load(:id=>1)]
587
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
587
+ DB.sqls.should == ['SELECT * FROM artists',
588
588
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) WHERE b']
589
589
  a.first.tags.should == [Tag.load(:id=>2)]
590
- MODEL_DB.sqls.length.should == 0
590
+ DB.sqls.length.should == 0
591
591
  end
592
592
 
593
593
  it "should respect the :limit option on a many_through_many association" do
@@ -595,80 +595,77 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
595
595
  Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}, {:x_foreign_key_x=>1, :id=>7}]
596
596
  a = @c1.eager(:first_two_tags).all
597
597
  a.should == [@c1.load(:id=>1)]
598
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
598
+ DB.sqls.should == ['SELECT * FROM artists',
599
599
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
600
600
  a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
601
- MODEL_DB.sqls.length.should == 0
601
+ DB.sqls.length.should == 0
602
602
 
603
- @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1]
603
+ @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[1,1]
604
604
  a = @c1.eager(:first_two_tags).all
605
605
  a.should == [@c1.load(:id=>1)]
606
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
606
+ DB.sqls.should == ['SELECT * FROM artists',
607
+ 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
608
+ a.first.first_two_tags.should == [Tag.load(:id=>6)]
609
+ DB.sqls.length.should == 0
610
+
611
+ @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1]
612
+ a = @c1.eager(:first_two_tags).all
613
+ a.should == [@c1.load(:id=>1)]
614
+ DB.sqls.should == ['SELECT * FROM artists',
607
615
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
608
616
  a.first.first_two_tags.should == [Tag.load(:id=>6), Tag.load(:id=>7)]
609
- MODEL_DB.sqls.length.should == 0
617
+ DB.sqls.length.should == 0
610
618
  end
611
619
 
612
620
  it "should respect the :limit option on a many_through_many association using a :window_function strategy" do
613
621
  Tag.dataset.meta_def(:supports_window_functions?){true}
614
- @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :eager_limit_strategy=>true, :order=>:name
622
+ @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :order=>:name
615
623
  Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}]
616
624
  a = @c1.eager(:first_two_tags).all
617
625
  a.should == [@c1.load(:id=>1)]
618
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
626
+ DB.sqls.should == ['SELECT * FROM artists',
619
627
  'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))) AS t1 WHERE (x_sequel_row_number_x <= 2)']
620
628
  a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
621
- MODEL_DB.sqls.length.should == 0
629
+ DB.sqls.length.should == 0
622
630
 
623
- @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :eager_limit_strategy=>true, :order=>:name
631
+ @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :order=>:name
624
632
  a = @c1.eager(:first_two_tags).all
625
633
  a.should == [@c1.load(:id=>1)]
626
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
634
+ DB.sqls.should == ['SELECT * FROM artists',
627
635
  'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))) AS t1 WHERE ((x_sequel_row_number_x >= 2) AND (x_sequel_row_number_x < 4))']
628
636
  a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
629
- MODEL_DB.sqls.length.should == 0
637
+ DB.sqls.length.should == 0
638
+
639
+ @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[nil,1], :order=>:name
640
+ a = @c1.eager(:first_two_tags).all
641
+ a.should == [@c1.load(:id=>1)]
642
+ DB.sqls.should == ['SELECT * FROM artists',
643
+ 'SELECT * FROM (SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x, row_number() OVER (PARTITION BY albums_artists.artist_id ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))) AS t1 WHERE (x_sequel_row_number_x >= 2)']
644
+ a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
645
+ DB.sqls.length.should == 0
630
646
  end
631
647
 
632
648
  it "should respect the :limit option on a many_through_many association with composite primary keys on the main table using a :window_function strategy" do
633
649
  Tag.dataset.meta_def(:supports_window_functions?){true}
634
650
  @c1.set_primary_key([:id1, :id2])
635
651
  @c1.columns :id1, :id2
636
- @c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :eager_limit_strategy=>true, :order=>:name
652
+ @c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :order=>:name
637
653
  @c1.dataset._fetch = [{:id1=>1, :id2=>2}]
638
654
  Tag.dataset._fetch = [{:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>5}, {:x_foreign_key_0_x=>1, :x_foreign_key_1_x=>2, :id=>6}]
639
655
  a = @c1.eager(:first_two_tags).all
640
656
  a.should == [@c1.load(:id1=>1, :id2=>2)]
641
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
657
+ DB.sqls.should == ['SELECT * FROM artists',
642
658
  'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x, row_number() OVER (PARTITION BY albums_artists.artist_id1, albums_artists.artist_id2 ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND ((albums_artists.artist_id1, albums_artists.artist_id2) IN ((1, 2))))) AS t1 WHERE (x_sequel_row_number_x <= 2)']
643
659
  a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
644
- MODEL_DB.sqls.length.should == 0
660
+ DB.sqls.length.should == 0
645
661
 
646
- @c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :eager_limit_strategy=>true, :order=>:name
662
+ @c1.many_through_many :first_two_tags, [[:albums_artists, [:artist_id1, :artist_id2], :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :order=>:name
647
663
  a = @c1.eager(:first_two_tags).all
648
664
  a.should == [@c1.load(:id1=>1, :id2=>2)]
649
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
665
+ DB.sqls.should == ['SELECT * FROM artists',
650
666
  'SELECT * FROM (SELECT tags.*, albums_artists.artist_id1 AS x_foreign_key_0_x, albums_artists.artist_id2 AS x_foreign_key_1_x, row_number() OVER (PARTITION BY albums_artists.artist_id1, albums_artists.artist_id2 ORDER BY name) AS x_sequel_row_number_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND ((albums_artists.artist_id1, albums_artists.artist_id2) IN ((1, 2))))) AS t1 WHERE ((x_sequel_row_number_x >= 2) AND (x_sequel_row_number_x < 4))']
651
667
  a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
652
- MODEL_DB.sqls.length.should == 0
653
- end
654
-
655
- qspecify "should respect the :limit option on a many_through_many association using a :correlated_subquery strategy" do
656
- @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>2, :eager_limit_strategy=>:correlated_subquery, :order=>:name
657
- Tag.dataset._fetch = [{:x_foreign_key_x=>1, :id=>5},{:x_foreign_key_x=>1, :id=>6}]
658
- a = @c1.eager(:first_two_tags).all
659
- a.should == [@c1.load(:id=>1)]
660
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
661
- 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) WHERE (tags.id IN (SELECT t1.id FROM tags AS t1 INNER JOIN albums_tags ON (albums_tags.tag_id = t1.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists AS t2 ON ((t2.album_id = albums.id) AND (t2.artist_id = albums_artists.artist_id)) ORDER BY name LIMIT 2)) ORDER BY name']
662
- a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
663
- MODEL_DB.sqls.length.should == 0
664
-
665
- @c1.many_through_many :first_two_tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :class=>Tag, :limit=>[2,1], :eager_limit_strategy=>:correlated_subquery, :order=>:name
666
- a = @c1.eager(:first_two_tags).all
667
- a.should == [@c1.load(:id=>1)]
668
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
669
- 'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1))) WHERE (tags.id IN (SELECT t1.id FROM tags AS t1 INNER JOIN albums_tags ON (albums_tags.tag_id = t1.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists AS t2 ON ((t2.album_id = albums.id) AND (t2.artist_id = albums_artists.artist_id)) ORDER BY name LIMIT 2 OFFSET 1)) ORDER BY name']
670
- a.first.first_two_tags.should == [Tag.load(:id=>5), Tag.load(:id=>6)]
671
- MODEL_DB.sqls.length.should == 0
668
+ DB.sqls.length.should == 0
672
669
  end
673
670
 
674
671
  it "should raise an error when attempting to eagerly load an association with the :allow_eager option set to false" do
@@ -681,10 +678,10 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
681
678
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :select=>:tags__name
682
679
  a = @c1.eager(:tags).all
683
680
  a.should == [@c1.load(:id=>1)]
684
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
681
+ DB.sqls.should == ['SELECT * FROM artists',
685
682
  'SELECT tags.name, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
686
683
  a.first.tags.should == [Tag.load(:id=>2)]
687
- MODEL_DB.sqls.length.should == 0
684
+ DB.sqls.length.should == 0
688
685
  end
689
686
 
690
687
  it "should respect many_through_many association's :left_primary_key and :right_primary_key options" do
@@ -694,10 +691,10 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
694
691
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :left_primary_key=>:yyy, :right_primary_key=>:tag_id
695
692
  a = @c1.eager(:tags).all
696
693
  a.should == [@c1.load(:id=>1, :yyy=>8)]
697
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
694
+ DB.sqls.should == ['SELECT * FROM artists',
698
695
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.tag_id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (8)))']
699
696
  a.first.tags.should == [Tag.load(:tag_id=>2)]
700
- MODEL_DB.sqls.length.should == 0
697
+ DB.sqls.length.should == 0
701
698
  end
702
699
 
703
700
  it "should handle composite keys" do
@@ -707,20 +704,20 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
707
704
  @c1.many_through_many :tags, [[:albums_artists, [:b1, :b2], [:c1, :c2]], [:albums, [:d1, :d2], [:e1, :e2]], [:albums_tags, [:f1, :f2], [:g1, :g2]]], :right_primary_key=>[:h1, :h2], :left_primary_key=>[:id, :yyy]
708
705
  a = @c1.eager(:tags).all
709
706
  a.should == [@c1.load(:id=>1, :yyy=>8)]
710
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
707
+ DB.sqls.should == ['SELECT * FROM artists',
711
708
  'SELECT tags.*, albums_artists.b1 AS x_foreign_key_0_x, albums_artists.b2 AS x_foreign_key_1_x FROM tags INNER JOIN albums_tags ON ((albums_tags.g1 = tags.h1) AND (albums_tags.g2 = tags.h2)) INNER JOIN albums ON ((albums.e1 = albums_tags.f1) AND (albums.e2 = albums_tags.f2)) INNER JOIN albums_artists ON ((albums_artists.c1 = albums.d1) AND (albums_artists.c2 = albums.d2) AND ((albums_artists.b1, albums_artists.b2) IN ((1, 8))))']
712
709
  a.first.tags.should == [Tag.load(:id=>2)]
713
- MODEL_DB.sqls.length.should == 0
710
+ DB.sqls.length.should == 0
714
711
  end
715
712
 
716
713
  it "should respect :after_load callbacks on associations when eager loading" do
717
714
  @c1.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]], :after_load=>lambda{|o, as| o[:id] *= 2; as.each{|a| a[:id] *= 3}}
718
715
  a = @c1.eager(:tags).all
719
716
  a.should == [@c1.load(:id=>2)]
720
- MODEL_DB.sqls.should == ['SELECT * FROM artists',
717
+ DB.sqls.should == ['SELECT * FROM artists',
721
718
  'SELECT tags.*, albums_artists.artist_id AS x_foreign_key_x FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) INNER JOIN albums ON (albums.id = albums_tags.album_id) INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
722
719
  a.first.tags.should == [Tag.load(:id=>6)]
723
- MODEL_DB.sqls.length.should == 0
720
+ DB.sqls.length.should == 0
724
721
  end
725
722
 
726
723
  it "should raise an error if called without a symbol or hash" do
@@ -730,39 +727,39 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
730
727
  it "should eagerly graph a single many_through_many association" do
731
728
  a = @c1.eager_graph(:tags).all
732
729
  a.should == [@c1.load(:id=>1)]
733
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
730
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
734
731
  a.first.tags.should == [Tag.load(:id=>2)]
735
- MODEL_DB.sqls.length.should == 0
732
+ DB.sqls.length.should == 0
736
733
  end
737
734
 
738
735
  it "should eagerly graph multiple associations in a single call" do
739
736
  a = @c1.eager_graph(:tags, :albums).all
740
737
  a.should == [@c1.load(:id=>1)]
741
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
738
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
742
739
  a = a.first
743
740
  a.tags.should == [Tag.load(:id=>2)]
744
741
  a.albums.should == [Album.load(:id=>3)]
745
- MODEL_DB.sqls.length.should == 0
742
+ DB.sqls.length.should == 0
746
743
  end
747
744
 
748
745
  it "should eagerly graph multiple associations in separate calls" do
749
746
  a = @c1.eager_graph(:tags).eager_graph(:albums).all
750
747
  a.should == [@c1.load(:id=>1)]
751
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
748
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
752
749
  a = a.first
753
750
  a.tags.should == [Tag.load(:id=>2)]
754
751
  a.albums.should == [Album.load(:id=>3)]
755
- MODEL_DB.sqls.length.should == 0
752
+ DB.sqls.length.should == 0
756
753
  end
757
754
 
758
755
  it "should allow cascading of eager graphing for associations of associated models" do
759
756
  a = @c1.eager_graph(:tags=>:tracks).all
760
757
  a.should == [@c1.load(:id=>1)]
761
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
758
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
762
759
  a = a.first
763
760
  a.tags.should == [Tag.load(:id=>2)]
764
761
  a.tags.first.tracks.should == [Track.load(:id=>4)]
765
- MODEL_DB.sqls.length.should == 0
762
+ DB.sqls.length.should == 0
766
763
  end
767
764
 
768
765
  it "eager graphing should eliminate duplicates caused by cartesian products" do
@@ -771,29 +768,29 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
771
768
  ds._fetch = [{:id=>1, :tags_id=>2}, {:id=>1, :tags_id=>3}, {:id=>1, :tags_id=>2}, {:id=>1, :tags_id=>3}]
772
769
  a = ds.all
773
770
  a.should == [@c1.load(:id=>1)]
774
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
771
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
775
772
  a.first.tags.should == [Tag.load(:id=>2), Tag.load(:id=>3)]
776
- MODEL_DB.sqls.length.should == 0
773
+ DB.sqls.length.should == 0
777
774
  end
778
775
 
779
776
  it "should eager graph multiple associations from the same table" do
780
777
  a = @c1.eager_graph(:tags, :other_tags).all
781
778
  a.should == [@c1.load(:id=>1)]
782
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, other_tags.id AS other_tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.album_id = albums_0.id) LEFT OUTER JOIN tags AS other_tags ON (other_tags.id = albums_tags_0.tag_id)']
779
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, other_tags.id AS other_tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.album_id = albums_0.id) LEFT OUTER JOIN tags AS other_tags ON (other_tags.id = albums_tags_0.tag_id)']
783
780
  a = a.first
784
781
  a.tags.should == [Tag.load(:id=>2)]
785
782
  a.other_tags.should == [Tag.load(:id=>9)]
786
- MODEL_DB.sqls.length.should == 0
783
+ DB.sqls.length.should == 0
787
784
  end
788
785
 
789
786
  it "should eager graph a self_referential association" do
790
787
  a = @c1.eager_graph(:tags, :artists).all
791
788
  a.should == [@c1.load(:id=>1)]
792
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, artists_0.id AS artists_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.id = albums_artists_1.artist_id)']
789
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, artists_0.id AS artists_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id) LEFT OUTER JOIN albums_artists AS albums_artists_1 ON (albums_artists_1.album_id = albums_0.id) LEFT OUTER JOIN artists AS artists_0 ON (artists_0.id = albums_artists_1.artist_id)']
793
790
  a = a.first
794
791
  a.tags.should == [Tag.load(:id=>2)]
795
792
  a.artists.should == [@c1.load(:id=>10)]
796
- MODEL_DB.sqls.length.should == 0
793
+ DB.sqls.length.should == 0
797
794
  end
798
795
 
799
796
  it "eager graphing should give you a plain hash when called without .all" do
@@ -803,12 +800,12 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
803
800
  it "should be able to use eager and eager_graph together" do
804
801
  a = @c1.eager_graph(:tags).eager(:albums).all
805
802
  a.should == [@c1.load(:id=>1)]
806
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)',
803
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)',
807
804
  'SELECT albums.*, albums_artists.artist_id AS x_foreign_key_x FROM albums INNER JOIN albums_artists ON ((albums_artists.album_id = albums.id) AND (albums_artists.artist_id IN (1)))']
808
805
  a = a.first
809
806
  a.tags.should == [Tag.load(:id=>2)]
810
807
  a.albums.should == [Album.load(:id=>3)]
811
- MODEL_DB.sqls.length.should == 0
808
+ DB.sqls.length.should == 0
812
809
  end
813
810
 
814
811
  it "should handle no associated records when eagerly graphing a single many_through_many association" do
@@ -816,9 +813,9 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
816
813
  ds._fetch = {:id=>1, :tags_id=>nil}
817
814
  a = ds.all
818
815
  a.should == [@c1.load(:id=>1)]
819
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
816
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
820
817
  a.first.tags.should == []
821
- MODEL_DB.sqls.length.should == 0
818
+ DB.sqls.length.should == 0
822
819
  end
823
820
 
824
821
  it "should handle no associated records when eagerly graphing multiple many_through_many associations" do
@@ -826,12 +823,12 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
826
823
  ds._fetch = [{:id=>1, :tags_id=>nil, :albums_0_id=>3}, {:id=>1, :tags_id=>2, :albums_0_id=>nil}, {:id=>1, :tags_id=>5, :albums_0_id=>6}, {:id=>7, :tags_id=>nil, :albums_0_id=>nil}]
827
824
  a = ds.all
828
825
  a.should == [@c1.load(:id=>1), @c1.load(:id=>7)]
829
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
826
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, albums_0.id AS albums_0_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_artists AS albums_artists_0 ON (albums_artists_0.artist_id = artists.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_artists_0.album_id)']
830
827
  a.first.tags.should == [Tag.load(:id=>2), Tag.load(:id=>5)]
831
828
  a.first.albums.should == [Album.load(:id=>3), Album.load(:id=>6)]
832
829
  a.last.tags.should == []
833
830
  a.last.albums.should == []
834
- MODEL_DB.sqls.length.should == 0
831
+ DB.sqls.length.should == 0
835
832
  end
836
833
 
837
834
  it "should handle missing associated records when cascading eager graphing for associations of associated models" do
@@ -839,13 +836,13 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
839
836
  ds._fetch = [{:id=>1, :tags_id=>2, :tracks_id=>4}, {:id=>1, :tags_id=>3, :tracks_id=>nil}, {:id=>2, :tags_id=>nil, :tracks_id=>nil}]
840
837
  a = ds.all
841
838
  a.should == [@c1.load(:id=>1), @c1.load(:id=>2)]
842
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
839
+ DB.sqls.should == ['SELECT artists.id, tags.id AS tags_id, tracks.id AS tracks_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id) LEFT OUTER JOIN albums_tags AS albums_tags_0 ON (albums_tags_0.tag_id = tags.id) LEFT OUTER JOIN albums AS albums_0 ON (albums_0.id = albums_tags_0.album_id) LEFT OUTER JOIN tracks ON (tracks.album_id = albums_0.id)']
843
840
  a.last.tags.should == []
844
841
  a = a.first
845
842
  a.tags.should == [Tag.load(:id=>2), Tag.load(:id=>3)]
846
843
  a.tags.first.tracks.should == [Track.load(:id=>4)]
847
844
  a.tags.last.tracks.should == []
848
- MODEL_DB.sqls.length.should == 0
845
+ DB.sqls.length.should == 0
849
846
  end
850
847
 
851
848
  it "eager graphing should respect :left_primary_key and :right_primary_key options" do
@@ -856,9 +853,9 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
856
853
  ds._fetch = {:id=>1, :yyy=>8, :tags_id=>2, :tag_id=>4}
857
854
  a = ds.all
858
855
  a.should == [@c1.load(:id=>1, :yyy=>8)]
859
- MODEL_DB.sqls.should == ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.yyy) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.tag_id = albums_tags.tag_id)']
856
+ DB.sqls.should == ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.yyy) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.tag_id = albums_tags.tag_id)']
860
857
  a.first.tags.should == [Tag.load(:id=>2, :tag_id=>4)]
861
- MODEL_DB.sqls.length.should == 0
858
+ DB.sqls.length.should == 0
862
859
  end
863
860
 
864
861
  it "eager graphing should respect composite keys" do
@@ -869,9 +866,9 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
869
866
  ds._fetch = {:id=>1, :yyy=>8, :tags_id=>2, :tag_id=>4}
870
867
  a = ds.all
871
868
  a.should == [@c1.load(:id=>1, :yyy=>8)]
872
- MODEL_DB.sqls.should == ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON ((albums_artists.b1 = artists.id) AND (albums_artists.b2 = artists.yyy)) LEFT OUTER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) LEFT OUTER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.g1) AND (tags.tag_id = albums_tags.g2))']
869
+ DB.sqls.should == ['SELECT artists.id, artists.yyy, tags.id AS tags_id, tags.tag_id FROM artists LEFT OUTER JOIN albums_artists ON ((albums_artists.b1 = artists.id) AND (albums_artists.b2 = artists.yyy)) LEFT OUTER JOIN albums ON ((albums.d1 = albums_artists.c1) AND (albums.d2 = albums_artists.c2)) LEFT OUTER JOIN albums_tags ON ((albums_tags.f1 = albums.e1) AND (albums_tags.f2 = albums.e2)) LEFT OUTER JOIN tags ON ((tags.id = albums_tags.g1) AND (tags.tag_id = albums_tags.g2))']
873
870
  a.first.tags.should == [Tag.load(:id=>2, :tag_id=>4)]
874
- MODEL_DB.sqls.length.should == 0
871
+ DB.sqls.length.should == 0
875
872
  end
876
873
 
877
874
  it "should respect the association's :graph_select option" do
@@ -880,9 +877,9 @@ describe "Sequel::Plugins::ManyThroughMany eager loading methods" do
880
877
  ds._fetch = {:id=>1, :b=>2}
881
878
  a = ds.all
882
879
  a.should == [@c1.load(:id=>1)]
883
- MODEL_DB.sqls.should == ['SELECT artists.id, tags.b FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
880
+ DB.sqls.should == ['SELECT artists.id, tags.b FROM artists LEFT OUTER JOIN albums_artists ON (albums_artists.artist_id = artists.id) LEFT OUTER JOIN albums ON (albums.id = albums_artists.album_id) LEFT OUTER JOIN albums_tags ON (albums_tags.album_id = albums.id) LEFT OUTER JOIN tags ON (tags.id = albums_tags.tag_id)']
884
881
  a.first.tags.should == [Tag.load(:b=>2)]
885
- MODEL_DB.sqls.length.should == 0
882
+ DB.sqls.length.should == 0
886
883
  end
887
884
 
888
885
  it "should respect the association's :graph_join_type option" do