sequel 5.87.0 → 5.90.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.
- checksums.yaml +4 -4
- data/lib/sequel/adapters/ibmdb.rb +1 -0
- data/lib/sequel/adapters/mysql2.rb +8 -1
- data/lib/sequel/adapters/shared/access.rb +1 -0
- data/lib/sequel/adapters/shared/mssql.rb +1 -0
- data/lib/sequel/adapters/shared/oracle.rb +1 -0
- data/lib/sequel/adapters/shared/postgres.rb +34 -4
- data/lib/sequel/core.rb +15 -0
- data/lib/sequel/database/dataset_defaults.rb +3 -3
- data/lib/sequel/database/misc.rb +5 -1
- data/lib/sequel/database/schema_generator.rb +8 -0
- data/lib/sequel/dataset/deprecated_singleton_class_methods.rb +1 -1
- data/lib/sequel/dataset/prepared_statements.rb +2 -1
- data/lib/sequel/dataset/query.rb +2 -2
- data/lib/sequel/dataset/sql.rb +6 -1
- data/lib/sequel/extensions/connection_validator.rb +15 -10
- data/lib/sequel/extensions/migration.rb +19 -3
- data/lib/sequel/extensions/null_dataset.rb +2 -2
- data/lib/sequel/extensions/pg_auto_parameterize_in_array.rb +9 -4
- data/lib/sequel/extensions/pg_enum.rb +3 -3
- data/lib/sequel/extensions/pg_row.rb +3 -1
- data/lib/sequel/extensions/query_blocker.rb +172 -0
- data/lib/sequel/extensions/string_agg.rb +2 -2
- data/lib/sequel/extensions/virtual_row_method_block.rb +1 -0
- data/lib/sequel/model/associations.rb +19 -2
- data/lib/sequel/model/base.rb +29 -12
- data/lib/sequel/plugins/composition.rb +1 -1
- data/lib/sequel/plugins/enum.rb +1 -1
- data/lib/sequel/plugins/inverted_subsets.rb +1 -0
- data/lib/sequel/plugins/lazy_attributes.rb +1 -1
- data/lib/sequel/plugins/nested_attributes.rb +1 -1
- data/lib/sequel/plugins/pg_eager_any_typed_array.rb +95 -0
- data/lib/sequel/plugins/rcte_tree.rb +1 -1
- data/lib/sequel/plugins/serialization.rb +11 -5
- data/lib/sequel/plugins/sql_comments.rb +7 -2
- data/lib/sequel/plugins/static_cache_cache.rb +50 -13
- data/lib/sequel/plugins/subset_conditions.rb +1 -0
- data/lib/sequel/plugins/subset_static_cache.rb +263 -0
- data/lib/sequel/sql.rb +8 -1
- data/lib/sequel/version.rb +1 -1
- metadata +6 -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.
|
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
|
@@ -1915,6 +1921,7 @@ module Sequel
|
|
1915
1921
|
end
|
1916
1922
|
|
1917
1923
|
m = Module.new do
|
1924
|
+
Sequel.set_temp_name(Module.new){"Sequel::SQL::VirtualRow::_BaseMethodMissing"}
|
1918
1925
|
# Return an +Identifier+, +QualifiedIdentifier+, or +Function+, depending
|
1919
1926
|
# on arguments and whether a block is provided. Does not currently call the block.
|
1920
1927
|
# See the class level documentation.
|
data/lib/sequel/version.rb
CHANGED
@@ -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 =
|
9
|
+
MINOR = 90
|
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.
|
4
|
+
version: 5.90.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-03-01 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: bigdecimal
|
@@ -274,6 +273,7 @@ files:
|
|
274
273
|
- lib/sequel/extensions/pretty_table.rb
|
275
274
|
- lib/sequel/extensions/provenance.rb
|
276
275
|
- lib/sequel/extensions/query.rb
|
276
|
+
- lib/sequel/extensions/query_blocker.rb
|
277
277
|
- lib/sequel/extensions/round_timestamps.rb
|
278
278
|
- lib/sequel/extensions/run_transaction_hooks.rb
|
279
279
|
- lib/sequel/extensions/s.rb
|
@@ -371,6 +371,7 @@ files:
|
|
371
371
|
- lib/sequel/plugins/paged_operations.rb
|
372
372
|
- lib/sequel/plugins/pg_array_associations.rb
|
373
373
|
- lib/sequel/plugins/pg_auto_constraint_validations.rb
|
374
|
+
- lib/sequel/plugins/pg_eager_any_typed_array.rb
|
374
375
|
- lib/sequel/plugins/pg_row.rb
|
375
376
|
- lib/sequel/plugins/pg_xmin_optimistic_locking.rb
|
376
377
|
- lib/sequel/plugins/prepared_statements.rb
|
@@ -392,6 +393,7 @@ files:
|
|
392
393
|
- lib/sequel/plugins/string_stripper.rb
|
393
394
|
- lib/sequel/plugins/subclasses.rb
|
394
395
|
- lib/sequel/plugins/subset_conditions.rb
|
396
|
+
- lib/sequel/plugins/subset_static_cache.rb
|
395
397
|
- lib/sequel/plugins/table_select.rb
|
396
398
|
- lib/sequel/plugins/tactical_eager_loading.rb
|
397
399
|
- lib/sequel/plugins/throw_failures.rb
|
@@ -424,7 +426,6 @@ metadata:
|
|
424
426
|
documentation_uri: https://sequel.jeremyevans.net/documentation.html
|
425
427
|
mailing_list_uri: https://github.com/jeremyevans/sequel/discussions
|
426
428
|
source_code_uri: https://github.com/jeremyevans/sequel
|
427
|
-
post_install_message:
|
428
429
|
rdoc_options:
|
429
430
|
- "--quiet"
|
430
431
|
- "--line-numbers"
|
@@ -446,8 +447,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
446
447
|
- !ruby/object:Gem::Version
|
447
448
|
version: '0'
|
448
449
|
requirements: []
|
449
|
-
rubygems_version: 3.
|
450
|
-
signing_key:
|
450
|
+
rubygems_version: 3.6.2
|
451
451
|
specification_version: 4
|
452
452
|
summary: The Database Toolkit for Ruby
|
453
453
|
test_files: []
|