sequel 3.21.0 → 3.32.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 (234) hide show
  1. data/CHANGELOG +539 -3
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +20 -6
  4. data/Rakefile +37 -19
  5. data/bin/sequel +1 -5
  6. data/doc/advanced_associations.rdoc +17 -0
  7. data/doc/association_basics.rdoc +293 -43
  8. data/doc/dataset_basics.rdoc +4 -4
  9. data/doc/dataset_filtering.rdoc +8 -0
  10. data/doc/mass_assignment.rdoc +54 -0
  11. data/doc/migration.rdoc +15 -538
  12. data/doc/model_hooks.rdoc +72 -27
  13. data/doc/opening_databases.rdoc +89 -50
  14. data/doc/prepared_statements.rdoc +40 -13
  15. data/doc/reflection.rdoc +8 -2
  16. data/doc/release_notes/3.22.0.txt +39 -0
  17. data/doc/release_notes/3.23.0.txt +172 -0
  18. data/doc/release_notes/3.24.0.txt +420 -0
  19. data/doc/release_notes/3.25.0.txt +88 -0
  20. data/doc/release_notes/3.26.0.txt +88 -0
  21. data/doc/release_notes/3.27.0.txt +82 -0
  22. data/doc/release_notes/3.28.0.txt +304 -0
  23. data/doc/release_notes/3.29.0.txt +459 -0
  24. data/doc/release_notes/3.30.0.txt +135 -0
  25. data/doc/release_notes/3.31.0.txt +146 -0
  26. data/doc/release_notes/3.32.0.txt +202 -0
  27. data/doc/schema_modification.rdoc +547 -0
  28. data/doc/sharding.rdoc +7 -1
  29. data/doc/testing.rdoc +129 -0
  30. data/doc/transactions.rdoc +137 -0
  31. data/doc/virtual_rows.rdoc +2 -2
  32. data/lib/sequel/adapters/ado/mssql.rb +18 -6
  33. data/lib/sequel/adapters/ado.rb +40 -18
  34. data/lib/sequel/adapters/amalgalite.rb +17 -9
  35. data/lib/sequel/adapters/db2.rb +175 -86
  36. data/lib/sequel/adapters/dbi.rb +15 -15
  37. data/lib/sequel/adapters/do/mysql.rb +5 -7
  38. data/lib/sequel/adapters/do/postgres.rb +0 -5
  39. data/lib/sequel/adapters/do/sqlite.rb +0 -5
  40. data/lib/sequel/adapters/do.rb +17 -36
  41. data/lib/sequel/adapters/firebird.rb +27 -208
  42. data/lib/sequel/adapters/ibmdb.rb +448 -0
  43. data/lib/sequel/adapters/informix.rb +6 -23
  44. data/lib/sequel/adapters/jdbc/as400.rb +16 -34
  45. data/lib/sequel/adapters/jdbc/db2.rb +52 -0
  46. data/lib/sequel/adapters/jdbc/derby.rb +260 -0
  47. data/lib/sequel/adapters/jdbc/firebird.rb +29 -0
  48. data/lib/sequel/adapters/jdbc/h2.rb +66 -30
  49. data/lib/sequel/adapters/jdbc/hsqldb.rb +177 -0
  50. data/lib/sequel/adapters/jdbc/informix.rb +26 -0
  51. data/lib/sequel/adapters/jdbc/jtds.rb +34 -0
  52. data/lib/sequel/adapters/jdbc/mssql.rb +3 -33
  53. data/lib/sequel/adapters/jdbc/mysql.rb +11 -11
  54. data/lib/sequel/adapters/jdbc/oracle.rb +88 -25
  55. data/lib/sequel/adapters/jdbc/postgresql.rb +9 -13
  56. data/lib/sequel/adapters/jdbc/sqlite.rb +2 -6
  57. data/lib/sequel/adapters/jdbc/sqlserver.rb +71 -0
  58. data/lib/sequel/adapters/jdbc/transactions.rb +83 -0
  59. data/lib/sequel/adapters/jdbc.rb +231 -78
  60. data/lib/sequel/adapters/mock.rb +339 -0
  61. data/lib/sequel/adapters/mysql.rb +85 -184
  62. data/lib/sequel/adapters/mysql2.rb +26 -25
  63. data/lib/sequel/adapters/odbc/db2.rb +17 -0
  64. data/lib/sequel/adapters/odbc/mssql.rb +1 -6
  65. data/lib/sequel/adapters/odbc.rb +28 -9
  66. data/lib/sequel/adapters/openbase.rb +3 -5
  67. data/lib/sequel/adapters/oracle.rb +355 -53
  68. data/lib/sequel/adapters/postgres.rb +194 -33
  69. data/lib/sequel/adapters/shared/access.rb +32 -9
  70. data/lib/sequel/adapters/shared/db2.rb +328 -0
  71. data/lib/sequel/adapters/shared/firebird.rb +221 -0
  72. data/lib/sequel/adapters/shared/informix.rb +53 -0
  73. data/lib/sequel/adapters/shared/mssql.rb +278 -107
  74. data/lib/sequel/adapters/shared/mysql.rb +203 -65
  75. data/lib/sequel/adapters/shared/mysql_prepared_statements.rb +149 -0
  76. data/lib/sequel/adapters/shared/oracle.rb +237 -46
  77. data/lib/sequel/adapters/shared/postgres.rb +214 -82
  78. data/lib/sequel/adapters/shared/progress.rb +5 -8
  79. data/lib/sequel/adapters/shared/sqlite.rb +200 -50
  80. data/lib/sequel/adapters/sqlite.rb +29 -21
  81. data/lib/sequel/adapters/swift/mysql.rb +12 -7
  82. data/lib/sequel/adapters/swift/postgres.rb +0 -5
  83. data/lib/sequel/adapters/swift/sqlite.rb +8 -6
  84. data/lib/sequel/adapters/swift.rb +5 -5
  85. data/lib/sequel/adapters/tinytds.rb +160 -19
  86. data/lib/sequel/adapters/utils/emulate_offset_with_row_number.rb +66 -0
  87. data/lib/sequel/adapters/utils/stored_procedures.rb +1 -11
  88. data/lib/sequel/ast_transformer.rb +190 -0
  89. data/lib/sequel/connection_pool/threaded.rb +3 -2
  90. data/lib/sequel/connection_pool.rb +1 -1
  91. data/lib/sequel/core.rb +68 -5
  92. data/lib/sequel/database/connecting.rb +5 -6
  93. data/lib/sequel/database/dataset.rb +4 -4
  94. data/lib/sequel/database/dataset_defaults.rb +59 -1
  95. data/lib/sequel/database/logging.rb +1 -1
  96. data/lib/sequel/database/misc.rb +104 -19
  97. data/lib/sequel/database/query.rb +163 -59
  98. data/lib/sequel/database/schema_generator.rb +17 -7
  99. data/lib/sequel/database/schema_methods.rb +59 -29
  100. data/lib/sequel/dataset/actions.rb +240 -76
  101. data/lib/sequel/dataset/features.rb +91 -8
  102. data/lib/sequel/dataset/graph.rb +38 -11
  103. data/lib/sequel/dataset/misc.rb +50 -23
  104. data/lib/sequel/dataset/mutation.rb +12 -7
  105. data/lib/sequel/dataset/prepared_statements.rb +59 -27
  106. data/lib/sequel/dataset/query.rb +236 -43
  107. data/lib/sequel/dataset/sql.rb +676 -267
  108. data/lib/sequel/dataset.rb +8 -0
  109. data/lib/sequel/exceptions.rb +4 -0
  110. data/lib/sequel/extensions/columns_introspection.rb +61 -0
  111. data/lib/sequel/extensions/migration.rb +6 -4
  112. data/lib/sequel/extensions/named_timezones.rb +5 -0
  113. data/lib/sequel/extensions/query.rb +1 -1
  114. data/lib/sequel/extensions/thread_local_timezones.rb +1 -1
  115. data/lib/sequel/extensions/to_dot.rb +95 -83
  116. data/lib/sequel/model/associations.rb +1081 -344
  117. data/lib/sequel/model/base.rb +420 -133
  118. data/lib/sequel/model/errors.rb +1 -1
  119. data/lib/sequel/model/exceptions.rb +5 -1
  120. data/lib/sequel/model.rb +20 -10
  121. data/lib/sequel/plugins/active_model.rb +13 -1
  122. data/lib/sequel/plugins/association_pks.rb +22 -4
  123. data/lib/sequel/plugins/class_table_inheritance.rb +4 -4
  124. data/lib/sequel/plugins/composition.rb +43 -10
  125. data/lib/sequel/plugins/dataset_associations.rb +100 -0
  126. data/lib/sequel/plugins/defaults_setter.rb +58 -0
  127. data/lib/sequel/plugins/force_encoding.rb +6 -6
  128. data/lib/sequel/plugins/identity_map.rb +114 -7
  129. data/lib/sequel/plugins/many_through_many.rb +68 -7
  130. data/lib/sequel/plugins/nested_attributes.rb +65 -10
  131. data/lib/sequel/plugins/prepared_statements.rb +151 -0
  132. data/lib/sequel/plugins/prepared_statements_associations.rb +84 -0
  133. data/lib/sequel/plugins/prepared_statements_safe.rb +82 -0
  134. data/lib/sequel/plugins/prepared_statements_with_pk.rb +59 -0
  135. data/lib/sequel/plugins/rcte_tree.rb +29 -15
  136. data/lib/sequel/plugins/serialization.rb +98 -49
  137. data/lib/sequel/plugins/serialization_modification_detection.rb +63 -0
  138. data/lib/sequel/plugins/sharding.rb +21 -54
  139. data/lib/sequel/plugins/single_table_inheritance.rb +3 -1
  140. data/lib/sequel/plugins/typecast_on_load.rb +9 -12
  141. data/lib/sequel/plugins/update_primary_key.rb +2 -2
  142. data/lib/sequel/plugins/xml_serializer.rb +3 -3
  143. data/lib/sequel/sql.rb +109 -52
  144. data/lib/sequel/timezones.rb +57 -38
  145. data/lib/sequel/version.rb +1 -1
  146. data/spec/adapters/db2_spec.rb +146 -0
  147. data/spec/adapters/mssql_spec.rb +135 -30
  148. data/spec/adapters/mysql_spec.rb +244 -212
  149. data/spec/adapters/oracle_spec.rb +67 -77
  150. data/spec/adapters/postgres_spec.rb +267 -78
  151. data/spec/adapters/spec_helper.rb +6 -5
  152. data/spec/adapters/sqlite_spec.rb +35 -21
  153. data/spec/core/connection_pool_spec.rb +71 -92
  154. data/spec/core/core_sql_spec.rb +21 -30
  155. data/spec/core/database_spec.rb +789 -512
  156. data/spec/core/dataset_spec.rb +1176 -1061
  157. data/spec/core/expression_filters_spec.rb +159 -42
  158. data/spec/core/mock_adapter_spec.rb +419 -0
  159. data/spec/core/object_graph_spec.rb +86 -114
  160. data/spec/core/schema_generator_spec.rb +3 -3
  161. data/spec/core/schema_spec.rb +298 -38
  162. data/spec/core/spec_helper.rb +6 -48
  163. data/spec/extensions/active_model_spec.rb +13 -0
  164. data/spec/extensions/association_autoreloading_spec.rb +1 -10
  165. data/spec/extensions/association_dependencies_spec.rb +2 -12
  166. data/spec/extensions/association_pks_spec.rb +66 -32
  167. data/spec/extensions/caching_spec.rb +23 -50
  168. data/spec/extensions/class_table_inheritance_spec.rb +31 -83
  169. data/spec/extensions/columns_introspection_spec.rb +91 -0
  170. data/spec/extensions/composition_spec.rb +18 -13
  171. data/spec/extensions/dataset_associations_spec.rb +199 -0
  172. data/spec/extensions/defaults_setter_spec.rb +64 -0
  173. data/spec/extensions/hook_class_methods_spec.rb +65 -91
  174. data/spec/extensions/identity_map_spec.rb +114 -22
  175. data/spec/extensions/instance_filters_spec.rb +11 -21
  176. data/spec/extensions/instance_hooks_spec.rb +53 -0
  177. data/spec/extensions/json_serializer_spec.rb +4 -5
  178. data/spec/extensions/lazy_attributes_spec.rb +16 -20
  179. data/spec/extensions/list_spec.rb +17 -39
  180. data/spec/extensions/many_through_many_spec.rb +283 -250
  181. data/spec/extensions/migration_spec.rb +35 -32
  182. data/spec/extensions/named_timezones_spec.rb +23 -3
  183. data/spec/extensions/nested_attributes_spec.rb +97 -88
  184. data/spec/extensions/optimistic_locking_spec.rb +9 -20
  185. data/spec/extensions/prepared_statements_associations_spec.rb +111 -0
  186. data/spec/extensions/prepared_statements_safe_spec.rb +61 -0
  187. data/spec/extensions/prepared_statements_spec.rb +53 -0
  188. data/spec/extensions/prepared_statements_with_pk_spec.rb +31 -0
  189. data/spec/extensions/pretty_table_spec.rb +1 -6
  190. data/spec/extensions/query_spec.rb +6 -0
  191. data/spec/extensions/rcte_tree_spec.rb +41 -43
  192. data/spec/extensions/schema_dumper_spec.rb +5 -8
  193. data/spec/extensions/schema_spec.rb +13 -21
  194. data/spec/extensions/serialization_modification_detection_spec.rb +72 -0
  195. data/spec/extensions/serialization_spec.rb +81 -43
  196. data/spec/extensions/sharding_spec.rb +66 -140
  197. data/spec/extensions/single_table_inheritance_spec.rb +18 -29
  198. data/spec/extensions/spec_helper.rb +13 -61
  199. data/spec/extensions/sql_expr_spec.rb +20 -60
  200. data/spec/extensions/tactical_eager_loading_spec.rb +9 -19
  201. data/spec/extensions/thread_local_timezones_spec.rb +22 -2
  202. data/spec/extensions/timestamps_spec.rb +6 -6
  203. data/spec/extensions/to_dot_spec.rb +4 -7
  204. data/spec/extensions/touch_spec.rb +13 -14
  205. data/spec/extensions/tree_spec.rb +11 -26
  206. data/spec/extensions/typecast_on_load_spec.rb +1 -6
  207. data/spec/extensions/update_primary_key_spec.rb +30 -24
  208. data/spec/extensions/validation_class_methods_spec.rb +30 -51
  209. data/spec/extensions/validation_helpers_spec.rb +16 -35
  210. data/spec/extensions/xml_serializer_spec.rb +16 -4
  211. data/spec/integration/associations_test.rb +661 -21
  212. data/spec/integration/database_test.rb +4 -2
  213. data/spec/integration/dataset_test.rb +500 -67
  214. data/spec/integration/eager_loader_test.rb +19 -21
  215. data/spec/integration/model_test.rb +80 -1
  216. data/spec/integration/plugin_test.rb +367 -119
  217. data/spec/integration/prepared_statement_test.rb +222 -125
  218. data/spec/integration/schema_test.rb +217 -39
  219. data/spec/integration/spec_helper.rb +39 -30
  220. data/spec/integration/timezone_test.rb +38 -12
  221. data/spec/integration/transaction_test.rb +182 -5
  222. data/spec/integration/type_test.rb +17 -3
  223. data/spec/model/association_reflection_spec.rb +201 -7
  224. data/spec/model/associations_spec.rb +955 -662
  225. data/spec/model/base_spec.rb +275 -95
  226. data/spec/model/dataset_methods_spec.rb +7 -27
  227. data/spec/model/eager_loading_spec.rb +677 -676
  228. data/spec/model/hooks_spec.rb +349 -67
  229. data/spec/model/model_spec.rb +222 -168
  230. data/spec/model/plugins_spec.rb +24 -13
  231. data/spec/model/record_spec.rb +381 -218
  232. data/spec/model/spec_helper.rb +13 -71
  233. data/spec/model/validations_spec.rb +11 -0
  234. metadata +117 -73
