我们如何使用Corda提供的VaultQuery API来运行带有EXISTS子句的子查询?

时间:2018-05-25 22:27:06

标签: corda

我的状态有以下配置

List<PaymentModel> paymentDetails = xml.Descendants("payment")
        .Where(
            d => d.Element("invoiceId").Value.Contains("K")
        )
        .Select(x => new Invoice
        {
            InvoiceId = x.Element("invoiceId").Value.ToString(),
            InvoiceAmount = (decimal)x.Element("invoiceAmount"),
            InvoiceDate = (DateTime)x.Element("invoiceDate")
        }).ToList<InvoiceModel>();

我的Persistent架构正在关注

data class NodeState(
    override val linearId: UniqueIdentifier = UniqueIdentifier(),
    val name: CordaX500Name,
    val active: Boolean,
    val services: List<Service>):LinearState, QueryableState 

现在我想使用类似于以下SQL查询的VaultQuery查询状态。我该怎么办?

class PersistentNodeState(
        @Column(name = "linearId")
        var linearId: String,

        @Column(name = "name")
        var name: String,

        @Column(name = "active")
        var active: Boolean,

        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(name = "node_services",
                joinColumns = arrayOf(
                        JoinColumn(name = "output_index", referencedColumnName = "output_index"),
                        JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id")
                )
        )
        @Column(name = "service")
        var services: Set<String>

我们如何使用Corda提供的VaultQuery API来运行带有EXISTS子句的子查询?

1 个答案:

答案 0 :(得分:1)

使用当前VaultQuery API无法做到这一点,此处记录在案:https://docs.corda.net/api-vault-query.html。您必须直接连接到节点的数据库并发出SQL查询。

您可以在流程中执行以下操作:

// Connecting to the node's database.
val session = serviceHub.jdbcSession()

// Writing to the node's database.
val preparedStatementWrite = session.prepareStatement("update my_table set value = ? where key = ?")
preparedStatementWrite.setInt(0, 100)
preparedStatementWrite.setString(1, "my_key")
preparedStatementWrite.executeUpdate()

// Reading from the node's database.
val preparedStatementRead = session.prepareStatement("select value from my_table where key = ?")
preparedStatementRead.setString(0, "my_key")
preparedStatementRead.executeQuery()

您也可以使用标准Java从节点外部执行此操作。