sequel 2.11.0 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/CHANGELOG +168 -0
  2. data/README.rdoc +77 -95
  3. data/Rakefile +100 -80
  4. data/bin/sequel +2 -1
  5. data/doc/advanced_associations.rdoc +23 -32
  6. data/doc/cheat_sheet.rdoc +23 -40
  7. data/doc/dataset_filtering.rdoc +6 -6
  8. data/doc/prepared_statements.rdoc +22 -22
  9. data/doc/release_notes/2.12.0.txt +534 -0
  10. data/doc/schema.rdoc +3 -1
  11. data/doc/sharding.rdoc +8 -8
  12. data/doc/virtual_rows.rdoc +65 -0
  13. data/lib/sequel.rb +1 -1
  14. data/lib/{sequel_core → sequel}/adapters/ado.rb +3 -3
  15. data/lib/{sequel_core → sequel}/adapters/db2.rb +0 -0
  16. data/lib/{sequel_core → sequel}/adapters/dbi.rb +1 -1
  17. data/lib/{sequel_core → sequel}/adapters/do.rb +9 -5
  18. data/lib/{sequel_core → sequel}/adapters/do/mysql.rb +1 -1
  19. data/lib/{sequel_core → sequel}/adapters/do/postgres.rb +1 -1
  20. data/lib/{sequel_core → sequel}/adapters/do/sqlite.rb +1 -1
  21. data/lib/{sequel_core → sequel}/adapters/firebird.rb +84 -80
  22. data/lib/{sequel_core → sequel}/adapters/informix.rb +1 -1
  23. data/lib/{sequel_core → sequel}/adapters/jdbc.rb +21 -14
  24. data/lib/{sequel_core → sequel}/adapters/jdbc/h2.rb +14 -13
  25. data/lib/{sequel_core → sequel}/adapters/jdbc/mysql.rb +1 -1
  26. data/lib/{sequel_core → sequel}/adapters/jdbc/oracle.rb +1 -1
  27. data/lib/{sequel_core → sequel}/adapters/jdbc/postgresql.rb +1 -1
  28. data/lib/{sequel_core → sequel}/adapters/jdbc/sqlite.rb +1 -1
  29. data/lib/{sequel_core → sequel}/adapters/mysql.rb +60 -39
  30. data/lib/{sequel_core → sequel}/adapters/odbc.rb +8 -4
  31. data/lib/{sequel_core → sequel}/adapters/openbase.rb +0 -0
  32. data/lib/{sequel_core → sequel}/adapters/oracle.rb +38 -7
  33. data/lib/{sequel_core → sequel}/adapters/postgres.rb +24 -24
  34. data/lib/{sequel_core → sequel}/adapters/shared/mssql.rb +5 -5
  35. data/lib/{sequel_core → sequel}/adapters/shared/mysql.rb +126 -71
  36. data/lib/{sequel_core → sequel}/adapters/shared/oracle.rb +7 -10
  37. data/lib/{sequel_core → sequel}/adapters/shared/postgres.rb +159 -125
  38. data/lib/{sequel_core → sequel}/adapters/shared/progress.rb +1 -2
  39. data/lib/{sequel_core → sequel}/adapters/shared/sqlite.rb +72 -67
  40. data/lib/{sequel_core → sequel}/adapters/sqlite.rb +11 -7
  41. data/lib/{sequel_core → sequel}/adapters/utils/date_format.rb +0 -0
  42. data/lib/{sequel_core → sequel}/adapters/utils/stored_procedures.rb +0 -0
  43. data/lib/{sequel_core → sequel}/adapters/utils/unsupported.rb +19 -0
  44. data/lib/{sequel_core → sequel}/connection_pool.rb +7 -5
  45. data/lib/sequel/core.rb +221 -0
  46. data/lib/{sequel_core → sequel}/core_sql.rb +91 -49
  47. data/lib/{sequel_core → sequel}/database.rb +264 -149
  48. data/lib/{sequel_core/schema/generator.rb → sequel/database/schema_generator.rb} +6 -2
  49. data/lib/{sequel_core/database/schema.rb → sequel/database/schema_methods.rb} +12 -12
  50. data/lib/sequel/database/schema_sql.rb +224 -0
  51. data/lib/{sequel_core → sequel}/dataset.rb +78 -236
  52. data/lib/{sequel_core → sequel}/dataset/convenience.rb +99 -61
  53. data/lib/{sequel_core/object_graph.rb → sequel/dataset/graph.rb} +16 -14
  54. data/lib/{sequel_core → sequel}/dataset/prepared_statements.rb +1 -1
  55. data/lib/{sequel_core → sequel}/dataset/sql.rb +150 -99
  56. data/lib/sequel/deprecated.rb +593 -0
  57. data/lib/sequel/deprecated_migration.rb +91 -0
  58. data/lib/sequel/exceptions.rb +48 -0
  59. data/lib/sequel/extensions/blank.rb +42 -0
  60. data/lib/{sequel_model → sequel/extensions}/inflector.rb +8 -1
  61. data/lib/{sequel_core → sequel/extensions}/migration.rb +1 -1
  62. data/lib/{sequel_core/dataset → sequel/extensions}/pagination.rb +0 -0
  63. data/lib/{sequel_core → sequel/extensions}/pretty_table.rb +7 -0
  64. data/lib/{sequel_core/dataset → sequel/extensions}/query.rb +7 -0
  65. data/lib/sequel/extensions/string_date_time.rb +47 -0
  66. data/lib/sequel/metaprogramming.rb +43 -0
  67. data/lib/sequel/model.rb +110 -0
  68. data/lib/sequel/model/associations.rb +1300 -0
  69. data/lib/sequel/model/base.rb +937 -0
  70. data/lib/sequel/model/deprecated.rb +204 -0
  71. data/lib/sequel/model/deprecated_hooks.rb +103 -0
  72. data/lib/sequel/model/deprecated_inflector.rb +335 -0
  73. data/lib/sequel/model/deprecated_validations.rb +388 -0
  74. data/lib/sequel/model/errors.rb +39 -0
  75. data/lib/{sequel_model → sequel/model}/exceptions.rb +4 -4
  76. data/lib/sequel/model/inflections.rb +208 -0
  77. data/lib/sequel/model/plugins.rb +76 -0
  78. data/lib/sequel/plugins/caching.rb +122 -0
  79. data/lib/sequel/plugins/hook_class_methods.rb +122 -0
  80. data/lib/sequel/plugins/schema.rb +53 -0
  81. data/lib/sequel/plugins/serialization.rb +117 -0
  82. data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
  83. data/lib/sequel/plugins/validation_class_methods.rb +384 -0
  84. data/lib/sequel/plugins/validation_helpers.rb +150 -0
  85. data/lib/{sequel_core → sequel}/sql.rb +125 -190
  86. data/lib/{sequel_core → sequel}/version.rb +2 -1
  87. data/lib/sequel_core.rb +1 -172
  88. data/lib/sequel_model.rb +1 -91
  89. data/spec/adapters/firebird_spec.rb +5 -5
  90. data/spec/adapters/informix_spec.rb +1 -1
  91. data/spec/adapters/mysql_spec.rb +128 -42
  92. data/spec/adapters/oracle_spec.rb +47 -19
  93. data/spec/adapters/postgres_spec.rb +64 -52
  94. data/spec/adapters/spec_helper.rb +1 -1
  95. data/spec/adapters/sqlite_spec.rb +12 -17
  96. data/spec/{sequel_core → core}/connection_pool_spec.rb +10 -10
  97. data/spec/{sequel_core → core}/core_ext_spec.rb +19 -19
  98. data/spec/{sequel_core → core}/core_sql_spec.rb +68 -71
  99. data/spec/{sequel_core → core}/database_spec.rb +135 -99
  100. data/spec/{sequel_core → core}/dataset_spec.rb +398 -242
  101. data/spec/{sequel_core → core}/expression_filters_spec.rb +13 -13
  102. data/spec/core/migration_spec.rb +263 -0
  103. data/spec/{sequel_core → core}/object_graph_spec.rb +10 -10
  104. data/spec/{sequel_core → core}/pretty_table_spec.rb +2 -2
  105. data/spec/{sequel_core → core}/schema_generator_spec.rb +0 -0
  106. data/spec/{sequel_core → core}/schema_spec.rb +8 -10
  107. data/spec/{sequel_core → core}/spec_helper.rb +29 -2
  108. data/spec/{sequel_core → core}/version_spec.rb +0 -0
  109. data/spec/extensions/blank_spec.rb +67 -0
  110. data/spec/extensions/caching_spec.rb +201 -0
  111. data/spec/{sequel_model/hooks_spec.rb → extensions/hook_class_methods_spec.rb} +8 -23
  112. data/spec/{sequel_model → extensions}/inflector_spec.rb +3 -0
  113. data/spec/{sequel_core → extensions}/migration_spec.rb +4 -4
  114. data/spec/extensions/pagination_spec.rb +99 -0
  115. data/spec/extensions/pretty_table_spec.rb +91 -0
  116. data/spec/extensions/query_spec.rb +85 -0
  117. data/spec/{sequel_model → extensions}/schema_spec.rb +22 -1
  118. data/spec/extensions/serialization_spec.rb +109 -0
  119. data/spec/extensions/single_table_inheritance_spec.rb +53 -0
  120. data/spec/{sequel_model → extensions}/spec_helper.rb +13 -4
  121. data/spec/extensions/string_date_time_spec.rb +93 -0
  122. data/spec/{sequel_model/validations_spec.rb → extensions/validation_class_methods_spec.rb} +15 -103
  123. data/spec/extensions/validation_helpers_spec.rb +291 -0
  124. data/spec/integration/dataset_test.rb +31 -0
  125. data/spec/integration/eager_loader_test.rb +17 -30
  126. data/spec/integration/schema_test.rb +8 -5
  127. data/spec/integration/spec_helper.rb +17 -0
  128. data/spec/integration/transaction_test.rb +68 -0
  129. data/spec/{sequel_model → model}/association_reflection_spec.rb +0 -0
  130. data/spec/{sequel_model → model}/associations_spec.rb +23 -10
  131. data/spec/{sequel_model → model}/base_spec.rb +29 -20
  132. data/spec/{sequel_model → model}/caching_spec.rb +16 -14
  133. data/spec/{sequel_model → model}/dataset_methods_spec.rb +0 -0
  134. data/spec/{sequel_model → model}/eager_loading_spec.rb +8 -8
  135. data/spec/model/hooks_spec.rb +472 -0
  136. data/spec/model/inflector_spec.rb +126 -0
  137. data/spec/{sequel_model → model}/model_spec.rb +25 -20
  138. data/spec/model/plugins_spec.rb +142 -0
  139. data/spec/{sequel_model → model}/record_spec.rb +121 -62
  140. data/spec/model/schema_spec.rb +92 -0
  141. data/spec/model/spec_helper.rb +124 -0
  142. data/spec/model/validations_spec.rb +1080 -0
  143. metadata +136 -107
  144. data/lib/sequel_core/core_ext.rb +0 -217
  145. data/lib/sequel_core/dataset/callback.rb +0 -13
  146. data/lib/sequel_core/dataset/schema.rb +0 -15
  147. data/lib/sequel_core/deprecated.rb +0 -26
  148. data/lib/sequel_core/exceptions.rb +0 -44
  149. data/lib/sequel_core/schema.rb +0 -2
  150. data/lib/sequel_core/schema/sql.rb +0 -325
  151. data/lib/sequel_model/association_reflection.rb +0 -267
  152. data/lib/sequel_model/associations.rb +0 -499
  153. data/lib/sequel_model/base.rb +0 -539
  154. data/lib/sequel_model/caching.rb +0 -82
  155. data/lib/sequel_model/dataset_methods.rb +0 -26
  156. data/lib/sequel_model/eager_loading.rb +0 -370
  157. data/lib/sequel_model/hooks.rb +0 -101
  158. data/lib/sequel_model/plugins.rb +0 -62
  159. data/lib/sequel_model/record.rb +0 -568
  160. data/lib/sequel_model/schema.rb +0 -49
  161. data/lib/sequel_model/validations.rb +0 -429
  162. data/spec/sequel_model/plugins_spec.rb +0 -80
@@ -13,11 +13,13 @@ describe Sequel::Model, "caching" do
13
13
  @cache = cache
14
14
 
15
15
  @c = Class.new(Sequel::Model(:items))
16
- @c.class_eval do
17
- set_cache cache
18
- def self.name; 'Item' end
16
+ deprec do
17
+ @c.class_eval do
18
+ set_cache cache
19
+ def self.name; 'Item' end
19
20
 
20
- columns :name, :id
21
+ columns :name, :id
22
+ end
21
23
  end
22
24
 
23
25
  $cache_dataset_row = {:name => 'sharon', :id => 1}
@@ -145,31 +147,31 @@ describe Sequel::Model, "caching" do
145
147
  it "should delete the cache when writing to the database" do
146
148
  m = @c[1]
147
149
  @cache[m.cache_key].should == m
148
-
149
- m.update_values(:name => 'tutu')
150
+ m.name = 'hey'
151
+ m.save
150
152
  @cache.has_key?(m.cache_key).should be_false
151
- $sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
152
-
153
- m = @c[1]
153
+ $sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
154
+
155
+ m = @c2[1]
154
156
  @cache[m.cache_key].should == m
155
157
  m.name = 'hey'
156
158
  m.save
157
159
  @cache.has_key?(m.cache_key).should be_false
