Skip to content

Commit

Permalink
mix format (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
chulkilee authored and José Valim committed Jul 17, 2019
1 parent 6ae91d6 commit 1cfa939
Show file tree
Hide file tree
Showing 70 changed files with 2,947 additions and 1,598 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{lib,test}/**/*.{ex,exs}"]
]
71 changes: 38 additions & 33 deletions lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ defmodule Postgrex do
A connection reference is used when making multiple requests to the same
connection, see `transaction/3`.
"""
@type conn :: DBConnection.conn
@type conn :: DBConnection.conn()

@type start_option ::
{:hostname, String.t}
| {:socket_dir, Path.t}
| {:socket, Path.t}
| {:port, :inet.port_number}
| {:database, String.t}
| {:username, String.t}
| {:password, String.t}
{:hostname, String.t()}
| {:socket_dir, Path.t()}
| {:socket, Path.t()}
| {:port, :inet.port_number()}
| {:database, String.t()}
| {:username, String.t()}
| {:password, String.t()}
| {:parameters, keyword}
| {:timeout, timeout}
| {:connect_timeout, timeout}
| {:handshake_timeout, timeout}
| {:ssl, boolean}
| {:ssl_opts, [:ssl.ssl_option]}
| {:socket_options, [:gen_tcp.connect_option]}
| {:ssl_opts, [:ssl.ssl_option()]}
| {:socket_options, [:gen_tcp.connect_option()]}
| {:prepare, :named | :unnamed}
| {:transactions, :strict | :naive}
| {:types, module}
| {:disconnect_on_error_codes, [atom]}
| DBConnection.start_option
| DBConnection.start_option()

@type option ::
{:mode, :transaction | :savepoint}
| DBConnection.option
| DBConnection.option()

@type execute_option ::
{:decode_mapper, (list -> term)}
Expand Down Expand Up @@ -148,7 +148,7 @@ defmodule Postgrex do
This cause the connection process to attempt to reconnect according
to the backoff configuration.
"""
@spec start_link([start_option]) :: {:ok, pid} | {:error, Postgrex.Error.t | term}
@spec start_link([start_option]) :: {:ok, pid} | {:error, Postgrex.Error.t() | term}
def start_link(opts) do
ensure_deps_started!(opts)
opts = Postgrex.Utils.default_opts(opts)
Expand Down Expand Up @@ -189,7 +189,8 @@ defmodule Postgrex do
Postgrex.query(conn, "COPY posts TO STDOUT", [])
"""
@spec query(conn, iodata, list, [execute_option]) :: {:ok, Postgrex.Result.t} | {:error, Exception.t}
@spec query(conn, iodata, list, [execute_option]) ::
{:ok, Postgrex.Result.t()} | {:error, Exception.t()}
def query(conn, statement, params, opts \\ []) do
if name = Keyword.get(opts, :cache_statement) do
query = %Query{name: name, cache: :statement, statement: IO.iodata_to_binary(statement)}
Expand All @@ -198,7 +199,7 @@ defmodule Postgrex do
{:ok, _, result} ->
{:ok, result}

{:error, %Postgrex.Error{postgres: %{code: :feature_not_supported}}} = error->
{:error, %Postgrex.Error{postgres: %{code: :feature_not_supported}}} = error ->
with %DBConnection{} <- conn,
:error <- DBConnection.status(conn) do
error
Expand All @@ -225,7 +226,7 @@ defmodule Postgrex do
Runs an (extended) query and returns the result or raises `Postgrex.Error` if
there was an error. See `query/3`.
"""
@spec query!(conn, iodata, list, [execute_option]) :: Postgrex.Result.t
@spec query!(conn, iodata, list, [execute_option]) :: Postgrex.Result.t()
def query!(conn, statement, params, opts \\ []) do
case query(conn, statement, params, opts) do
{:ok, result} -> result
Expand Down Expand Up @@ -255,7 +256,8 @@ defmodule Postgrex do
Postgrex.prepare(conn, "", "CREATE TABLE posts (id serial, title text)")
"""
@spec prepare(conn, iodata, iodata, [option]) :: {:ok, Postgrex.Query.t} | {:error, Exception.t}
@spec prepare(conn, iodata, iodata, [option]) ::
{:ok, Postgrex.Query.t()} | {:error, Exception.t()}
def prepare(conn, name, statement, opts \\ []) do
query = %Query{name: name, statement: statement}
opts = Keyword.put(opts, :postgrex_prepare, true)
Expand All @@ -266,7 +268,7 @@ defmodule Postgrex do
Prepares an (extended) query and returns the prepared query or raises
`Postgrex.Error` if there was an error. See `prepare/4`.
"""
@spec prepare!(conn, iodata, iodata, [option]) :: Postgrex.Query.t
@spec prepare!(conn, iodata, iodata, [option]) :: Postgrex.Query.t()
def prepare!(conn, name, statement, opts \\ []) do
opts = Keyword.put(opts, :postgrex_prepare, true)
DBConnection.prepare!(conn, %Query{name: name, statement: statement}, opts)
Expand Down Expand Up @@ -298,7 +300,7 @@ defmodule Postgrex do
"""
@spec prepare_execute(conn, iodata, iodata, list, [execute_option]) ::
{:ok, Postgrex.Query.t, Postgrex.Result.t} | {:error, Postgrex.Error.t}
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def prepare_execute(conn, name, statement, params, opts \\ []) do
query = %Query{name: name, statement: statement}
DBConnection.prepare_execute(conn, query, params, opts)
Expand All @@ -309,7 +311,7 @@ defmodule Postgrex do
`Postgrex.Error` if there was an error. See `prepare_execute/5`.
"""
@spec prepare_execute!(conn, iodata, iodata, list, [execute_option]) ::
{Postgrex.Query.t, Postgrex.Result.t}
{Postgrex.Query.t(), Postgrex.Result.t()}
def prepare_execute!(conn, name, statement, params, opts \\ []) do
query = %Query{name: name, statement: statement}
DBConnection.prepare_execute!(conn, query, params, opts)
Expand Down Expand Up @@ -343,8 +345,8 @@ defmodule Postgrex do
query = Postgrex.prepare!(conn, "", "SELECT id FROM posts WHERE title like $1")
Postgrex.execute(conn, query, ["%my%"])
"""
@spec execute(conn, Postgrex.Query.t, list, [execute_option]) ::
{:ok, Postgrex.Query.t, Postgrex.Result.t} | {:error, Postgrex.Error.t}
@spec execute(conn, Postgrex.Query.t(), list, [execute_option]) ::
{:ok, Postgrex.Query.t(), Postgrex.Result.t()} | {:error, Postgrex.Error.t()}
def execute(conn, query, params, opts \\ []) do
DBConnection.execute(conn, query, params, opts)
end
Expand All @@ -353,8 +355,8 @@ defmodule Postgrex do
Runs an (extended) prepared query and returns the result or raises
`Postgrex.Error` if there was an error. See `execute/4`.
"""
@spec execute!(conn, Postgrex.Query.t, list, [execute_option]) ::
Postgrex.Result.t
@spec execute!(conn, Postgrex.Query.t(), list, [execute_option]) ::
Postgrex.Result.t()
def execute!(conn, query, params, opts \\ []) do
DBConnection.execute!(conn, query, params, opts)
end
Expand All @@ -381,7 +383,7 @@ defmodule Postgrex do
query = Postgrex.prepare!(conn, "", "CREATE TABLE posts (id serial, title text)")
Postgrex.close(conn, query)
"""
@spec close(conn, Postgrex.Query.t, [option]) :: :ok | {:error, Exception.t}
@spec close(conn, Postgrex.Query.t(), [option]) :: :ok | {:error, Exception.t()}
def close(conn, query, opts \\ []) do
with {:ok, _} <- DBConnection.close(conn, query, opts) do
:ok
Expand All @@ -392,7 +394,7 @@ defmodule Postgrex do
Closes an (extended) prepared query and returns `:ok` or raises
`Postgrex.Error` if there was an error. See `close/3`.
"""
@spec close!(conn, Postgrex.Query.t, [option]) :: :ok
@spec close!(conn, Postgrex.Query.t(), [option]) :: :ok
def close!(conn, query, opts \\ []) do
DBConnection.close!(conn, query, opts)
:ok
Expand Down Expand Up @@ -433,8 +435,9 @@ defmodule Postgrex do
Postgrex.query!(conn, "SELECT title FROM posts", [])
end)
"""
@spec transaction(conn, ((DBConnection.t) -> result), [option]) ::
{:ok, result} | {:error, any} when result: var
@spec transaction(conn, (DBConnection.t() -> result), [option]) ::
{:ok, result} | {:error, any}
when result: var
def transaction(conn, fun, opts \\ []) do
DBConnection.transaction(conn, fun, opts)
end
Expand All @@ -452,7 +455,7 @@ defmodule Postgrex do
IO.puts "never reaches here!"
end)
"""
@spec rollback(DBConnection.t, reason :: any) :: no_return()
@spec rollback(DBConnection.t(), reason :: any) :: no_return()
defdelegate rollback(conn, reason), to: DBConnection

