@@ -71,62 +71,6 @@ defmodule ReqCH do
7171
7272 defguardp is_query_params ( value ) when is_list ( value ) or is_map ( value )
7373
74- @ doc """
75- Performs a query against ClickHouse API.
76-
77- See docs from `new/1` for details about the options.
78-
79- ## Examples
80-
81- Queries can be performed using both `Req.get/2` or `Req.post/2`, but GET
82- is "read-only" and commands like `CREATE` or `INSERT` cannot be used with it.
83- For that reason, by default we perform a `POST` request.
84- To change that, use `query/4` with a pre-configured `req`.
85-
86- A plain query:
87-
88- iex> {:ok, response} = ReqCH.query("SELECT number FROM system.numbers LIMIT 3")
89- iex> response.body
90- "0\\ n1\\ n2\\ n"
91-
92- Changing the format to `:explorer` will return a dataframe:
93-
94- iex> {:ok, response} = ReqCH.query("SELECT number FROM system.numbers LIMIT 3", [], format: :explorer)
95- iex> response.body
96- #Explorer.DataFrame<
97- Polars[3 x 1]
98- number u64 [0, 1, 2]
99- >
100-
101- Using parameters is also possible:
102-
103- iex> opts = [format: :explorer, database: "system"]
104- iex> {:ok, response} = ReqCH.query("SELECT number FROM numbers WHERE number > {num:UInt8} LIMIT 3", [num: 5], opts)
105- #Explorer.DataFrame<
106- Polars[3 x 1]
107- number u64 [6, 7, 8]
108- >
109-
110- This function can accept `Req` options, as well as mixing them with `ReqCH` options:
111-
112- iex> opts = [base_url: "http://example.org:8123", database: "system", auth: {:basic, "user:pass"}]
113- iex> {:ok, response} = ReqCH.query("SELECT number FROM numbers LIMIT 3", [], opts)
114- iex> response.body
115- "0\\ n1\\ n2\\ n"
116-
117- """
118- @ spec query ( sql_query :: binary ( ) , params :: Map . t ( ) | Keyword . t ( ) , opts :: Keyword . t ( ) ) ::
119- { :ok , Req.Response . t ( ) } | { :error , binary ( ) }
120- def query ( sql_query , params \\ [ ] , opts \\ [ ] )
121-
122- def query ( sql_query , params , opts )
123- when is_binary ( sql_query ) and is_query_params ( params ) and is_list ( opts ) do
124- opts
125- |> new ( )
126- |> put_params ( prepare_params ( params ) )
127- |> Req . post ( body: sql_query )
128- end
129-
13074 @ doc """
13175 Performs a query against the ClickHouse API.
13276
@@ -136,14 +80,19 @@ defmodule ReqCH do
13680 By default, it will use the `http://localhost:8123` as `:base_url`.
13781 You can change that either providing in your Req request, or in passing
13882 down in the options.
139- See `new/1` for the options.
83+ See `new/1` for the options. Like that function, `query/4` accepts any
84+ option that `Req.new/1` accepts.
14085
14186 ## Examples
14287
88+ Queries can be performed using both `Req.get/2` or `Req.post/2`, but GET
89+ is "read-only" and commands like `CREATE` or `INSERT` cannot be used with it.
90+ For that reason, by default we perform a `POST` request.
91+
14392 A plain query:
14493
14594 iex> req = ReqCH.new(database: "system")
146- iex> {:ok, response} = ReqCH.query(req, "SELECT number FROM numbers LIMIT 3", [], [] )
95+ iex> {:ok, response} = ReqCH.query(req, "SELECT number FROM numbers LIMIT 3")
14796 iex> response.body
14897 "0\\ n1\\ n2\\ n"
14998
@@ -163,14 +112,22 @@ defmodule ReqCH do
163112 iex> {:ok, response} = ReqCH.query(req, "SELECT number FROM numbers WHERE number > {num:UInt8} LIMIT 3", [num: 5], [])
164113 iex> response.body
165114 "6\\ n7\\ n8\\ n"
115+
116+ This function can accept `Req` options, as well as mixing them with `ReqCH` options:
117+
118+ iex> opts = [base_url: "http://example.org:8123", database: "system", auth: {:basic, "user:pass"}]
119+ iex> {:ok, response} = ReqCH.query(ReqCH.new(), "SELECT number FROM numbers LIMIT 3", [], opts)
120+ iex> response.body
121+ "0\\ n1\\ n2\\ n"
122+
166123 """
167124 @ spec query (
168125 Req . t ( ) ,
169126 sql_query :: binary ( ) ,
170127 sql_query_params :: Map . t ( ) | Keyword . t ( ) ,
171128 opts :: Keyword . t ( )
172129 ) :: { :ok , Req.Response . t ( ) } | { :error , binary ( ) }
173- def query ( req , sql_query , sql_query_params , opts )
130+ def query ( req , sql_query , sql_query_params \\ [ ] , opts \\ [ ] )
174131
175132 def query ( % Req.Request { } = req , sql_query , sql_query_params , opts )
176133 when is_binary ( sql_query ) and is_query_params ( sql_query_params ) and is_list ( opts ) do
@@ -180,31 +137,16 @@ defmodule ReqCH do
180137 |> Req . post ( body: sql_query )
181138 end
182139
183- @ doc """
184- Same as `query/3`, but raises in case of error.
185- """
186- @ spec query! ( sql_query :: binary ( ) , params :: Map . t ( ) | Keyword . t ( ) , opts :: Keyword . t ( ) ) ::
187- Req.Response . t ( )
188- def query! ( sql_query , params \\ [ ] , opts \\ [ ] )
189-
190- def query! ( sql_query , params , opts )
191- when is_binary ( sql_query ) and is_query_params ( params ) and is_list ( opts ) do
192- case query ( sql_query , params , opts ) do
193- { :ok , response } -> response
194- { :error , exception } -> raise exception
195- end
196- end
197-
198140 @ doc """
199141 Same as `query/4`, but raises in case of error.
200142 """
201- @ spec query (
143+ @ spec query! (
202144 Req . t ( ) ,
203145 sql_query :: binary ( ) ,
204146 sql_query_params :: Map . t ( ) | Keyword . t ( ) ,
205147 opts :: Keyword . t ( )
206148 ) :: Req.Response . t ( )
207- def query! ( req , sql_query , sql_query_params , opts )
149+ def query! ( req , sql_query , sql_query_params \\ [ ] , opts \\ [ ] )
208150
209151 def query! ( % Req.Request { } = req , sql_query , sql_query_params , opts ) do
210152 case query ( req , sql_query , sql_query_params , opts ) do
0 commit comments