Skip to content

Commit 53bfcea

Browse files
authored
improve docs (#291)
* rm duplicate line * improve docs
1 parent 391dacb commit 53bfcea

File tree

4 files changed

+74
-39
lines changed

4 files changed

+74
-39
lines changed

lib/ch.ex

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,32 @@ defmodule Ch do
22
@moduledoc "Minimal HTTP ClickHouse client."
33
alias Ch.{Connection, Query, Result}
44

5+
@typedoc """
6+
Options shared by both connection startup and query execution.
7+
8+
* `:database` - Database, defaults to `"default"`
9+
* `:username` - Username
10+
* `:password` - User password
11+
* `:settings` - Keyword list of ClickHouse settings
12+
* `:timeout` - HTTP request/receive timeout in milliseconds
13+
"""
514
@type common_option ::
615
{:database, String.t()}
716
| {:username, String.t()}
817
| {:password, String.t()}
918
| {:settings, Keyword.t()}
1019
| {:timeout, timeout}
1120

21+
@typedoc """
22+
Options for starting the connection pool.
23+
24+
Includes all keys from `t:common_option/0` and `t:DBConnection.start_option/0` plus:
25+
26+
* `:scheme` - HTTP scheme, defaults to `"http"`
27+
* `:hostname` - server hostname, defaults to `"localhost"`
28+
* `:port` - HTTP port, defaults to `8123`
29+
* `:transport_opts` - options to be given to the transport being used. See `Mint.HTTP1.connect/4` for more info
30+
"""
1231
@type start_option ::
1332
common_option
1433
| {:scheme, String.t()}
@@ -18,38 +37,36 @@ defmodule Ch do
1837
| DBConnection.start_option()
1938

2039
@doc """
21-
Start the connection process and connect to ClickHouse.
22-
23-
## Options
24-
25-
* `:scheme` - HTTP scheme, defaults to `"http"`
26-
* `:hostname` - server hostname, defaults to `"localhost"`
27-
* `:port` - HTTP port, defaults to `8123`
28-
* `:transport_opts` - options to be given to the transport being used. See `Mint.HTTP1.connect/4` for more info
29-
* `:database` - Database, defaults to `"default"`
30-
* `:username` - Username
31-
* `:password` - User password
32-
* `:settings` - Keyword list of ClickHouse settings
33-
* `:timeout` - HTTP receive timeout in milliseconds
34-
* `:transport_opts` - options to be given to the transport being used. See `Mint.HTTP1.connect/4` for more info
35-
* [`DBConnection.start_option()`](https://hexdocs.pm/db_connection/DBConnection.html#t:start_option/0)
40+
Start the connection pool process.
3641
42+
See `t:start_option/0` for available options.
3743
"""
3844
@spec start_link([start_option]) :: GenServer.on_start()
3945
def start_link(opts \\ []) do
4046
DBConnection.start_link(Connection, opts)
4147
end
4248

4349
@doc """
44-
Returns a supervisor child specification for a DBConnection pool.
50+
Returns a supervisor child specification for a connection pool.
4551
46-
See `start_link/1` for supported options.
52+
See `t:start_option/0` for supported options.
4753
"""
4854
@spec child_spec([start_option]) :: :supervisor.child_spec()
4955
def child_spec(opts) do
5056
DBConnection.child_spec(Connection, opts)
5157
end
5258

59+
@typedoc """
60+
Options for executing a query.
61+
62+
Includes all keys from `t:common_option/0` and `t:DBConnection.connection_option/0` plus:
63+
64+
* `:command` - Command tag for the query
65+
* `:headers` - Custom HTTP headers for the request
66+
* `:format` - Custom response format for the request
67+
* `:decode` - Whether to automatically decode the response
68+
* `:multipart` - Whether to send the query as multipart/form-data
69+
"""
5370
@type query_option ::
5471
common_option
5572
| {:command, Ch.Query.command()}
@@ -66,20 +83,7 @@ defmodule Ch do
6683
Runs a query and returns the result as `{:ok, %Ch.Result{}}` or
6784
`{:error, Exception.t()}` if there was a database error.
6885
69-
## Options
70-
71-
* `:database` - Database
72-
* `:username` - Username
73-
* `:password` - User password
74-
* `:settings` - Keyword list of settings
75-
* `:timeout` - Query request timeout
76-
* `:command` - Command tag for the query
77-
* `:headers` - Custom HTTP headers for the request
78-
* `:format` - Custom response format for the request
79-
* `:decode` - Whether to automatically decode the response
80-
* `:multipart` - Whether to send the query as multipart/form-data
81-
* [`DBConnection.connection_option()`](https://hexdocs.pm/db_connection/DBConnection.html#t:connection_option/0)
82-
86+
See `t:query_option/0` for available options.
8387
"""
8488
@spec query(DBConnection.conn(), iodata, params, [query_option]) ::
8589
{:ok, Result.t()} | {:error, Exception.t()}

lib/ch/error.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
defmodule Ch.Error do
22
@moduledoc "Error struct wrapping ClickHouse error responses."
33
defexception [:code, :message]
4+
5+
@typedoc """
6+
The Error struct.
7+
8+
## Fields
9+
10+
* `:code` - The ClickHouse numeric error code
11+
* `:message` - The error message returned by the server
12+
"""
413
@type t :: %__MODULE__{code: pos_integer | nil, message: String.t()}
514
end

lib/ch/query.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ defmodule Ch.Query do
22
@moduledoc "Query struct wrapping the SQL statement."
33
defstruct [:statement, :command, :encode, :decode, :multipart]
44

5+
@typedoc """
6+
The Query struct.
7+
8+
## Fields
9+
10+
* `:statement` - The SQL statement to be executed (as `t:iodata/0`).
11+
* `:command` - The detected or enforced SQL command type (e.g., `:select`, `:insert`).
12+
* `:encode` - Whether to encode parameters (defaults to `true`).
13+
* `:decode` - Whether to decode the response (defaults to `true`).
14+
* `:multipart` - Whether to use `multipart/form-data` for the request (defaults to `false`).
15+
"""
516
@type t :: %__MODULE__{
617
statement: iodata,
718
command: command,
@@ -65,6 +76,12 @@ defmodule Ch.Query do
6576
|> Enum.map(fn {_, command} -> command end)
6677
|> Enum.reduce(&{:|, [], [&1, &2]})
6778

79+
@typedoc """
80+
Atom representing the type of SQL command.
81+
82+
Derived automatically from the start of the SQL statement (e.g., `"SELECT ..."` -> `:select`),
83+
or provided explicitly via options.
84+
"""
6885
@type command :: unquote(command_union)
6986

7087
defp extract_command(statement)

lib/ch/result.ex

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
defmodule Ch.Result do
22
@moduledoc """
3-
Result struct returned from any successful query. Its fields are:
4-
5-
* `command` - An atom of the query command, for example: `:select`, `:insert`
6-
* `columns` - A list of column names
7-
* `rows` - A list of lists, each inner list corresponding to a row, each element in the inner list corresponds to a column
8-
* `num_rows` - The number of fetched or affected rows
9-
* `headers` - The HTTP response headers
10-
* `data` - The raw iodata from the response
3+
Result struct returned from any successful query.
114
"""
125

136
defstruct [:command, :num_rows, :columns, :rows, :headers, :data]
147

8+
@typedoc """
9+
The Result struct.
10+
11+
## Fields
12+
13+
* `:command` - An atom of the query command, for example: `:select`, `:insert`
14+
* `:columns` - A list of column names
15+
* `:rows` - A list of lists (each inner list corresponding to a row, each element in the inner list corresponds to a column)
16+
* `:num_rows` - The number of fetched or affected rows
17+
* `:headers` - The HTTP response headers
18+
* `:data` - The raw iodata from the response
19+
"""
1520
@type t :: %__MODULE__{
1621
command: Ch.Query.command() | nil,
1722
num_rows: non_neg_integer | nil,

0 commit comments

Comments
 (0)