158
160
  $sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
161
+ end
159
162
 
160
- m = @c2[1]
163
+ deprec_specify "should delete the cache when using update_values" do
164
+ m = @c[1]
161
165
  @cache[m.cache_key].should == m
162
-
163
166
  m.update_values(:name => 'tutu')
164
167
  @cache.has_key?(m.cache_key).should be_false
165
168
  $sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
166
169
 
167
170
  m = @c2[1]
168
171
  @cache[m.cache_key].should == m
169
- m.name = 'hey'
170
- m.save
172
+ m.update_values(:name => 'tutu')
171
173
  @cache.has_key?(m.cache_key).should be_false
172
- $sqls.last.should == "UPDATE items SET name = 'hey', id = 1 WHERE (id = 1)"
174
+ $sqls.last.should == "UPDATE items SET name = 'tutu' WHERE (id = 1)"
173
175
  end
174
176
 
175
177
  it "should delete the cache when deleting the record" do
@@ -1042,7 +1042,7 @@ describe Sequel::Model, "#eager_graph" do
1042
1042
 
1043
1043
  it "should respect the association's :conditions option" do
1044
1044
  GraphAlbum.many_to_one :active_band, :class=>'GraphBand', :key=>:band_id, :conditions=>{:active=>true}
1045
- GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active = 't'))"
1045
+ GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active IS TRUE))"
1046
1046
 
