Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 6924417

Browse files
committed
Elixir 1.2.x doesn't have the else clause on with statement.
Refactor to make it work anyway.
1 parent e35ec1f commit 6924417

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

lib/sqlitex/server.ex

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,22 @@ defmodule Sqlitex.Server do
6666
end
6767

6868
def handle_call({:query, sql, opts}, _from, {db, stmt_cache}) do
69-
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql),
70-
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
71-
{:ok, rows} <- Statement.fetch_all(stmt, Keyword.get(opts, :into, []))
72-
do
73-
{:reply, {:ok, rows}, {db, new_cache}}
74-
else
69+
case query_impl(sql, opts, stmt_cache) do
70+
{:ok, result, new_cache} -> {:reply, {:ok, result}, {db, new_cache}}
7571
err -> {:reply, err, {db, stmt_cache}}
7672
end
7773
end
7874

7975
def handle_call({:query_rows, sql, opts}, _from, {db, stmt_cache}) do
80-
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql),
81-
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
82-
{:ok, rows} <- Statement.fetch_all(stmt, :raw_list)
83-
do
84-
{:reply,
85-
{:ok, %{rows: rows, columns: stmt.column_names, types: stmt.column_types}},
86-
{db, new_cache}}
87-
else
76+
case query_rows_impl(sql, opts, stmt_cache) do
77+
{:ok, result, new_cache} -> {:reply, {:ok, result}, {db, new_cache}}
8878
err -> {:reply, err, {db, stmt_cache}}
8979
end
9080
end
9181

9282
def handle_call({:prepare, sql}, _from, {db, stmt_cache}) do
93-
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql)
94-
do
95-
{:reply,
96-
{:ok, %{columns: stmt.column_names, types: stmt.column_types}},
97-
{db, new_cache}}
98-
else
83+
case prepare_impl(sql, stmt_cache) do
84+
{:ok, result, new_cache} -> {:reply, {:ok, result}, {db, new_cache}}
9985
err -> {:reply, err, {db, stmt_cache}}
10086
end
10187
end
@@ -142,5 +128,26 @@ defmodule Sqlitex.Server do
142128

143129
## Helpers
144130

131+
defp query_impl(sql, opts, stmt_cache) do
132+
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql),
133+
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
134+
{:ok, rows} <- Statement.fetch_all(stmt, Keyword.get(opts, :into, [])),
135+
do: {:ok, rows, new_cache}
136+
end
137+
138+
defp query_rows_impl(sql, opts, stmt_cache) do
139+
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql),
140+
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
141+
{:ok, rows} <- Statement.fetch_all(stmt, :raw_list),
142+
do: {:ok,
143+
%{rows: rows, columns: stmt.column_names, types: stmt.column_types},
144+
new_cache}
145+
end
146+
147+
defp prepare_impl(sql, stmt_cache) do
148+
with {%Cache{} = new_cache, stmt} <- Cache.prepare(stmt_cache, sql),
149+
do: {:ok, %{columns: stmt.column_names, types: stmt.column_types}, new_cache}
150+
end
151+
145152
defp timeout(kwopts), do: Keyword.get(kwopts, :timeout, 5000)
146153
end

0 commit comments

Comments
 (0)