@@ -109,7 +109,15 @@ module Sequel
109
109
  ka = opts[:key_alias] ||= :x_root_x
110
110
  t = opts[:cte_name] ||= :t
111
111
  opts[:reciprocal] = nil
112
- c_all = SQL::ColumnAll.new(model.table_name)
112
+ c_all = if model.dataset.recursive_cte_requires_column_aliases?
113
+ # Work around Oracle/ruby-oci8 bug that returns integers as BigDecimals in recursive queries.
114
+ conv_bd = model.db.database_type == :oracle
115
+ col_aliases = model.dataset.columns
116
+ model_table = model.table_name
117
+ col_aliases.map{|c| SQL::QualifiedIdentifier.new(model_table, c)}
118
+ else
119
+ [SQL::ColumnAll.new(model.table_name)]
120
+ end
113
121
 
114
122
  a = opts.merge(opts.fetch(:ancestors, {}))
115
123
  ancestors = a.fetch(:name, :ancestors)
@@ -125,9 +133,9 @@ module Sequel
125
133
  end
126
134
  table_alias = model.dataset.schema_and_table(model.table_name)[1].to_sym
127
135
  model.from(t => table_alias).
128
- with_recursive(t, base_ds.select_all,
129
- recursive_ds.
130
- select(c_all))
136
+ with_recursive(t, col_aliases ? base_ds.select(*col_aliases) : base_ds.select_all,
137
+ recursive_ds.select(*c_all),
138
+ :args=>col_aliases)
131
139
  end
