orfeas_lyra 0.9.2 → 1.0.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2021 -17
  3. data/README.md +22 -9
  4. data/Rakefile +40 -1
  5. data/app/controllers/lyra/application_controller.rb +127 -0
  6. data/app/controllers/lyra/dashboard_controller.rb +33 -31
  7. data/app/controllers/lyra/flow_controller.rb +32 -34
  8. data/app/controllers/lyra/privacy_controller.rb +57 -18
  9. data/app/views/lyra/dashboard/discrepancies.html.erb +5 -5
  10. data/app/views/lyra/dashboard/index.html.erb +1 -0
  11. data/app/views/lyra/flow/crud_mapping.html.erb +1 -1
  12. data/app/views/lyra/privacy/pii_detection.html.erb +1 -1
  13. data/app/views/lyra/privacy/policy.html.erb +7 -0
  14. data/app/views/lyra/privacy/rights.html.erb +78 -0
  15. data/config/privacy_policies.rb +0 -3
  16. data/config/routes.rb +1 -0
  17. data/lib/lyra/access_log.rb +187 -6
  18. data/lib/lyra/advisory_lock.rb +236 -0
  19. data/lib/lyra/aggregate.rb +1 -1
  20. data/lib/lyra/associations/habtm_links.rb +382 -19
  21. data/lib/lyra/associations/maintenance.rb +229 -0
  22. data/lib/lyra/attribution.rb +48 -0
  23. data/lib/lyra/bypass_events.rb +446 -46
  24. data/lib/lyra/command_handler.rb +19 -14
  25. data/lib/lyra/configuration.rb +237 -10
  26. data/lib/lyra/consistency/read_your_writes.rb +4 -2
  27. data/lib/lyra/correlation.rb +121 -0
  28. data/lib/lyra/domain_events.rb +4 -0
  29. data/lib/lyra/dual_view.rb +53 -16
  30. data/lib/lyra/encrypted_attributes.rb +5 -0
  31. data/lib/lyra/engine.rb +67 -0
  32. data/lib/lyra/erasure.rb +1011 -106
  33. data/lib/lyra/event.rb +35 -1
  34. data/lib/lyra/event_data.rb +38 -0
  35. data/lib/lyra/event_flow.rb +31 -15
  36. data/lib/lyra/event_store_adapter.rb +28 -0
  37. data/lib/lyra/event_window.rb +58 -0
  38. data/lib/lyra/genesis.rb +548 -44
  39. data/lib/lyra/interceptors/association_interceptor.rb +689 -34
  40. data/lib/lyra/interceptors/crud_interceptor.rb +578 -91
  41. data/lib/lyra/internal.rb +26 -0
  42. data/lib/lyra/mode_sync.rb +51 -3
  43. data/lib/lyra/mode_transition.rb +257 -30
  44. data/lib/lyra/model_selection.rb +35 -0
  45. data/lib/lyra/own_connection.rb +17 -2
  46. data/lib/lyra/privacy/adapters/pam.rb +25 -2
  47. data/lib/lyra/privacy/gdpr_compliance.rb +314 -48
  48. data/lib/lyra/privacy/interface.rb +27 -7
  49. data/lib/lyra/privacy/subject_links.rb +143 -0
  50. data/lib/lyra/privacy/subject_records.rb +369 -0
  51. data/lib/lyra/projection.rb +7 -1
  52. data/lib/lyra/projections/async_checkpoint.rb +32 -0
  53. data/lib/lyra/projections/async_projection_job.rb +166 -23
  54. data/lib/lyra/projections/cached_joins.rb +33 -18
  55. data/lib/lyra/projections/cached_projection.rb +74 -33
  56. data/lib/lyra/projections/cached_relation.rb +3175 -321
  57. data/lib/lyra/projections/default_scopes.rb +254 -0
  58. data/lib/lyra/projections/deferred_relation.rb +23 -3
  59. data/lib/lyra/projections/event_store_reader.rb +47 -8
  60. data/lib/lyra/projections/foreign_keys.rb +92 -0
  61. data/lib/lyra/projections/lazy_projection.rb +165 -27
  62. data/lib/lyra/projections/model_projection.rb +165 -14
  63. data/lib/lyra/projections/rebuild.rb +34 -6
  64. data/lib/lyra/projections/reference_links.rb +463 -72
  65. data/lib/lyra/projections/typed_values.rb +629 -0
  66. data/lib/lyra/projections/unique_indexes.rb +595 -0
  67. data/lib/lyra/projections/uniqueness.rb +784 -0
  68. data/lib/lyra/projections/value_digest.rb +93 -0
  69. data/lib/lyra/purpose_bound_reads.rb +912 -77
  70. data/lib/lyra/repair.rb +119 -28
  71. data/lib/lyra/retention.rb +114 -8
  72. data/lib/lyra/rights/consent_store.rb +415 -0
  73. data/lib/lyra/rights/erasure_guard.rb +135 -0
  74. data/lib/lyra/rights/pending.rb +74 -0
  75. data/lib/lyra/rights/queries.rb +269 -0
  76. data/lib/lyra/rights/store.rb +512 -0
  77. data/lib/lyra/rights/subject_keys.rb +224 -0
  78. data/lib/lyra/rights.rb +281 -0
  79. data/lib/lyra/saves_without_effect.rb +69 -0
  80. data/lib/lyra/stream_tail.rb +319 -0
  81. data/lib/lyra/strict_data_access.rb +916 -106
  82. data/lib/lyra/temporal.rb +4 -2
  83. data/lib/lyra/touch_registry.rb +404 -0
  84. data/lib/lyra/verification/bypass_workflow.rb +452 -42
  85. data/lib/lyra/verification/rights_workflows.rb +170 -0
  86. data/lib/lyra/version.rb +1 -1
  87. data/lib/lyra/visualization/event_graph.rb +1 -1
  88. data/lib/lyra/visualization/timeline.rb +2 -2
  89. data/lib/lyra.rb +43 -2
  90. data/lib/tasks/lyra_mode.rake +110 -8
  91. data/lib/tasks/lyra_projections.rake +43 -2
  92. metadata +34 -4
  93. data/lib/lyra/associations/event_aware.rb +0 -224
