-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
132825c
commit 8a94dcc
Showing
6 changed files
with
102 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lib/ecto/adapters/foundationdb/ecto_adapter_transaction.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Ecto.Adapters.FoundationDB.EctoAdapterTransaction do | ||
@moduledoc """ | ||
Implemenation of Ecto.Adapter.Transaction | ||
""" | ||
alias Ecto.Adapters.FoundationDB | ||
alias Ecto.Adapters.FoundationDB.Layer.Tx | ||
@behaviour Ecto.Adapter.Transaction | ||
|
||
@rollback :__ectofdbtxrollback__ | ||
|
||
@doc """ | ||
Runs the given function inside a transaction. | ||
Returns `{:ok, value}` if the transaction was successful where `value` | ||
is the value return by the function or `{:error, value}` if the transaction | ||
was rolled back where `value` is the value given to `rollback/1`. | ||
""" | ||
@impl true | ||
def transaction(_adapter_meta, options, function) when is_function(function, 0) do | ||
FoundationDB.transactional(options[:prefix], fn -> | ||
function.() | ||
end) | ||
catch | ||
{@rollback, value} -> {:error, value} | ||
end | ||
|
||
def transaction(_adapter_meta, options, function) when is_function(function, 1) do | ||
FoundationDB.transactional(options[:prefix], fn repo -> | ||
function.(repo) | ||
end) | ||
catch | ||
{:__ectofdbtxrollback__, value} -> {:error, value} | ||
end | ||
|
||
@doc """ | ||
Returns true if the given process is inside a transaction. | ||
""" | ||
@impl true | ||
def in_transaction?(_adapter_meta) do | ||
Tx.in_tx?() | ||
end | ||
|
||
@doc """ | ||
Rolls back the current transaction. | ||
The transaction will return the value given as `{:error, value}`. | ||
See `c:Ecto.Repo.rollback/1`. | ||
""" | ||
@impl true | ||
def rollback(adapter_meta, value) do | ||
if in_transaction?(adapter_meta) do | ||
throw({@rollback, value}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters