Skip to content

Commit

Permalink
mv rows to query arg
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Oct 18, 2024
1 parent 1b7919f commit b1fc1d3
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 23 deletions.
24 changes: 11 additions & 13 deletions lib/ch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ defmodule Ch do
| {:username, String.t()}
| {:password, String.t()}
| {:settings, Keyword.t()}
| {:timeout, timeout}

@type start_option ::
common_option
| {:scheme, String.t()}
| {:hostname, String.t()}
| {:port, :inet.port_number()}
| {:transport_opts, :gen_tcp.connect_option() | :ssl.tls_client_option()}
| {:transport_opts, [:gen_tcp.connect_option() | :ssl.tls_client_option()]}
| DBConnection.start_option()

@doc """
Expand All @@ -30,8 +29,6 @@ defmodule Ch do
* `:username` - Username
* `:password` - User password
* `:settings` - Keyword list of ClickHouse settings to send wtih every query

Check warning on line 31 in lib/ch.ex

View workflow job for this annotation

GitHub Actions / typos

"wtih" should be "with".

Check failure on line 31 in lib/ch.ex

View workflow job for this annotation

GitHub Actions / codespell

wtih ==> with
* `:timeout` - HTTP receive timeout in milliseconds
* `:transport_opts` - options to be given to the transport being used. See `Mint.HTTP1.connect/4` for more info
* [`DBConnection.start_option()`](https://hexdocs.pm/db_connection/DBConnection.html#t:start_option/0)
"""
Expand All @@ -50,13 +47,18 @@ defmodule Ch do
DBConnection.child_spec(Connection, opts)
end

@type query :: iodata

@type query_option ::
common_option
| {:command, Ch.Query.command()}
| {:headers, [{String.t(), String.t()}]}
| {:format, String.t()}
| DBConnection.connection_option()

@type query_params ::
%{(name :: String.t()) => value :: term}
| [{name :: String.t(), value :: term}]

@doc """
Runs a query and returns the result as `{:ok, %Ch.Result{}}` or
`{:error, Exception.t()}` if there was a database error.
Expand All @@ -67,16 +69,13 @@ defmodule Ch do
* `:username` - Username
* `:password` - User password
* `:settings` - Keyword list of settings to merge with `:settings` from `start_link` and send with this query
* `:timeout` - Configures both query request timeout and HTTP receive timeout in milliseconds, whichever happens faster
* `:command` - Command tag for the query
* `:command` - Command tag for the query like `:insert` or `:select`, to avoid extracting it from SQL. Used in some Ecto.Repo `:telemetry` events
* `:headers` - Custom HTTP headers for the request
* `:format` - Custom response format for the request, if provided, the response is not decoded automatically
* [`DBConnection.connection_option()`](https://hexdocs.pm/db_connection/DBConnection.html#t:connection_option/0)
"""
@spec query(DBConnection.conn(), iodata, params, [query_option]) ::
@spec query(DBConnection.conn(), query, query_params, [query_option]) ::
{:ok, Result.t()} | {:error, Exception.t()}
when params: map | [term] | [row :: [term]] | iodata | Enumerable.t()
def query(conn, statement, params \\ [], opts \\ []) do
query = Query.build(statement, opts)

Expand All @@ -89,15 +88,14 @@ defmodule Ch do
Runs a query and returns the result or raises `Ch.Error` if
there was an error. See `query/4`.
"""
@spec query!(DBConnection.conn(), iodata, params, [query_option]) :: Result.t()
when params: map | [term] | [row :: [term]] | iodata | Enumerable.t()
@spec query!(DBConnection.conn(), query, query_params, [query_option]) :: Result.t()
def query!(conn, statement, params \\ [], opts \\ []) do
query = Query.build(statement, opts)
DBConnection.execute!(conn, query, params, opts)
end

@doc false
@spec stream(DBConnection.t(), iodata, map | [term], [query_option]) :: Ch.Stream.t()
@spec stream(DBConnection.t(), query, query_params, [query_option]) :: Ch.Stream.t()
def stream(conn, statement, params \\ [], opts \\ []) do
query = Query.build(statement, opts)
%Ch.Stream{conn: conn, query: query, params: params, opts: opts}
Expand Down
1 change: 0 additions & 1 deletion lib/ch/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ defmodule Ch.Connection do
with {:ok, conn} <- HTTP.connect(scheme, address, port, mint_opts) do
conn =
conn
|> HTTP.put_private(:timeout, opts[:timeout] || :timer.seconds(15))
|> maybe_put_private(:database, opts[:database])
|> maybe_put_private(:username, opts[:username])
|> maybe_put_private(:password, opts[:password])
Expand Down
2 changes: 0 additions & 2 deletions lib/ch/http.ex

This file was deleted.

3 changes: 0 additions & 3 deletions lib/ch/native.ex

This file was deleted.

3 changes: 0 additions & 3 deletions lib/ch/ssl.ex

This file was deleted.

2 changes: 1 addition & 1 deletion lib/ch/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Ch.Stream do
conn: DBConnection.conn(),
ref: Mint.Types.request_ref() | nil,
query: Ch.Query.t(),
params: term,
params: Ch.query_params(),
opts: [Ch.query_option()]
}

Expand Down

0 comments on commit b1fc1d3

Please sign in to comment.