sequel 5.85.0 → 5.93.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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sequel/adapters/ado.rb +1 -1
  3. data/lib/sequel/adapters/ibmdb.rb +1 -0
  4. data/lib/sequel/adapters/jdbc/db2.rb +2 -2
  5. data/lib/sequel/adapters/jdbc/derby.rb +2 -2
  6. data/lib/sequel/adapters/jdbc/h2.rb +2 -2
  7. data/lib/sequel/adapters/jdbc/hsqldb.rb +2 -2
  8. data/lib/sequel/adapters/jdbc/jtds.rb +2 -2
  9. data/lib/sequel/adapters/jdbc/mysql.rb +1 -1
  10. data/lib/sequel/adapters/jdbc/oracle.rb +5 -5
  11. data/lib/sequel/adapters/jdbc/postgresql.rb +5 -5
  12. data/lib/sequel/adapters/jdbc/sqlanywhere.rb +6 -6
  13. data/lib/sequel/adapters/jdbc/sqlite.rb +2 -2
  14. data/lib/sequel/adapters/jdbc/sqlserver.rb +2 -2
  15. data/lib/sequel/adapters/jdbc.rb +8 -8
  16. data/lib/sequel/adapters/mysql2.rb +8 -1
  17. data/lib/sequel/adapters/oracle.rb +16 -0
  18. data/lib/sequel/adapters/shared/access.rb +1 -0
  19. data/lib/sequel/adapters/shared/mssql.rb +4 -3
  20. data/lib/sequel/adapters/shared/mysql.rb +8 -4
  21. data/lib/sequel/adapters/shared/oracle.rb +1 -0
  22. data/lib/sequel/adapters/shared/postgres.rb +140 -9
  23. data/lib/sequel/adapters/sqlite.rb +4 -0
  24. data/lib/sequel/adapters/trilogy.rb +1 -2
  25. data/lib/sequel/core.rb +15 -0
  26. data/lib/sequel/database/dataset_defaults.rb +3 -3
  27. data/lib/sequel/database/misc.rb +17 -4
  28. data/lib/sequel/database/query.rb +11 -11
  29. data/lib/sequel/database/schema_generator.rb +8 -0
  30. data/lib/sequel/dataset/deprecated_singleton_class_methods.rb +1 -1
  31. data/lib/sequel/dataset/prepared_statements.rb +70 -25
  32. data/lib/sequel/dataset/query.rb +9 -5
  33. data/lib/sequel/dataset/sql.rb +19 -9
  34. data/lib/sequel/extensions/connection_validator.rb +15 -10
  35. data/lib/sequel/extensions/migration.rb +23 -3
  36. data/lib/sequel/extensions/null_dataset.rb +2 -2
  37. data/lib/sequel/extensions/pg_auto_parameterize.rb +6 -1
  38. data/lib/sequel/extensions/pg_auto_parameterize_in_array.rb +93 -10
  39. data/lib/sequel/extensions/pg_enum.rb +3 -3
  40. data/lib/sequel/extensions/pg_row.rb +3 -1
  41. data/lib/sequel/extensions/pg_schema_caching.rb +90 -0
  42. data/lib/sequel/extensions/query_blocker.rb +172 -0
  43. data/lib/sequel/extensions/schema_caching.rb +24 -9
  44. data/lib/sequel/extensions/schema_dumper.rb +16 -4
  45. data/lib/sequel/extensions/sqlite_json_ops.rb +1 -1
  46. data/lib/sequel/extensions/string_agg.rb +2 -2
  47. data/lib/sequel/extensions/virtual_row_method_block.rb +1 -0
  48. data/lib/sequel/model/associations.rb +28 -3
  49. data/lib/sequel/model/base.rb +67 -18
  50. data/lib/sequel/plugins/composition.rb +1 -1
  51. data/lib/sequel/plugins/enum.rb +1 -1
  52. data/lib/sequel/plugins/forbid_lazy_load.rb +14 -1
  53. data/lib/sequel/plugins/inspect_pk.rb +44 -0
  54. data/lib/sequel/plugins/instance_filters.rb +4 -1
  55. data/lib/sequel/plugins/inverted_subsets.rb +1 -0
  56. data/lib/sequel/plugins/lazy_attributes.rb +1 -1
  57. data/lib/sequel/plugins/nested_attributes.rb +10 -5
  58. data/lib/sequel/plugins/paged_operations.rb +5 -2
  59. data/lib/sequel/plugins/pg_auto_constraint_validations.rb +6 -1
  60. data/lib/sequel/plugins/pg_auto_validate_enums.rb +88 -0
  61. data/lib/sequel/plugins/pg_eager_any_typed_array.rb +95 -0
  62. data/lib/sequel/plugins/rcte_tree.rb +1 -1
  63. data/lib/sequel/plugins/serialization.rb +11 -5
  64. data/lib/sequel/plugins/sql_comments.rb +7 -2
  65. data/lib/sequel/plugins/static_cache_cache.rb +50 -13
  66. data/lib/sequel/plugins/subset_conditions.rb +85 -5
  67. data/lib/sequel/plugins/subset_static_cache.rb +263 -0
  68. data/lib/sequel/sql.rb +15 -6
  69. data/lib/sequel/version.rb +1 -1
  70. metadata +9 -6
@@ -0,0 +1,263 @@
1
+ # frozen-string-literal: true
2
+
3
+ module Sequel
4
+ module Plugins
5
+ # The subset_static_cache plugin is designed for model subsets that are not modified at all
6
+ # in production use cases, or at least where modifications to them would usually
7
+ # coincide with an application restart. When caching a model subset, it
8
+ # retrieves all rows in the database and statically caches a ruby array and hash
9
+ # keyed on primary key containing all of the model instances. All of these cached
10
+ # instances are frozen so they won't be modified unexpectedly.
11
+ #
12
+ # With the following code:
13
+ #
14
+ # class StatusType < Sequel::Model
15
+ # dataset_module do
16
+ # where :available, hidden: false
17
+ # end
18
+ # cache_subset :available
19
+ # end
20
+ #
21
+ # The following methods will use the cache and not issue a database query:
22
+ #
23
+ # * StatusType.available.with_pk
24
+ # * StatusType.available.all
25
+ # * StatusType.available.each
26
+ # * StatusType.available.first (without block, only supporting no arguments or single integer argument)
27
+ # * StatusType.available.count (without an argument or block)
28
+ # * StatusType.available.map
29
+ # * StatusType.available.as_hash
30
+ # * StatusType.available.to_hash
31
+ # * StatusType.available.to_hash_groups
32
+ #
33
+ # The cache is not used if you chain methods before or after calling the cached
34
+ # method, as doing so would not be safe:
35
+ #
36
+ # StatusType.where{number > 1}.available.all
37
+ # StatusType.available.where{number > 1}.all
38
+ #
39
+ # The cache is also not used if you change the class's dataset after caching
40
+ # the subset, or in subclasses of the model.
41
+ #
42
+ # You should not modify any row that is statically cached when using this plugin,
43
+ # as otherwise you will get different results for cached and uncached method
44
+ # calls.
45
+ module SubsetStaticCache
46
+ def self.configure(model)
47
+ model.class_exec do
48
+ @subset_static_caches ||= ({}.compare_by_identity)
49
+ end
50
+ end
51
+
52
+ module ClassMethods
53
+ # Cache the given subset statically, so that calling the subset method on
54
+ # the model will return a dataset that will return cached results instead
55
+ # of issuing database queries (assuming the cache has the necessary
56
+ # information).
57
+ #
58
+ # The model must already respond to the given method before cache_subset
59
+ # is called.
60
+ def cache_subset(meth)
61
+ ds = send(meth).with_extend(CachedDatasetMethods)
62
+ cache = ds.instance_variable_get(:@cache)
63
+
64
+ rows, hash = subset_static_cache_rows(ds, meth)
65
+ cache[:subset_static_cache_all] = rows
66
+ cache[:subset_static_cache_map] = hash
67
+
68
+ caches = @subset_static_caches
69
+ caches[meth] = ds
70
+ model = self
71
+ subset_static_cache_module.send(:define_method, meth) do
72
+ if (model == self) && (cached_dataset = caches[meth])
73
+ cached_dataset
74
+ else
75
+ super()
76
+ end
77
+ end
78
+ nil
79
+ end
80
+
81
+ Plugins.after_set_dataset(self, :clear_subset_static_caches)
82
+ Plugins.inherited_instance_variables(self, :@subset_static_caches=>proc{{}.compare_by_identity})
83
+
84
+ private
85
+
86
+ # Clear the subset_static_caches. This is used if the model dataset
87
+ # changes, to prevent cached values from being used.
88
+ def clear_subset_static_caches
89
+ @subset_static_caches.clear
90
+ end
91
+
92
+ # A module for the subset static cache methods, so that you can define
93
+ # a singleton method in the class with the same name, and call super
94
+ # to get default behavior.
95
+ def subset_static_cache_module
96
+ return @subset_static_cache_module if @subset_static_cache_module
97
+
98
+ # Ensure dataset_methods module is defined and class is extended with
99
+ # it before calling creating this module.
100
+ dataset_methods_module
101
+
102
+ mod_name = "#{name}::@subset_static_cache_module"
103
+ Sequel.synchronize{@subset_static_cache_module ||= Sequel.set_temp_name(Module.new){mod_name}}
104
+ extend(@subset_static_cache_module)
105
+ @subset_static_cache_module
106
+ end
107
+
108
+ # Return the frozen array and hash used for caching the subset
109
+ # of the given dataset.
110
+ def subset_static_cache_rows(ds, meth)
111
+ all = load_subset_static_cache_rows(ds, meth)
112
+ h = {}
113
+ all.each do |o|
114
+ o.errors.freeze
115
+ h[o.pk.freeze] = o.freeze
116
+ end
117
+ [all, h.freeze]
118
+ end
119
+
120
+ # Return a frozen array for all rows in the dataset.
121
+ def load_subset_static_cache_rows(ds, meth)
122
+ ret = super if defined?(super)
123
+ ret || ds.all.freeze
124
+ end
125
+ end
126
+
127
+ module CachedDatasetMethods
128
+ # An array of all of the dataset's instances, without issuing a database
129
+ # query. If a block is given, yields each instance to the block.
130
+ def all(&block)
131
+ return super unless all = @cache[:subset_static_cache_all]
132
+
133
+ array = all.dup
134
+ array.each(&block) if block
135
+ array
136
+ end
137
+
138
+ # Get the number of records in the cache, without issuing a database query,
139
+ # if no arguments or block are provided.
140
+ def count(*a, &block)
141
+ if a.empty? && !block && (all = @cache[:subset_static_cache_all])
142
+ all.size
143
+ else
144
+ super
145
+ end
146
+ end
147
+
148
+ # If a block is given, multiple arguments are given, or a single
149
+ # non-Integer argument is given, performs the default behavior of
150
+ # issuing a database query. Otherwise, uses the cached values
151
+ # to return either the first cached instance (no arguments) or an
152
+ # array containing the number of instances specified (single integer
153
+ # argument).
154
+ def first(*args)
155
+ if !defined?(yield) && args.length <= 1 && (args.length == 0 || args[0].is_a?(Integer)) && (all = @cache[:subset_static_cache_all])
156
+ all.first(*args)
157
+ else
158
+ super
159
+ end
160
+ end
161
+
162
+ # Return the frozen object with the given pk, or nil if no such object exists
163
+ # in the cache, without issuing a database query.
164
+ def with_pk(pk)
165
+ if cache = @cache[:subset_static_cache_map]
166
+ cache[pk]
167
+ else
168
+ super
169
+ end
170
+ end
171
+
172
+ # Yield each of the dataset's frozen instances to the block, without issuing a database
173
+ # query.
174
+ def each(&block)
175
+ return super unless all = @cache[:subset_static_cache_all]
176
+ all.each(&block)
177
+ end
178
+
179
+ # Use the cache instead of a query to get the results.
180
+ def map(column=nil, &block)
181
+ return super unless all = @cache[:subset_static_cache_all]
182
+ if column
183
+ raise(Error, "Cannot provide both column and block to map") if block
184
+ if column.is_a?(Array)
185
+ all.map{|r| r.values.values_at(*column)}
186
+ else
187
+ all.map{|r| r[column]}
188
+ end
189
+ else
190
+ all.map(&block)
191
+ end
192
+ end
193
+
194
+ # Use the cache instead of a query to get the results if possible
195
+ def as_hash(key_column = nil, value_column = nil, opts = OPTS)
196
+ return super unless all = @cache[:subset_static_cache_all]
197
+
198
+ if key_column.nil? && value_column.nil?
199
+ if opts[:hash]
200
+ key_column = model.primary_key
201
+ else
202
+ return Hash[@cache[:subset_static_cache_map]]
203
+ end
204
+ end
205
+
206
+ h = opts[:hash] || {}
207
+ if value_column
208
+ if value_column.is_a?(Array)
209
+ if key_column.is_a?(Array)
210
+ all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)}
211
+ else
212
+ all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)}
213
+ end
214
+ else
215
+ if key_column.is_a?(Array)
216
+ all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]}
217
+ else
218
+ all.each{|r| h[r[key_column]] = r[value_column]}
219
+ end
220
+ end
221
+ elsif key_column.is_a?(Array)
222
+ all.each{|r| h[r.values.values_at(*key_column)] = r}
223
+ else
224
+ all.each{|r| h[r[key_column]] = r}
225
+ end
226
+ h
227
+ end
228
+
229
+ # Alias of as_hash for backwards compatibility.
230
+ def to_hash(*a)
231
+ as_hash(*a)
232
+ end
233
+
234
+ # Use the cache instead of a query to get the results
235
+ def to_hash_groups(key_column, value_column = nil, opts = OPTS)
236
+ return super unless all = @cache[:subset_static_cache_all]
237
+
238
+ h = opts[:hash] || {}
239
+ if value_column
240
+ if value_column.is_a?(Array)
241
+ if key_column.is_a?(Array)
242
+ all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)}
243
+ else
244
+ all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)}
245
+ end
246
+ else
247
+ if key_column.is_a?(Array)
248
+ all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]}
249
+ else
250
+ all.each{|r| (h[r[key_column]] ||= []) << r[value_column]}
251
+ end
252
+ end
253
+ elsif key_column.is_a?(Array)
254
+ all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r}
255
+ else
256
+ all.each{|r| (h[r[key_column]] ||= []) << r}
257
+ end
258
+ h
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
data/lib/sequel/sql.rb CHANGED
@@ -1127,7 +1127,13 @@ module Sequel
1127
1127
  when DelayedEvaluation
1128
1128
  Sequel.delay{|ds| from_value_pair(l, r.call(ds))}
1129
1129
  when Dataset::PlaceholderLiteralizer::Argument
1130
- r.transform{|v| from_value_pair(l, v)}
1130
+ prev_transform = r.instance_variable_get(:@transformer)
1131
+ r.transform do |v|
1132
+ if prev_transform
1133
+ v = prev_transform.call(v)
1134
+ end
1135
+ from_value_pair(l, v)
1136
+ end
1131
1137
  else
1132
1138
  new(:'=', l, r)
1133
1139
  end
@@ -1727,12 +1733,11 @@ module Sequel
1727
1733
 
1728
1734
  # Automatically convert SQL::Identifiers to strings
1729
1735
  def convert_identifier(identifier)
1730
- case identifier
1731
- when SQL::Identifier
1732
- identifier.value.to_s
1733
- else
1734
- identifier
1736
+ if identifier.is_a?(SQL::Identifier)
1737
+ identifier = identifier.value
1738
+ identifier = identifier.to_s unless identifier.is_a?(LiteralString)
1735
1739
  end
1740
+ identifier
1736
1741
  end
1737
1742
  end
1738
1743
 
@@ -1915,6 +1920,7 @@ module Sequel
1915
1920
  end