132
140
  aal = Array(a[:after_load])
133
141
  aal << proc do |m, ancs|
@@ -163,9 +171,9 @@ module Sequel
163
171
  end
164
172
  r = model.association_reflection(ancestors)
165
173
  base_case = model.filter(prkey=>id_map.keys).
166
- select(SQL::AliasedExpression.new(prkey, ka), c_all)
174
+ select(SQL::AliasedExpression.new(prkey, ka), *c_all)
167
175
  recursive_case = model.join(t, key=>prkey).
168
- select(SQL::QualifiedIdentifier.new(t, ka), c_all)
176
+ select(SQL::QualifiedIdentifier.new(t, ka), *c_all)
169
177
  if c = r[:conditions]
170
178
  (base_case, recursive_case) = [base_case, recursive_case].collect do |ds|
171
179
  (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
@@ -175,7 +183,8 @@ module Sequel
175
183
  elds = model.eager_loading_dataset(r,
176
184
  model.from(t => table_alias).
177
185
  with_recursive(t, base_case,
178
- recursive_case),
186
+ recursive_case,
187
+ :args=>(([ka] + col_aliases) if col_aliases)),
179
188
  r.select,
180
189
  eo[:associations], eo)
181
190
  elds = elds.select_append(ka) unless elds.opts[:select] == nil
