我想在Corda(M14)数据库中选择与6个字段中至少3个匹配的条件的行,并通过匹配字段对这些结果进行排序。
以下是选择字段的SQL语法:
WHERE (field1 = ?) + (field2 = ?) + (... = ?) > 3
并订购:
ORDER BY ((field1 = ?) + (field2 = ?) + (... = ?)) DESC
另一种方法:
SELECT *, ((field1 = @inputFirst) + (field2 = @inputLast)) as Matches
FROM mytable
HAVING Matches > 1
ORDER BY Matches DESC
我开始创建标准:
vaultCriteria
.or(QueryCriteria.VaultCustomQueryCriteria(field1))
.or(QueryCriteria.VaultCustomQueryCriteria(field12))
.or(QueryCriteria.VaultCustomQueryCriteria(field3))
但是现在我被困在如何通过字段的匹配数字来分组这些结果并排序这些,任何想法?
谢谢,
Loup的
答案 0 :(得分:2)
我还没有测试过采用API的可能性,但我通过编写自定义SQL获得了成功(在当前的Master上) - 例如。
val session = services.jdbcSession()
val consensusQuery = """
SELECT COUNT(*) - COUNT(NULLIF(factObject, ?)), COUNT(*)
FROM submission_states
WHERE factSubject = ? AND factPredicate = ?
"""
val consensusStatement = session.prepareStatement(consensusQuery)
consensusStatement.setString(1, factConsensusQuery.factObject)
consensusStatement.setString(2, factConsensusQuery.factSubject)
consensusStatement.setString(3, factConsensusQuery.factPredicate)
log.info("SQL to execute: " + consensusStatement.toString())
val rs = consensusStatement.executeQuery()
答案 1 :(得分:1)
对于高级自定义SQL查询,建议您使用可从JDBCSession
获取的标准ServiceHub
。
请参阅https://docs.corda.net/head/api-persistence.html?highlight=jdbcsession
答案 2 :(得分:1)
对于M14版本,您有2个选项:
1)直接从DatabaseTransactionManager获取jdbcSession:
val jdbcSession1 = DatabaseTransactionManager.current().connection
2)从RequeryConfiguration对象间接获取jdbcSession:
val jdbcSession2 = RequeryConfiguration(<dataSourceProperties>).jdbcSession()
其中<dataSourceProperties>
看起来像这样:
private fun makePersistentDataSourceProperties(): Properties {
val props = Properties()
props.setProperty("dataSourceClassName", "org.h2.jdbcx.JdbcDataSource")
props.setProperty("dataSource.url", "jdbc:h2:~/test/vault_query_persistence;DB_CLOSE_ON_EXIT=TRUE")
props.setProperty("dataSource.user", "sa")
props.setProperty("dataSource.password", "")
return props
}