h2 cash selection query to support multiple onlyFromIssuerParties and… (#3659)

* h2 cash selection query to support multiple onlyFromIssuerParties and withIssuerRefs

* Rid of the compilation warnings
This commit is contained in:
cxyzhang0 2018-07-24 02:09:37 -07:00 committed by Joel Dudley
parent b9efbaa228
commit 2a5b7371d0
2 changed files with 23 additions and 8 deletions

View File

@ -43,10 +43,14 @@ class CashSelectionH2Impl : AbstractCashSelection() {
""" +
(if (notary != null)
" AND vs.notary_name = ?" else "") +
(if (onlyFromIssuerParties.isNotEmpty())
" AND ccs.issuer_key_hash IN (?)" else "") +
(if (withIssuerRefs.isNotEmpty())
" AND ccs.issuer_ref IN (?)" else "")
(if (onlyFromIssuerParties.isNotEmpty()) {
val repeats = generateSequence { "?" }.take(onlyFromIssuerParties.size).joinToString(",")
" AND ccs.issuer_key_hash IN ($repeats)"
} else "") +
(if (withIssuerRefs.isNotEmpty()) {
val repeats = generateSequence { "?" }.take(withIssuerRefs.size).joinToString(",")
" AND ccs.issuer_ref IN ($repeats)"
} else "")
// Use prepared statement for protection against SQL Injection (http://www.h2database.com/html/advanced.html#sql_injection)
connection.prepareStatement(selectJoin).use { psSelectJoin ->
@ -56,10 +60,12 @@ class CashSelectionH2Impl : AbstractCashSelection() {
psSelectJoin.setString(++pIndex, lockId.toString())
if (notary != null)
psSelectJoin.setString(++pIndex, notary.name.toString())
if (onlyFromIssuerParties.isNotEmpty())
psSelectJoin.setObject(++pIndex, onlyFromIssuerParties.map { it.owningKey.toStringShort() as Any }.toTypedArray())
if (withIssuerRefs.isNotEmpty())
psSelectJoin.setObject(++pIndex, withIssuerRefs.map { it.bytes as Any }.toTypedArray())
onlyFromIssuerParties.forEach {
psSelectJoin.setString(++pIndex, it.owningKey.toStringShort())
}
withIssuerRefs.forEach {
psSelectJoin.setBytes(++pIndex, it.bytes)
}
log.debug { psSelectJoin.toString() }
psSelectJoin.executeQuery().use { rs ->

View File

@ -69,4 +69,13 @@ class CashSelectionH2ImplTest {
val paymentResult = node.startFlow(CashPaymentFlow(999.POUNDS, node.info.legalIdentities[0], false)).getOrThrow()
assertNotNull(paymentResult.recipient)
}
@Test
fun `multiple issuers in issuerConstraint condition`() {
val node = mockNet.createNode()
node.startFlow(CashIssueFlow(1.POUNDS, OpaqueBytes.of(1), mockNet.defaultNotaryIdentity)).getOrThrow()
val request = CashPaymentFlow.PaymentRequest(1.POUNDS, node.info.legalIdentities[0], true, setOf(node.info.legalIdentities[0], mockNet.defaultNotaryIdentity))
val paymentResult = node.startFlow(CashPaymentFlow(request)).getOrThrow()
assertNotNull(paymentResult.recipient)
}
}