@@ -192,7 +201,9 @@ module Sequel
192
201
  (children_map[obj[key]] ||= []) << obj
193
202
  end
194
203
 
195
- if roots = id_map[obj.values.delete(ka)]
204
+ kv = obj.values.delete(ka)
205
+ kv = kv.to_i if conv_bd && kv.is_a?(BigDecimal)
206
+ if roots = id_map[kv]
196
207
  roots.each do |root|
197
208
  root.associations[ancestors] << obj
198
209
  end
@@ -222,9 +233,9 @@ module Sequel
222
233
  end
223
234
  table_alias = model.dataset.schema_and_table(model.table_name)[1].to_sym
224
235
  model.from(t => table_alias).
225
- with_recursive(t, base_ds.select_all,
226
- recursive_ds.
227
- select(SQL::ColumnAll.new(model.table_name)))
236
+ with_recursive(t, col_aliases ? base_ds.select(*col_aliases) : base_ds.select_all,
237
+ recursive_ds.select(*c_all),
238
+ :args=>col_aliases)
228
239
  end
229
240
  dal = Array(d[:after_load])
230
241
  dal << proc do |m, descs|
@@ -259,9 +270,9 @@ module Sequel
259
270
  end
260
271
  r = model.association_reflection(descendants)
261
272
  base_case = model.filter(key=>id_map.keys).
262
- select(SQL::AliasedExpression.new(key, ka), c_all)
273
+ select(SQL::AliasedExpression.new(key, ka), *c_all)
263
274
  recursive_case = model.join(t, prkey=>key).
264
- select(SQL::QualifiedIdentifier.new(t, ka), c_all)
275
+ select(SQL::QualifiedIdentifier.new(t, ka), *c_all)
265
276
  if c = r[:conditions]
266
277
  (base_case, recursive_case) = [base_case, recursive_case].collect do |ds|
