-
Notifications
You must be signed in to change notification settings - Fork 10
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
[AUTO-3289] General improvements, add DBConnection adapter #32
Conversation
|
||
def to_string(%{statement: statement}) do | ||
case statement do | ||
statement when is_binary(statement) -> IO.iodata_to_binary(statement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's already a binary, it should be OK to pass through directly.
statement when is_binary(statement) -> IO.iodata_to_binary(statement) | |
statement when is_binary(statement) -> statement |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Result.t()
, statement
is typed as String.t() | nil
. If it can be iodata()
, I would update the typespec to indicate that. A Query.t()
typespec would also be a nice addition here.
columns: Enum.map(columns, &to_string(&1)), | ||
rows: rows, | ||
num_rows: Enum.count(rows) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll probably want to set success
and statement
on the result (ignore if you were already planning to do this 🙂).
def disconnect(_err, %{pid: pid}), do: Server.disconnect(pid) | ||
|
||
@impl DBConnection | ||
def checkout(state), do: {:ok, state} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there should be any sort of limit on the maximum number of connections that can be checked out at once? Maybe it doesn't matter 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we only want to use own DbConnection implementation. Was there a plan/discussion about using own Adapter module for Snowflex?
lib/snowflex/db_connection.ex
Outdated
opts = | ||
Keyword.merge(config, | ||
timeout: @timeout, | ||
connection: connection | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't always override timeout from config with a compile-time @timeout ?
@timeout timeout | ||
@name __MODULE__ | ||
|
||
def child_spec(_) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we ignore child spec input options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opts
should be provided statically when configuring your connection module with use Snowflake.DBConnection, opt1: _, opt2: _, ...
. Ignoring the opts in child_spec/1
ensures that the static config in the use
macro is always correct, and not accidentally overridden in the supervision tree. I like this approach 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just a few small comments, nothing blocking.
end | ||
|
||
defp parse_result(columns, rows) do | ||
%Result{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should %Result
structs always include statement
? If not, it'd be nice to add something to the docs describing when it will/won't be set.
case worker.param_query(state.pid, query.statement, params, opts) do | ||
{:ok, {:selected, columns, rows}} -> | ||
result = parse_result(columns, rows) | ||
{:ok, query, result, state} | ||
|
||
{:ok, {:selected, columns, rows, _}} -> | ||
result = parse_result(columns, rows) | ||
{:ok, query, result, state} | ||
|
||
{:ok, result} -> | ||
{:ok, query, result, state} | ||
|
||
{:error, reason} -> | ||
{:error, reason, state} | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could pull this case
out into its own function so you don't have to duplicate it between sql_query
and param_query
.
lib/snowflex/db_connection/query.ex
Outdated
def decode(_query, [], _opts), do: [] | ||
def decode(_query, result, _opts), do: result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can be combined
def decode(_query, [], _opts), do: [] | |
def decode(_query, result, _opts), do: result | |
def decode(_query, result, _opts), do: result |
|
||
def to_string(%{statement: statement}) do | ||
case statement do | ||
statement when is_binary(statement) -> IO.iodata_to_binary(statement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Result.t()
, statement
is typed as String.t() | nil
. If it can be iodata()
, I would update the typespec to indicate that. A Query.t()
typespec would also be a nice addition here.
This is the first attempt to add
DBConnection
support.I tried to keep functionality the same, except for adding an extra function
process_result/2
which will return results as current implementation instead of an{:ok, %Result{}}
tuple.See
config/test.exs
for an example of configuration required, speciallypool_size
option.