1047
1047
  GraphAlbum.one_to_many :right_tracks, :class=>'GraphTrack', :key=>:album_id, :conditions=>{:id=>(0..100)}
1048
1048
  GraphAlbum.eager_graph(:right_tracks).sql.should == 'SELECT albums.id, albums.band_id, right_tracks.id AS right_tracks_id, right_tracks.album_id FROM albums LEFT OUTER JOIN tracks AS right_tracks ON ((right_tracks.album_id = albums.id) AND ((right_tracks.id >= 0) AND (right_tracks.id <= 100)))'
@@ -1053,7 +1053,7 @@ describe Sequel::Model, "#eager_graph" do
1053
1053
 
1054
1054
  it "should respect the association's :graph_conditions option" do
1055
1055
  GraphAlbum.many_to_one :active_band, :class=>'GraphBand', :key=>:band_id, :graph_conditions=>{:active=>true}
1056
- GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active = 't'))"
1056
+ GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active IS TRUE))"
1057
1057
 
1058
1058
  GraphAlbum.one_to_many :right_tracks, :class=>'GraphTrack', :key=>:album_id, :graph_conditions=>{:id=>(0..100)}
1059
1059
  GraphAlbum.eager_graph(:right_tracks).sql.should == 'SELECT albums.id, albums.band_id, right_tracks.id AS right_tracks_id, right_tracks.album_id FROM albums LEFT OUTER JOIN tracks AS right_tracks ON ((right_tracks.album_id = albums.id) AND ((right_tracks.id >= 0) AND (right_tracks.id <= 100)))'
@@ -1064,7 +1064,7 @@ describe Sequel::Model, "#eager_graph" do
1064
1064
 
1065
1065
  it "should respect the association's :graph_join_table_conditions option" do
1066
1066
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_join_table_conditions=>{:active=>true}
1067
- GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND (ag.active = 't')) LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1067
+ GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND (ag.active IS TRUE)) LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1068
1068
 
1069
1069
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_conditions=>{true=>:active}, :graph_join_table_conditions=>{true=>:active}
1070
1070
  GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND ('t' = albums.active)) LEFT OUTER JOIN genres AS active_genres ON ((active_genres.id = ag.genre_id) AND ('t' = ag.active))"
@@ -1072,7 +1072,7 @@ describe Sequel::Model, "#eager_graph" do
1072
1072
 
1073
1073
  it "should respect the association's :graph_block option" do
1074
1074
  GraphAlbum.many_to_one :active_band, :class=>'GraphBand', :key=>:band_id, :graph_block=>proc{|ja,lja,js| {:active.qualify(ja)=>true}}
1075
- GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active = 't'))"
1075
+ GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON ((active_band.id = albums.band_id) AND (active_band.active IS TRUE))"
1076
1076
 
1077
1077
  GraphAlbum.one_to_many :right_tracks, :class=>'GraphTrack', :key=>:album_id, :graph_block=>proc{|ja,lja,js| {:id.qualify(ja)=>(0..100)}}
1078
1078
  GraphAlbum.eager_graph(:right_tracks).sql.should == 'SELECT albums.id, albums.band_id, right_tracks.id AS right_tracks_id, right_tracks.album_id FROM albums LEFT OUTER JOIN tracks AS right_tracks ON ((right_tracks.album_id = albums.id) AND ((right_tracks.id >= 0) AND (right_tracks.id <= 100)))'
@@ -1083,7 +1083,7 @@ describe Sequel::Model, "#eager_graph" do
1083
1083
 
1084
1084
  it "should respect the association's :graph_join_block option" do
1085
1085
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_join_table_block=>proc{|ja,lja,js| {:active.qualify(ja)=>true}}
1086
- GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND (ag.active = 't')) LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1086
+ GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND (ag.active IS TRUE)) LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1087
1087
 
1088
1088
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_block=>proc{|ja,lja,js| {true=>:active.qualify(lja)}}, :graph_join_table_block=>proc{|ja,lja,js| {true=>:active.qualify(lja)}}
1089
1089
  GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON ((ag.album_id = albums.id) AND ('t' = albums.active)) LEFT OUTER JOIN genres AS active_genres ON ((active_genres.id = ag.genre_id) AND ('t' = ag.active))"
@@ -1091,7 +1091,7 @@ describe Sequel::Model, "#eager_graph" do
1091
1091
 
1092
1092
  it "should respect the association's :eager_grapher option" do
1093
1093
  GraphAlbum.many_to_one :active_band, :class=>'GraphBand', :key=>:band_id, :eager_grapher=>proc{|ds, aa, ta| ds.graph(GraphBand, {:active=>true}, :table_alias=>aa, :join_type=>:inner)}