267
278
  (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
@@ -276,7 +287,8 @@ module Sequel
276
287
  end
277
288
  table_alias = model.dataset.schema_and_table(model.table_name)[1].to_sym
278
289
  elds = model.eager_loading_dataset(r,
279
- model.from(t => table_alias).with_recursive(t, base_case, recursive_case),
290
+ model.from(t => table_alias).with_recursive(t, base_case, recursive_case,
291
+ :args=>(([ka] + col_aliases + (level ? [la] : [])) if col_aliases)),
280
292
  r.select,
281
293
  associations, eo)
282
294
  elds = elds.select_append(ka) unless elds.opts[:select] == nil
@@ -296,7 +308,9 @@ module Sequel
296
308
  parent_map[opk] = obj
297
309
  end
298
310
 
299
- if root = id_map[obj.values.delete(ka)].first
311
+ kv = obj.values.delete(ka)
312
+ kv = kv.to_i if conv_bd && kv.is_a?(BigDecimal)
313
+ if root = id_map[kv].first
300
314
  root.associations[descendants] << obj
301
315
  end
302
316
 
@@ -11,8 +11,17 @@ module Sequel
11
11
  # set the @deserialized_values entry. This plugin adds a before_save hook
12
12
  # that serializes all @deserialized_values to @values.
13
13
  #
14
- # You can use either marshal, yaml, or json as the serialization format.
15
- # If you use yaml or json, you should require them by yourself.
14
+ # You can specify the serialization format as a pair of serializer/deserializer
15
+ # callable objects. You can also specify the serialization format as a single
16
+ # symbol, if such a symbol has a registered serializer/deserializer pair in the
17
+ # plugin. By default, the plugin registers the :marshal, :yaml, and :json
18
+ # serialization formats. To register your own serialization formats, use
19
+ # Sequel::Plugins::Serialization.register_format.
20
+ # If you use yaml or json format, you need to require the libraries, Sequel
21
+ # does not do the requiring for you.
22
+ #
23
+ # You can specify the columns to serialize when loading the plugin, or later
24
+ # using the serialize_attributes class method.
16
25
  #
17
26
  # Because of how this plugin works, it must be used inside each model class
18
27
  # that needs serialization, after any set_dataset method calls in that class.
@@ -22,56 +31,96 @@ module Sequel
22
31
  # == Example
23
32
  #
24
33
  # require 'sequel'
34
+ # # Require json, as the plugin doesn't require it for you.
25
35
  # require 'json'
36
+ #
37
+ # # Register custom serializer/deserializer pair
38
+ # Sequel::Plugins::Serialization.register_format(:reverse,
39
+ # lambda{|v| v.reverse},
40
+ # lambda{|v| v.reverse})
41
+ #
26
42
  # class User < Sequel::Model
43
+ # # Built-in format support when loading the plugin
27
44
  # plugin :serialization, :json, :permissions
28
- # # or
45
+ #
46
+ # # Built-in format support after loading the plugin using serialize_attributes
29
47
  # plugin :serialization
30
- # serialize_attributes :marshal, :permissions, :attributes
48
+ # serialize_attributes :marshal, :permissions
49
+ #
50
+ # # Use custom registered serialization format just like built-in format
51
+ # serialize_attributes :reverse, :password
52
+ #
53
+ # # Use a custom serializer/deserializer pair without registering
54
+ # serialize_attributes [lambda{|v| v.reverse}, lambda{|v| v.reverse}], :password
31
55
  # end
32
56
  # user = User.create
33
57
  # user.permissions = { :global => 'read-only' }
34
58
  # user.save
35
59
  module Serialization
60
+ # The default serializers supported by the serialization module.
61
+ # Use register_format to add serializers to this hash.
62
+ REGISTERED_FORMATS = {}
63
+
36
64
  # Set up the column readers to do deserialization and the column writers
37
65
  # to save the value in deserialized_values.
38
66
  def self.apply(model, *args)
39
- model.instance_eval{@serialization_map = {}}
67
+ model.instance_eval do
68
+ @deserialization_map = {}
69
+ @serialization_map = {}
70
+ end
40
71
  end
41
72
 
73
+ # Automatically call serialize_attributes with the format and columns unless
74
+ # no columns were provided.
42
75
  def self.configure(model, format=nil, *columns)
43
76
  model.serialize_attributes(format, *columns) unless columns.empty?
44
77
  end
45
78
 
79
+ # Register a serializer/deserializer pair with a format symbol, to allow
80
+ # models to pick this format by name. Both serializer and deserializer
81
+ # should be callable objects.
82
+ def self.register_format(format, serializer, deserializer)
83
+ REGISTERED_FORMATS[format] = [serializer, deserializer]
84
+ end
85
+ register_format(:marshal, lambda{|v| [Marshal.dump(v)].pack('m')}, lambda{|v| Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)})
86
+ register_format(:yaml, lambda{|v| v.to_yaml}, lambda{|v| YAML.load(v)})
87
+ register_format(:json, lambda{|v| v.to_json}, lambda{|v| JSON.parse(v)})
88
+
46
89
  module ClassMethods
47
- # A map of the serialized columns for this model. Keys are column
48
- # symbols, values are serialization formats (:marshal, :yaml, or :json).
90
+ # A hash with column name symbols and callable values, with the value
91
+ # called to deserialize the column.
92
+ attr_reader :deserialization_map
93
+
94
+ # A hash with column name symbols and callable values, with the value
95
+ # called to serialize the column.
49
96
  attr_reader :serialization_map
50
97
 
51
98
  # Module to store the serialized column accessor methods, so they can
52
99
  # call be overridden and call super to get the serialization behavior
53
100
  attr_accessor :serialization_module
54
101
 
55
- # Copy the serialization format and columns to serialize into the subclass.
102
+ # Copy the serialization_map and deserialization map into the subclass.
56
103
  def inherited(subclass)
57
104
  super
58
105
  sm = serialization_map.dup
