sequel 3.21.0 → 3.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. data/CHANGELOG +485 -3
  2. data/README.rdoc +20 -6
  3. data/Rakefile +37 -19
  4. data/bin/sequel +1 -5
  5. data/doc/association_basics.rdoc +249 -43
  6. data/doc/dataset_basics.rdoc +4 -4
  7. data/doc/dataset_filtering.rdoc +8 -0
  8. data/doc/mass_assignment.rdoc +54 -0
  9. data/doc/migration.rdoc +15 -538
  10. data/doc/model_hooks.rdoc +72 -27
  11. data/doc/opening_databases.rdoc +89 -50
  12. data/doc/prepared_statements.rdoc +40 -13
  13. data/doc/reflection.rdoc +8 -2
  14. data/doc/release_notes/3.22.0.txt +39 -0
  15. data/doc/release_notes/3.23.0.txt +172 -0
  16. data/doc/release_notes/3.24.0.txt +420 -0
  17. data/doc/release_notes/3.25.0.txt +88 -0
  18. data/doc/release_notes/3.26.0.txt +88 -0
  19. data/doc/release_notes/3.27.0.txt +82 -0
  20. data/doc/release_notes/3.28.0.txt +304 -0
  21. data/doc/release_notes/3.29.0.txt +459 -0
  22. data/doc/release_notes/3.30.0.txt +135 -0
  23. data/doc/release_notes/3.31.0.txt +146 -0
  24. data/doc/schema_modification.rdoc +547 -0
  25. data/doc/sharding.rdoc +7 -1
  26. data/doc/testing.rdoc +129 -0
  27. data/doc/transactions.rdoc +137 -0
  28. data/doc/virtual_rows.rdoc +2 -2
  29. data/lib/sequel/adapters/ado/mssql.rb +18 -6
  30. data/lib/sequel/adapters/ado.rb +40 -18
  31. data/lib/sequel/adapters/amalgalite.rb +17 -9
  32. data/lib/sequel/adapters/db2.rb +175 -86
  33. data/lib/sequel/adapters/dbi.rb +15 -15
  34. data/lib/sequel/adapters/do/mysql.rb +5 -7
  35. data/lib/sequel/adapters/do/postgres.rb +0 -5
  36. data/lib/sequel/adapters/do/sqlite.rb +0 -5
  37. data/lib/sequel/adapters/do.rb +17 -36
  38. data/lib/sequel/adapters/firebird.rb +27 -208
  39. data/lib/sequel/adapters/ibmdb.rb +448 -0
  40. data/lib/sequel/adapters/informix.rb +6 -23
  41. data/lib/sequel/adapters/jdbc/as400.rb +16 -34
  42. data/lib/sequel/adapters/jdbc/db2.rb +45 -0
  43. data/lib/sequel/adapters/jdbc/derby.rb +247 -0
  44. data/lib/sequel/adapters/jdbc/firebird.rb +29 -0
  45. data/lib/sequel/adapters/jdbc/h2.rb +56 -29
  46. data/lib/sequel/adapters/jdbc/hsqldb.rb +170 -0
  47. data/lib/sequel/adapters/jdbc/informix.rb +26 -0
  48. data/lib/sequel/adapters/jdbc/jtds.rb +34 -0
  49. data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
  50. data/lib/sequel/adapters/jdbc/mysql.rb +11 -11
  51. data/lib/sequel/adapters/jdbc/oracle.rb +81 -25
  52. data/lib/sequel/adapters/jdbc/postgresql.rb +9 -13
  53. data/lib/sequel/adapters/jdbc/sqlite.rb +2 -6
  54. data/lib/sequel/adapters/jdbc/sqlserver.rb +71 -0
  55. data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
  56. data/lib/sequel/adapters/jdbc.rb +231 -78
  57. data/lib/sequel/adapters/mock.rb +335 -0
  58. data/lib/sequel/adapters/mysql.rb +82 -184
  59. data/lib/sequel/adapters/mysql2.rb +26 -25
  60. data/lib/sequel/adapters/odbc/db2.rb +17 -0
  61. data/lib/sequel/adapters/odbc/mssql.rb +1 -6
  62. data/lib/sequel/adapters/odbc.rb +28 -9
  63. data/lib/sequel/adapters/openbase.rb +3 -5
  64. data/lib/sequel/adapters/oracle.rb +351 -53
  65. data/lib/sequel/adapters/postgres.rb +194 -33
  66. data/lib/sequel/adapters/shared/access.rb +32 -9
  67. data/lib/sequel/adapters/shared/db2.rb +321 -0
  68. data/lib/sequel/adapters/shared/firebird.rb +221 -0
  69. data/lib/sequel/adapters/shared/informix.rb +53 -0
  70. data/lib/sequel/adapters/shared/mssql.rb +231 -106
  71. data/lib/sequel/adapters/shared/mysql.rb +181 -63
  72. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +149 -0
  73. data/lib/sequel/adapters/shared/oracle.rb +231 -41
  74. data/lib/sequel/adapters/shared/postgres.rb +214 -82
  75. data/lib/sequel/adapters/shared/progress.rb +4 -7
  76. data/lib/sequel/adapters/shared/sqlite.rb +201 -57
  77. data/lib/sequel/adapters/sqlite.rb +29 -21
  78. data/lib/sequel/adapters/swift/mysql.rb +12 -7
  79. data/lib/sequel/adapters/swift/postgres.rb +0 -5
  80. data/lib/sequel/adapters/swift/sqlite.rb +8 -6
  81. data/lib/sequel/adapters/swift.rb +5 -5
  82. data/lib/sequel/adapters/tinytds.rb +160 -19
  83. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
  84. data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
  85. data/lib/sequel/ast_transformer.rb +190 -0
  86. data/lib/sequel/connection_pool/threaded.rb +3 -2
  87. data/lib/sequel/connection_pool.rb +1 -1
  88. data/lib/sequel/core.rb +46 -5
  89. data/lib/sequel/database/connecting.rb +5 -6
  90. data/lib/sequel/database/dataset.rb +4 -4
  91. data/lib/sequel/database/dataset_defaults.rb +59 -1
  92. data/lib/sequel/database/logging.rb +1 -1
  93. data/lib/sequel/database/misc.rb +104 -19
  94. data/lib/sequel/database/query.rb +153 -58
  95. data/lib/sequel/database/schema_generator.rb +17 -7
  96. data/lib/sequel/database/schema_methods.rb +59 -29
  97. data/lib/sequel/dataset/actions.rb +231 -76
  98. data/lib/sequel/dataset/features.rb +91 -8
  99. data/lib/sequel/dataset/graph.rb +38 -11
  100. data/lib/sequel/dataset/misc.rb +50 -23
  101. data/lib/sequel/dataset/mutation.rb +5 -6
  102. data/lib/sequel/dataset/prepared_statements.rb +48 -27
  103. data/lib/sequel/dataset/query.rb +236 -43
  104. data/lib/sequel/dataset/sql.rb +665 -253
  105. data/lib/sequel/dataset.rb +8 -0
  106. data/lib/sequel/exceptions.rb +4 -0
  107. data/lib/sequel/extensions/columns_introspection.rb +61 -0
  108. data/lib/sequel/extensions/migration.rb +6 -4
  109. data/lib/sequel/extensions/named_timezones.rb +5 -0
  110. data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
  111. data/lib/sequel/extensions/to_dot.rb +95 -83
  112. data/lib/sequel/model/associations.rb +1020 -337
  113. data/lib/sequel/model/base.rb +414 -132
  114. data/lib/sequel/model/errors.rb +1 -1
  115. data/lib/sequel/model/exceptions.rb +5 -1
  116. data/lib/sequel/model.rb +15 -8
  117. data/lib/sequel/plugins/association_pks.rb +22 -4
  118. data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
  119. data/lib/sequel/plugins/dataset_associations.rb +100 -0
  120. data/lib/sequel/plugins/defaults_setter.rb +58 -0
  121. data/lib/sequel/plugins/force_encoding.rb +6 -6
  122. data/lib/sequel/plugins/identity_map.rb +114 -7
  123. data/lib/sequel/plugins/many_through_many.rb +65 -7
  124. data/lib/sequel/plugins/prepared_statements.rb +151 -0
  125. data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
  126. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  127. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  128. data/lib/sequel/plugins/rcte_tree.rb +29 -15
  129. data/lib/sequel/plugins/serialization.rb +88 -44
  130. data/lib/sequel/plugins/serialization_modification_detection.rb +51 -0
  131. data/lib/sequel/plugins/sharding.rb +21 -54
  132. data/lib/sequel/plugins/single_table_inheritance.rb +3 -1
  133. data/lib/sequel/plugins/typecast_on_load.rb +9 -12
  134. data/lib/sequel/plugins/update_primary_key.rb +2 -2
  135. data/lib/sequel/plugins/xml_serializer.rb +3 -3
  136. data/lib/sequel/sql.rb +109 -52
  137. data/lib/sequel/timezones.rb +57 -38
  138. data/lib/sequel/version.rb +1 -1
  139. data/spec/adapters/db2_spec.rb +146 -0
  140. data/spec/adapters/mssql_spec.rb +111 -29
  141. data/spec/adapters/mysql_spec.rb +135 -123
  142. data/spec/adapters/oracle_spec.rb +49 -77
  143. data/spec/adapters/postgres_spec.rb +267 -78
  144. data/spec/adapters/spec_helper.rb +6 -5
  145. data/spec/adapters/sqlite_spec.rb +35 -21
  146. data/spec/core/connection_pool_spec.rb +71 -92
  147. data/spec/core/core_sql_spec.rb +21 -30
  148. data/spec/core/database_spec.rb +788 -512
  149. data/spec/core/dataset_spec.rb +1073 -1053
  150. data/spec/core/expression_filters_spec.rb +159 -42
  151. data/spec/core/mock_adapter_spec.rb +419 -0
  152. data/spec/core/object_graph_spec.rb +86 -114
  153. data/spec/core/schema_generator_spec.rb +3 -3
  154. data/spec/core/schema_spec.rb +298 -38
  155. data/spec/core/spec_helper.rb +6 -48
  156. data/spec/extensions/association_autoreloading_spec.rb +1 -10
  157. data/spec/extensions/association_dependencies_spec.rb +2 -12
  158. data/spec/extensions/association_pks_spec.rb +66 -32
  159. data/spec/extensions/caching_spec.rb +23 -50
  160. data/spec/extensions/class_table_inheritance_spec.rb +31 -83
  161. data/spec/extensions/columns_introspection_spec.rb +91 -0
  162. data/spec/extensions/composition_spec.rb +18 -13
  163. data/spec/extensions/dataset_associations_spec.rb +199 -0
  164. data/spec/extensions/defaults_setter_spec.rb +64 -0
  165. data/spec/extensions/hook_class_methods_spec.rb +65 -91
  166. data/spec/extensions/identity_map_spec.rb +114 -22
  167. data/spec/extensions/instance_filters_spec.rb +11 -21
  168. data/spec/extensions/instance_hooks_spec.rb +53 -0
  169. data/spec/extensions/json_serializer_spec.rb +4 -5
  170. data/spec/extensions/lazy_attributes_spec.rb +16 -20
  171. data/spec/extensions/list_spec.rb +17 -39
  172. data/spec/extensions/many_through_many_spec.rb +283 -250
  173. data/spec/extensions/migration_spec.rb +35 -32
  174. data/spec/extensions/named_timezones_spec.rb +23 -3
  175. data/spec/extensions/nested_attributes_spec.rb +97 -88
  176. data/spec/extensions/optimistic_locking_spec.rb +9 -20
  177. data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
  178. data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
  179. data/spec/extensions/prepared_statements_spec.rb +53 -0
  180. data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
  181. data/spec/extensions/pretty_table_spec.rb +1 -6
  182. data/spec/extensions/rcte_tree_spec.rb +41 -43
  183. data/spec/extensions/schema_dumper_spec.rb +5 -8
  184. data/spec/extensions/schema_spec.rb +13 -21
  185. data/spec/extensions/serialization_modification_detection_spec.rb +37 -0
  186. data/spec/extensions/serialization_spec.rb +72 -43
  187. data/spec/extensions/sharding_spec.rb +66 -140
  188. data/spec/extensions/single_table_inheritance_spec.rb +18 -29
  189. data/spec/extensions/spec_helper.rb +13 -61
  190. data/spec/extensions/sql_expr_spec.rb +20 -60
  191. data/spec/extensions/tactical_eager_loading_spec.rb +9 -19
  192. data/spec/extensions/thread_local_timezones_spec.rb +22 -2
  193. data/spec/extensions/timestamps_spec.rb +6 -6
  194. data/spec/extensions/to_dot_spec.rb +4 -7
  195. data/spec/extensions/touch_spec.rb +13 -14
  196. data/spec/extensions/tree_spec.rb +11 -26
  197. data/spec/extensions/typecast_on_load_spec.rb +1 -6
  198. data/spec/extensions/update_primary_key_spec.rb +30 -24
  199. data/spec/extensions/validation_class_methods_spec.rb +30 -51
  200. data/spec/extensions/validation_helpers_spec.rb +16 -35
  201. data/spec/extensions/xml_serializer_spec.rb +16 -4
  202. data/spec/integration/associations_test.rb +537 -21
  203. data/spec/integration/database_test.rb +4 -2
  204. data/spec/integration/dataset_test.rb +461 -65
  205. data/spec/integration/eager_loader_test.rb +19 -21
  206. data/spec/integration/model_test.rb +80 -1
  207. data/spec/integration/plugin_test.rb +304 -116
  208. data/spec/integration/prepared_statement_test.rb +212 -122
  209. data/spec/integration/schema_test.rb +161 -30
  210. data/spec/integration/spec_helper.rb +39 -30
  211. data/spec/integration/timezone_test.rb +38 -12
  212. data/spec/integration/transaction_test.rb +172 -5
  213. data/spec/integration/type_test.rb +17 -3
  214. data/spec/model/association_reflection_spec.rb +201 -7
  215. data/spec/model/associations_spec.rb +865 -662
  216. data/spec/model/base_spec.rb +275 -95
  217. data/spec/model/dataset_methods_spec.rb +7 -27
  218. data/spec/model/eager_loading_spec.rb +677 -676
  219. data/spec/model/hooks_spec.rb +309 -67
  220. data/spec/model/model_spec.rb +222 -168
  221. data/spec/model/plugins_spec.rb +24 -13
  222. data/spec/model/record_spec.rb +381 -218
  223. data/spec/model/spec_helper.rb +13 -71
  224. data/spec/model/validations_spec.rb +11 -0
  225. metadata +115 -73