1916
1921
 
1917
1922
  m = Module.new do
1923
+ Sequel.set_temp_name(Module.new){"Sequel::SQL::VirtualRow::_BaseMethodMissing"}
1918
1924
  # Return an +Identifier+, +QualifiedIdentifier+, or +Function+, depending
1919
1925
  # on arguments and whether a block is provided. Does not currently call the block.
1920
1926
  # See the class level documentation.
@@ -2046,6 +2052,9 @@ module Sequel
2046
2052
  end
2047
2053
  end
2048
2054
 
2055
+ SQL::Constants::OLD = SQL::Identifier.new(LiteralString.new("OLD").freeze)
2056
+ SQL::Constants::NEW = SQL::Identifier.new(LiteralString.new("NEW").freeze)
2057
+
2049
2058
  include SQL::Constants
2050
2059
  extend SQL::Builders
2051
2060
  extend SQL::OperatorBuilders
@@ -6,7 +6,7 @@ module Sequel
6
6
 
7
7
  # The minor version of Sequel. Bumped for every non-patch level
8
8
  # release, generally around once a month.
9
- MINOR = 85
9
+ MINOR = 93
10
10
 
11
11
  # The tiny version of Sequel. Usually 0, only bumped for bugfix
12
12
  # releases that fix regressions from previous versions.
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.85.0
4
+ version: 5.93.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-10-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bigdecimal
@@ -268,11 +267,13 @@ files:
268
267
  - lib/sequel/extensions/pg_range_ops.rb