@@ -0,0 +1,629 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lyra
4
+ module Projections
5
+ # Conditions and orders on records rebuilt from events (ES-NoProj),
6
+ # decided as Active Record and the database decide them on a table.
7
+ #
8
+ # Active Record binds a condition's value through the column's type
9
+ # before the database compares it: where(amount: 250.0), (amount: "250.00")
10
+ # and (amount: 250) all find a decimal 250.00; where(kind: :card) finds
11
+ # "card"; where(active: "t") finds true; where(qty: "abc") binds NULL and
12
+ # finds nothing. The comparison is SQL's, with NULL unknown: where.not(tag:
13
+ # "x") leaves out the rows whose tag is NULL. Comparing the Ruby values as
14
+ # given missed all of these (a Float against the event's "250.0", a Symbol
15
+ # against a String).
16
+ #
17
+ # A condition's value is bound here as Active Record binds it (the type
18
+ # serializes it) and read back as a record holds it (the type
19
+ # deserializes it); the record's value is the attribute's; the two are
20
+ # compared as SQL would: true, false or nil (unknown).
21
+ #
22
+ # Conditions are compiled once per query, and what each model's types
23
+ # need is worked out once per model (Schema): ES-NoProj answers every
24
+ # query by testing every record, so the per-record work is kept to a read
25
+ # and a comparison.
26
+ module TypedValues
27
+ # A value the database is given as NULL: one the type serializes to nil
28
+ # ("abc" or "" on an integer column, "" on a boolean one).
29
+ NULL = Object.new.freeze
30
+ # The type a serialized attribute's stored text is compared as.
31
+ TEXT = ActiveModel::Type::String.new
32
+ # A value out of the column's range (Active Record's unboundable): no
33
+ # row equals it.
34
+ OUT_OF_RANGE = Object.new.freeze
35
+ # A value the column's type binds but cannot read back (an integer
36
+ # that is no enum's label: where(status: 5) binds = 5): no row read as
37
+ # the type reads it equals it, and NULL is unknown against it.
38
+ UNREADABLE = Object.new.freeze
39
+ # Column types Active Record converts to and from Time.zone.
40
+ TIME_TYPES = %i[datetime timestamptz time].freeze
41
+ # Types whose values are worth remembering once read from an event's
42
+ # text (a time is parsed, a decimal built).
43
+ PARSED_TYPES = %i[datetime timestamptz time date decimal].freeze
44
+ # Values read from events, per attribute, before the memo starts again.
45
+ MEMO_LIMIT = 100_000
46
+
47
+ # What a model's attribute types need, worked out once per model (and
48
+ # again after reset_column_information, which replaces the types).
49
+ class Schema
50
+ attr_reader :model_class, :types, :names, :aliases, :conversions, :enums, :encrypted, :nondeterministic, :sti,
51
+ :serialized, :blank_padded
52
+
53
+ def initialize(model_class)
54
+ @model_class = model_class
55
+ @types = model_class.attribute_types
56
+ @names = model_class.attribute_names.to_set
57
+ @aliases = model_class.attribute_aliases
58
+ @encrypted = encrypted_names(model_class)
59
+ @enums = @names.select { enum?(@types[_1]) }.to_set
60
+ # Event attributes hold the attributes' values as the model gives
61
+ # them (Model#attributes: an enum's label, a serialized column's
62
+ # Hash); a record is instantiated from the values the table holds.
63
+ @conversions = @names.filter_map do |name|
64
+ type = @types[name]
65
+ [name, type] if (enum?(type) || serialized?(type)) && !@encrypted.include?(name)
66
+ end.to_h
67
+ @nondeterministic = @names.select { nondeterministic?(@types[_1]) }.to_set
68
+ # A serialized attribute is compared as the table compares it: as
69
+ # the text it is stored as ({"a"=>1,"b"=>2} and {"b"=>2,"a"=>1}
70
+ # are two values there).
71
+ @serialized = @names.select { serialized?(@types[_1]) && !@encrypted.include?(_1) }.to_set
72
+ # A character(n) column (PostgreSQL's bpchar) compares without its
73
+ # trailing spaces: 'ab' and 'ab ' are one value there.
74
+ @blank_padded = @names.select { blank_padded?(model_class, _1) }.to_set
75
+ @inheritance_column = model_class.inheritance_column.to_s if model_class.respond_to?(:inheritance_column)
76
+ @sti = @inheritance_column && @names.include?(@inheritance_column)
77
+ @subclasses = {}
78
+ @memo = Hash.new { |hash, name| hash[name] = {} }
79
+ # Read from an event's text with work worth remembering: a time
80
+ # parsed, a decimal built, a ciphertext decrypted.
81
+ @memoized = @names.select { PARSED_TYPES.include?(@types[_1].type) || @encrypted.include?(_1) }.to_set
82
+ # The attributes whose value, when the entry holds it as this Ruby
83
+ # class, is already what the type reads (an integer, a string, a
84
+ # boolean): not passed through the type.
85
+ @as_is = @names.filter_map do |name|
86
+ next if @encrypted.include?(name)
87
+
88
+ klass = as_is_class(@types[name])
89
+ [name, klass] if klass
90
+ end.to_h
91
+ end
92
+
93
+ def current? = @types.equal?(@model_class.attribute_types)
94
+
95
+ def attribute_name(key)
96
+ name = key.to_s
97
+ @aliases.fetch(name, name)
98
+ end
99
+
100
+ # The class a record of these event attributes is: an STI subclass
101
+ # when its inheritance column names one (its enums and serialized
102
+ # attributes are its own).
103
+ def class_for(attributes)
104
+ return @model_class unless @sti
105
+
106
+ type_name = attributes[@inheritance_column] || attributes[@inheritance_column.to_sym]
107
+ return @model_class if type_name.nil? || type_name == "" || type_name == @model_class.sti_name
108
+
109
+ @subclasses.fetch(type_name) do
110
+ @subclasses[type_name] =
111
+ begin
112
+ @model_class.sti_class_for(type_name)
113
+ rescue ActiveRecord::SubclassNotFound, NameError
114
+ @model_class
115
+ end
116
+ end
117
+ end
118
+
119
+ # The value of +name+ in the record that +attributes+ (a cache
120
+ # entry's, as the table holds them) build, without building it.
121
+ def event_value(attributes, name)
122
+ value = attributes.fetch(name) { attributes[name.to_sym] }
123
+ return nil if value.nil?
124
+ return value if (klass = @as_is[name]) && klass === value
125
+
126
+ type = @types[name]
127
+ return type.deserialize(value) unless value.is_a?(String) && @memoized.include?(name)
128
+
129
+ memo = @memo[name]
130
+ memo.fetch(value) do
131
+ memo.clear if memo.size >= MEMO_LIMIT
132
+ read = type.deserialize(value)
133
+ # Only compared: a datetime as a plain UTC time, which compares faster.
134
+ read = read.utc if read.is_a?(ActiveSupport::TimeWithZone) && type.type != :time
135
+ memo[value.frozen? ? value : value.dup.freeze] = read
136
+ end
137
+ end
138
+
139
+ private
140
+
141
+ BOOLEAN = ->(value) { true.equal?(value) || false.equal?(value) }
142
+ private_constant :BOOLEAN
143
+
144
+ def as_is_class(type)
145
+ case type
146
+ when ActiveModel::Type::Integer then Integer
147
+ when ActiveModel::Type::ImmutableString then String
148
+ when ActiveModel::Type::Boolean then BOOLEAN
149
+ end
150
+ end
151
+
152
+ def enum?(type) = type.is_a?(ActiveRecord::Enum::EnumType)
153
+
154
+ def blank_padded?(model_class, name)
155
+ column = model_class.columns_hash[name] if model_class.respond_to?(:columns_hash)
156
+ column&.sql_type.to_s.match?(/\A(character|char|bpchar)(\(\d+\))?\z/i)
157
+ rescue StandardError
158
+ false
159
+ end
160
+ def serialized?(type) = type.is_a?(ActiveRecord::Type::Serialized)
161
+
162
+ def nondeterministic?(type)
163
+ defined?(ActiveRecord::Encryption::EncryptedAttributeType) &&
164
+ type.is_a?(ActiveRecord::Encryption::EncryptedAttributeType) && !type.deterministic?
165
+ end
166
+
167
+ def encrypted_names(model_class)
168
+ return Set.new unless model_class.respond_to?(:encrypted_attributes)
169
+
170
+ Array(model_class.encrypted_attributes).to_set(&:to_s)
171
+ end
172
+ end
173
+
174
+ SCHEMAS = ObjectSpace::WeakKeyMap.new
175
+ SCHEMAS_LOCK = Mutex.new
176
+
177
+ # A condition on one attribute, compiled: +test+ takes the attribute's
178
+ # value and answers true, false or nil.
179
+ Condition = Struct.new(:name, :test) do
180
+ def evaluate(reader, source) = test.call(reader.read(source, name))
181
+ end
182
+
183
+ # A tuple condition, where([:a, :b] => [[1, 2], ...]), as Active Record
184
+ # builds for a record's query constraints (an association's
185
+ # delete(record)): the record matches one of the tuples.
186
+ TupleCondition = Struct.new(:tuples) do
187
+ def evaluate(reader, source)
188
+ TypedValues.any_of(tuples.map { |tests| tests ? TypedValues.all_of(tests.map { _1.evaluate(reader, source) }) : false })
189
+ end
190
+ end
191
+
192
+ # Reads a built record's attribute (CachedRelation).
193
+ module RecordReader
194
+ def self.read(record, name) = record.read_attribute(name)
195
+ end
196
+
197
+ # Reads an attribute from event attributes (CachedProjection), as the
198
+ # record they build would hold it.
199
+ class EventReader
200
+ def initialize(model_class)
201
+ @schema = TypedValues.schema(model_class)
202
+ end
203
+
204
+ def read(attributes, name)
205
+ return @schema.event_value(attributes, name) unless @schema.sti
206
+
207
+ klass = @schema.class_for(attributes)
208
+ schema = klass.equal?(@schema.model_class) ? @schema : TypedValues.schema(klass)
209
+ schema.event_value(attributes, name)
210
+ end
211
+ end
212
+
213
+ module_function
214
+
215
+ def schema(model_class)
216
+ found = SCHEMAS[model_class]
217
+ return found if found&.current?
218
+
219
+ SCHEMAS_LOCK.synchronize { SCHEMAS[model_class] = Schema.new(model_class) }
220
+ end
221
+
222
+ def attribute_name(model_class, key) = schema(model_class).attribute_name(key)
223
+
224
+ # The condition +key+ => +value+ on +model_class+, compiled once for
225
+ # every record it is tested on (Condition, TupleCondition). A condition
226
+ # on a name that is not an attribute is refused, as the table refuses
227
+ # it (an unknown column).
228
+ def condition(model_class, key, value)
229
+ if key.is_a?(Array)
230
+ tuples = Array(value).map do |tuple|
231
+ tuple = Array(tuple)
232
+ key.zip(tuple).map { |column, v| condition(model_class, column, v) } if tuple.size == key.size
233
+ end
234
+ return TupleCondition.new(tuples)
235
+ end
236
+
237
+ facts = schema(model_class)
238
+ name = facts.attribute_name(key)
239
+ raise UnsupportedQuery.new(model_class, "a condition on #{key}, which is not an attribute") unless facts.names.include?(name)
240
+
241
+ value = normalize(value)
242
+ type = facts.types[name]
243
+ padded = facts.blank_padded.include?(name)
244
+ value = unpadded(value) if padded
245
+ test =
246
+ if facts.nondeterministic.include?(name)
247
+ ciphertext_test(model_class, name, value)
248
+ elsif facts.serialized.include?(name)
249
+ serialized_test(model_class, name, type, value)
250
+ else
251
+ time_of_day_test(type, value_test(type, value))
252
+ end
253
+ test = unpadded_test(test) if padded
254
+ Condition.new(name, test)
255
+ end
256
+
257
+ # A serialized attribute's value as the table holds it: the text its
258
+ # coder writes (nil for NULL, which a default value is stored as).
259
+ # Equality and IN compare those texts; a range or a pattern on them
260
+ # is refused.
261
+ def serialized_test(model_class, name, type, value)
262
+ if value.is_a?(Range) || value.is_a?(Regexp) || (value.is_a?(Array) && value.any? { _1.is_a?(Range) || _1.is_a?(Regexp) })
263
+ raise UnsupportedQuery.new(model_class, "a range or pattern on #{name}, which is serialized")
264
+ end
265
+
266
+ texts = value.is_a?(Array) ? value.map { serialized_text(type, _1) } : serialized_text(type, value)
267
+ test = value_test(TEXT, texts)
268
+ ->(stored) { test.call(stored.nil? ? nil : serialized_text(type, stored)) }
269
+ end
270
+
271
+ def serialized_text(type, value)
272
+ return nil if value.nil?
273
+
274
+ text = type.serialize(value)
275
+ text&.to_s
276
+ end
277
+
278
+ # character(n): trailing spaces are not part of the value.
279
+ def unpadded(value)
280
+ case value
281
+ when String then value.sub(/ +\z/, "")
282
+ when Array then value.map { unpadded(_1) }
283
+ when Range then Range.new(unpadded(value.begin), unpadded(value.end), value.exclude_end?)
284
+ else value
285
+ end
286
+ end
287
+
288
+ def unpadded_test(test)
289
+ ->(stored) { test.call(stored.is_a?(String) ? unpadded(stored) : stored) }
290
+ end
291
+
292
+ # A time column holds a time of day, in UTC: the record's value (a
293
+ # time on 2000-01-01 in Time.zone, which wraps at midnight) is compared
294
+ # as its time of day in UTC, as the bound value is (query_value).
295
+ def time_of_day_test(type, test)
296
+ return test unless type.type == :time
297
+
298
+ ->(stored) { test.call(stored && time_of_day(stored)) }
299
+ end
300
+
301
+ # Microseconds since midnight UTC.
302
+ def time_of_day(time)
303
+ time = time.utc
304
+ (((time.hour * 60) + time.min) * 60 + time.sec) * 1_000_000 + time.usec
305
+ end
306
+
307
+ # Whether every one of +conditions+ holds for +source+, as SQL's AND:
308
+ # true, false (at the first that fails) or nil.
309
+ def all_hold(conditions, reader, source)
310
+ unknown = false
311
+ conditions.each do |condition|
312
+ result = condition.evaluate(reader, source)
313
+ return false if false.equal?(result)
314
+
315
+ unknown = true if result.nil?
316
+ end
317
+ unknown ? nil : true
318
+ end
319
+
320
+ # A record, or a relation of records, stands for its primary key, as in
321
+ # Active Record: where(variant_id: [variant]) matched nothing, and
322
+ # Solidus's stock check found no stock under ES-NoProj.
323
+ def normalize(value)
324
+ value = value.to_a if value.is_a?(ActiveRecord::Relation) || value.is_a?(CachedRelation) || value.is_a?(Set)
325
+ value = value.id if value.is_a?(ActiveRecord::Base)
326
+ value = value.map { |v| v.is_a?(ActiveRecord::Base) ? v.id : v } if value.is_a?(Array)
327
+ value
328
+ end
329
+
330
+ # A test of an attribute's value against +value+, a condition's value
331
+ # on an attribute of +type+.
332
+ def value_test(type, value)
333
+ case value
334
+ when Array then list_test(type, value)
335
+ when Range then range_test(type, value)
336
+ when nil then ->(stored) { stored.nil? }
337
+ when Regexp then ->(stored) { stored.nil? ? nil : stored.to_s.match?(value) }
338
+ else
339
+ bound = query_value(type, value)
340
+ return ->(_) { false } if bound.equal?(OUT_OF_RANGE)
341
+ # Active Record binds such a value as IS NULL when the type wraps
342
+ # another (an enum's unknown label, a serialized default), else as
343
+ # = NULL, which no row satisfies (Relation::QueryAttribute#nil?).
344
+ return ->(stored) { stored.nil? } if bound.equal?(NULL) && wraps_a_type?(type)
345
+ return ->(_) { nil } if bound.equal?(NULL)
346
+ return ->(stored) { stored.nil? ? nil : false } if bound.equal?(UNREADABLE)
347
+ # An integer or a string equals what it is ==: no <=> needed.
348
+ return ->(stored) { stored.nil? ? nil : stored == bound } if bound.is_a?(Integer) || bound.is_a?(String)
349
+
350
+ ->(stored) { stored.nil? ? nil : (cmp = compare(stored, bound)) ? cmp.zero? : false }
351
+ end
352
+ end
353
+
354
+ # A non-deterministically encrypted attribute: the table holds a
355
+ # ciphertext no bound value can equal, so an equality finds no row
356
+ # (and IS NULL its NULLs), as on the table; an order or a pattern on
357
+ # ciphertexts has no answer here, and is refused.
358
+ def ciphertext_test(model_class, name, value)
359
+ case value
360
+ when nil then ->(stored) { stored.nil? }
361
+ when Array
362
+ tests = value.map { ciphertext_test(model_class, name, _1) }
363
+ ->(stored) { any_of(tests.map { _1.call(stored) }) }
364
+ when Range, Regexp
365
+ raise UnsupportedQuery.new(model_class, "a range or pattern on #{name}, which is encrypted non-deterministically")
366
+ else ->(stored) { stored.nil? ? nil : false }
367
+ end
368
+ end
369
+
370
+ # A list of values, as Active Record builds its condition
371
+ # (PredicateBuilder::ArrayHandler): one value is an equality; two or
372
+ # more are an IN of those the type binds (HomogeneousIn: a value it
373
+ # binds as NULL or cannot bind is left out, and with none left the IN
374
+ # is IN (NULL), unknown); a nil adds OR IS NULL, a range OR BETWEEN.
375
+ # So where.not(rank: [9, "abc"]) is NOT IN (9): it used to be unknown
376
+ # for every row. A list of integers or strings (ids, codes) is looked
377
+ # up in a set.
378
+ def list_test(type, values)
379
+ return ->(_) { false } if values.empty?
380
+ if values.any? { _1.is_a?(Array) || _1.is_a?(Regexp) }
381
+ tests = values.map { value_test(type, _1) }
382
+ return ->(stored) { any_of(tests.map { _1.call(stored) }) }
383
+ end
384
+
385
+ scalars = values.reject { _1.nil? || _1.is_a?(Range) }
386
+ tests = []
387
+ tests << (scalars.size == 1 ? value_test(type, scalars.first) : homogeneous_test(type, scalars)) if scalars.any?
388
+ tests << ->(stored) { stored.nil? } if values.any?(&:nil?)
389
+ values.each { tests << range_test(type, _1) if _1.is_a?(Range) }
390
+ return tests.first if tests.size == 1
391
+
392
+ ->(stored) { any_of(tests.map { _1.call(stored) }) }
393
+ end
394
+
395
+ # IN over two or more values (Arel::Nodes::HomogeneousIn#casted_values:
396
+ # the values the type cannot bind, or binds as NULL, are left out; with
397
+ # none left, IN (NULL)). A value bound but not readable back (5 on an
398
+ # enum without that label) stays a member, which no row equals.
399
+ def homogeneous_test(type, values)
400
+ bound = values.map { query_value(type, _1) }.reject { _1.equal?(NULL) || _1.equal?(OUT_OF_RANGE) }
401
+ return ->(_) { nil } if bound.empty?
402
+
403
+ bounds = bound.reject { _1.equal?(UNREADABLE) }
404
+ return ->(stored) { stored.nil? ? nil : false } if bounds.empty?
405
+
406
+ if bounds.all? { _1.is_a?(Integer) || _1.is_a?(String) }
407
+ set = bounds.to_set
408
+ return ->(stored) { stored.nil? ? nil : set.include?(stored) }
409
+ end
410
+
411
+ ->(stored) { stored.nil? ? nil : bounds.any? { |bound| (cmp = compare(stored, bound)) && cmp.zero? } }
412
+ end
413
+
414
+ def wraps_a_type?(type) = type.respond_to?(:subtype) || type.respond_to?(:normalizer)
415
+
416
+ # +value+ as Active Record binds it for a condition on +type+ (the
417
+ # type serializes it), read back as a record holds it (the type
418
+ # deserializes it); NULL when it binds NULL, OUT_OF_RANGE when it
419
+ # cannot be bound. So an enum compares in its column's domain
420
+ # (where(status: "0") is the label of 0), a decimal at its scale, a
421
+ # deterministically encrypted attribute as its ciphertext's plaintext
422
+ # (downcased, if it is), and a time-zone-aware time as the database
423
+ # reads the bound value: a string or a date in its own clock (Active
424
+ # Record's default_timezone), not in Time.zone.
425
+ def query_value(type, value)
426
+ return nil if value.nil?
427
+ return OUT_OF_RANGE unless type.serializable?(value)
428
+
429
+ bound = type.serialize(value)
430
+ return NULL if bound.nil?
431
+ return time_of_day(bound) if type.type == :time && bound.respond_to?(:utc)
432
+
433
+ result = type.deserialize(bound)
434
+ return UNREADABLE if result.nil?
435
+
436
+ return result unless TIME_TYPES.include?(type.type)
437
+
438
+ # A plain UTC time: the same instant, and cheaper to compare a
439
+ # record's time with than a time in a zone.
440
+ result = clock_time(result)
441
+ result.respond_to?(:utc) ? result.utc : result
442
+ rescue ActiveModel::RangeError
443
+ OUT_OF_RANGE
444
+ end
445
+
446
+ # A date given for a time-zone-aware column is its midnight in the
447
+ # database's clock, as the database reads '2026-02-01'.
448
+ def clock_time(value)
449
+ return value unless value.is_a?(Date) && !value.is_a?(DateTime)
450
+
451
+ if ActiveRecord.default_timezone == :utc
452
+ Time.utc(value.year, value.month, value.day)
453
+ else
454
+ Time.local(value.year, value.month, value.day)
455
+ end
456
+ end
457
+
458
+ # BETWEEN, with an endless or infinite end unbounded.
459
+ def range_test(type, range)
460
+ low = range_end(type, range.begin)
461
+ high = range_end(type, range.end)
462
+ # Out of the column's range: a begin above it or an end below it
463
+ # selects nothing; one below or above it leaves that side open.
464
+ return ->(_) { false } if low.equal?(OUT_OF_RANGE) && (range.begin <=> 0) == 1
465
+ return ->(_) { false } if high.equal?(OUT_OF_RANGE) && (range.end <=> 0) == -1
466
+
467
+ low = nil if low.equal?(OUT_OF_RANGE)
468
+ high = nil if high.equal?(OUT_OF_RANGE)
469
+ return ->(_) { nil } if low.equal?(NULL) || high.equal?(NULL) || low.equal?(UNREADABLE) || high.equal?(UNREADABLE)
470
+
471
+ exclusive = range.exclude_end?
472
+ lambda do |stored|
473
+ next nil if stored.nil?
474
+
475
+ above = low.nil? || ((cmp = compare(stored, low)) && cmp >= 0)
476
+ below = high.nil? || ((cmp = compare(stored, high)) && (exclusive ? cmp.negative? : cmp <= 0))
477
+ (above && below) ? true : false
478
+ end
479
+ end
480
+
481
+ def range_end(type, value)
482
+ return nil if value.nil? || (value.respond_to?(:infinite?) && value.infinite?)
483
+
484
+ query_value(type, value)
485
+ end
486
+
487
+ # The value bound in an SQL fragment ("col OP ?") on +name+. Active
488
+ # Record quotes it, not casting it through the attribute's type, and
489
+ # the database casts the literal to the column's type: a number is
490
+ # compared as it is (qty >= 2.5, amount = 250.004: no rounding to the
491
+ # column's scale), a string that is not one of the column's values
492
+ # is an error (fail closed: refused), and a time-zone-aware column
493
+ # reads a string or a date in the database's clock. nil for NULL.
494
+ def fragment_value(model_class, name, value)
495
+ facts = schema(model_class)
496
+ type = facts.types[name]
497
+ if facts.nondeterministic.include?(name)
498
+ raise UnsupportedQuery.new(model_class, "an SQL fragment on #{name}, which is encrypted non-deterministically")
499
+ end
500
+ return nil if value.nil?
501
+
502
+ case type.type
503
+ when *TIME_TYPES, :date
504
+ bound = query_value(type, value)
505
+ bound.equal?(NULL) || bound.equal?(OUT_OF_RANGE) || bound.equal?(UNREADABLE) ? nil : bound
506
+ when :integer, :decimal, :float
507
+ return type.cast(value) if facts.enums.include?(name)
508
+
509
+ numeric_literal(model_class, name, type.type, value)
510
+ else
511
+ type.cast(value)
512
+ end
513
+ end
514
+
515
+ def numeric_literal(model_class, name, kind, value)
516
+ case value
517
+ when Integer then value
518
+ # Quoted ('2.5', '3.0'): the database reads a numeric column's
519
+ # literal, and rejects it for an integer column.
520
+ when Float, BigDecimal, Rational then kind == :integer ? raise(ArgumentError) : value
521
+ when String
522
+ stripped = value.strip
523
+ case kind
524
+ when :integer then Integer(stripped, 10)
525
+ when :float then Float(stripped)
526
+ else BigDecimal(stripped)
527
+ end
528
+ else raise ArgumentError
529
+ end
530
+ rescue ArgumentError, TypeError
531
+ raise UnsupportedQuery.new(model_class, "an SQL fragment comparing #{name} (#{kind}) with #{value.inspect[0, 40]}, " \
532
+ "which the database rejects")
533
+ end
534
+
535
+ # <=> on two values of one attribute; booleans compare as false < true,
536
+ # as in SQL. nil when they cannot be compared.
537
+ def compare(a, b)
538
+ a = 1 if true.equal?(a)
539
+ a = 0 if false.equal?(a)
540
+ b = 1 if true.equal?(b)
541
+ b = 0 if false.equal?(b)
542
+ a <=> b
543
+ end
544
+
545
+ # SQL's OR and AND over true, false and nil (unknown).
546
+ def any_of(results)
547
+ return true if results.include?(true)
548
+
549
+ results.include?(nil) ? nil : false
550
+ end
551
+
552
+ def all_of(results)
553
+ return false if results.include?(false)
554
+
555
+ results.include?(nil) ? nil : true
556
+ end
557
+
558
+ # How a record's value of +column+ is read for an order, worked out
559
+ # once: the attribute's value, except an enum's, which is ordered by
560
+ # the integer (or string) the column holds, not by its label.
561
+ def sort_reader(model_class, column)
562
+ facts = schema(model_class)
563
+ name = facts.attribute_name(column)
564
+ # The table rejects an order on a column it lacks (id, where the
565
+ # primary key is code); it used to be ordered by the method's value.
566
+ raise UnsupportedQuery.new(model_class, "an order on #{column}, which is not an attribute") unless facts.names.include?(name)
567
+
568
+ if facts.encrypted.include?(name)
569
+ # The table orders the ciphertexts, an order the plaintexts here
570
+ # do not have: refused rather than answered in another order.
571
+ raise UnsupportedQuery.new(model_class, "an order on #{name}, which is encrypted")
572
+ end
573
+
574
+ if facts.enums.include?(name)
575
+ type = facts.types[name]
576
+ ->(record) { type.serialize(record.read_attribute(name)) }
577
+ else
578
+ comparable_reader(model_class, name)
579
+ end
580
+ end
581
+
582
+ # How a record's value of the attribute +name+ is read to be compared
583
+ # with a bound value: a time column's as its time of day in UTC.
584
+ def comparable_reader(model_class, name)
585
+ if schema(model_class).types[name].type == :time
586
+ ->(record) { (value = record.read_attribute(name)) && time_of_day(value) }
587
+ else
588
+ ->(record) { record.read_attribute(name) }
589
+ end
590
+ end
591
+
592
+ # +attributes+ (event attributes) as the table holds them, for
593
+ # instantiate: an enum's label turned into the column's value, a
594
+ # serialized attribute's Hash into its text, as a projection writing
595
+ # the row does (insert serializes). An enum's label read as the
596
+ # column's integer was 0, the first label, for every record.
597
+ # Encrypted attributes are left alone: the event holds their
598
+ # ciphertext, as the row does. An STI record is converted with its
599
+ # own class's types.
600
+ def database_attributes(model_class, attributes)
601
+ conversions = conversions_for(model_class, attributes)
602
+ return attributes if conversions.empty? || conversions.none? { |name, _| !attributes[name].nil? }
603
+
604
+ convert_event_values!(model_class, attributes.dup, nil)
605
+ end
606
+
607
+ # Converts, in place, the values of +names+ (nil: all) in +attributes+
608
+ # from the events' form to the table's (database_attributes).
609
+ def convert_event_values!(model_class, attributes, names)
610
+ conversions = conversions_for(model_class, attributes)
611
+ return attributes if conversions.empty?
612
+
613
+ conversions.each do |name, type|
614
+ next if names && !names.include?(name)
615
+
616
+ value = attributes[name]
617
+ attributes[name] = type.serialize(value) unless value.nil?
618
+ end
619
+ attributes
620
+ end
621
+
622
+ def conversions_for(model_class, attributes)
623
+ facts = schema(model_class)
624
+ klass = facts.class_for(attributes)
625
+ klass.equal?(model_class) ? facts.conversions : schema(klass).conversions
626
+ end
627
+ end
628
+ end
629
+ end