@@ -23,22 +23,19 @@ describe Sequel::Model, "dataset & schema" do
23
23
  end
24
24
 
25
25
  describe Sequel::Model, "table_exists?" do
26
-
27
- before(:each) do
26
+ before do
28
27
  MODEL_DB.reset
29
28
  @model = Class.new(Sequel::Model(:items))
30
29
  end
31
30
 
32
31
  it "should get the table name and question the model's db if table_exists?" do
33
- @model.should_receive(:table_name).and_return(:items)
34
- @model.db.should_receive(:table_exists?)
35
- @model.table_exists?
32
+ @model.db.should_receive(:table_exists?).and_return(false)
33
+ @model.table_exists?.should == false
36
34
  end
37
35
  end
38
36
 
39
37
  describe Sequel::Model, "create_table and schema" do
40
-
41
- before(:each) do
38
+ before do
42
39
  MODEL_DB.reset
43
40
  @model = Class.new(Sequel::Model) do
44
41
  set_schema(:items) do
@@ -60,7 +57,7 @@ describe Sequel::Model, "create_table and schema" do
60
57
 
61
58
  it "should reload the schema from the database" do
62
59
  schem = {:name=>{:type=>:string}, :price=>{:type=>:float}}
63
- @model.db.should_receive(:schema).with(:items, :reload=>true).and_return(schem.to_a.sort_by{|x| x[0].to_s})
60
+ @model.db.should_receive(:schema).with(@model.dataset, :reload=>true).and_return(schem.to_a.sort_by{|x| x[0].to_s})
64
61
  @model.create_table