59
- subclass.instance_eval{@serialization_map = sm}
60
- end
61
-
62
- # The first value in the serialization map. This is only for
63
- # backwards compatibility, use serialization_map in new code.
64
- def serialization_format
65
- serialization_map.values.first
106
+ dsm = deserialization_map.dup
107
+ subclass.instance_eval do
108
+ @deserialization_map = dsm
109
+ @serialization_map = sm
110
+ end
66
111
  end
67
112
 
68
113
  # Create instance level reader that deserializes column values on request,
69
- # and instance level writer that stores new deserialized value in deserialized
70
- # columns
114
+ # and instance level writer that stores new deserialized values.
71
115
  def serialize_attributes(format, *columns)
72
- raise(Error, "Unsupported serialization format (#{format}), should be :marshal, :yaml, or :json") unless [:marshal, :yaml, :json].include?(format)
116
+ if format.is_a?(Symbol)
117
+ unless format = REGISTERED_FORMATS[format]
118
+ raise(Error, "Unsupported serialization format: #{format} (valid formats: #{REGISTERED_FORMATS.keys.map{|k| k.inspect}.join})")
119
+ end
120
+ end
121
+ serializer, deserializer = format
73
122
  raise(Error, "No columns given. The serialization plugin requires you specify which columns to serialize") if columns.empty?
74
- define_serialized_attribute_accessor(format, *columns)
123
+ define_serialized_attribute_accessor(serializer, deserializer, *columns)
75
124
  end
76
125
 
77
126
  # The columns that will be serialized. This is only for
@@ -83,12 +132,13 @@ module Sequel
83
132
  private
84
133
 
85
134
  # Add serializated attribute acessor methods to the serialization_module
86
- def define_serialized_attribute_accessor(format, *columns)
135
+ def define_serialized_attribute_accessor(serializer, deserializer, *columns)
87
136
  m = self
88
137
  include(self.serialization_module ||= Module.new) unless serialization_module
89
138
  serialization_module.class_eval do
90
139
  columns.each do |column|
91
- m.serialization_map[column] = format
140
+ m.serialization_map[column] = serializer
141
+ m.deserialization_map[column] = deserializer
92
142
  define_method(column) do
93
143
  if deserialized_values.has_key?(column)
94
144
  deserialized_values[column]
@@ -110,52 +160,51 @@ module Sequel
110
160
  attr_reader :deserialized_values
111
161
 
112
162
  # Set @deserialized_values to the empty hash
113
- def initialize(*args, &block)
163
+ def initialize_set(values)
114
164
  @deserialized_values = {}
115
165
  super
116
166
  end
117
167
 
118
- # Serialize all deserialized values
168
+ # Serialize deserialized values before saving
119
169
  def before_save
120
- deserialized_values.each{|k,v| @values[k] = serialize_value(k, v)}
170
+ serialize_deserialized_values
121
171
  super
122
172
  end
123
173
 
124
- # Empty the deserialized values when refreshing.
125
- def refresh
126
- @deserialized_values = {}
174
+ # Initialization the deserialized values for objects retrieved from the database.
175
+ def set_values(*)
176
+ @deserialized_values ||= {}
127
177
  super
128
178
  end
129
179
 
130
180
  private
131
181
 
132
- # Deserialize the column from either marshal or yaml format
182
+ # Empty the deserialized values when refreshing.
183
+ def _refresh(*)
184
+ @deserialized_values = {}
185
+ super
186
+ end
187
+
188
+ # Deserialize the column value. Called when the model column accessor is called to
189
+ # return a deserialized value.
133
190
  def deserialize_value(column, v)
134
- return v if v.nil?
135
- case model.serialization_map[column]
136
- when :marshal
137
- Marshal.load(v.unpack('m')[0]) rescue Marshal.load(v)
138
- when :yaml
139
- YAML.load v if v
140
- when :json
141
- JSON.parse v if v
142
- else
143
- raise Error, "Bad serialization format (#{model.serialization_map[column].inspect}) for column #{column.inspect}"
191
+ unless v.nil?
192
+ raise Sequel::Error, "no entry in deserialization_map for #{column.inspect}" unless callable = model.deserialization_map[column]
193
+ callable.call(v)
144
194
  end
145
195
  end
146
196
 
147
- # Serialize the column to either marshal or yaml format
197
+ # Serialize all deserialized values
198
+ def serialize_deserialized_values
199
+ deserialized_values.each{|k,v| @values[k] = serialize_value(k, v)}
200
+ end
201
+
202
+ # Serialize the column value. Called before saving to ensure the serialized value
203
+ # is saved in the database.
148
204
  def serialize_value(column, v)
149
- return v if v.nil?
150
- case model.serialization_map[column]
151
- when :marshal
152
- [Marshal.dump(v)].pack('m')
153
- when :yaml
154
- v.to_yaml
155
- when :json
156
- v.to_json
157
- else
158
- raise Error, "Bad serialization format (#{model.serialization_map[column].inspect}) for column #{column.inspect}"
205
+ unless v.nil?
206
+ raise Sequel::Error, "no entry in serialization_map for #{column.inspect}" unless callable = model.serialization_map[column]
207
+ callable.call(v)
159
208
  end
160
209
  end
161
210
  end
