Skip to content

Commit

Permalink
use regex instead of String.split to match query terms, #40 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Mar 6, 2019
1 parent 8c6d232 commit c934649
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use Mix.Config

config :alog, Alog.Repo,
username: "postgres",
password: "postgres",
database: "test_app_dev",
password: "docker",
database: "routinedb",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox,
priv: "priv/repo/test_app/"
Expand Down
25 changes: 23 additions & 2 deletions lib/alog/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,29 @@ defmodule Alog.Connection do
query
end

defp distinct_entry_id("SELECT " <> query) do
IO.iodata_to_binary(["SELECT ", "DISTINCT ON (\"entry_id\" ) ", query])
# defp distinct_entry_id("SELECT " <> fields <> " FROM " <> table_name <> " AS " <> table_as <> " " <> rest_query) do
#
# IO.iodata_to_binary(["SELECT ", "DISTINCT ON (#{table_as}\".entry_id\" ) ", query])
# end

defp distinct_entry_id(query) do
query_data = get_query_data(query)
if (query_data["table_name"] == "\"schema_migrations\"") do
query
else
IO.iodata_to_binary(
[ "SELECT DISTINCT ON (#{query_data["table_as"]}.\"entry_id\" ) ",
query_data["fields"],
" FROM ",
query_data["table_name"], " AS ", query_data["table_as"],
query_data["rest_query"]
]
)
end
end

defp get_query_data(query) do
Regex.named_captures(~r/(\bSELECT\b)\s(?<fields>.*)\sFROM\s(?<table_name>.*)\sas\s(?<table_as>.*)(?<rest_query>.*)/i, query)
end

@impl true
Expand Down

0 comments on commit c934649

Please sign in to comment.