65
62
  @model.db_schema.should == schem
66
63
  @model.instance_variable_get(:@columns).should == [:name, :price]
@@ -84,19 +81,15 @@ describe Sequel::Model, "create_table and schema" do
84
81
  end
85
82
 
86
83
  describe Sequel::Model, "drop_table" do
87
-
88
- before(:each) do
89
- MODEL_DB.reset
84
+ before do
90
85
  @model = Class.new(Sequel::Model(:items))
86
+ MODEL_DB.reset
91
87
  end
92
88
 
93
89
  it "should get the drop table SQL for the associated table and then execute the SQL." do
94
- @model.should_receive(:table_name).and_return(:items)
95
- @model.db.should_receive(:drop_table_sql).with(:items)
96
- @model.db.should_receive(:execute).and_return(:true)
97
90
  @model.drop_table
91
+ MODEL_DB.sqls.should == ['DROP TABLE items']
98
92
  end
99
-
100
93
  end
101
94
 
102
95
  describe Sequel::Model, "create_table!" do
@@ -106,9 +99,8 @@ describe Sequel::Model, "create_table!" do
106
99
  end
107
100
 
108
101
  it "should drop table if it exists and then create the table" do
109
- @model.should_receive(:drop_table).and_return(true)
110
- @model.should_receive(:create_table).and_return(true)
111
102
  @model.create_table!