@@ -0,0 +1,63 @@
1
+ module Sequel
2
+ module Plugins
3
+ # This plugin extends the serialization plugin and enables it to detect
4
+ # changes in serialized values by checking whether the current
5
+ # deserialized value is the same as the original deserialized value.
6
+ # The serialization plugin does not do such checks by default, as they
7
+ # often aren't needed and can hurt performance.
8
+ #
9
+ # Note that for this plugin to work correctly, the values you are
10
+ # serializing must roundtrip correctly (i.e. deserialize(serialize(value))
11
+ # should equal value). This is true in most cases, but not in all. For
12
+ # example, ruby symbols round trip through yaml, but not json (as they get
13
+ # turned into strings in json).
14
+ #
15
+ # == Example
16
+ #
17
+ # require 'sequel'
18
+ # require 'json'
19
+ # class User < Sequel::Model
20
+ # plugin :serialization, :json, :permissions
21
+ # plugin :serialization_modification_detection
22
+ # end
23
+ # user = User.create(:permissions => {})
24
+ # user.permissions[:global] = 'read-only'
25
+ # user.save_changes
26
+ module SerializationModificationDetection
27
+ # Load the serialization plugin automatically.
28
+ def self.apply(model)
29
+ model.plugin :serialization
30
+ end
31
+
32
+ module InstanceMethods
33
+ # Clear the cache of original deserialized values after saving so that it doesn't
34
+ # show the column is modified after saving.
35
+ def after_save
36
+ super
37
+ @original_deserialized_values = {}
38
+ end
39
+
40
+ # Detect which serialized columns have changed.
41
+ def changed_columns
42
+ cc = super
43
+ deserialized_values.each{|c, v| cc << c if !cc.include?(c) && original_deserialized_value(c) != v}
44
+ cc
45
+ end
46
+
47
+ private
48
+
49
+ # For new objects, serialize any existing deserialized values so that changes can
50
+ # be detected.
51
+ def initialize_set(values)
52
+ super
53
+ serialize_deserialized_values
54
+ end
55
+
56
+ # Return the original deserialized value of the column, caching it to improve performance.
57
+ def original_deserialized_value(column)
58
+ (@original_deserialized_values ||= {})[column] ||= deserialize_value(column, self[column])
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,13 +1,14 @@
1
1
  module Sequel
2
2
  module Plugins
3
- # The sharding plugin makes it easy to use Sequel's sharding features
4
- # with models. It lets you create model objects on specific shards,
5
- # and any models retrieved from specific shards are automatically
6
- # saved back to those shards. It also works with associations,
7
- # so that model objects retrieved from specific shards will only
8
- # return associated objects from that shard, and using the
9
- # add/remove/remove_all association methods will only affect
10
- # that shard.
3
+ # The sharding plugin augments Sequel's default model sharding support
4
+ # in the following ways:
5
+ #
6
+ # 1) It automatically sets model instances to be saved back to the
7
+ # shard they were retreived from.
8
+ # 2) It makes model associations use the same shard as the model
9
+ # object.
10
+ # 3) It adds a slightly nicer API for creating model instances on
11
+ # specific shards.
11
12
  #
12
13
  # Usage:
13
14
  #
@@ -40,15 +41,21 @@ module Sequel
40
41
  def new_using_server(s, values={}, &block)
41
42
  new(values, &block).set_server(s)
42
43
  end
43
- end
44
44
 
45
- module InstanceMethods
46
- # Set the shard that this object is tied to. Returns self.
47
- def set_server(s)
48
- @server = s
49
- self
45
+ private
46
+
47
+ # Set the server for each graphed dataset to the current server
48
+ # unless the graphed dataset already has a server set.
49
+ def eager_graph_dataset(opts, eager_options)
50
+ ds = super
51
+ if s = eager_options[:self].opts[:server]
52
+ ds = ds.server(s) unless ds.opts[:server]
53
+ end
54
+ ds
50
55
  end
56
+ end
51
57
 
58
+ module InstanceMethods
52
59
  # Set the server that this object is tied to, unless it has
53
60
  # already been set. Returns self.
54
61
  def set_server?(s)
@@ -56,11 +63,6 @@ module Sequel
56
63
  self
57
64
  end
58
65
 
59
- # Ensure that the instance dataset is tied to the correct shard.
60
- def this
61
- use_server(super)
62
- end
63
-
64
66
  private
65
67
 
66
68
  # Ensure that association datasets are tied to the correct shard.
@@ -68,21 +70,11 @@ module Sequel
68
70
  use_server(super)
69
71
  end
70
72
 
71
- # Ensure that the object is inserted into the correct shard.
72
- def _insert_dataset
73
- use_server(super)
74
- end
75
-
76
73
  # Ensure that the join table for many_to_many associations uses the correct shard.
77
74
  def _join_table_dataset(opts)
78
75
  use_server(super)
79
76
  end
80
77
 
81
- # Make sure to use the correct shard when using a transaction
82
- def checked_transaction(opts={}, &block)
83
- super(@server ? {:server=>@server}.merge(opts) : opts, &block)
84
- end
85
-
86
78
  # If creating the object by doing <tt>add_association</tt> for a
87
79
  # +many_to_many+ association, make sure the associated object is created on the
88
80
  # current object's shard, unless the passed object already has an assigned shard.
@@ -90,11 +82,6 @@ module Sequel
90
82
  o.set_server?(@server) if o.respond_to?(:set_server?)
91
83
  super
92
84
  end
93
-
94
- # Set the given dataset to use the current object's shard.
95
- def use_server(ds)
96
- @server ? ds.server(@server) : ds
97
- end
98
85
  end
99
86
 
100
87
  module DatasetMethods
@@ -109,26 +96,6 @@ module Sequel
109
96
  end
110
97
  ds
111
98
  end
112
-
113
- private
114
-
115
- # Set the shard of all retrieved objects to the shard of
116
- # the table's dataset, or the current shard if the table's
117
- # dataset does not have a shard.
118
- def graph_each
119
- ta = @opts[:graph][:table_aliases]
120
- s = @opts[:server]
121
- super do |r|
122
- r.each do |k, v|
123
- if ds = ta[k]
124
- dss = ds.opts[:server]
125
- end
126
- vs = dss || s
127
- v.set_server(vs) if vs && v.respond_to?(:set_server)
128
- end
129
- yield r
130
- end
131
- end
132
99
  end
133
100
  end
134
101
  end
@@ -108,8 +108,10 @@ module Sequel
108
108
  key = skm[subclass]
109
109
  sti_subclass_added(key)
110
110
  ska = [key]
111
+ rp = dataset.row_proc
111
112
  subclass.set_dataset(sd.filter(SQL::QualifiedIdentifier.new(table_name, sk)=>ska), :inherited=>true)
112
113
  subclass.instance_eval do
114
+ dataset.row_proc = rp
113
115
  @sti_key = sk
114
116
  @sti_key_array = ska
115
117
  @sti_dataset = sd
@@ -122,7 +124,7 @@ module Sequel
122
124
  # Return an instance of the class specified by sti_key,
123
125
  # used by the row_proc.
124
126
  def sti_load(r)
125
- sti_class(sti_model_map[r[sti_key]]).load(r)
127
+ sti_class(sti_model_map[r[sti_key]]).call(r)
126
128
  end
127
129
 
128
130
  # Make sure that all subclasses of the parent class correctly include
@@ -5,7 +5,7 @@ module Sequel
5
5
  # typecast correctly (with correct being defined as how the model object
6
6
  # would typecast the same column values).
7
7
  #
8
- # This plugin modifies Model.load to call the setter methods (which typecast
8
+ # This plugin modifies Model#set_values to call the setter methods (which typecast
9
9
  # by default) for all columns given. You can either specify the columns to
10
10
  # typecast on load in the plugin call itself, or afterwards using
11
11
  # add_typecast_on_load_columns:
@@ -41,16 +41,11 @@ module Sequel
41
41
  super
42
42
  subclass.instance_variable_set(:@typecast_on_load_columns, typecast_on_load_columns.dup)
43
43
  end
44
-
45
- # Call the setter method for each of the typecast on load columns,
46
- # ensuring the model object will have the correct typecasting even
47
- # if the database doesn't typecast the columns correctly.
48
- def load(values)
49
- super.load_typecast
50
- end
51
44
  end
52
45
 
53
46
  module InstanceMethods
47
+ # Call the setter method for each of the model's typecast_on_load_columns
48
+ # with the current value, so it can be typecasted correctly.
54
49
  def load_typecast
55
50
  model.typecast_on_load_columns.each do |c|
56
51
  if v = values[c]
@@ -61,10 +56,12 @@ module Sequel
61
56
  self
62
57
  end
63
58
 
64
- private
65
-
66
- def _refresh(dataset)
67
- super.load_typecast
59
+ # Typecast values using #load_typecast when the values are retrieved from
60
+ # the database.
61
+ def set_values(values)
62
+ ret = super
63
+ load_typecast
64
+ ret
68
65
  end
69
66
  end
70
67
  end
@@ -1,6 +1,6 @@
1
1
  module Sequel
2
2
  module Plugins
3
- # The update_primary_key plugin allows you to modify an objects
3
+ # The update_primary_key plugin allows you to modify an object's
4
4
  # primary key and then save the record. Sequel does not work
5
5
  # correctly with primary key modifications by default. Sequel
6
6
  # is designed to work with surrogate primary keys that never need to be
@@ -22,7 +22,7 @@ module Sequel
22
22
  module UpdatePrimaryKey
23
23
  module ClassMethods
24
24
  # Cache the pk_hash when loading records
25
- def load(h)
25
+ def call(h)
26
26
  r = super(h)
27
27
  r.pk_hash
28
28
  r
@@ -205,9 +205,9 @@ module Sequel
205
205
  klass.from_xml_node(node)
206
206
  end
207
207
  elsif cols.include?(k)
208
- self[k.to_sym] = node[:nil] ? nil : node.children.first.to_s
208
+ self[k.to_sym] = node.key?('nil') ? nil : node.children.first.to_s
209
209
  elsif meths.include?("#{k}=")
210
- send("#{k}=", node[:nil] ? nil : node.children.first.to_s)
210
+ send("#{k}=", node.key?('nil') ? nil : node.children.first.to_s)
211
211
  else
212
212
  raise Error, "Entry in XML not an association or column and no setter method exists: #{k}"
213
213
  end
@@ -266,7 +266,7 @@ module Sequel
266
266
 
267
267
  name_proc = model.xml_serialize_name_proc(opts)
268
268
  x = model.xml_builder(opts)
269
- x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name)).to_s]) do |x1|
269
+ x.send(name_proc[opts.fetch(:root_name, model.send(:underscore, model.name).gsub('/', '__')).to_s]) do |x1|
270
270
  cols.each do |c|
271
271
  attrs = {}
272
272
  if types