Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BuildQuery): Turn SQL Queries into Ecto.Query #6

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 68 additions & 92 deletions lib/query_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,80 @@ defmodule KinoEcto.QueryBuilder do
import Ecto.Query
alias KinoEcto.QueryBuilder.Renderer

defmodule MyParser do
import NimbleParsec

@alphanumeric [?a..?z, ?A..?Z, ?0..?9, ?_, ?*, ?.]
@string_prefix [?']

whitespace = ascii_string([?\s, ?\n], min: 1)
select_choice = choice([string("SELECT"), string("select")])
from_choice = choice([string("FROM"), string("from")])
join_choice = choice([string("JOIN"), string("join")])
on_choice = choice([string("ON"), string("on")])
where_choice = choice([string("WHERE"), string("where")])
comparisson_choice = choice([string("="), string(">="), string("<="), string("<>")])

from_part =
select_choice
|> ignore(whitespace)
|> ascii_string(@alphanumeric, min: 1)
|> ignore(whitespace)
|> optional(from_choice)
|> ignore(whitespace)
|> optional(ascii_string(@alphanumeric, min: 1))

join_part =
ignore(from_part)
|> ignore(whitespace)
|> optional(join_choice)
|> ignore(whitespace)
|> ascii_string(@alphanumeric, min: 1)
|> ignore(whitespace)
|> optional(on_choice)
|> ignore(whitespace)
|> ascii_string(@alphanumeric, min: 1)
|> ignore(whitespace)
|> string("=")
|> ignore(whitespace)
|> ascii_string(@alphanumeric, min: 1)
|> optional(ignore(whitespace))

where_part =
ignore(from_part)
|> optional(ignore(join_part))
|> ignore(whitespace)
|> optional(where_choice)
|> ignore(whitespace)
|> ascii_string(@alphanumeric, min: 1)
|> ignore(whitespace)
|> optional(comparisson_choice)
|> ignore(whitespace)
|> optional(ascii_string(@string_prefix, min: 0, max: 1))
|> ascii_string(@alphanumeric, min: 1)
|> optional(ascii_string(@string_prefix, min: 0, max: 1))

defparsec(:from_part, from_part)
defparsec(:join_part, optional(join_part))
defparsec(:where_part, where_part)
# def call(%__MODULE__{sql_query: query}) do
# query = String.downcase(query)
# {:ok, tokens, _} = :build_query_lexer.string(query)
# {:ok, ast} = :build_query_parser.parse(tokens)
# ast
# end

defmodule KinoEcto.QueryBuilder.Node do
defstruct [:name, :value, :lhs, :rhs]
end

def test(query) do
from_part = MyParser.from_part(query)
join_part = MyParser.join_part(query)
where_part = MyParser.where_part(query)
def call(query) do
query =
query
|> String.downcase()
|> String.to_charlist()

[elem(from_part, 1), elem(join_part, 1), elem(where_part, 1)]
end
{:ok, tokens, _} = :build_query_lexer.string(query)
{:ok, ast} = :build_query_parser.parse(tokens)

def call(%__MODULE__{sql_query: query}) do
from_part = MyParser.from_part(query)
join_part = MyParser.join_part(query)
where_part = MyParser.where_part(query)
ast
|> cleanup()
|> build_query()

from = elem(from_part, 1)
join_part = elem(join_part, 1)
where_part = elem(where_part, 1)
# [, elem(join_part, 1), elem(where_part, 1)]
# [:select, [:fields, [:all]], [:from, ["customers"]]]
# [:select, [:fields, ["name", "age"]], [:from, ["customers"]]]
end

build_from(from)
|> build_join(join_part)
|> build_where(where_part)
|> Renderer.call()
defp cleanup(ast) when is_tuple(ast) do
ast
|> Tuple.to_list()
|> Enum.map(&cleanup/1)
|> Enum.reject(&is_nil/1)
end

defp build_from(from_part) do
result = %Ecto.Query{
from: %Ecto.Query.FromExpr{
source: get_source(List.last(from_part))
}
}
defp cleanup(ast) when is_list(ast), do: to_string(ast)
defp cleanup(ast), do: ast

defp build_query(ast) do
dbg()

Ecto.Query.from(result)
ast
|> Enum.reduce(%Ecto.Query{}, fn item, acc ->
acc = do_build_query(item, acc)
acc
end)
end

defp build_join(query, []), do: query
defp do_build_query(:select, query), do: query

defp do_build_query([:fields, [:all]], query), do: query

defp do_build_query([:fields, fields], query) do
flatten_fields = List.flatten(fields) |> Enum.map(&String.to_atom/1)

defp build_join(query, _join_part) do
query
|> select(^flatten_fields)
end

defp build_where(query, []), do: query
defp do_build_query([:from, [table]], query) do
query
|> build_from(table)
end

defp build_from(query, from_part) do
result = %{
query
| from: %Ecto.Query.FromExpr{
source: get_source(from_part)
}
}

result
end

defp build_where(query, [_where, field_name, "=" | tail]) do
tail = tail |> Enum.join() |> String.replace("'", "")
Expand All @@ -114,6 +89,7 @@ defmodule KinoEcto.QueryBuilder do
{:ok, modules} = :application.get_key(:kino_ecto, :modules)

modules
|> Enum.reject(fn item -> item == :build_query_lexer || item == :build_query_parser end)
|> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
|> Enum.find(fn module -> module.__schema__(:source) == table_name end)
|> then(fn schema -> {table_name, schema} end)
Expand Down Expand Up @@ -149,14 +125,14 @@ defmodule KinoEcto.QueryBuilder do
# %Ecto.Query.BooleanExpr{
# end

# defp get_source(table_name) do
# {:ok, modules} = :application.get_key(:kino_ecto, :modules)
# defp get_source(table_name) do
# {:ok, modules} = :application.get_key(:kino_ecto, :modules)

# modules
# |> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
# |> Enum.find(fn module -> module.__schema__(:source) == table_name end)
# |> IO.inspect()
# |> then(fn schema -> {table_name, schema} end)
# end
# modules
# |> Enum.filter(&({:__schema__, 1} in &1.__info__(:functions)))
# |> Enum.find(fn module -> module.__schema__(:source) == table_name end)
# |> IO.inspect()
# |> then(fn schema -> {table_name, schema} end)
# end
# end
end
9 changes: 9 additions & 0 deletions lib/query_builder/domain/customer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule KinoEcto.QueryBuilder.Domain.Customer do
use Ecto.Schema

schema "customers" do
field(:name, :string)
field(:age, :string)
has_many(:sales, KinoEcto.QueryBuilder.Domain.Sale)
end
end
8 changes: 8 additions & 0 deletions lib/query_builder/domain/sale.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule KinoEcto.QueryBuilder.Domain.Sale do
use Ecto.Schema

schema "sales" do
field(:total, :string)
belongs_to(:customer, KinoEcto.QueryBuilder.Domain.Customer)
end
end
Loading