@doc """
Expand All @@ -472,7 +475,7 @@ defmodule Postgrex do
@doc """
Returns a supervisor child specification for a DBConnection pool.
"""
@spec child_spec([start_option]) :: Supervisor.Spec.spec
@spec child_spec([start_option]) :: Supervisor.Spec.spec()
def child_spec(opts) do
ensure_deps_started!(opts)
opts = Postgrex.Utils.default_opts(opts)
Expand Down Expand Up @@ -519,17 +522,19 @@ defmodule Postgrex do
Enum.into(File.stream!("posts"), stream)
end)
"""
@spec stream(DBConnection.t, iodata | Postgrex.Query.t, list, [option]) :: Postgrex.Stream.t
@spec stream(DBConnection.t(), iodata | Postgrex.Query.t(), list, [option]) ::
Postgrex.Stream.t()
when option: execute_option | {:max_rows, pos_integer}
def stream(%DBConnection{} = conn, query, params, options \\ []) do
def stream(%DBConnection{} = conn, query, params, options \\ []) do
options = Keyword.put_new(options, :max_rows, @max_rows)
%Postgrex.Stream{conn: conn, query: query, params: params, options: options}
end

## Helpers

defp ensure_deps_started!(opts) do
if Keyword.get(opts, :ssl, false) and not List.keymember?(:application.which_applications(), :ssl, 0) do
if Keyword.get(opts, :ssl, false) and
not List.keymember?(:application.which_applications(), :ssl, 0) do
raise """
SSL connection can not be established because `:ssl` application is not started,
you can add it to `extra_application` in your `mix.exs`:
Expand Down
3 changes: 1 addition & 2 deletions lib/postgrex/binary_extension.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule Postgrex.BinaryExtension do

defmacro __using__(matching) do
quote location: :keep do

@behaviour Postgrex.Extension

def init(_), do: nil
Expand All @@ -12,7 +11,7 @@ defmodule Postgrex.BinaryExtension do

def format(_), do: :binary

defoverridable [init: 1]
defoverridable init: 1
end
end
end
20 changes: 10 additions & 10 deletions lib/postgrex/binary_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ defmodule Postgrex.BinaryUtils do
@moduledoc false

defmacro int64 do
quote do: signed-64
quote do: signed - 64
end

defmacro int32 do
quote do: signed-32
quote do: signed - 32
end

defmacro int16 do
quote do: signed-16
quote do: signed - 16
end

defmacro uint16 do
quote do: unsigned-16
quote do: unsigned - 16
end

defmacro uint32 do
quote do: unsigned-32
quote do: unsigned - 32
end

defmacro int8 do
quote do: signed-8
quote do: signed - 8
end

defmacro float64 do
quote do: float-64
quote do: float - 64
end

defmacro float32 do
quote do: float-32
quote do: float - 32
end

defmacro binary(size) do
quote do: binary-size(unquote(size))
quote do: binary - size(unquote(size))
end

defmacro binary(size, unit) do
quote do: binary-size(unquote(size))-unit(unquote(unit))
quote do: binary - size(unquote(size)) - unit(unquote(unit))
end
end
4 changes: 2 additions & 2 deletions lib/postgrex/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Postgrex.Error do

case metadata do
[] -> []
_ -> ["\n" | metadata]
_ -> ["\n" | metadata]
end
end

Expand All @@ -57,4 +57,4 @@ end

defmodule Postgrex.QueryError do
defexception [:message]
end
end
36 changes: 23 additions & 13 deletions lib/postgrex/error_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ defmodule Postgrex.ErrorCode do
# https://github.com/postgres/postgres/blob/master/src/backend/utils/errcodes.txt
@external_resource errcodes_path = Path.join(__DIR__, "errcodes.txt")

errcodes = for line <- File.stream!(errcodes_path),
match?(<<_code::(5 * 8), " ", _::binary>>, line) do
case String.split(line, " ", trim: true) do
[code, _, _, name] -> {code, name |> String.trim |> String.to_atom}
[code, _, _] -> {code} # duplicated code without name
errcodes =
for line <- File.stream!(errcodes_path),
match?(<<_code::5*8, " ", _::binary>>, line) do
case String.split(line, " ", trim: true) do
[code, _, _, name] -> {code, name |> String.trim() |> String.to_atom()}
# duplicated code without name
[code, _, _] -> {code}
end
end
end

{errcodes, duplicates} = Enum.split_with(errcodes, &match?({_, _}, &1))

Expand All @@ -30,13 +32,14 @@ defmodule Postgrex.ErrorCode do
iex> code_to_name("23505")
:unique_violation
"""
@spec code_to_name(String.t) :: atom | no_return
@spec code_to_name(String.t()) :: atom | no_return
def code_to_name(code)