269
268
  - lib/sequel/extensions/pg_row.rb
270
269
  - lib/sequel/extensions/pg_row_ops.rb
270
+ - lib/sequel/extensions/pg_schema_caching.rb
271
271
  - lib/sequel/extensions/pg_static_cache_updater.rb
272
272
  - lib/sequel/extensions/pg_timestamptz.rb
273
273
  - lib/sequel/extensions/pretty_table.rb
274
274
  - lib/sequel/extensions/provenance.rb
275
275
  - lib/sequel/extensions/query.rb
276
+ - lib/sequel/extensions/query_blocker.rb
276
277
  - lib/sequel/extensions/round_timestamps.rb
277
278
  - lib/sequel/extensions/run_transaction_hooks.rb
278
279
  - lib/sequel/extensions/s.rb
@@ -353,6 +354,7 @@ files:
353
354
  - lib/sequel/plugins/input_transformer.rb
354
355
  - lib/sequel/plugins/insert_conflict.rb
355
356
  - lib/sequel/plugins/insert_returning_select.rb
357
+ - lib/sequel/plugins/inspect_pk.rb
356
358
  - lib/sequel/plugins/instance_filters.rb
357
359
  - lib/sequel/plugins/instance_hooks.rb
358
360
  - lib/sequel/plugins/instance_specific_default.rb
@@ -369,6 +371,8 @@ files:
369
371
  - lib/sequel/plugins/paged_operations.rb
370
372
  - lib/sequel/plugins/pg_array_associations.rb
371
373
  - lib/sequel/plugins/pg_auto_constraint_validations.rb
374
+ - lib/sequel/plugins/pg_auto_validate_enums.rb
375
+ - lib/sequel/plugins/pg_eager_any_typed_array.rb
372
376
  - lib/sequel/plugins/pg_row.rb
373
377
  - lib/sequel/plugins/pg_xmin_optimistic_locking.rb
374
378
  - lib/sequel/plugins/prepared_statements.rb
@@ -390,6 +394,7 @@ files:
390
394
  - lib/sequel/plugins/string_stripper.rb
391
395
  - lib/sequel/plugins/subclasses.rb
392
396
  - lib/sequel/plugins/subset_conditions.rb
397
+ - lib/sequel/plugins/subset_static_cache.rb
393
398
  - lib/sequel/plugins/table_select.rb
394
399
  - lib/sequel/plugins/tactical_eager_loading.rb
395
400
  - lib/sequel/plugins/throw_failures.rb
@@ -422,7 +427,6 @@ metadata:
422
427
  documentation_uri: https://sequel.jeremyevans.net/documentation.html
423
428
  mailing_list_uri: https://github.com/jeremyevans/sequel/discussions
424
429
  source_code_uri: https://github.com/jeremyevans/sequel
425
- post_install_message:
426
430
  rdoc_options:
427
431
  - "--quiet"
428
432
  - "--line-numbers"
@@ -444,8 +448,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
444
448
  - !ruby/object:Gem::Version
445
449
  version: '0'
446
450
  requirements: []
447
- rubygems_version: 3.5.16
448
- signing_key:
451
+ rubygems_version: 3.6.7
449
452
  specification_version: 4
450
453
  summary: The Database Toolkit for Ruby
451
454
  test_files: []