103
+ MODEL_DB.sqls.should == ['DROP TABLE items', 'CREATE TABLE items ()']
112
104
  end
113
105
  end
114
106
 
@@ -120,13 +112,13 @@ describe Sequel::Model, "create_table?" do
120
112
 
121
113
  it "should not create the table if it already exists" do
122
114
  @model.should_receive(:table_exists?).and_return(true)
123
- @model.should_not_receive(:create_table)
124
- @model.create_table?.should == nil
115
+ @model.create_table?
116
+ MODEL_DB.sqls.should == []
125
117
  end
126
118
 
127
119
  it "should create the table if it doesn't exist" do
128
120
  @model.should_receive(:table_exists?).and_return(false)
129
- @model.should_receive(:create_table).and_return(3)
130
- @model.create_table?.should == 3
121
+ @model.create_table?
122
+ MODEL_DB.sqls.should == ['CREATE TABLE items ()']
131
123
  end
132
124
  end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
2
+ require 'yaml'
3
+
4
+ describe "serialization_modification_detection plugin" do
5
+ before do
6
+ @c = Class.new(Sequel::Model(:items))
7
+ @c.class_eval do
8
+ columns :id, :h
9
+ plugin :serialization, :yaml, :h
10
+ plugin :serialization_modification_detection
11
+ end
12
+ @o1 = @c.new
13
+ @o2 = @c.load(:id=>1, :h=>"--- {}\n\n")
14
+ MODEL_DB.reset
15
+ end
16
+
17
+ it "should not detect columns that haven't been changed" do
18
+ @o2.changed_columns.should == []
19
+ @o2.h.should == {}
20
+ @o2.h[1] = 2
21
+ @o2.h.clear
22
+ @o2.changed_columns.should == []
23
+ end
24
+
25
+ it "should detect columns that have been changed" do
26
+ @o2.changed_columns.should == []
27
+ @o2.h.should == {}
28
+ @o2.h[1] = 2
29
+ @o2.changed_columns.should == [:h]
30
+ end
31
+
32
+ it "should report correct changed_columns after saving" do
33
+ @o2.h[1] = 2
34
+ @o2.save_changes
35
+ @o2.changed_columns.should == []
36
+ end
37
+ end
@@ -9,10 +9,6 @@ else
9
9
  describe "Serialization plugin" do
10
10
  before do
11
11
  @c = Class.new(Sequel::Model(:items)) do
12
- include(Module.new do
13
- def before_save
14
- end
15
- end)
16
12
  no_primary_key
17
13
  columns :id, :abc, :def, :ghi
18
14
  end
@@ -22,11 +18,11 @@ describe "Serialization plugin" do
22
18
  it "should allow setting additional serializable attributes via plugin :serialization call" do
23
19
  @c.plugin :serialization, :yaml, :abc
24
20
  @c.create(:abc => 1, :def=> 2)
25
- MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n', 2|2, '--- 1\n')\)/
21
+ MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 2|2, '--- 1\n(\.\.\.\n)?')\)/
26
22
 
27
23
  @c.plugin :serialization, :marshal, :def
28
24
  @c.create(:abc => 1, :def=> 1)
29
- MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n')\)/
25
+ MODEL_DB.sqls.last.should =~ /INSERT INTO items \((abc, def|def, abc)\) VALUES \(('--- 1\n(\.\.\.\n)?', 'BAhpBg==\n'|'BAhpBg==\n', '--- 1\n(\.\.\.\n)?')\)/
30
26
 
31
27
  @c.plugin :serialization, :json, :ghi
32
28
  @c.create(:ghi => [123])
@@ -38,15 +34,7 @@ describe "Serialization plugin" do
38
34
  @c.create(:abc => 1)
