|
@ -3,7 +3,7 @@ package com.xsn.explorer.data.anorm.dao |
|
|
import java.sql.Connection |
|
|
import java.sql.Connection |
|
|
|
|
|
|
|
|
import anorm._ |
|
|
import anorm._ |
|
|
import com.alexitc.playsonify.models.ordering.FieldOrdering |
|
|
import com.alexitc.playsonify.models.ordering.{FieldOrdering, OrderingCondition} |
|
|
import com.alexitc.playsonify.models.pagination.{Count, Limit, PaginatedQuery} |
|
|
import com.alexitc.playsonify.models.pagination.{Count, Limit, PaginatedQuery} |
|
|
import com.alexitc.playsonify.sql.FieldOrderingSQLInterpreter |
|
|
import com.alexitc.playsonify.sql.FieldOrderingSQLInterpreter |
|
|
import com.xsn.explorer.data.anorm.parsers.TransactionParsers._ |
|
|
import com.xsn.explorer.data.anorm.parsers.TransactionParsers._ |
|
@ -89,15 +89,17 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Get the latest transactions by the given address. |
|
|
* Get the transactions by the given address (sorted by time). |
|
|
*/ |
|
|
*/ |
|
|
def getLatestBy(address: Address, limit: Limit)(implicit conn: Connection): List[Transaction] = { |
|
|
def getBy(address: Address, limit: Limit, orderingCondition: OrderingCondition)(implicit conn: Connection): List[Transaction] = { |
|
|
|
|
|
val order = toSQL(orderingCondition) |
|
|
|
|
|
|
|
|
SQL( |
|
|
SQL( |
|
|
""" |
|
|
s""" |
|
|
|SELECT t.txid, t.blockhash, t.time, t.size |
|
|
|SELECT t.txid, t.blockhash, t.time, t.size |
|
|
|FROM transactions t JOIN address_transaction_details USING (txid) |
|
|
|FROM transactions t JOIN address_transaction_details USING (txid) |
|
|
|WHERE address = {address} |
|
|
|WHERE address = {address} |
|
|
|ORDER BY time DESC |
|
|
|ORDER BY time $order |
|
|
|LIMIT {limit} |
|
|
|LIMIT {limit} |
|
|
""".stripMargin |
|
|
""".stripMargin |
|
|
).on( |
|
|
).on( |
|
@ -107,16 +109,26 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Get the latest transactions by the given address that occurred before the last seen transaction. |
|
|
* Get the transactions by the given address (sorted by time). |
|
|
|
|
|
* |
|
|
|
|
|
* - When orderingCondition = DescendingOrder, the transactions that occurred before the last seen transaction are retrieved. |
|
|
|
|
|
* - When orderingCondition = AscendingOrder, the transactions that occurred after the last seen transaction are retrieved. |
|
|
*/ |
|
|
*/ |
|
|
def getLatestBy( |
|
|
def getBy( |
|
|
address: Address, |
|
|
address: Address, |
|
|
lastSeenTxid: TransactionId, |
|
|
lastSeenTxid: TransactionId, |
|
|
limit: Limit)( |
|
|
limit: Limit, |
|
|
|
|
|
orderingCondition: OrderingCondition)( |
|
|
implicit conn: Connection): List[Transaction] = { |
|
|
implicit conn: Connection): List[Transaction] = { |
|
|
|
|
|
|
|
|
|
|
|
val order = toSQL(orderingCondition) |
|
|
|
|
|
val comparator = orderingCondition match { |
|
|
|
|
|
case OrderingCondition.DescendingOrder => "<" |
|
|
|
|
|
case OrderingCondition.AscendingOrder => ">" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
SQL( |
|
|
SQL( |
|
|
""" |
|
|
s""" |
|
|
|WITH CTE AS ( |
|
|
|WITH CTE AS ( |
|
|
| SELECT time AS lastSeenTime |
|
|
| SELECT time AS lastSeenTime |
|
|
| FROM transactions |
|
|
| FROM transactions |
|
@ -126,8 +138,8 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi |
|
|
|FROM CTE CROSS JOIN transactions t |
|
|
|FROM CTE CROSS JOIN transactions t |
|
|
| JOIN address_transaction_details USING (txid) |
|
|
| JOIN address_transaction_details USING (txid) |
|
|
|WHERE address = {address} AND |
|
|
|WHERE address = {address} AND |
|
|
| (t.time < lastSeenTime OR (t.time = lastSeenTime AND t.txid > {lastSeenTxid})) |
|
|
| (t.time $comparator lastSeenTime OR (t.time = lastSeenTime AND t.txid > {lastSeenTxid})) |
|
|
|ORDER BY time DESC |
|
|
|ORDER BY time $order |
|
|
|LIMIT {limit} |
|
|
|LIMIT {limit} |
|
|
""".stripMargin |
|
|
""".stripMargin |
|
|
).on( |
|
|
).on( |
|
@ -453,4 +465,9 @@ class TransactionPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderi |
|
|
|
|
|
|
|
|
result |
|
|
result |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private def toSQL(condition: OrderingCondition): String = condition match { |
|
|
|
|
|
case OrderingCondition.AscendingOrder => "ASC" |
|
|
|
|
|
case OrderingCondition.DescendingOrder => "DESC" |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|