1094
- GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums INNER JOIN bands AS active_band ON (active_band.active = 't')"
1094
+ GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums INNER JOIN bands AS active_band ON (active_band.active IS TRUE)"
1095
1095
 
1096
1096
  GraphAlbum.one_to_many :right_tracks, :class=>'GraphTrack', :key=>:album_id, :eager_grapher=>proc{|ds, aa, ta| ds.graph(GraphTrack, nil, :join_type=>:natural, :table_alias=>aa)}
1097
1097
  GraphAlbum.eager_graph(:right_tracks).sql.should == 'SELECT albums.id, albums.band_id, right_tracks.id AS right_tracks_id, right_tracks.album_id FROM albums NATURAL JOIN tracks AS right_tracks'
@@ -1102,7 +1102,7 @@ describe Sequel::Model, "#eager_graph" do
1102
1102
 
1103
1103
  it "should respect the association's :graph_only_conditions option" do
1104
1104
  GraphAlbum.many_to_one :active_band, :class=>'GraphBand', :key=>:band_id, :graph_only_conditions=>{:active=>true}
1105
- GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON (active_band.active = 't')"
1105
+ GraphAlbum.eager_graph(:active_band).sql.should == "SELECT albums.id, albums.band_id, active_band.id AS active_band_id, active_band.vocalist_id FROM albums LEFT OUTER JOIN bands AS active_band ON (active_band.active IS TRUE)"
1106
1106
 
1107
1107
  GraphAlbum.one_to_many :right_tracks, :class=>'GraphTrack', :key=>:album_id, :graph_only_conditions=>nil, :graph_join_type=>:natural
1108
1108
  GraphAlbum.eager_graph(:right_tracks).sql.should == 'SELECT albums.id, albums.band_id, right_tracks.id AS right_tracks_id, right_tracks.album_id FROM albums NATURAL JOIN tracks AS right_tracks'
@@ -1113,7 +1113,7 @@ describe Sequel::Model, "#eager_graph" do
1113
1113
 
1114
1114
  it "should respect the association's :graph_join_table_only_conditions option" do
1115
1115
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_join_table_only_conditions=>{:active=>true}
1116
- GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON (ag.active = 't') LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1116
+ GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON (ag.active IS TRUE) LEFT OUTER JOIN genres AS active_genres ON (active_genres.id = ag.genre_id)"
1117
1117
 
1118
1118
  GraphAlbum.many_to_many :active_genres, :class=>'GraphGenre', :left_key=>:album_id, :right_key=>:genre_id, :join_table=>:ag, :graph_only_conditions=>(:price + 2 > 100), :graph_join_table_only_conditions=>"active"
1119
1119
  GraphAlbum.eager_graph(:active_genres).sql.should == "SELECT albums.id, albums.band_id, active_genres.id AS active_genres_id FROM albums LEFT OUTER JOIN ag ON (active) LEFT OUTER JOIN genres AS active_genres ON ((price + 2) > 100)"
@@ -0,0 +1,472 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe "Model hooks" do
4
+ before do
5
+ MODEL_DB.reset
6
+ end
7
+
8
+ deprec_specify "should be definable using a block" do
9
+ $adds = []
10
+ c = Class.new(Sequel::Model)
11
+ c.class_eval do
12
+ before_save {$adds << 'hi'}
13
+ end
14
+
15
+ c.new.before_save
16
+ $adds.should == ['hi']
17
+ end
18
+
19
+ deprec_specify "should be definable using a method name" do
20
+ $adds = []
21
+ c = Class.new(Sequel::Model)
22
+ c.class_eval do
23
+ def bye; $adds << 'bye'; end
24
+ before_save :bye
25
+ end
26
+
27
+ c.new.before_save
28
+ $adds.should == ['bye']
29
+ end
30
+
31
+ deprec_specify "should be additive" do
32
+ $adds = []
33
+ c = Class.new(Sequel::Model)
34
+ c.class_eval do
35
+ after_save {$adds << 'hyiyie'}
36
+ after_save {$adds << 'byiyie'}
37
+ end
38
+
39
+ c.new.after_save
40
+ $adds.should == ['hyiyie', 'byiyie']
41
+ end
42
+
43
+ deprec_specify "before hooks should run in reverse order" do
44
+ $adds = []
45
+ c = Class.new(Sequel::Model)
46
+ c.class_eval do
47
+ before_save {$adds << 'hyiyie'}
48
+ before_save {$adds << 'byiyie'}
49
+ end
50
+
51
+ c.new.before_save
52
+ $adds.should == ['byiyie', 'hyiyie']
53
+ end
54
+
55
+ deprec_specify "should not be additive if the method or tag already exists" do
56
+ $adds = []
57
+ c = Class.new(Sequel::Model)
58
+ c.class_eval do
59
+ def bye; $adds << 'bye'; end
60
+ before_save :bye
61
+ before_save :bye
62
+ end
63
+
64
+ c.new.before_save
65
+ $adds.should == ['bye']
66
+
67
+ $adds = []
68
+ d = Class.new(Sequel::Model)
69
+ d.class_eval do
70
+ before_save(:bye){$adds << 'hyiyie'}
71
+ before_save(:bye){$adds << 'byiyie'}
72
+ end
73
+
74
+ d.new.before_save
75
+ $adds.should == ['byiyie']
76
+
77
+ $adds = []
78
+ e = Class.new(Sequel::Model)
79
+ e.class_eval do
80
+ def bye; $adds << 'bye'; end
81
+ before_save :bye
82
+ before_save(:bye){$adds << 'byiyie'}
83
+ end
84
+
85
+ e.new.before_save
86
+ $adds.should == ['byiyie']
87
+
88
+ $adds = []
89
+ e = Class.new(Sequel::Model)
90
+ e.class_eval do
91
+ def bye; $adds << 'bye'; end
92
+ before_save(:bye){$adds << 'byiyie'}
93
+ before_save :bye
94
+ end
95
+
96
+ e.new.before_save
97
+ $adds.should == ['bye']
98
+ end
99
+
100
+ deprec_specify "should be inheritable" do
101
+ $adds = []
102
+ a = Class.new(Sequel::Model)
103
+ a.class_eval do
104
+ after_save {$adds << '123'}
105
+ end
106
+
107
+ b = Class.new(a)
108
+ b.class_eval do
109
+ after_save {$adds << '456'}
110
+ after_save {$adds << '789'}
111
+ end
112
+
113
+ b.new.after_save
114
+ $adds.should == ['123', '456', '789']
115
+ end
116
+
117
+ deprec_specify "should be overridable in descendant classes" do
118
+ $adds = []
119
+ a = Class.new(Sequel::Model)
120
+ a.class_eval do
121
+ before_save {$adds << '123'}
122
+ end
123
+
124
+ b = Class.new(a)
125
+ b.class_eval do
126
+ def before_save; $adds << '456'; end
127
+ end
128
+
129
+ a.new.before_save
130
+ $adds.should == ['123']
131
+ $adds = []
132
+ b.new.before_save
133
+ $adds.should == ['456']
134
+ end
135
+
136
+ deprec_specify "should stop processing if a hook returns false" do
137
+ $flag = true
138
+ $adds = []
139
+
140
+ a = Class.new(Sequel::Model)
141
+ a.class_eval do
142
+ after_save {$adds << 'blah'; $flag}
143
+ after_save {$adds << 'cruel'}
144
+ end
145
+
146
+ a.new.after_save
147
+ $adds.should == ['blah', 'cruel']
148
+
149
+ # chain should not break on nil
150
+ $adds = []
151
+ $flag = nil
152
+ a.new.after_save
153
+ $adds.should == ['blah', 'cruel']
154
+
155
+ $adds = []
156
+ $flag = false
157
+ a.new.after_save
158
+ $adds.should == ['blah']
159
+
160
+ b = Class.new(a)
161
+ b.class_eval do
162
+ after_save {$adds << 'mau'}
163
+ end
164
+
165
+ $adds = []
166
+ b.new.after_save
167
+ $adds.should == ['blah']
168
+ end
169
+ end
170
+
171
+ describe "Model#after_initialize" do
172
+ specify "should be called after initialization" do
173
+ $values1 = nil
174
+ $reached_after_initialized = false
175
+
176
+ a = Class.new(Sequel::Model)
177
+ a.class_eval do
178
+ columns :x, :y
179
+ def after_initialize
180
+ $values1 = @values.clone
181
+ $reached_after_initialized = true
182
+ end
183
+ end
184
+
185
+ a.new(:x => 1, :y => 2)
186
+ $values1.should == {:x => 1, :y => 2}
187
+ $reached_after_initialized.should == true
188
+ end
189
+ end
190
+
191
+ describe "Model#before_create && Model#after_create" do
192
+ before do
193
+ MODEL_DB.reset
194
+
195
+ @c = Class.new(Sequel::Model(:items))
196
+ @c.class_eval do
197
+ columns :x
198
+ no_primary_key
199
+
200
+ def after_create
201
+ MODEL_DB << "BLAH after"
202
+ end
203
+ end
204
+ end
205
+
206
+ specify "should be called around new record creation" do
207
+ @c.send(:define_method, :before_create){MODEL_DB << "BLAH before"}
208
+ @c.create(:x => 2)
209
+ MODEL_DB.sqls.should == [
210
+ 'BLAH before',
211
+ 'INSERT INTO items (x) VALUES (2)',
212
+ 'BLAH after'
213
+ ]
214
+ end
215
+
216
+ specify ".create should cancel the save and raise an error if before_create returns false and raise_on_save_failure is true" do
217
+ @c.send(:define_method, :before_create){false}
218
+ proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
219
+ proc{@c.create(:x => 2)}.should raise_error(Sequel::BeforeHookFailed)
220
+ MODEL_DB.sqls.should == []
221
+ end
222
+
223
+ specify ".create should cancel the save and return nil if before_create returns false and raise_on_save_failure is false" do
224
+ @c.send(:define_method, :before_create){false}
225
+ @c.raise_on_save_failure = false
226
+ @c.create(:x => 2).should == nil
227
+ MODEL_DB.sqls.should == []
228
+ end
229
+ end
230
+
231
+ describe "Model#before_update && Model#after_update" do
232
+ before do
233
+ MODEL_DB.reset
234
+
235
+ @c = Class.new(Sequel::Model(:items))
236
+ @c.class_eval do
237
+ def after_update; MODEL_DB << "BLAH after" end
238
+ end
239
+ end
240
+
241
+ specify "should be called around record update" do
242
+ @c.send(:define_method, :before_update){MODEL_DB << "BLAH before"}
243
+ m = @c.load(:id => 2233)
244
+ m.save
245
+ MODEL_DB.sqls.should == [
246
+ 'BLAH before',
247
+ 'UPDATE items SET id = 2233 WHERE (id = 2233)',
248
+ 'BLAH after'
249
+ ]
250
+ end
251
+
252
+ specify "#save should cancel the save and raise an error if before_update returns false and raise_on_save_failure is true" do
253
+ @c.send(:define_method, :before_update){false}
254
+ proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
255
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
256
+ MODEL_DB.sqls.should == []
257
+ end
258
+
259
+ specify "#save should cancel the save and return nil if before_update returns false and raise_on_save_failure is false" do
260
+ @c.send(:define_method, :before_update){false}
261
+ @c.raise_on_save_failure = false
262
+ @c.load(:id => 2233).save.should == nil
263
+ MODEL_DB.sqls.should == []
264
+ end
265
+ end
266
+
267
+ describe "Model#before_save && Model#after_save" do
268
+ before do
269
+ MODEL_DB.reset
270
+
271
+ @c = Class.new(Sequel::Model(:items))
272
+ @c.class_eval do
273
+ columns :x
274
+ def after_save; MODEL_DB << "BLAH after" end
275
+ end
276
+ end
277
+
278
+ specify "should be called around record update" do
279
+ @c.send(:define_method, :before_save){MODEL_DB << "BLAH before"}
280
+ m = @c.load(:id => 2233)
281
+ m.save
282
+ MODEL_DB.sqls.should == [
283
+ 'BLAH before',
284
+ 'UPDATE items SET id = 2233 WHERE (id = 2233)',
285
+ 'BLAH after'
286
+ ]
287
+ end
288
+
289
+ specify "should be called around record creation" do
290
+ @c.send(:define_method, :before_save){MODEL_DB << "BLAH before"}
291
+ @c.no_primary_key
292
+ @c.create(:x => 2)
293
+ MODEL_DB.sqls.should == [
294
+ 'BLAH before',
295
+ 'INSERT INTO items (x) VALUES (2)',
296
+ 'BLAH after'
297
+ ]
298
+ end
299
+
300
+ specify "#save should cancel the save and raise an error if before_save returns false and raise_on_save_failure is true" do
301
+ @c.send(:define_method, :before_save){false}
302
+ proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
303
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
304
+ MODEL_DB.sqls.should == []
305
+ end
306
+
307
+ specify "#save should cancel the save and return nil if before_save returns false and raise_on_save_failure is false" do
308
+ @c.send(:define_method, :before_save){false}
309
+ @c.raise_on_save_failure = false
310
+ @c.load(:id => 2233).save.should == nil
311
+ MODEL_DB.sqls.should == []
312
+ end
313
+ end
314
+
315
+ describe "Model#before_destroy && Model#after_destroy" do
316
+ before do
317
+ MODEL_DB.reset
318
+
319
+ @c = Class.new(Sequel::Model(:items))
320
+ @c.class_eval do
321
+ def after_destroy; MODEL_DB << "BLAH after"; end
322
+
323
+ def delete
324
+ MODEL_DB << "DELETE BLAH"
325
+ end
326
+ end
327
+ end
328
+
329
+ specify "should be called around record destruction" do
330
+ @c.send(:define_method, :before_destroy){MODEL_DB << "BLAH before"}
331
+ m = @c.load(:id => 2233)
332
+ m.destroy
333
+ MODEL_DB.sqls.should == [
334
+ 'BLAH before',
335
+ 'DELETE BLAH',
336
+ 'BLAH after'
337
+ ]
338
+ end
339
+
340
+ specify "#destroy should cancel the destroy and raise an error if before_destroy returns false and raise_on_save_failure is true" do
341
+ @c.send(:define_method, :before_destroy){false}
342
+ proc{@c.load(:id => 2233).destroy}.should raise_error(Sequel::BeforeHookFailed)
343
+ MODEL_DB.sqls.should == []
344
+ end
345
+
346
+ specify "#destroy should cancel the destroy and return nil if before_destroy returns false and raise_on_save_failure is false" do
347
+ @c.send(:define_method, :before_destroy){false}
348
+ @c.raise_on_save_failure = false
349
+ @c.load(:id => 2233).destroy.should == nil
350
+ MODEL_DB.sqls.should == []
351
+ end
352
+ end
353
+
354
+ describe "Model#before_validation && Model#after_validation" do
355
+ before do
356
+ MODEL_DB.reset
357
+
358
+ @c = Class.new(Sequel::Model(:items))
359
+ @c.class_eval do
360
+ def after_validation; MODEL_DB << "BLAH after" end
361
+
362
+ def self.validate(o)
363
+ o.errors[:id] << 'not valid' unless o[:id] == 2233
364
+ end
365
+ columns :id
366
+ end
367
+ end
368
+
369
+ specify "should be called around validation" do
370
+ @c.send(:define_method, :before_validation){MODEL_DB << "BLAH before"}
371
+ m = @c.load(:id => 2233)
372
+ m.should be_valid
373
+ MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
374
+
375
+ MODEL_DB.sqls.clear
376
+ m = @c.load(:id => 22)
377
+ m.should_not be_valid
378
+ MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
379
+ end
380
+
381
+ specify "should be called when calling save" do
382
+ @c.send(:define_method, :before_validation){MODEL_DB << "BLAH before"}
383
+ m = @c.load(:id => 2233)
384
+ m.save.should == m
385
+ MODEL_DB.sqls.should == ['BLAH before', 'BLAH after', 'UPDATE items SET id = 2233 WHERE (id = 2233)']
386
+
387
+ MODEL_DB.sqls.clear
388
+ m = @c.load(:id => 22)
389
+ m.raise_on_save_failure = false
390
+ m.save.should == nil
391
+ MODEL_DB.sqls.should == ['BLAH before', 'BLAH after']
392
+ end
393
+
394
+ specify "#save should cancel the save and raise an error if before_validation returns false and raise_on_save_failure is true" do
395
+ @c.send(:define_method, :before_validation){false}
396
+ proc{@c.load(:id => 2233).save}.should_not raise_error(Sequel::ValidationFailed)
397
+ proc{@c.load(:id => 2233).save}.should raise_error(Sequel::BeforeHookFailed)
398
+ MODEL_DB.sqls.should == []
399
+ end
400
+
401
+ specify "#save should cancel the save and return nil if before_validation returns false and raise_on_save_failure is false" do
402
+ @c.send(:define_method, :before_validation){false}
403
+ @c.raise_on_save_failure = false
404
+ @c.load(:id => 2233).save.should == nil
405
+ MODEL_DB.sqls.should == []
406
+ end
407
+ end
408
+
409
+ describe "Model.has_hooks?" do
410
+ before do
411
+ @c = Class.new(Sequel::Model(:items))
412
+ end
413
+
414
+ deprec_specify "should return false if no hooks are defined" do
415
+ @c.has_hooks?(:before_save).should be_false
416
+ end
417
+
418
+ deprec_specify "should return true if hooks are defined" do
419
+ @c.before_save {'blah'}
420
+ @c.has_hooks?(:before_save).should be_true
421
+ end
422
+
423
+ deprec_specify "should return true if hooks are inherited" do
424
+ @d = Class.new(@c)
425
+ @d.has_hooks?(:before_save).should be_false
426
+ end
427
+ end
428
+
429
+ describe "Model#add_hook_type" do
430
+ before do
431
+ deprec do
432
+ class Foo < Sequel::Model(:items)
433
+ add_hook_type :before_bar, :after_bar
434
+
435
+ def bar
436
+ return :b if before_bar == false
437
+ return :a if after_bar == false
438
+ true
439
+ end
440
+ end
441
+ end
442
+ @f = Class.new(Foo)
443
+ end
444
+
445
+ deprec_specify "should have before_bar and after_bar class methods" do
446
+ @f.should respond_to(:before_bar)
447
+ @f.should respond_to(:before_bar)
448
+ end
449
+
450
+ deprec_specify "should have before_bar and after_bar instance methods" do
451
+ @f.new.should respond_to(:before_bar)
452
+ @f.new.should respond_to(:before_bar)
453
+ end
454
+
455
+ deprec_specify "it should return true for bar when before_bar and after_bar hooks are returing true" do
456
+ a = 1
457
+ @f.before_bar { a += 1}
458
+ @f.new.bar.should be_true
459
+ a.should == 2
460
+ @f.after_bar { a *= 2}
461
+ @f.new.bar.should be_true
462
+ a.should == 6
463
+ end
464
+
465
+ deprec_specify "it should return nil for bar when before_bar and after_bar hooks are returing false" do
466
+ @f.new.bar.should be_true
467
+ @f.after_bar { false }
468
+ @f.new.bar.should == :a
469
+ @f.before_bar { false }
470
+ @f.new.bar.should == :b
471
+ end
472
+ end