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

Update to DBConnection 2.0 #235

Merged
merged 19 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 11 additions & 30 deletions lib/mariaex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,11 @@ defmodule Mariaex do
def query(conn, statement, params, opts) do
case Keyword.get(opts, :query_type) do
:text ->
query = %Query{type: :text, statement: statement, ref: make_ref(),
num_params: 0}
execute(conn, query, [], opts)
query = %Query{type: :text, statement: statement, ref: make_ref(), num_params: 0}
run_query(:execute, conn, query, [], opts)
type when type in [:binary, nil] ->
query = %Query{type: type, statement: statement}
prepare_execute(conn, query, params, defaults(opts))
run_query(:prepare_execute, conn, query, params, defaults(opts))
end
end

Expand Down Expand Up @@ -237,16 +236,9 @@ defmodule Mariaex do
Mariaex.execute(conn, query, ["%my%"])
"""
@spec execute(conn, Mariaex.Query.t, list, Keyword.t) ::
{:ok, Mariaex.Result.t} | {:error, Mariaex.Error.t}
{:ok, Mariaex.Query.t, Mariaex.Result.t} | {:error, Mariaex.Error.t}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can now return other exceptions in the error case?

def execute(conn, %Query{} = query, params, opts \\ []) do
case DBConnection.execute(conn, query, params, defaults(opts)) do
{:ok, _} = ok ->
ok
{:error, %Mariaex.Error{}} = error ->
error
{:error, err} ->
raise err
end
DBConnection.execute(conn, query, params, defaults(opts))
end

@doc """
Expand All @@ -255,12 +247,7 @@ defmodule Mariaex do
"""
@spec execute!(conn, Mariaex.Query.t, list, Keyword.t) :: Mariaex.Result.t
def execute!(conn, query, params, opts \\ []) do
case execute(conn, query, params, opts) do
{:ok, res} ->
res
{:error, err} ->
raise err
end
DBConnection.execute!(conn, query, params, defaults(opts))
end

@doc """
Expand Down Expand Up @@ -291,10 +278,8 @@ defmodule Mariaex do
case DBConnection.close(conn, query, defaults(opts)) do
{:ok, _} ->
:ok
{:error, %Mariaex.Error{}} = error ->
error
{:error, err} ->
raise err
err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think this right, don't we want to return the error tuple?

end
end

Expand Down Expand Up @@ -419,25 +404,21 @@ defmodule Mariaex do

## Helpers

defp prepare_execute(conn, query, params, opts) do
case DBConnection.prepare_execute(conn, query, params, defaults(opts)) do
defp run_query(op, conn, query, params, opts) do
case apply(DBConnection, op, [conn, query, params, defaults(opts)]) do
{:ok, _, result} ->
{:ok, result}
{:error, %Mariaex.Error{}} = error ->
{:error, _} = error ->
error
{:error, err} ->
raise err
end
end

defp prepare_binary(conn, query, opts) do
case DBConnection.prepare(conn, query, defaults(opts)) do
{:ok, _} = ok ->
ok
{:error, %Mariaex.Error{}} = error ->
error
{:error, err} ->
raise err
err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

end
end

Expand Down
28 changes: 15 additions & 13 deletions lib/mariaex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ defmodule Mariaex.Protocol do
case send_text_query(state, statement) |> text_query_recv(query) do
{:error, error, _} ->
{:error, error}
{:ok, _, state} ->
{:ok, _, _, state} ->
activate(state, state.buffer) |> connected()
end
end
Expand Down Expand Up @@ -505,9 +505,10 @@ defmodule Mariaex.Protocol do
case text_query_recv(state) do
{:resultset, columns, rows, flags, state} ->
result = %Mariaex.Result{rows: rows, connection_id: state.connection_id}
{:ok, {result, columns}, clean_state(state, flags)}
{:ok, query, {result, columns}, clean_state(state, flags)}
{:ok, packet(msg: ok_resp()) = packet, state} ->
handle_ok_packet(packet, query, state)
{:ok, result, state} = handle_ok_packet(packet, query, state)
{:ok, query, result, state}
{:ok, packet, state} ->
handle_error(packet, query, state)
{:error, reason} ->
Expand Down Expand Up @@ -566,7 +567,8 @@ defmodule Mariaex.Protocol do
{:resultset, columns, bin_rows, flags, state} ->
binary_query_resultset(state, query, columns, bin_rows, flags)
{:ok, packet(msg: ok_resp()) = packet, state} ->
handle_ok_packet(packet, query, state)
{:ok, result, state} = handle_ok_packet(packet, query, state)
{:ok, query, result, state}
{:ok, packet, state} ->
handle_error(packet, query, state)
{:error, reason} ->
Expand Down Expand Up @@ -626,7 +628,7 @@ defmodule Mariaex.Protocol do
binary_query_more(state, query, columns, rows)
true ->
result = %Mariaex.Result{rows: rows, connection_id: state.connection_id}
{:ok, {result, columns}, clean_state(state, flags)}
{:ok, query, {result, columns}, clean_state(state, flags)}
end
end

Expand All @@ -635,7 +637,7 @@ defmodule Mariaex.Protocol do
{:ok, packet(msg: ok_resp(affected_rows: affected_rows, last_insert_id: last_insert_id, status_flags: flags)), state} ->
result = %Mariaex.Result{rows: rows, num_rows: affected_rows,
last_insert_id: last_insert_id, connection_id: state.connection_id}
{:ok, {result, columns}, clean_state(state, flags)}
{:ok, query, {result, columns}, clean_state(state, flags)}
{:ok, packet, state} ->
handle_error(packet, query, state)
{:error, reason} ->
Expand Down Expand Up @@ -737,14 +739,14 @@ defmodule Mariaex.Protocol do
case declare_lookup(query, state) do
{:declare, id} ->
cursor = %Cursor{statement_id: id, ref: make_ref()}
declare(cursor, params, state)
declare(cursor, query, params, state)
{:prepare_declare, query} ->
prepare_declare(&prepare(query, &1), params, state)
{:close_prepare_declare, id, query} ->
prepare_declare(&close_prepare(id, query, &1), params, state)
{:text, _} ->
cursor = %Cursor{statement_id: :text, ref: make_ref()}
declare(cursor, params, state)
declare(cursor, query, params, state)
end
end

Expand All @@ -770,18 +772,18 @@ defmodule Mariaex.Protocol do
end
end

defp declare(%Cursor{ref: ref, statement_id: id} = cursor, params, state) do
defp declare(%Cursor{ref: ref, statement_id: id} = cursor, query, params, state) do
state = put_in(state.cursors[ref], {:first, id, params})
# close cursor if idle
{:ok, cursor, clean_state(state, nil)}
{:ok, query, cursor, clean_state(state, nil)}
end

defp prepare_declare(prepare, params, state) do
case prepare.(state) do
{:ok, query, state} ->
id = prepare_declare_lookup(query, state)
cursor = %Cursor{statement_id: id, ref: make_ref()}
declare(cursor, params, state)
declare(cursor, query, params, state)
{err, _, _} = error when err in [:error, :disconnect] ->
error
end
Expand Down Expand Up @@ -827,7 +829,7 @@ defmodule Mariaex.Protocol do

defp first(query, %Cursor{statement_id: :text}, params, opts, state) do
case handle_execute(query, params, opts, state) do
{:ok, result, state} ->
{:ok, _, result, state} ->
{:halt, result, state}
other ->
other
Expand Down Expand Up @@ -878,7 +880,7 @@ defmodule Mariaex.Protocol do

defp binary_first_more(state, query, columns, rows) do
case binary_query_more(state, query, columns, rows) do
{:ok, res, state} ->
{:ok, _query, res, state} ->
{:halt, res, state}
other ->
other
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Mariaex.Mixfile do

