From 2926e42c38610923c719183044914bae9b070427 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Mon, 27 May 2024 10:00:35 -0400 Subject: [PATCH] address PR feedback --- lib/ecto/adapters/myxql/connection.ex | 53 +++++++++++++-------------- mix.exs | 2 +- mix.lock | 2 +- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/ecto/adapters/myxql/connection.ex b/lib/ecto/adapters/myxql/connection.ex index dca60b2c..fb363b87 100644 --- a/lib/ecto/adapters/myxql/connection.ex +++ b/lib/ecto/adapters/myxql/connection.ex @@ -512,7 +512,7 @@ if Code.ensure_loaded?(MyXQL) do [ " GROUP BY " | Enum.map_intersperse(group_bys, ", ", fn %ByExpr{expr: expr} -> - Enum.map_intersperse(expr, ", ", &expr({:top_level, &1}, sources, query)) + Enum.map_intersperse(expr, ", ", &top_level_expr(&1, sources, query)) end) ] end @@ -556,7 +556,7 @@ if Code.ensure_loaded?(MyXQL) do end defp order_by_expr({dir, expr}, sources, query) do - str = expr({:top_level, expr}, sources, query) + str = top_level_expr(expr, sources, query) case dir do :asc -> str @@ -627,6 +627,21 @@ if Code.ensure_loaded?(MyXQL) do [?(, expr(expr, sources, query), ?)] end + defp top_level_expr(%Ecto.SubQuery{query: query}, sources, parent_query) do + combinations = + Enum.map(query.combinations, fn {type, combination_query} -> + {type, put_in(combination_query.aliases[@parent_as], {parent_query, sources})} + end) + + query = put_in(query.combinations, combinations) + query = put_in(query.aliases[@parent_as], {parent_query, sources}) + [all(query, subquery_as_prefix(sources))] + end + + defp top_level_expr(other, sources, parent_query) do + expr(other, sources, parent_query) + end + defp expr({:^, [], [_ix]}, _sources, _query) do ~c"?" end @@ -687,26 +702,8 @@ if Code.ensure_loaded?(MyXQL) do error!(query, "MySQL adapter does not support aggregate filters") end - defp expr({:top_level, %Ecto.SubQuery{query: query}}, sources, parent_query) do - combinations = - Enum.map(query.combinations, fn {type, combination_query} -> - {type, put_in(combination_query.aliases[@parent_as], {parent_query, sources})} - end) - - query = put_in(query.combinations, combinations) - query = put_in(query.aliases[@parent_as], {parent_query, sources}) - [all(query, subquery_as_prefix(sources))] - end - - defp expr(%Ecto.SubQuery{query: query}, sources, parent_query) do - combinations = - Enum.map(query.combinations, fn {type, combination_query} -> - {type, put_in(combination_query.aliases[@parent_as], {parent_query, sources})} - end) - - query = put_in(query.combinations, combinations) - query = put_in(query.aliases[@parent_as], {parent_query, sources}) - [?(, all(query, subquery_as_prefix(sources)), ?)] + defp expr(%Ecto.SubQuery{} = subquery, sources, parent_query) do + [?(, top_level_expr(subquery, sources, parent_query), ?)] end defp expr({:fragment, _, [kw]}, _sources, query) when is_list(kw) or tuple_size(kw) == 3 do @@ -801,14 +798,16 @@ if Code.ensure_loaded?(MyXQL) do [op_to_binary(left, sources, query), op | op_to_binary(right, sources, query)] {:fun, fun} -> - [fun, ?(, modifier, Enum.map_intersperse(args, ", ", &expr({:top_level, &1}, sources, query)), ?)] + [ + fun, + ?(, + modifier, + Enum.map_intersperse(args, ", ", &top_level_expr(&1, sources, query)), + ?) + ] end end - defp expr({:top_level, other}, sources, parent_query) do - expr(other, sources, parent_query) - end - defp expr(list, _sources, query) when is_list(list) do error!(query, "Array type is not supported by MySQL") end diff --git a/mix.exs b/mix.exs index 6d9338de..19a65349 100644 --- a/mix.exs +++ b/mix.exs @@ -76,7 +76,7 @@ defmodule EctoSQL.MixProject do if path = System.get_env("ECTO_PATH") do {:ecto, path: path} else - {:ecto, github: "zachdaniel/ecto", branch: "subqueries-in-order-by"} + {:ecto, github: "elixir-ecto/ecto"} end end diff --git a/mix.lock b/mix.lock index 57efabb0..3d059342 100644 --- a/mix.lock +++ b/mix.lock @@ -4,7 +4,7 @@ "decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"}, "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, - "ecto": {:git, "https://github.com/zachdaniel/ecto.git", "57fdecbf000720061be15c2ce24e2c6fc3ef0c3a", [branch: "subqueries-in-order-by"]}, + "ecto": {:git, "https://github.com/elixir-ecto/ecto.git", "a898915d2f16dbf1257b8c83a11a1ae07193de42", []}, "ex_doc": {:hex, :ex_doc, "0.33.0", "690562b153153c7e4d455dc21dab86e445f66ceba718defe64b0ef6f0bd83ba0", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "3f69adc28274cb51be37d09b03e4565232862a4b10288a3894587b0131412124"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "makeup": {:hex, :makeup, "1.1.2", "9ba8837913bdf757787e71c1581c21f9d2455f4dd04cfca785c70bbfff1a76a3", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cce1566b81fbcbd21eca8ffe808f33b221f9eee2cbc7a1706fc3da9ff18e6cac"},