Extract duplicate where condition

pull/156/head
Tim Mecklem 7 years ago
parent 7e2f2c4ba6
commit 78d2379998
  1. 27
      apps/explorer/lib/explorer/chain.ex

@ -75,13 +75,7 @@ defmodule Explorer.Chain do
|> join(:inner, [internal_transaction], transaction in assoc(internal_transaction, :transaction))
|> join(:left, [internal_transaction, transaction], block in assoc(transaction, :block))
|> where_address_fields_match(direction, id)
|> where(
[_it, t],
fragment(
"(SELECT COUNT(sibling.id) FROM internal_transactions as sibling WHERE sibling.transaction_id = ?) > 1",
t.id
)
)
|> where_transaction_has_multiple_internal_transactions()
|> order_by([it, transaction, block], desc: block.number, desc: transaction.transaction_index, desc: it.index)
|> preload(transaction: :block)
|> join_associations(necessity_by_association)
@ -477,13 +471,7 @@ defmodule Explorer.Chain do
InternalTransaction
|> for_parent_transaction(hash)
|> join_associations(necessity_by_association)
|> where(
[_it, t],
fragment(
"(SELECT COUNT(sibling.id) FROM internal_transactions as sibling WHERE sibling.transaction_id = ?) > 1",
t.id
)
)
|> where_transaction_has_multiple_internal_transactions()
|> order_by(:index)
|> Repo.paginate(pagination)
end
@ -701,4 +689,15 @@ defmodule Explorer.Chain do
)
)
end
defp where_transaction_has_multiple_internal_transactions(query) do
where(
query,
[_it, transaction],
fragment(
"(SELECT COUNT(sibling.id) FROM internal_transactions as sibling WHERE sibling.transaction_id = ?) > 1",
transaction.id
)
)
end
end

Loading…
Cancel
Save