Class: Pakyow::Data::Sources::Relational::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/data/sources/relational/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, block:, source:, provides_dataset:, creates:, updates:, deletes:) ⇒ Command

Returns a new instance of Command.



15
16
17
# File 'lib/pakyow/data/sources/relational/command.rb', line 15

def initialize(name, block:, source:, provides_dataset:, creates:, updates:, deletes:)
  @name, @block, @source, @provides_dataset, @creates, @updates, @deletes = name, block, source, provides_dataset, creates, updates, deletes
end

Instance Method Details

#call(values = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/pakyow/data/sources/relational/command.rb', line 19

def call(values = {})
  future_associated_changes = []

  if values
    # Enforce required attributes.
    #
    @source.class.attributes.each do |attribute_name, attribute|
      if attribute.meta[:required]
        if @creates && !values.include?(attribute_name)
          raise NotNullViolation.new_with_message(attribute: attribute_name)
        end

        if values.include?(attribute_name) && values[attribute_name].nil?
          raise NotNullViolation.new_with_message(attribute: attribute_name)
        end
      end
    end

    # Fail if unexpected values were passed.
    #
    values.keys.each do |key|
      key = key.to_sym
      unless @source.class.attributes.include?(key) || @source.class.association_with_name?(key)
        raise UnknownAttribute.new_with_message(attribute: key, source: @source.class.__object_name.name)
      end
    end

    # Coerce values into the appropriate type.
    #
    final_values = values.each_with_object({}) { |(key, value), values_hash|
      key = key.to_sym

      begin
        if attribute = @source.class.attributes[key]
          if value.is_a?(Proxy) || value.is_a?(Result) || value.is_a?(Object)
            raise TypeMismatch.new_with_message(type: value.class, mapping: attribute.meta[:mapping])
          end

          values_hash[key] = value.nil? ? value : attribute[value]
        else
          values_hash[key] = value
        end
      rescue TypeError, Dry::Types::CoercionError => error
        raise TypeMismatch.build(error, type: value.class, mapping: attribute.meta[:mapping])
      end
    }

    # Update timestamp fields.
    #
    if timestamp_fields = @source.class.timestamp_fields
      if @creates
        timestamp_fields.values.each do |timestamp_field|
          final_values[timestamp_field] = Time.now
        end
      # Don't update timestamps if we aren't also updating other values.
      #
      elsif values.any? && timestamp_field = timestamp_fields[@name]
        final_values[timestamp_field] = Time.now
      end
    end

    if @creates
      # Set default values.
      #
      @source.class.attributes.each do |attribute_name, attribute|
        if !final_values.include?(attribute_name) && attribute.meta.include?(:default)
          default = attribute.meta[:default]
          final_values[attribute_name] = if default.is_a?(Proc)
            default.call
          else
            default
          end
        end
      end
    end

    # Enforce constraints on association values passed by access name.
    #
    @source.class.associations.values.flatten.select { |association|
      final_values.key?(association.name)
    }.each do |association|
      association_value = raw_result(final_values[association.name])

      case association_value
      when Proxy
        if association_value.source.class == association.associated_source
          if association.result_type == :one && (association_value.count > 1 || (@updates && @source.count > 1))
            raise ConstraintViolation.new_with_message(
              :associate_multiple,
              association: association.name
            )
          end
        else
          raise TypeMismatch.new_with_message(
            :associate_wrong_source,
            source: association_value.source.class.__object_name.name,
            association: association.name
          )
        end
      when Object
        if association.result_type == :one
          if association_value.originating_source
            if association_value.originating_source.__object_name.name == association.associated_source.__object_name.name
              if association.associated_source.instance.send(:"by_#{association.associated_source.primary_key_field}", association_value[association.associated_source.primary_key_field]).count == 0
                raise ConstraintViolation.new_with_message(
                  :associate_missing,
                  source: association.name,
                  field: association.associated_source.primary_key_field,
                  value: association_value[association.associated_source.primary_key_field]
                )
              end
            else
              raise TypeMismatch.new_with_message(
                :associate_wrong_object,
                source: association_value.originating_source.__object_name.name,
                association: association.name
              )
            end
          else
            raise TypeMismatch.new_with_message(
              :associate_unknown_object,
              association: association.name
            )
          end
        else
          raise TypeMismatch.new_with_message(
            :associate_wrong_type,
            type: association_value.class,
            association: association.name
          )
        end
      when Array
        if association.result_type == :many
          if association_value.any? { |value| !value.is_a?(Object) }
            raise TypeMismatch.new_with_message(
              :associate_many_not_object,
              association: association.name
            )
          else
            if association_value.any? { |value| value.originating_source.nil? }
              raise TypeMismatch.new_with_message(
                :associate_unknown_object,
                association: association.name
              )
            else
              if association_value.find { |value| value.originating_source != association.associated_source }
                raise TypeMismatch.new_with_message(
                  :associate_many_wrong_source,
                  association: association.name,
                  source: association.associated_source_name
                )
              else
                associated_column_value = association_value.map { |object| object[association.associated_source.primary_key_field] }
                associated_object_query = association.associated_source.instance.send(
                  :"by_#{association.associated_source.primary_key_field}", associated_column_value
                )

                if associated_object_query.count != association_value.count
                  raise ConstraintViolation.new_with_message(
                    :associate_many_missing,
                    association: association.name
                  )
                end
              end
            end
          end
        else
          raise ConstraintViolation.new_with_message(
            :associate_multiple,
            association: association.name
          )
        end
      when NilClass
      else
        raise TypeMismatch.new_with_message(
          :associate_wrong_type,
          type: association_value.class,
          association: association.name
        )
      end
    end

    # Enforce constraints for association values passed by foreign key.
    #
    @source.class.associations.values.flatten.select { |association|
      association.type == :belongs && final_values.key?(association.foreign_key_field) && !final_values[association.foreign_key_field].nil?
    }.each do |association|
      associated_column_value = final_values[association.foreign_key_field]
      associated_object_query = association.associated_source.instance.send(
        :"by_#{association.associated_query_field}", associated_column_value
      )

      if associated_object_query.count == 0
        raise ConstraintViolation.new_with_message(
          :associate_missing,
          source: association.name,
          field: association.associated_query_field,
          value: associated_column_value
        )
      end
    end

    # Set values for associations passed by access name.
    #
    @source.class.associations.values.flatten.select { |association|
      final_values.key?(association.name)
    }.each do |association|
      case association.specific_type
      when :belongs_to
        association_value = raw_result(final_values.delete(association.name))
        final_values[association.query_field] = case association_value
        when Proxy
          if association_value.one.nil?
            nil
          else
            association_value.one[association.associated_source.primary_key_field]
          end
        when Object
          association_value[association.associated_source.primary_key_field]
        when NilClass
          nil
        end
      when :has_one, :has_many
        future_associated_changes << [association, final_values.delete(association.name)]
      end
    end
  end

  original_dataset = if @updates
    # Hold on to the original values so we can update them locally.
    @source.dup.to_a
  else
    nil
  end

  unless @provides_dataset || @updates
    # Cache the result prior to running the command.
    @source.to_a
  end

  @source.transaction do
    if @deletes
      @source.class.associations.values.flatten.select(&:dependents?).each do |association|
        dependent_values = @source.class.container.connection.adapter.restrict_to_attribute(
          @source.class.primary_key_field, @source
        )

        # If objects are located in two different connections, fetch the raw values.
        #
        unless @source.class.container.connection == association.associated_source.container.connection
          dependent_values = dependent_values.map { |dependent_value|
            dependent_value[@source.class.primary_key_field]
          }
        end

        if association.type == :through
          joining_data = association.joining_source.instance.send(
            :"by_#{association.right_foreign_key_field}",
            dependent_values
          )

          dependent_data = association.associated_source.instance.send(
            :"by_#{association.associated_source.primary_key_field}",
            association.associated_source.container.connection.adapter.restrict_to_attribute(
              association.left_foreign_key_field, joining_data
            ).map { |result|
              result[association.left_foreign_key_field]
            }
          )

          case association.dependent
          when :delete
            joining_data.delete
          when :nullify
            joining_data.update(association.right_foreign_key_field => nil)
          end
        else
          dependent_data = association.associated_source.instance.send(
            :"by_#{association.associated_query_field}",
            dependent_values
          )
        end

        case association.dependent
        when :delete
          dependent_data.delete
        when :nullify
          unless association.type == :through
            dependent_data.update(association.associated_query_field => nil)
          end
        when :raise
          dependent_count = dependent_data.count
          if dependent_count > 0
            dependent_name = if dependent_count > 1
              Support.inflector.pluralize(association.associated_source_name)
            else
              Support.inflector.singularize(association.associated_source_name)
            end

            raise ConstraintViolation.new_with_message(
              :dependent_delete,
              source: @source.class.__object_name.name,
              count: dependent_count,
              dependent: dependent_name
            )
          end
        end
      end
    end

    if @creates || @updates
      # Ensure that has_one associations only have one associated object.
      #
      @source.class.associations[:belongs_to].flat_map { |belongs_to_association|
        belongs_to_association.associated_source.associations[:has_one].select { |has_one_association|
          has_one_association.associated_source == @source.class &&
            has_one_association.associated_query_field == belongs_to_association.query_field
        }
      }.each do |association|
        value = final_values.dig(
          association.associated_name, association.query_field
        ) || final_values.dig(association.associated_query_field)

        if value
          @source.class.instance.tap do |impacted_source|
            impacted_source.__setobj__(
              @source.class.container.connection.adapter.result_for_attribute_value(
                association.associated_query_field, value, impacted_source
              )
            )

            impacted_source.update(association.associated_query_field => nil)
          end
        end
      end
    end

    command_result = @source.instance_exec(final_values, &@block)

    final_result = if @updates
      # For updates, we fetch the values prior to performing the update and
      # return a source containing locally updated values. This lets us see
      # the original values but prevents us from fetching twice.

      @source.class.container.source(@source.class.__object_name.name).tap do |updated_source|
        updated_source.__setobj__(
          @source.class.container.connection.adapter.result_for_attribute_value(
            @source.class.primary_key_field, command_result, updated_source
          )
        )

        updated_source.instance_variable_set(:@results, original_dataset.map { |original_object|
          new_object = original_object.class.new(original_object.values.merge(final_values))
          new_object.originating_source = original_object.originating_source
          new_object
        })

        updated_source.instance_variable_set(:@original_results, original_dataset)
      end
    elsif @provides_dataset
      @source.dup.tap { |source|
        source.__setobj__(command_result)
      }
    else
      @source
    end

    if @creates || @updates
      # Update records associated with the data we just changed.
      #
      future_associated_changes.each do |association, association_value|
        association_value = raw_result(association_value)
        associated_dataset = case association_value
        when Proxy
          association_value
        when Object, Array
          updatable = Array.ensure(association_value).map { |value|
            case value
            when Object
              value[association.associated_source.primary_key_field]
            else
              value
            end
          }

          association.associated_source.instance.send(
            :"by_#{association.associated_source.primary_key_field}", updatable
          )
        when NilClass
          nil
        end

        if association.type == :through
          associated_column_value = final_result.class.container.connection.adapter.restrict_to_attribute(
            association.query_field, final_result
          )

          # If objects are located in two different connections, fetch the raw values.
          #
          if association.joining_source.container.connection == final_result.class.container.connection
            disassociate_column_value = associated_column_value
          else
            disassociate_column_value = associated_column_value.map { |value|
              value[association.query_field]
            }
          end

          # Disassociate old data.
          #
          association.joining_source.instance.send(
            :"by_#{association.right_foreign_key_field}",
            disassociate_column_value
          ).delete

          if associated_dataset
            associated_dataset_source = case raw_result(associated_dataset)
            when Proxy
              associated_dataset.source
            else
              associated_dataset
            end

            # Ensure that has_one through associations only have one associated object.
            #
            if association.result_type == :one
              joined_column_value = association.associated_source.container.connection.adapter.restrict_to_attribute(
                association.associated_source.primary_key_field, associated_dataset_source
              )

              # If objects are located in two different connections, fetch the raw values.
              #
              unless association.joining_source.container.connection == association.associated_source.container.connection
                joined_column_value = joined_column_value.map { |value|
                  value[association.associated_source.primary_key_field]
                }
              end

              association.joining_source.instance.send(
                :"by_#{association.left_foreign_key_field}",
                joined_column_value
              ).delete
            end

            # Associate the correct data.
            #
            associated_column_value.each do |result|
              association.associated_source.container.connection.adapter.restrict_to_attribute(
                association.associated_source.primary_key_field, associated_dataset_source
              ).each do |associated_result|
                association.joining_source.instance.command(:create).call(
                  association.left_foreign_key_field => associated_result[association.associated_source.primary_key_field],
                  association.right_foreign_key_field => result[association.source.primary_key_field]
                )
              end
            end
          end
        else
          if final_result.one
            associated_column_value = final_result.one[association.query_field]

            # Disassociate old data.
            #
            association.associated_source.instance.send(
              :"by_#{association.associated_query_field}", associated_column_value
            ).update(association.associated_query_field => nil)

            # Associate the correct data.
            #
            if associated_dataset
              associated_dataset.update(
                association.associated_query_field => associated_column_value
              )

              # Update the column value in passed objects.
              #
              case association_value
              when Proxy
                association_value.source.reload
              when Object, Array
                Array.ensure(association_value).each do |object|
                  values = object.values.dup
                  values[association.associated_query_field] = associated_column_value
                  object.instance_variable_set(:@values, values.freeze)
                end
              end
            end
          end
        end
      end
    end

    yield final_result if block_given?
    final_result
  end
end