39
35
  @c.create(:abc => "hello")
40
36
 
41
- MODEL_DB.sqls.should == [ \
42
- "INSERT INTO items (abc) VALUES ('--- 1\n')", \
43
- "INSERT INTO items (abc) VALUES ('--- hello\n')", \
44
- ]
45
- end
46
-
47
- it "serialization_format should be the serialization format used" do
48
- @c.plugin :serialization, :yaml, :abc
49
- @c.serialization_format.should == :yaml
37
+ MODEL_DB.sqls.map{|s| s.sub("...\n", '')}.should == ["INSERT INTO items (abc) VALUES ('--- 1\n')", "INSERT INTO items (abc) VALUES ('--- hello\n')"]
50
38
  end
51
39
 
52
40
  it "serialized_columns should be the columns serialized" do
@@ -78,15 +66,17 @@ describe "Serialization plugin" do
78
66
  ]
79
67
  end
80
68
 
69
+ it "should allow serializing attributes using arbitrary callable" do
70
+ @c.plugin :serialization, [proc{|s| s.reverse}, proc{}], :abc
71
+ @c.create(:abc => "hello")
72
+ MODEL_DB.sqls.should == ["INSERT INTO items (abc) VALUES ('olleh')"]
73
+ end
74
+
81
75
  it "should translate values to and from yaml serialization format using accessor methods" do
82
76
  @c.set_primary_key :id
83
77
  @c.plugin :serialization, :yaml, :abc, :def
84
78
  vals = nil
85
-
86
- ds = @c.dataset
87
- def ds.fetch_rows(sql, &block)
88
- block.call(:id => 1, :abc => "--- 1\n", :def => "--- hello\n")
89
- end
79
+ @c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
90
80
 
91
81
  o = @c.first
92
82
  o.id.should == 1
@@ -97,18 +87,16 @@ describe "Serialization plugin" do
97
87
 
98
88
  o.update(:abc => 23)
99
89
  @c.create(:abc => [1, 2, 3])
100
- MODEL_DB.sqls.should == ["UPDATE items SET abc = '--- 23\n' WHERE (id = 1)",
101
- "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')"]
90
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
91
+ "UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)",
92
+ "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')",
93
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
102
94
  end
103
95
 
104
96
  it "should translate values to and from marshal serialization format using accessor methods" do
105
97
  @c.set_primary_key :id
106
98
  @c.plugin :serialization, :marshal, :abc, :def
107
-
108
- ds = @c.dataset
109
- def ds.fetch_rows(sql, &block)
110
- block.call(:id => 1, :abc =>[Marshal.dump(1)].pack('m'), :def =>[Marshal.dump('hello')].pack('m'))
111
- end
99
+ @c.dataset._fetch = [:id => 1, :abc =>[Marshal.dump(1)].pack('m'), :def =>[Marshal.dump('hello')].pack('m')]
112
100
 
113
101
  o = @c.first
114
102
  o.id.should == 1
@@ -119,18 +107,16 @@ describe "Serialization plugin" do
119
107
 
120
108
  o.update(:abc => 23)
121
109
  @c.create(:abc => [1, 2, 3])
122
- MODEL_DB.sqls.should == ["UPDATE items SET abc = '#{[Marshal.dump(23)].pack('m')}' WHERE (id = 1)",
123
- "INSERT INTO items (abc) VALUES ('#{[Marshal.dump([1, 2, 3])].pack('m')}')"]
110
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
111
+ "UPDATE items SET abc = '#{[Marshal.dump(23)].pack('m')}' WHERE (id = 1)",
112
+ "INSERT INTO items (abc) VALUES ('#{[Marshal.dump([1, 2, 3])].pack('m')}')",
113
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
124
114
  end
125
115
 
126
116
  it "should translate values to and from json serialization format using accessor methods" do
127
117
  @c.set_primary_key :id
128
118
  @c.plugin :serialization, :json, :abc, :def
129
-
130
- ds = @c.dataset
131
- def ds.fetch_rows(sql, &block)
132
- block.call(:id => 1, :abc => [1].to_json, :def => ["hello"].to_json)
133
- end
119
+ @c.dataset._fetch = {:id => 1, :abc => [1].to_json, :def => ["hello"].to_json}
134
120
 
135
121
  o = @c.first
136
122
  o.id.should == 1
@@ -142,18 +128,59 @@ describe "Serialization plugin" do
142
128
  o.update(:abc => [23])
143
129
  @c.create(:abc => [1,2,3])
144
130
 
145
- MODEL_DB.sqls.should == ["UPDATE items SET abc = '#{[23].to_json}' WHERE (id = 1)",
146
- "INSERT INTO items (abc) VALUES ('#{[1,2,3].to_json}')"]
131
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
132
+ "UPDATE items SET abc = '#{[23].to_json}' WHERE (id = 1)",
133
+ "INSERT INTO items (abc) VALUES ('#{[1,2,3].to_json}')",
134
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
135
+ end
136
+
137
+ it "should translate values to and from arbitrary callables using accessor methods" do
138
+ @c.set_primary_key :id
139
+ @c.plugin :serialization, [proc{|s| s.reverse}, proc{|s| s.reverse}], :abc, :def
140
+ @c.dataset._fetch = {:id => 1, :abc => 'cba', :def => 'olleh'}
141
+
142
+ o = @c.first
143
+ o.id.should == 1
144
+ o.abc.should == 'abc'
145
+ o.abc.should == 'abc'
146
+ o.def.should == "hello"
147
+ o.def.should == "hello"
148
+
149
+ o.update(:abc => 'foo')
150
+ @c.create(:abc => 'bar')
151
+
152
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
153
+ "UPDATE items SET abc = 'oof' WHERE (id = 1)",
154
+ "INSERT INTO items (abc) VALUES ('rab')",
155
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
156
+ end
157
+
158
+ it "should handle registration of custom serializer/deserializer pairs" do
159
+ @c.set_primary_key :id
160
+ Sequel::Plugins::Serialization.register_format(:reverse, proc{|s| s.reverse}, proc{|s| s.reverse})
161
+ @c.plugin :serialization, :reverse, :abc, :def
162
+ @c.dataset._fetch = {:id => 1, :abc => 'cba', :def => 'olleh'}
163
+
164
+ o = @c.first
165
+ o.id.should == 1
166
+ o.abc.should == 'abc'
167
+ o.abc.should == 'abc'
168
+ o.def.should == "hello"
169
+ o.def.should == "hello"
170
+
171
+ o.update(:abc => 'foo')
172
+ @c.create(:abc => 'bar')
173
+
174
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
175
+ "UPDATE items SET abc = 'oof' WHERE (id = 1)",
176
+ "INSERT INTO items (abc) VALUES ('rab')",
177
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
147
178
  end
148
179
 
149
180
  it "should copy serialization formats and columns to subclasses" do
150
181
  @c.set_primary_key :id
151
182
  @c.plugin :serialization, :yaml, :abc, :def
152
-
153
- ds = @c.dataset
154
- def ds.fetch_rows(sql, &block)
155
- block.call(:id => 1, :abc => "--- 1\n", :def => "--- hello\n")
156
- end
183
+ @c.dataset._fetch = {:id => 1, :abc => "--- 1\n", :def => "--- hello\n"}
157
184
 
158
185
  o = Class.new(@c).first
159
186
  o.id.should == 1
@@ -164,8 +191,10 @@ describe "Serialization plugin" do
164
191
 
165
192
  o.update(:abc => 23)
166
193
  Class.new(@c).create(:abc => [1, 2, 3])
167
- MODEL_DB.sqls.should == ["UPDATE items SET abc = '--- 23\n' WHERE (id = 1)",
168
- "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')"]
194
+ MODEL_DB.sqls.should == ["SELECT * FROM items LIMIT 1",
195
+ "UPDATE items SET abc = '#{23.to_yaml}' WHERE (id = 1)",
196
+ "INSERT INTO items (abc) VALUES ('#{[1, 2, 3].to_yaml}')",
197
+ "SELECT * FROM items WHERE (id = 10) LIMIT 1"]
169
198
  end
170
199
 
171
200
  it "should clear the deserialized columns when refreshing" do