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 11 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
43 changes: 14 additions & 29 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,14 +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
{:error, %ArgumentError{} = err} ->
raise err
other ->
other
end
DBConnection.execute(conn, query, params, defaults(opts))
end

@doc """
Expand All @@ -253,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 @@ -289,10 +278,8 @@ defmodule Mariaex do
case DBConnection.close(conn, query, defaults(opts)) do
{:ok, _} ->
:ok
{:error, %ArgumentError{} = err} ->
raise err
other ->
other
{:error, 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 @@ -417,23 +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, %ArgumentError{} = err} ->
raise err
{:error, _} = error ->
error
end
end

defp prepare_binary(conn, query, opts) do
case DBConnection.prepare(conn, query, defaults(opts)) do
{:error, %ArgumentError{} = err} ->
raise err
other ->
other
{:ok, _} = ok ->
ok
{:error, 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
Loading