Skip to content

Commit

Permalink
fix: split filters into proper format by first "=" and "."
Browse files Browse the repository at this point in the history
  • Loading branch information
w3b6x9 committed Jul 25, 2022
1 parent f700e0c commit 5136303
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions lib/extensions/postgres/postgres_subscriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Extensions.Postgres.Subscriptions do
@type conn() :: DBConnection.conn()

@spec create(conn(), String.t(), map()) ::
{:ok, Postgrex.Result.t()} | {:error, Postgrex.Result.t() | Exception.t()}
{:ok, Postgrex.Result.t()} | {:error, Postgrex.Result.t() | Exception.t() | String.t()}
def create(conn, publication, %{id: id, config: config, claims: claims}) do
sql = "with sub_tables as (
select
Expand Down Expand Up @@ -46,29 +46,37 @@ defmodule Extensions.Postgres.Subscriptions do
returning
id"

[schema, table, filters] =
case config do
%{"schema" => schema, "table" => table, "filter" => filter} ->
filters = [filter |> String.split(~r(\=|\.)) |> List.to_tuple()]
[schema, table, filters]

%{"schema" => schema, "table" => table} ->
[schema, table, []]

%{"schema" => schema} ->
[schema, "*", []]
end

case query(conn, sql, [publication, schema, table, id, claims, filters]) do
{:ok,
%Postgrex.Result{
num_rows: num_rows
} = result}
when num_rows > 0 ->
{:ok, result}

with [schema, table, filters] <-
(case config do
%{"schema" => schema, "table" => table, "filter" => filter} ->
with [col, rest] <- String.split(filter, "=", parts: 2),
[filter_type, value] <- String.split(rest, ".", parts: 2) do
[schema, table, [{col, filter_type, value}]]
else
_ -> []
end

%{"schema" => schema, "table" => table} ->
[schema, table, []]

%{"schema" => schema} ->
[schema, "*", []]

_ ->
[]
end),
{:ok,
%Postgrex.Result{
num_rows: num_rows
} = result}
when num_rows > 0 <- query(conn, sql, [publication, schema, table, id, claims, filters]) do
{:ok, result}
else
{_, other} ->
{:error, other}

[] ->
{:error, "malformed postgres config"}
end
end

Expand Down

0 comments on commit 5136303

Please sign in to comment.