@@ -23,7 +23,7 @@ defmodule Postgrex do
2323 you need to start a separate (notifications) connection.
2424 """
2525
26- alias Postgrex.Query
26+ alias Postgrex . { Query , TextQuery }
2727
2828 @ typedoc """
2929 A connection process name, pid or reference.
@@ -252,12 +252,18 @@ defmodule Postgrex do
252252 end
253253
254254 @ doc """
255- Runs an (extended) query and returns the result as `{:ok, %Postgrex.Result{}}`
256- or `{:error, %Postgrex.Error{}}` if there was a database error. Parameters can
257- be set in the query as `$1` embedded in the query string. Parameters are given
258- as a list of elixir values. See the README for information on how Postgrex
259- encodes and decodes Elixir values by default. See `Postgrex.Result` for the
260- result data.
255+ Runs a query and returns the result as `{:ok, %Postgrex.Result{}}` or
256+ `{:error, %Postgrex.Error{}}` if there was a database error.
257+
258+ Queries can be run using both the extended query protocol (binary format)
259+ or the simple query protocol (text format). This can be controlled using
260+ the `:query_type` option.
261+
262+ If using the extended query protocol, parameters can be set as `$1` embedded
263+ in the query string and results are encoded and decoded according to the
264+ [data representation chart](readme.html#data-representation). If using the
265+ simple query protocol, queries cannot be parameterized and results are encoded
266+ and decoded in text format. See `Postgrex.Result` for the result data.
261267
262268 This function may still raise an exception if there is an issue with types
263269 (`ArgumentError`), connection (`DBConnection.ConnectionError`), ownership
@@ -272,6 +278,9 @@ defmodule Postgrex do
272278 * `:mode` - set to `:savepoint` to use a savepoint to rollback to before the
273279 query on error, otherwise set to `:transaction` (default: `:transaction`);
274280 * `:cache_statement` - Caches the query with the given name
281+ * `:query_type` - Either `:binary` or `:text`. If `:binary` then the
282+ extended query protocol is used. If `:text` then the simple protocol
283+ is used. Defaults to `:binary`.
275284
276285 ## Examples
277286
@@ -290,6 +299,22 @@ defmodule Postgrex do
290299 @ spec query ( conn , iodata , list , [ execute_option ] ) ::
291300 { :ok , Postgrex.Result . t ( ) } | { :error , Exception . t ( ) }
292301 def query ( conn , statement , params \\ [ ] , opts \\ [ ] ) when is_list ( params ) and is_list ( opts ) do
302+ query_type = Keyword . get ( opts , :query_type , :binary )
303+
304+ case query_type do
305+ :binary ->
306+ binary_query ( conn , statement , params , opts )
307+
308+ :text ->
309+ text_query ( conn , statement , params , opts )
310+
311+ _ ->
312+ raise ArgumentError ,
313+ "allowed query types are `:binary` and `:text`, got: #{ inspect ( query_type ) } "
314+ end
315+ end
316+
317+ defp binary_query ( conn , statement , params , opts ) do
293318 name = Keyword . get ( opts , :cache_statement )
294319
295320 if comment_not_present! ( opts ) && name do
@@ -315,6 +340,15 @@ defmodule Postgrex do
315340 end
316341 end
317342
343+ defp text_query ( conn , statement , params , opts ) do
344+ query = % TextQuery { statement: statement }
345+
346+ case DBConnection . execute ( conn , query , params , opts ) do
347+ { :ok , _ , result } -> { :ok , result }
348+ { :error , _ } = error -> error
349+ end
350+ end
351+
318352 defp query_prepare_execute ( conn , query , params , opts ) do
319353 case DBConnection . prepare_execute ( conn , query , params , opts ) do
320354 { :ok , _ , result } -> { :ok , result }
0 commit comments