Skip to content

Commit 9cdc5bf

Browse files
committed
Only compute expression to string if necessary
1 parent 576665f commit 9cdc5bf

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

lib/ecto/query/builder.ex

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ defmodule Ecto.Query.Builder do
375375

376376
params =
377377
params
378-
|> wrap_nil(escaped_left, Macro.to_string(right))
379-
|> wrap_nil(escaped_right, Macro.to_string(left))
378+
|> wrap_nil(escaped_left, right)
379+
|> wrap_nil(escaped_right, left)
380380

381381
{{:{}, [], [comp_op, [], [escaped_left, escaped_right]]}, {params, acc}}
382382
end
@@ -591,18 +591,20 @@ defmodule Ecto.Query.Builder do
591591
defp validate_json_field!(unsupported_field),
592592
do: error!("`#{Macro.to_string(unsupported_field)}` is not a valid json field")
593593

594-
defp wrap_nil(params, {:{}, _, [:^, _, [ix]]}, compare_str),
595-
do: wrap_nil(params, length(params) - ix - 1, compare_str, [])
594+
defp wrap_nil(params, {:{}, _, [:^, _, [ix]]}, to_compare),
595+
do: wrap_nil(params, length(params) - ix - 1, to_compare, [])
596596

597-
defp wrap_nil(params, _other, _compare_str), do: params
597+
defp wrap_nil(params, _other, _to_compare), do: params
598+
599+
defp wrap_nil([{val, type} | params], 0, to_compare, acc) do
600+
val =
601+
quote do: Ecto.Query.Builder.not_nil!(unquote(val), unquote(Macro.to_string(to_compare)))
598602

599-
defp wrap_nil([{val, type} | params], 0, compare_str, acc) do
600-
val = quote do: Ecto.Query.Builder.not_nil!(unquote(val), unquote(compare_str))
601603
Enum.reverse(acc, [{val, type} | params])
602604
end
603605

604-
defp wrap_nil([pair | params], i, compare_str, acc) do
605-
wrap_nil(params, i - 1, compare_str, [pair | acc])
606+
defp wrap_nil([pair | params], i, to_compare, acc) do
607+
wrap_nil(params, i - 1, to_compare, [pair | acc])
606608
end
607609

608610
defp expand_and_split_fragment(query, env) do
@@ -879,8 +881,9 @@ defmodule Ecto.Query.Builder do
879881
do: {find_var!(var, vars), field}
880882

881883
def validate_type!({:field, _, [{var, _, context}, field]}, vars, _env)
882-
when is_atom(var) and is_atom(context) and (is_atom(field) or is_binary(field)),
883-
do: {find_var!(var, vars), field}
884+
when is_atom(var) and is_atom(context) and (is_atom(field) or is_binary(field)),
885+
do: {find_var!(var, vars), field}
886+
884887
def validate_type!({:field, _, [{var, _, context}, {:^, _, [field]}]}, vars, _env)
885888
when is_atom(var) and is_atom(context),
886889
do: {find_var!(var, vars), field}
@@ -1155,10 +1158,9 @@ defmodule Ecto.Query.Builder do
11551158
do:
11561159
error!(
11571160
"expected literal atom or string or interpolated value in #{used_ref}, got: " <>
1158-
"`#{Macro.to_string(other)}`"
1161+
"`#{Macro.to_string(other)}`"
11591162
)
11601163

1161-
11621164
@doc """
11631165
Called by escaper at runtime to verify that value is an atom.
11641166
"""
@@ -1178,7 +1180,7 @@ defmodule Ecto.Query.Builder do
11781180
do: string
11791181

11801182
def atom_or_string!(other, used_ref),
1181-
do: error!("expected atom or string in #{used_ref}, got: `#{inspect other}`")
1183+
do: error!("expected atom or string in #{used_ref}, got: `#{inspect(other)}`")
11821184

11831185
@doc """
11841186
Checks if the value of a late binding is an interpolation or
@@ -1365,8 +1367,8 @@ defmodule Ecto.Query.Builder do
13651367
end
13661368

13671369
def quoted_type({:field, _, [{var, _, context}, field]}, vars)
1368-
when is_atom(var) and is_atom(context) and (is_atom(field) or is_binary(field)),
1369-
do: {find_var!(var, vars), field}
1370+
when is_atom(var) and is_atom(context) and (is_atom(field) or is_binary(field)),
1371+
do: {find_var!(var, vars), field}
13701372

13711373
def quoted_type({:field, _, [{kind, _, [value]}, field]}, _vars)
13721374
when kind in [:as, :parent_as] and (is_atom(field) or is_binary(field)) do

0 commit comments

Comments
 (0)