defp deps do
[{:decimal, "~> 1.2"},
{:db_connection, "~> 2.0.0-dev", github: "elixir-ecto/db_connection", ref: "854d917"},
{:db_connection, "~> 2.0.0-dev", github: "elixir-ecto/db_connection", ref: "03b78e1"},
{:coverex, "~> 1.4.10", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev},
{:poison, ">= 0.0.0", optional: true}]
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"coverex": {:hex, :coverex, "1.4.10", "f6b68f95b3d51d04571a09dd2071c980e8398a38cf663db22b903ecad1083d51", [:mix], [{:httpoison, "~> 0.9", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"db_connection": {:git, "https://github.com/elixir-ecto/db_connection.git", "854d917721b820576da4b74b3620bf359c61179f", [ref: "854d917"]},
"db_connection": {:git, "https://github.com/elixir-ecto/db_connection.git", "03b78e12d42764d8c7474a5def232d2343517396", [ref: "03b78e1"]},
"decimal": {:hex, :decimal, "1.5.0", "b0433a36d0e2430e3d50291b1c65f53c37d56f83665b43d79963684865beab68", [:mix], [], "hexpm"},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
6 changes: 2 additions & 4 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ defmodule QueryTest do
conn = context[:pid]

assert capture_log(fn ->
assert_raise DBConnection.ConnectionError, "tcp recv: closed", fn ->
query("DO SLEEP(10)", [], timeout: 50)
end

%DBConnection.ConnectionError{message: message} = query("DO SLEEP(10)", [], timeout: 50)
assert message =~ "tcp recv: closed"
assert_receive {:EXIT, ^conn, :killed}, 5000
end) =~ "** (DBConnection.ConnectionError)"
end
Expand Down
14 changes: 7 additions & 7 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ defmodule Mariaex.TestHelper do
case Mariaex.Connection.query(pid, unquote(stat), unquote(params), unquote(opts)) do
{:ok, %Mariaex.Result{rows: nil}} -> :ok
{:ok, %Mariaex.Result{rows: rows}} -> rows
{:error, %Mariaex.Error{} = err} -> err
{:error, err} -> err
end
end
end
Expand All @@ -113,7 +113,7 @@ defmodule Mariaex.TestHelper do
case Mariaex.query(var!(context)[:pid], unquote(stat), unquote(params), opts) do
{:ok, %Mariaex.Result{rows: nil}} -> :ok
{:ok, %Mariaex.Result{rows: rows}} -> rows
{:error, %Mariaex.Error{} = err} -> err
{:error, err} -> err
end
end
end
Expand All @@ -134,17 +134,17 @@ defmodule Mariaex.TestHelper do
quote do
case Mariaex.prepare(var!(context)[:pid], unquote(stat), unquote(opts)) do
{:ok, %Mariaex.Query{} = query} -> query
{:error, %Mariaex.Error{} = err} -> err
{:error, err} -> err
end
end
end

defmacro execute(query, params, opts \\ []) do
quote do
case Mariaex.execute(var!(context)[:pid], unquote(query), unquote(params), unquote(opts)) do
{:ok, %Mariaex.Result{rows: nil}} -> :ok
{:ok, %Mariaex.Result{rows: rows}} -> rows
{:error, %Mariaex.Error{} = err} -> err
{:ok, %Mariaex.Query{}, %Mariaex.Result{rows: nil}} -> :ok
{:ok, %Mariaex.Query{}, %Mariaex.Result{rows: rows}} -> rows
{:error, err} -> err
end
end
end
Expand All @@ -153,7 +153,7 @@ defmodule Mariaex.TestHelper do
quote do
case Mariaex.close(var!(context)[:pid], unquote(query), unquote(opts)) do
:ok -> :ok
{:error, %Mariaex.Error{} = err} -> err
{:error, err} -> err
end
end
end
Expand Down