Skip to content

Commit 9efb99d

Browse files
authored
Advance to "Update DBConnection" commit from 9 Jan 2016. (#43)
* Advance to "Update DBConnection" commit from 9 Jan 2016. * Update db_connection and postgrex references to match then-current Ecto. * Pick up newer version of decimal library. * Pick up increased receive timeout from Postgrex of this time. * Mimic some of the changes that happened in Postgrex during this time. * Return a result object from transaction functions.
1 parent 9759d10 commit 9efb99d

File tree

7 files changed

+45
-57
lines changed

7 files changed

+45
-57
lines changed

config/config.exs

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
# This file is responsible for configuring your application
2-
# and its dependencies with the aid of the Mix.Config module.
31
use Mix.Config
42

5-
# This configuration is loaded before any dependency and is restricted
6-
# to this project. If another project depends on this project, this
7-
# file won't be loaded nor affect the parent project. For this reason,
8-
# if you want to provide default values for your application for third-
9-
# party users, it should be done in your mix.exs file.
10-
11-
# Sample configuration:
12-
#
13-
# config :logger, :console,
14-
# level: :info,
15-
# format: "$date $time [$level] $metadata$message\n",
16-
# metadata: [:user_id]
17-
18-
# It is also possible to import configuration files, relative to this
19-
# directory. For example, you can emulate configuration per environment
20-
# by uncommenting the line below and defining dev.exs, test.exs and such.
21-
# Configuration from the imported file will override the ones defined
22-
# here (which is why it is important to import them last).
23-
#
24-
# import_config "#{Mix.env}.exs"
3+
if Mix.env == :test do
4+
config :ex_unit, :assert_receive_timeout, 1000
5+
end

lib/sqlite_db_connection/connection.ex

+2-6
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ defmodule Sqlite.DbConnection.Connection do
7676
(default: `#{@pool_timeout}`)
7777
* `:queue` - Whether to wait for connection in a queue (default: `true`);
7878
* `:timeout` - Query request timeout (default: `#{@timeout}`);
79-
* `:encode_mapper` - Fun to map each parameter before encoding, see
80-
(default: `fn x -> x end`)
8179
* `:decode_mapper` - Fun to map each row in the result to a term after
8280
decoding, (default: `fn x -> x end`);
8381
* `:pool` - The pool module to use, must match that set on
@@ -173,8 +171,6 @@ defmodule Sqlite.DbConnection.Connection do
173171
(default: `#{@pool_timeout}`)
174172
* `:queue` - Whether to wait for connection in a queue (default: `true`);
175173
* `:timeout` - Execute request timeout (default: `#{@timeout}`);
176-
* `:encode_mapper` - Fun to map each parameter before encoding, see
177-
(default: `fn x -> x end`)
178174
* `:decode_mapper` - Fun to map each row in the result to a term after
179175
decoding, (default: `fn x -> x end`);
180176
* `:pool` - The pool module to use, must match that set on
@@ -235,10 +231,10 @@ defmodule Sqlite.DbConnection.Connection do
235231
@spec close(conn, Sqlite.DbConnection.Query.t, Keyword.t) :: :ok | {:error, Sqlite.DbConnection.Error.t}
236232
def close(conn, query, opts \\ []) do
237233
case DBConnection.close(conn, query, defaults(opts)) do
234+
{:ok, _} ->
235+
:ok
238236
{:error, %ArgumentError{} = err} ->
239237
raise err
240-
other ->
241-
other
242238
end
243239
end
244240

lib/sqlite_db_connection/error.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
defmodule Sqlite.DbConnection.Error do
2-
defexception [:message, :sqlite, :postgres]
2+
defexception [:message, :sqlite, :connection_id]
33
end

lib/sqlite_db_connection/protocol.ex

+21-9
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,34 @@ defmodule Sqlite.DbConnection.Protocol do
110110
handle_execute(query, params, :sync_close, opts, s)
111111
end
112112

113-
# @spec handle_close(Sqlite.DbConnection.Query.t, Keyword.t, state) ::
114-
# {:ok, state} |
115-
# {:error, ArgumentError.t, state} |
116-
# {:error | :disconnect, Sqlite.DbConnection.Error.t, state}
117-
# def handle_close(%Query{name: @reserved_prefix <> _} = query, _, s) do
118-
# reserved_error(query, s)
119-
# end
113+
@spec handle_close(Sqlite.DbConnection.Query.t, Keyword.t, state) ::
114+
{:ok, Sqlite.DbConnection.Result.t, state} |
115+
{:error, ArgumentError.t, state} |
116+
{:error | :disconnect, Sqlite.DbConnection.Error.t, state}
120117
def handle_close(_query, _opts, s) do
121118
# no-op: esqlite doesn't expose statement close.
122119
# Instead it relies on statements getting garbage collected.
123-
{:ok, s}
120+
res = %Sqlite.DbConnection.Result{command: :close}
121+
{:ok, res, s}
124122
end
125123

124+
@spec handle_begin(Keyword.t, state) ::
125+
{:ok, Sqlite.DbConnection.Result.t, state} |
126+
{:error | :disconnect, Sqlite.DbConnection.Error.t, state}
126127
def handle_begin(_opts, s) do
127128
handle_transaction("BEGIN", s)
128129
end
129130

131+
@spec handle_commit(Keyword.t, state) ::
132+
{:ok, Sqlite.DbConnection.Result.t, state} |
133+
{:error | :disconnect, Sqlite.DbConnection.Error.t, state}
130134
def handle_commit(_opts, s) do
131135
handle_transaction("COMMIT", s)
132136
end
133137

138+
@spec handle_rollback(Keyword.t, state) ::
139+
{:ok, Sqlite.DbConnection.Result.t, state} |
140+
{:error | :disconnect, Sqlite.DbConnection.Error.t, state}
134141
def handle_rollback(_opts, s) do
135142
handle_transaction("ROLLBACK", s)
136143
end
@@ -253,7 +260,12 @@ defmodule Sqlite.DbConnection.Protocol do
253260
defp handle_transaction(stmt, s) do
254261
case Sqlitex.Server.query_rows(s.db, stmt, into: :raw_list) do
255262
{:ok, _rows} ->
256-
{:ok, s}
263+
command = command_from_sql(stmt)
264+
result = %Sqlite.DbConnection.Result{rows: nil,
265+
num_rows: nil,
266+
columns: nil,
267+
command: command}
268+
{:ok, result, s}
257269
{:error, {_sqlite_errcode, _message}} = err ->
258270
sqlite_error(err, s)
259271
end

mix.exs

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ defmodule Sqlite.Ecto.Mixfile do
3333
[{:backoff, git: "https://github.com/scouten/backoff.git", ref: "8f10cb83b5fbc2401e6a06b341417cad4c632f34", override: true},
3434
{:connection, "~> 1.0.2", override: true},
3535
{:coverex, "~> 1.4.11", only: :test},
36-
{:db_connection, git: "https://github.com/fishcakez/db_connection", ref: "f438a1d66f238cc3d786f7aa7719fc36eaa283f2", override: true}, # version 0.1.7
36+
{:db_connection, git: "https://github.com/fishcakez/db_connection", ref: "05f3cc3dbf89f3cc475caaa229b20c30c589a5ef", override: true}, # version 0.1.7
37+
{:decimal, "1.1.1", override: true},
3738
{:esqlite, git: "https://github.com/mmzeeman/esqlite", ref: "3f1ef40b9011276eb8bdc366c5ef1e25d79befa5", override: true},
3839
{:ex_doc, "~> 0.14.5", only: :dev},
39-
{:ecto, git: "https://github.com/scouten/ecto.git", ref: "8e2a9274fc5ab6b9b973ca50f2045c5b952617bf"},
40+
{:ecto, git: "https://github.com/scouten/ecto.git", ref: "2f753223e26b3cda91acbe2ab130451cafafa3ec"},
4041
{:poison, "~> 1.0"},
41-
{:postgrex, git: "https://github.com/ericmj/postgrex.git", ref: "f773f8d4b88565aea435c6651b711ebfce74c9c2", override: true},
42+
{:postgrex, git: "https://github.com/ericmj/postgrex.git", ref: "1dd57fe884d88125fff0661be8af0b5b9b9355fe", override: true},
4243
{:sbroker, "~> 1.0", override: true},
4344
{:sqlitex, git: "https://github.com/scouten/sqlitex.git", ref: "8f1dcd4107cd99ca0687bf870b914e44a467722d", override: true}]
4445
end

mix.lock

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
%{"backoff": {:git, "https://github.com/scouten/backoff.git", "8f10cb83b5fbc2401e6a06b341417cad4c632f34", [ref: "8f10cb83b5fbc2401e6a06b341417cad4c632f34"]},
2-
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
2+
"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []},
33
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
4-
"coverex": {:hex, :coverex, "1.4.11", "fb963586be173e0e5253b200ca624e2708ed5dbe58b90ddbed27184a1f46d61e", [:mix], [{:hackney, "~> 1.5", [hex: :hackney, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
5-
"db_connection": {:git, "https://github.com/fishcakez/db_connection", "f438a1d66f238cc3d786f7aa7719fc36eaa283f2", [ref: "f438a1d66f238cc3d786f7aa7719fc36eaa283f2"]},
4+
"coverex": {:hex, :coverex, "1.4.12", "b18d737734edeac578a854cfaa5aa48c5d05e649c77ce02efb7f723a316b6a3b", [:mix], [{:hackney, "~> 1.5", [hex: :hackney, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
5+
"db_connection": {:git, "https://github.com/fishcakez/db_connection", "05f3cc3dbf89f3cc475caaa229b20c30c589a5ef", [ref: "05f3cc3dbf89f3cc475caaa229b20c30c589a5ef"]},
66
"decimal": {:hex, :decimal, "1.1.1", "a8ff5b673105e6cdaca96f799aeefc6f07142881b616c65db16e14e556b16e76", [:mix], []},
7-
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
8-
"ecto": {:git, "https://github.com/scouten/ecto.git", "8e2a9274fc5ab6b9b973ca50f2045c5b952617bf", [ref: "8e2a9274fc5ab6b9b973ca50f2045c5b952617bf"]},
7+
"earmark": {:hex, :earmark, "1.1.1", "433136b7f2e99cde88b745b3a0cfc3fbc81fe58b918a09b40fce7f00db4d8187", [:mix], []},
8+
"ecto": {:git, "https://github.com/scouten/ecto.git", "2f753223e26b3cda91acbe2ab130451cafafa3ec", [ref: "2f753223e26b3cda91acbe2ab130451cafafa3ec"]},
99
"esqlite": {:git, "https://github.com/mmzeeman/esqlite", "3f1ef40b9011276eb8bdc366c5ef1e25d79befa5", [ref: "3f1ef40b9011276eb8bdc366c5ef1e25d79befa5"]},
1010
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
11-
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
12-
"httpoison": {:hex, :httpoison, "0.10.0", "4727b3a5e57e9a4ff168a3c2883e20f1208103a41bccc4754f15a9366f49b676", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]},
13-
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
11+
"hackney": {:hex, :hackney, "1.6.6", "5564b4695d48fd87859e9df77a7fa4b4d284d24519f0cd7cc898f09e8fbdc8a3", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
12+
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
1413
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
1514
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
16-
"pipe": {:hex, :pipe, "0.0.2", "eff98a868b426745acef103081581093ff5c1b88100f8ff5949b4a30e81d0d9f", [:mix], []},
1715
"poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []},
1816
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
19-
"postgrex": {:git, "https://github.com/ericmj/postgrex.git", "f773f8d4b88565aea435c6651b711ebfce74c9c2", [ref: "f773f8d4b88565aea435c6651b711ebfce74c9c2"]},
17+
"postgrex": {:git, "https://github.com/ericmj/postgrex.git", "1dd57fe884d88125fff0661be8af0b5b9b9355fe", [ref: "1dd57fe884d88125fff0661be8af0b5b9b9355fe"]},
2018
"sbroker": {:hex, :sbroker, "1.0.0", "28ff1b5e58887c5098539f236307b36fe1d3edaa2acff9d6a3d17c2dcafebbd0", [:rebar3], []},
2119
"sqlitex": {:git, "https://github.com/scouten/sqlitex.git", "8f1dcd4107cd99ca0687bf870b914e44a467722d", [ref: "8f1dcd4107cd99ca0687bf870b914e44a467722d"]},
2220
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}

test/sqlite_db_connection/query_test.exs

+6-6
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ defmodule QueryTest do
121121
assert [[42]] = execute(query, [42])
122122
end
123123

124-
test "execute with encode mapper", context do
125-
assert (%Sqlite.DbConnection.Query{} = query) = prepare("mapper", "SELECT cast($1 as int)")
126-
assert [[84]] = execute(query, [42], [encode_mapper: fn(n) -> n * 2 end])
127-
assert :ok = close(query)
128-
assert [[42]] = query("SELECT 42", [])
129-
end
124+
# test "execute with encode mapper", context do
125+
# assert (%Sqlite.DbConnection.Query{} = query) = prepare("mapper", "SELECT cast($1 as int)")
126+
# assert [[84]] = execute(query, [42], [encode_mapper: fn(n) -> n * 2 end])
127+
# assert :ok = close(query)
128+
# assert [[42]] = query("SELECT 42", [])
129+
# end
130130

131131
test "closing prepared query that does not exist succeeds", context do
132132
assert (%Sqlite.DbConnection.Query{} = query) = prepare("42", "SELECT 42")

0 commit comments

Comments
 (0)