/
association.ex
1711 lines (1398 loc) · 54.5 KB
/
association.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Ecto.Query, only: [from: 1, from: 2, join: 4, join: 5, distinct: 3, where: 3]
defmodule Ecto.Association.NotLoaded do
@moduledoc """
Struct returned by associations when they are not loaded.
The fields are:
* `__field__` - the association field in `owner`
* `__owner__` - the schema that owns the association
* `__cardinality__` - the cardinality of the association
"""
@type t :: %__MODULE__{
__field__: atom(),
__owner__: any(),
__cardinality__: atom()
}
defstruct [:__field__, :__owner__, :__cardinality__]
defimpl Inspect do
def inspect(not_loaded, _opts) do
msg = "association #{inspect(not_loaded.__field__)} is not loaded"
~s(#Ecto.Association.NotLoaded<#{msg}>)
end
end
end
defmodule Ecto.Association do
@moduledoc false
@type t :: %{
required(:__struct__) => atom,
required(:on_cast) => nil | fun,
required(:cardinality) => :one | :many,
required(:relationship) => :parent | :child,
required(:owner) => atom,
required(:owner_key) => atom,
required(:field) => atom,
required(:unique) => boolean,
optional(atom) => any
}
alias Ecto.Query.Builder.OrderBy
@doc """
Helper to check if a queryable is compiled.
"""
def ensure_compiled(queryable, env) do
if not is_atom(queryable) or queryable in env.context_modules do
:skip
else
case Code.ensure_compiled(queryable) do
{:module, _} -> :compiled
{:error, :unavailable} -> :skip
{:error, _} -> :not_found
end
end
end
@doc """
Builds the association struct.
The struct must be defined in the module that implements the
callback and it must contain at least the following keys:
* `:cardinality` - tells if the association is one to one
or one/many to many
* `:field` - tells the field in the owner struct where the
association should be stored
* `:owner` - the owner module of the association
* `:owner_key` - the key in the owner with the association value
* `:relationship` - if the relationship to the specified schema is
of a `:child` or a `:parent`
"""
@callback struct(module, field :: atom, opts :: Keyword.t()) :: t
@doc """
Invoked after the schema is compiled to validate associations.
Useful for checking if associated modules exist without running
into deadlocks.
"""
@callback after_compile_validation(t, Macro.Env.t()) :: :ok | {:error, String.t()}
@doc """
Builds a struct for the given association.
The struct to build from is given as argument in case default values
should be set in the struct.
Invoked by `Ecto.build_assoc/3`.
"""
@callback build(t, owner :: Ecto.Schema.t(), %{atom => term} | [Keyword.t()]) :: Ecto.Schema.t()
@doc """
Returns an association join query.
This callback receives the association struct and it must return
a query that retrieves all associated entries using joins up to
the owner association.
For example, a `has_many :comments` inside a `Post` module would
return:
from c in Comment, join: p in Post, on: c.post_id == p.id
Note all the logic must be expressed inside joins, as fields like
`where` and `order_by` won't be used by the caller.
This callback is invoked when `join: assoc(p, :comments)` is used
inside queries.
"""
@callback joins_query(t) :: Ecto.Query.t()
@doc """
Returns the association query on top of the given query.
If the query is `nil`, the association target must be used.
This callback receives the association struct and it must return
a query that retrieves all associated entries with the given
values for the owner key.
This callback is used by `Ecto.assoc/2` and when preloading.
"""
@callback assoc_query(t, Ecto.Query.t() | nil, values :: [term]) :: Ecto.Query.t()
@doc """
Returns information used by the preloader.
"""
@callback preload_info(t) ::
{:assoc, t, {integer, atom} | {integer, atom, Ecto.Type.t()}}
| {:through, t, [atom]}
@doc """
Performs the repository change on the association.
Receives the parent changeset, the current changesets
and the repository action options. Must return the
persisted struct (or nil) or the changeset error.
"""
@callback on_repo_change(
t,
parent :: Ecto.Changeset.t(),
changeset :: Ecto.Changeset.t(),
Ecto.Adapter.t(),
Keyword.t()
) ::
{:ok, Ecto.Schema.t() | nil} | {:error, Ecto.Changeset.t()}
@doc """
Retrieves the association from the given schema.
"""
def association_from_schema!(schema, assoc) do
schema.__schema__(:association, assoc) ||
raise ArgumentError, "schema #{inspect(schema)} does not have association #{inspect(assoc)}"
end
@doc """
Returns the association key for the given module with the given suffix.
## Examples
iex> Ecto.Association.association_key(Hello.World, :id)
:world_id
iex> Ecto.Association.association_key(Hello.HTTP, :id)
:http_id
iex> Ecto.Association.association_key(Hello.HTTPServer, :id)
:http_server_id
"""
def association_key(module, suffix) do
prefix = module |> Module.split() |> List.last() |> Macro.underscore()
:"#{prefix}_#{suffix}"
end
@doc """
Build an association query through the given associations from the specified owner table
and through the given associations. Finally filter by the provided values of the owner_key of
the first relationship in the chain. Used in Ecto.assoc/2.
"""
def filter_through_chain(owner, through, values) do
chain_through(owner, through, nil, values)
|> distinct([x], true)
end
@doc """
Join the target table given a list of associations to go through starting from the owner table.
"""
def join_through_chain(owner, through, query) do
chain_through(owner, through, query, nil)
end
# This function is used by both join_through_chain/3 and filter_through_chain/3 since the algorithm for both
# is nearly identical barring a few differences.
defp chain_through(owner, through, join_to, values) do
# Flatten the chain of throughs. If any of the associations is a HasThrough this allows us to expand it so we have
# a list of atomic associations to join through.
{_, through} = flatten_through_chain(owner, through, [])
# If we're joining then we're going forward from the owner table to the destination table.
# Otherwise we're going backward from the destination table then filtering by values.
chain_direction = if(join_to != nil, do: :forward, else: :backward)
# This stage produces a list of joins represented as a keyword list with the following structure:
# [
# [schema: (The Schema), in_key: (The key used to join into the table), out_key: (The key used to join with the next), where: (The condition KW list)]
# ]
relation_list = resolve_through_tables(owner, through, chain_direction)
# Filter out the joins which are redundant
filtered_list =
Enum.with_index(relation_list)
|> Enum.filter(fn
# We always keep the first table in the chain since it's our source table for the query
{_, 0} ->
true
{rel, _} ->
# If the condition is not empty we need to join to the table. Otherwise if the in_key and out_key is the same
# then this join is redundant since we can just join to the next table in the chain.
rel.in_key != rel.out_key or rel.where != []
end)
|> Enum.map(&elem(&1, 0))
# If we're preloading we don't need the last table since it is the owner table.
filtered_list = if(join_to == nil, do: Enum.drop(filtered_list, -1), else: filtered_list)
[source | joins] = filtered_list
source_schema = source.schema
query = join_to || from(s in source_schema)
counter = Ecto.Query.Builder.count_binds(query) - 1
# We need to create the query by joining all the tables, and also we need the out_key of the final table to use
# for the final WHERE clause with values.
{_, query, _, dest_out_key} =
Enum.reduce(joins, {source, query, counter, source.out_key}, fn
curr_rel, {prev_rel, query, counter, _} ->
related_queryable = curr_rel.schema
next =
join(query, :inner, [{src, counter}], dest in ^related_queryable,
on: field(src, ^prev_rel.out_key) == field(dest, ^curr_rel.in_key)
)
|> combine_joins_query(curr_rel.where, counter + 1)
{curr_rel, next, counter + 1, curr_rel.out_key}
end)
final_bind = Ecto.Query.Builder.count_binds(query) - 1
values = List.wrap(values)
query =
case {join_to, values} do
{nil, [single_value]} ->
where(query, [{dest, final_bind}], field(dest, ^dest_out_key) == ^single_value)
{nil, values} ->
where(query, [{dest, final_bind}], field(dest, ^dest_out_key) in ^values)
{_, _} ->
query
end
combine_assoc_query(query, source.where || [])
end
defp flatten_through_chain(owner, [], acc), do: {owner, acc}
defp flatten_through_chain(owner, [assoc | tl], acc) do
refl = association_from_schema!(owner, assoc)
case refl do
%{through: nested_throughs} ->
{owner, acc} = flatten_through_chain(owner, nested_throughs, acc)
flatten_through_chain(owner, tl, acc)
_ ->
flatten_through_chain(refl.related, tl, acc ++ [assoc])
end
end
defp resolve_through_tables(owner, through, :backward) do
# This step generates a list of maps with the following keys:
# [
# %{schema: ..., out_key: ..., in_key: ..., where: ...}
# ]
# This is a list of all tables that we will need to join to follow the chain of throughs and which key is used
# to join in and out of the table, along with the where condition for that table. The final table of the chain will
# be "owner", and the first table of the chain will be the final destination table of all the throughs.
initial_owner_map = %{schema: owner, out_key: nil, in_key: nil, where: nil}
Enum.reduce(through, {owner, [initial_owner_map]}, fn assoc, {owner, table_list} ->
refl = association_from_schema!(owner, assoc)
[owner_map | table_list] = table_list
table_list =
case refl do
%{
join_through: join_through,
join_keys: join_keys,
join_where: join_where,
where: where
} ->
[{owner_join_key, owner_key}, {related_join_key, related_key}] = join_keys
owner_map = %{owner_map | in_key: owner_key}
join_map = %{
schema: join_through,
out_key: owner_join_key,
in_key: related_join_key,
where: join_where
}
related_map = %{schema: refl.related, out_key: related_key, in_key: nil, where: where}
[related_map, join_map, owner_map | table_list]
_ ->
owner_map = %{owner_map | in_key: refl.owner_key}
related_map = %{
schema: refl.related,
out_key: refl.related_key,
in_key: nil,
where: refl.where
}
[related_map, owner_map | table_list]
end
{refl.related, table_list}
end)
|> elem(1)
end
defp resolve_through_tables(owner, through, :forward) do
# In the forward case (joining) we need to reverse the list and swap the in_key for the out_key
# since we've changed directions.
resolve_through_tables(owner, through, :backward)
|> Enum.reverse()
|> Enum.map(fn %{out_key: out_key, in_key: in_key} = join ->
%{join | out_key: in_key, in_key: out_key}
end)
end
@doc """
Add the default assoc query where clauses to a join.
This handles only `where` and converts it to a `join`,
as that is the only information propagate in join queries.
"""
def combine_joins_query(query, [], _binding), do: query
def combine_joins_query(%{joins: joins} = query, [_ | _] = conditions, binding) do
{joins, [join_expr]} = Enum.split(joins, -1)
%{on: %{params: params, expr: expr} = join_on} = join_expr
{expr, params} = expand_where(conditions, expr, Enum.reverse(params), length(params), binding)
%{query | joins: joins ++ [%{join_expr | on: %{join_on | expr: expr, params: params}}]}
end
@doc """
Add the default assoc query where clauses a provided query.
"""
def combine_assoc_query(query, []), do: query
def combine_assoc_query(%{wheres: []} = query, conditions) do
{expr, params} = expand_where(conditions, true, [], 0, 0)
bool_expr = %Ecto.Query.BooleanExpr{
op: :and,
expr: expr,
params: params,
line: __ENV__.line,
file: __ENV__.file
}
%{query | wheres: [bool_expr]}
end
def combine_assoc_query(%{wheres: wheres} = query, conditions) do
{wheres, [where_expr]} = Enum.split(wheres, -1)
%{params: params, expr: expr} = where_expr
{expr, params} = expand_where(conditions, expr, Enum.reverse(params), length(params), 0)
%{query | wheres: wheres ++ [%{where_expr | expr: expr, params: params}]}
end
defp expand_where(conditions, expr, params, counter, binding) do
conjoin_exprs = fn
true, r -> r
l, r -> {:and, [], [l, r]}
end
{expr, params, _counter} =
Enum.reduce(conditions, {expr, params, counter}, fn
{key, nil}, {expr, params, counter} ->
expr = conjoin_exprs.(expr, {:is_nil, [], [to_field(binding, key)]})
{expr, params, counter}
{key, {:not, nil}}, {expr, params, counter} ->
expr = conjoin_exprs.(expr, {:not, [], [{:is_nil, [], [to_field(binding, key)]}]})
{expr, params, counter}
{key, {:fragment, frag}}, {expr, params, counter} when is_binary(frag) ->
pieces = Ecto.Query.Builder.fragment_pieces(frag, [to_field(binding, key)])
expr = conjoin_exprs.(expr, {:fragment, [], pieces})
{expr, params, counter}
{key, {:in, value}}, {expr, params, counter} when is_list(value) ->
expr = conjoin_exprs.(expr, {:in, [], [to_field(binding, key), {:^, [], [counter]}]})
{expr, [{value, {:in, {binding, key}}} | params], counter + 1}
{key, value}, {expr, params, counter} ->
expr = conjoin_exprs.(expr, {:==, [], [to_field(binding, key), {:^, [], [counter]}]})
{expr, [{value, {binding, key}} | params], counter + 1}
end)
{expr, Enum.reverse(params)}
end
defp to_field(binding, field),
do: {{:., [], [{:&, [], [binding]}, field]}, [], []}
@doc """
Build a join query with the given `through` associations starting at `counter`.
"""
def joins_query(query, through, counter) do
Enum.reduce(through, {query, counter}, fn current, {acc, counter} ->
query = join(acc, :inner, [{x, counter}], assoc(x, ^current))
{query, counter + 1}
end)
|> elem(0)
end
@doc """
Retrieves related module from queryable.
## Examples
iex> Ecto.Association.related_from_query({"custom_source", Schema}, :comments_v1)
Schema
iex> Ecto.Association.related_from_query(Schema, :comments_v1)
Schema
iex> Ecto.Association.related_from_query("wrong", :comments_v1)
** (ArgumentError) association :comments_v1 queryable must be a schema or a {source, schema}. got: "wrong"
"""
def related_from_query(atom, _name) when is_atom(atom), do: atom
def related_from_query({source, schema}, _name) when is_binary(source) and is_atom(schema),
do: schema
def related_from_query(queryable, name) do
raise ArgumentError,
"association #{inspect(name)} queryable must be a schema or " <>
"a {source, schema}. got: #{inspect(queryable)}"
end
@doc """
Applies default values into the struct.
"""
def apply_defaults(struct, defaults, _owner) when is_list(defaults) do
struct(struct, defaults)
end
def apply_defaults(struct, {mod, fun, args}, owner) do
apply(mod, fun, [struct.__struct__(), owner | args])
end
@doc """
Validates `defaults` for association named `name`.
"""
def validate_defaults!(_module, _name, {mod, fun, args} = defaults)
when is_atom(mod) and is_atom(fun) and is_list(args),
do: defaults
def validate_defaults!(module, _name, fun) when is_atom(fun),
do: {module, fun, []}
def validate_defaults!(_module, _name, defaults) when is_list(defaults),
do: defaults
def validate_defaults!(_module, name, defaults) do
raise ArgumentError,
"expected defaults for #{inspect(name)} to be a keyword list " <>
"or a {module, fun, args} tuple, got: `#{inspect(defaults)}`"
end
@doc """
Validates `preload_order` for association named `name`.
"""
def validate_preload_order!(_name, {mod, fun, args} = preload_order)
when is_atom(mod) and is_atom(fun) and is_list(args),
do: preload_order
def validate_preload_order!(name, preload_order) when is_list(preload_order) do
Enum.map(preload_order, fn
field when is_atom(field) ->
field
{direction, field} when is_atom(direction) and is_atom(field) ->
unless OrderBy.valid_direction?(direction) do
raise ArgumentError,
"expected `:preload_order` for #{inspect(name)} to be a keyword list or a list of atoms/fields, " <>
"got: `#{inspect(preload_order)}`, " <>
"`#{inspect(direction)}` is not a valid direction"
end
{direction, field}
item ->
raise ArgumentError,
"expected `:preload_order` for #{inspect(name)} to be a keyword list or a list of atoms/fields, " <>
"got: `#{inspect(preload_order)}`, " <>
"`#{inspect(item)}` is not valid"
end)
end
def validate_preload_order!(name, preload_order) do
raise ArgumentError,
"expected `:preload_order` for #{inspect(name)} to be a keyword list, a list of atoms/fields " <>
"or a {Mod, fun, args} tuple, got: `#{inspect(preload_order)}`"
end
@doc """
Merges source from query into to the given schema.
In case the query does not have a source, returns
the schema unchanged.
"""
def merge_source(schema, query)
def merge_source(%{__meta__: %{source: source}} = struct, {source, _}) do
struct
end
def merge_source(struct, {source, _}) do
Ecto.put_meta(struct, source: source)
end
def merge_source(struct, _query) do
struct
end
@doc """
Updates the prefix of a changeset based on the metadata.
"""
def update_parent_prefix(
%{data: %{__meta__: %{prefix: prefix}}} = changeset,
%{__meta__: %{prefix: prefix}}
),
do: changeset
def update_parent_prefix(
%{data: %{__meta__: %{prefix: nil}}} = changeset,
%{__meta__: %{prefix: prefix}}
),
do: update_in(changeset.data, &Ecto.put_meta(&1, prefix: prefix))
def update_parent_prefix(changeset, _),
do: changeset
@doc """
Performs the repository action in the related changeset,
returning `{:ok, data}` or `{:error, changes}`.
"""
def on_repo_change(%{data: struct}, [], _adapter, _opts) do
{:ok, struct}
end
def on_repo_change(changeset, assocs, adapter, opts) do
%{data: struct, changes: changes, action: action} = changeset
{struct, changes, _halt, valid?} =
Enum.reduce(assocs, {struct, changes, false, true}, fn {refl, value}, acc ->
on_repo_change(refl, value, changeset, action, adapter, opts, acc)
end)
case valid? do
true -> {:ok, struct}
false -> {:error, changes}
end
end
defp on_repo_change(
%{cardinality: :one, field: field} = meta,
nil,
parent_changeset,
_repo_action,
adapter,
opts,
{parent, changes, halt, valid?}
) do
if not halt, do: maybe_replace_one!(meta, nil, parent, parent_changeset, adapter, opts)
{Map.put(parent, field, nil), Map.put(changes, field, nil), halt, valid?}
end
defp on_repo_change(
%{cardinality: :one, field: field, __struct__: mod} = meta,
%{action: action, data: current} = changeset,
parent_changeset,
repo_action,
adapter,
opts,
{parent, changes, halt, valid?}
) do
check_action!(meta, action, repo_action)
if not halt, do: maybe_replace_one!(meta, current, parent, parent_changeset, adapter, opts)
case on_repo_change_unless_halted(halt, mod, meta, parent_changeset, changeset, adapter, opts) do
{:ok, struct} ->
{Map.put(parent, field, struct), Map.put(changes, field, changeset), halt, valid?}
{:error, error_changeset} ->
{parent, Map.put(changes, field, error_changeset),
halted?(halt, changeset, error_changeset), false}
end
end
defp on_repo_change(
%{cardinality: :many, field: field, __struct__: mod} = meta,
changesets,
parent_changeset,
repo_action,
adapter,
opts,
{parent, changes, halt, all_valid?}
) do
{changesets, structs, halt, valid?} =
Enum.reduce(changesets, {[], [], halt, true}, fn
%{action: action} = changeset, {changesets, structs, halt, valid?} ->
check_action!(meta, action, repo_action)
case on_repo_change_unless_halted(
halt,
mod,
meta,
parent_changeset,
changeset,
adapter,
opts
) do
{:ok, nil} ->
{[changeset | changesets], structs, halt, valid?}
{:ok, struct} ->
{[changeset | changesets], [struct | structs], halt, valid?}
{:error, error_changeset} ->
{[error_changeset | changesets], structs, halted?(halt, changeset, error_changeset),
false}
end
end)
if valid? do
{Map.put(parent, field, Enum.reverse(structs)),
Map.put(changes, field, Enum.reverse(changesets)), halt, all_valid?}
else
{parent, Map.put(changes, field, Enum.reverse(changesets)), halt, false}
end
end
defp check_action!(%{related: schema}, :delete, :insert) do
raise ArgumentError,
"got action :delete in changeset for associated #{inspect(schema)} while inserting"
end
defp check_action!(_, _, _), do: :ok
defp halted?(true, _, _), do: true
defp halted?(_, %{valid?: true}, %{valid?: false}), do: true
defp halted?(_, _, _), do: false
defp on_repo_change_unless_halted(true, _mod, _meta, _parent, changeset, _adapter, _opts) do
{:error, changeset}
end
defp on_repo_change_unless_halted(false, mod, meta, parent, changeset, adapter, opts) do
mod.on_repo_change(meta, parent, changeset, adapter, opts)
end
defp maybe_replace_one!(
%{field: field, __struct__: mod} = meta,
current,
parent,
parent_changeset,
adapter,
opts
) do
previous = Map.get(parent, field)
if replaceable?(previous) and primary_key!(previous) != primary_key!(current) do
changeset = %{Ecto.Changeset.change(previous) | action: :replace}
case mod.on_repo_change(meta, parent_changeset, changeset, adapter, opts) do
{:ok, _} ->
:ok
{:error, changeset} ->
raise Ecto.InvalidChangesetError, action: changeset.action, changeset: changeset
end
end
end
defp maybe_replace_one!(_, _, _, _, _, _), do: :ok
defp replaceable?(nil), do: false
defp replaceable?(%Ecto.Association.NotLoaded{}), do: false
defp replaceable?(%{__meta__: %{state: :built}}), do: false
defp replaceable?(_), do: true
defp primary_key!(nil), do: []
defp primary_key!(struct), do: Ecto.primary_key!(struct)
end
defmodule Ecto.Association.Has do
@moduledoc """
The association struct for `has_one` and `has_many` associations.
Its fields are:
* `cardinality` - The association cardinality
* `field` - The name of the association field on the schema
* `owner` - The schema where the association was defined
* `related` - The schema that is associated
* `owner_key` - The key on the `owner` schema used for the association
* `related_key` - The key on the `related` schema used for the association
* `queryable` - The real query to use for querying association
* `on_delete` - The action taken on associations when schema is deleted
* `on_replace` - The action taken on associations when schema is replaced
* `defaults` - Default fields used when building the association
* `relationship` - The relationship to the specified schema, default is `:child`
* `preload_order` - Default `order_by` of the association, used only by preload
"""
@behaviour Ecto.Association
@on_delete_opts [:nothing, :nilify_all, :delete_all]
@on_replace_opts [:raise, :mark_as_invalid, :delete, :delete_if_exists, :nilify]
@has_one_on_replace_opts @on_replace_opts ++ [:update]
defstruct [
:cardinality,
:field,
:owner,
:related,
:owner_key,
:related_key,
:on_cast,
:queryable,
:on_delete,
:on_replace,
where: [],
unique: true,
defaults: [],
relationship: :child,
ordered: false,
preload_order: []
]
@impl true
def after_compile_validation(%{queryable: queryable, related_key: related_key}, env) do
compiled = Ecto.Association.ensure_compiled(queryable, env)
cond do
compiled == :skip ->
:ok
compiled == :not_found ->
{:error, "associated schema #{inspect(queryable)} does not exist"}
not function_exported?(queryable, :__schema__, 2) ->
{:error, "associated module #{inspect(queryable)} is not an Ecto schema"}
is_nil(queryable.__schema__(:type, related_key)) ->
{:error, "associated schema #{inspect(queryable)} does not have field `#{related_key}`"}
true ->
:ok
end
end
@impl true
def struct(module, name, opts) do
queryable = Keyword.fetch!(opts, :queryable)
cardinality = Keyword.fetch!(opts, :cardinality)
related = Ecto.Association.related_from_query(queryable, name)
ref =
module
|> Module.get_attribute(:primary_key)
|> get_ref(opts[:references], name)
unless Module.get_attribute(module, :ecto_fields)[ref] do
raise ArgumentError,
"schema does not have the field #{inspect(ref)} used by " <>
"association #{inspect(name)}, please set the :references option accordingly"
end
if opts[:through] do
raise ArgumentError,
"invalid association #{inspect(name)}. When using the :through " <>
"option, the schema should not be passed as second argument"
end
on_delete = Keyword.get(opts, :on_delete, :nothing)
unless on_delete in @on_delete_opts do
raise ArgumentError,
"invalid :on_delete option for #{inspect(name)}. " <>
"The only valid options are: " <>
Enum.map_join(@on_delete_opts, ", ", &"`#{inspect(&1)}`")
end
on_replace = Keyword.get(opts, :on_replace, :raise)
on_replace_opts = if cardinality == :one, do: @has_one_on_replace_opts, else: @on_replace_opts
unless on_replace in on_replace_opts do
raise ArgumentError,
"invalid `:on_replace` option for #{inspect(name)}. " <>
"The only valid options are: " <>
Enum.map_join(@on_replace_opts, ", ", &"`#{inspect(&1)}`")
end
defaults = Ecto.Association.validate_defaults!(module, name, opts[:defaults] || [])
preload_order = Ecto.Association.validate_preload_order!(name, opts[:preload_order] || [])
where = opts[:where] || []
unless is_list(where) do
raise ArgumentError,
"expected `:where` for #{inspect(name)} to be a keyword list, got: `#{inspect(where)}`"
end
%__MODULE__{
field: name,
cardinality: cardinality,
owner: module,
related: related,
owner_key: ref,
related_key: opts[:foreign_key] || Ecto.Association.association_key(module, ref),
queryable: queryable,
on_delete: on_delete,
on_replace: on_replace,
defaults: defaults,
where: where,
preload_order: preload_order
}
end
defp get_ref(primary_key, nil, name) when primary_key in [nil, false] do
raise ArgumentError,
"need to set :references option for " <>
"association #{inspect(name)} when schema has no primary key"
end
defp get_ref(primary_key, nil, _name), do: elem(primary_key, 0)
defp get_ref(_primary_key, references, _name), do: references
@impl true
def build(%{owner_key: owner_key, related_key: related_key} = refl, owner, attributes) do
data = refl |> build(owner) |> struct(attributes)
%{data | related_key => Map.get(owner, owner_key)}
end
@impl true
def joins_query(
%{related_key: related_key, owner: owner, owner_key: owner_key, queryable: queryable} =
assoc
) do
from(o in owner, join: q in ^queryable, on: field(q, ^related_key) == field(o, ^owner_key))
|> Ecto.Association.combine_joins_query(assoc.where, 1)
end
@impl true
def assoc_query(%{related_key: related_key, queryable: queryable} = assoc, query, [value]) do
from(x in (query || queryable), where: field(x, ^related_key) == ^value)
|> Ecto.Association.combine_assoc_query(assoc.where)
end
@impl true
def assoc_query(%{related_key: related_key, queryable: queryable} = assoc, query, values) do
from(x in (query || queryable), where: field(x, ^related_key) in ^values)
|> Ecto.Association.combine_assoc_query(assoc.where)
end
@impl true
def preload_info(%{related_key: related_key} = refl) do
{:assoc, refl, {0, related_key}}
end
@impl true
def on_repo_change(
%{on_replace: :delete_if_exists} = refl,
parent_changeset,
%{action: :replace} = changeset,
adapter,
opts
) do
try do
on_repo_change(%{refl | on_replace: :delete}, parent_changeset, changeset, adapter, opts)
rescue
Ecto.StaleEntryError -> {:ok, nil}
end
end
def on_repo_change(
%{on_replace: on_replace} = refl,
%{data: parent} = parent_changeset,
%{action: :replace} = changeset,
adapter,
opts
) do
changeset =
case on_replace do
:nilify -> %{changeset | action: :update}
:update -> %{changeset | action: :update}
:delete -> %{changeset | action: :delete}
end
changeset = Ecto.Association.update_parent_prefix(changeset, parent)
case on_repo_change(refl, %{parent_changeset | data: nil}, changeset, adapter, opts) do
{:ok, _} -> {:ok, nil}
{:error, changeset} -> {:error, changeset}
end
end
def on_repo_change(assoc, parent_changeset, changeset, _adapter, opts) do
%{data: parent, repo: repo} = parent_changeset
%{action: action, changes: changes} = changeset
{key, value} = parent_key(assoc, parent)
changeset = update_parent_key(changeset, action, key, value)
changeset = Ecto.Association.update_parent_prefix(changeset, parent)
case apply(repo, action, [changeset, opts]) do
{:ok, _} = ok ->
if action == :delete, do: {:ok, nil}, else: ok
{:error, changeset} ->
original = Map.get(changes, key)
{:error, put_in(changeset.changes[key], original)}
end
end
defp update_parent_key(changeset, :delete, _key, _value),
do: changeset
defp update_parent_key(changeset, _action, key, value),
do: Ecto.Changeset.put_change(changeset, key, value)
defp parent_key(%{related_key: related_key}, nil) do
{related_key, nil}
end
defp parent_key(%{owner_key: owner_key, related_key: related_key}, owner) do
{related_key, Map.get(owner, owner_key)}
end
## Relation callbacks
@behaviour Ecto.Changeset.Relation
@impl true
def build(%{related: related, queryable: queryable, defaults: defaults}, owner) do
related
|> Ecto.Association.apply_defaults(defaults, owner)
|> Ecto.Association.merge_source(queryable)
end
## On delete callbacks
@doc false
def delete_all(refl, parent, repo_name, opts) do
if query = on_delete_query(refl, parent) do
Ecto.Repo.Queryable.delete_all(repo_name, query, opts)
end
end
@doc false
def nilify_all(%{related_key: related_key} = refl, parent, repo_name, opts) do
if query = on_delete_query(refl, parent) do
Ecto.Repo.Queryable.update_all(repo_name, query, [set: [{related_key, nil}]], opts)
end
end
defp on_delete_query(
%{owner_key: owner_key, related_key: related_key, queryable: queryable},
parent