forked from Qortal/qortal
Improve SQL prepared statement caching in HSQLDBATRepository, plus missing space in SQL in getMatchingFinalATStates
This commit is contained in:
parent
f3e1092dd5
commit
4209cc6ee4
@ -137,16 +137,21 @@ public class HSQLDBATRepository implements ATRepository {
|
|||||||
@Override
|
@Override
|
||||||
public List<ATData> getATsByFunctionality(byte[] codeHash, Boolean isExecutable, Integer limit, Integer offset, Boolean reverse) throws DataException {
|
public List<ATData> getATsByFunctionality(byte[] codeHash, Boolean isExecutable, Integer limit, Integer offset, Boolean reverse) throws DataException {
|
||||||
StringBuilder sql = new StringBuilder(512);
|
StringBuilder sql = new StringBuilder(512);
|
||||||
|
List<Object> bindParams = new ArrayList<>();
|
||||||
|
|
||||||
sql.append("SELECT AT_address, creator, created_when, version, asset_id, code_bytes, ")
|
sql.append("SELECT AT_address, creator, created_when, version, asset_id, code_bytes, ")
|
||||||
.append("is_sleeping, sleep_until_height, is_finished, had_fatal_error, ")
|
.append("is_sleeping, sleep_until_height, is_finished, had_fatal_error, ")
|
||||||
.append("is_frozen, frozen_balance ")
|
.append("is_frozen, frozen_balance ")
|
||||||
.append("FROM ATs ")
|
.append("FROM ATs ")
|
||||||
.append("WHERE code_hash = ? ");
|
.append("WHERE code_hash = ? ");
|
||||||
|
bindParams.add(codeHash);
|
||||||
|
|
||||||
if (isExecutable != null)
|
if (isExecutable != null) {
|
||||||
sql.append("AND is_finished = ").append(isExecutable ? "false" : "true");
|
sql.append("AND is_finished = ? ");
|
||||||
|
bindParams.add(isExecutable);
|
||||||
|
}
|
||||||
|
|
||||||
sql.append(" ORDER BY created_when ");
|
sql.append("ORDER BY created_when ");
|
||||||
if (reverse != null && reverse)
|
if (reverse != null && reverse)
|
||||||
sql.append("DESC");
|
sql.append("DESC");
|
||||||
|
|
||||||
@ -154,7 +159,7 @@ public class HSQLDBATRepository implements ATRepository {
|
|||||||
|
|
||||||
List<ATData> matchingATs = new ArrayList<>();
|
List<ATData> matchingATs = new ArrayList<>();
|
||||||
|
|
||||||
try (ResultSet resultSet = this.repository.checkedExecute(sql.toString(), codeHash)) {
|
try (ResultSet resultSet = this.repository.checkedExecute(sql.toString(), bindParams.toArray())) {
|
||||||
if (resultSet == null)
|
if (resultSet == null)
|
||||||
return matchingATs;
|
return matchingATs;
|
||||||
|
|
||||||
@ -296,6 +301,8 @@ public class HSQLDBATRepository implements ATRepository {
|
|||||||
Integer dataByteOffset, Long expectedValue, Integer minimumFinalHeight,
|
Integer dataByteOffset, Long expectedValue, Integer minimumFinalHeight,
|
||||||
Integer limit, Integer offset, Boolean reverse) throws DataException {
|
Integer limit, Integer offset, Boolean reverse) throws DataException {
|
||||||
StringBuilder sql = new StringBuilder(1024);
|
StringBuilder sql = new StringBuilder(1024);
|
||||||
|
List<Object> bindParams = new ArrayList<>();
|
||||||
|
|
||||||
sql.append("SELECT AT_address, height, created_when, state_data, state_hash, fees, is_initial "
|
sql.append("SELECT AT_address, height, created_when, state_data, state_hash, fees, is_initial "
|
||||||
+ "FROM ATs "
|
+ "FROM ATs "
|
||||||
+ "CROSS JOIN LATERAL("
|
+ "CROSS JOIN LATERAL("
|
||||||
@ -304,18 +311,16 @@ public class HSQLDBATRepository implements ATRepository {
|
|||||||
+ "WHERE ATStates.AT_address = ATs.AT_address ");
|
+ "WHERE ATStates.AT_address = ATs.AT_address ");
|
||||||
|
|
||||||
if (minimumFinalHeight != null) {
|
if (minimumFinalHeight != null) {
|
||||||
sql.append("AND height >= ");
|
sql.append("AND height >= ? ");
|
||||||
sql.append(minimumFinalHeight);
|
bindParams.add(minimumFinalHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
// AT_address then height so the compound primary key is used as an index
|
// AT_address then height so the compound primary key is used as an index
|
||||||
// Both must be the same direction also
|
// Both must be the same direction also
|
||||||
sql.append( "ORDER BY AT_address DESC, height DESC "
|
sql.append("ORDER BY AT_address DESC, height DESC "
|
||||||
+ "LIMIT 1 "
|
+ "LIMIT 1 "
|
||||||
+ ") AS FinalATStates "
|
+ ") AS FinalATStates "
|
||||||
+ "WHERE code_hash = ? ");
|
+ "WHERE code_hash = ? ");
|
||||||
|
|
||||||
List<Object> bindParams = new ArrayList<>();
|
|
||||||
bindParams.add(codeHash);
|
bindParams.add(codeHash);
|
||||||
|
|
||||||
if (isFinished != null) {
|
if (isFinished != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user