for {code, errcodes} <- Enum.group_by(errcodes, &elem(&1, 0)) do
[{^code, name}] = errcodes
def code_to_name(unquote(code)), do: unquote(name)
end

def code_to_name(_), do: nil

@doc ~S"""
Expand All @@ -47,23 +50,30 @@ defmodule Postgrex.ErrorCode do
iex> name_to_code(:prohibited_sql_statement_attempted)
"2F003"
"""
@spec name_to_code(atom) :: String.t
@spec name_to_code(atom) :: String.t()
def name_to_code(name)

@code_decision_table [
string_data_right_truncation: "22001", # 01004 not used
modifying_sql_data_not_permitted: nil, # 38002 or 2F002 not used
prohibited_sql_statement_attempted: "2F003", # 38003 not used
reading_sql_data_not_permitted: nil, # 38004 or 2F004 not used
null_value_not_allowed: "22004" # 39004 not used
# 01004 not used
string_data_right_truncation: "22001",
# 38002 or 2F002 not used
modifying_sql_data_not_permitted: nil,
# 38003 not used
prohibited_sql_statement_attempted: "2F003",
# 38004 or 2F004 not used
reading_sql_data_not_permitted: nil,
# 39004 not used
null_value_not_allowed: "22004"
]

for {name, errcodes} <- Enum.group_by(errcodes, &elem(&1, 1)) do
case Keyword.fetch(@code_decision_table, name) do
{:ok, nil} ->
:ok

{:ok, code} ->
def name_to_code(unquote(name)), do: unquote(code)

:error ->
[{code, ^name}] = errcodes
def name_to_code(unquote(name)), do: unquote(code)
Expand Down
Loading

0 comments on commit 1cfa939

Please sign in to comment.