use of io.jmix.data.Sequence in project jmix by jmix-framework.
the class SequencesImpl method executeScript.
protected Object executeScript(Sequence sequence, String sqlScript) {
JdbcTemplate jdbcTemplate = storeAwareLocator.getJdbcTemplate(getDataStore(sequence));
StringTokenizer tokenizer = new StringTokenizer(sqlScript, SequenceSupport.SQL_DELIMITER);
Object value = null;
while (tokenizer.hasNext()) {
String sql = tokenizer.nextToken();
try {
Object result = jdbcTemplate.execute(sql, (PreparedStatementCallback<Object>) ps -> {
if (ps.execute()) {
ResultSet rs = ps.getResultSet();
if (rs.next())
return rs.getLong(1);
}
return NO_RESULT;
});
if (result != NO_RESULT) {
value = result;
}
} catch (DataAccessException e) {
throw new IllegalStateException("Error executing SQL for getting next number", e);
}
}
return value;
}
use of io.jmix.data.Sequence in project jmix by jmix-framework.
the class UniqueNumbers method setCurrentNumber.
@Override
public void setCurrentNumber(String domain, long value) {
Sequence sequence = Sequence.withName(getSequenceName(domain)).setStore(getDataStore(domain));
sequences.setCurrentValue(sequence, value);
}
use of io.jmix.data.Sequence in project jmix by jmix-framework.
the class SequencesImpl method getResult.
protected long getResult(Sequence sequence, String sqlScript) {
lock.readLock().lock();
try {
TransactionTemplate transactionTemplate = storeAwareLocator.getTransactionTemplate(getDataStore(sequence));
Object value = transactionTemplate.execute((TransactionCallback<?>) status -> {
checkSequenceExists(sequence);
return executeScript(sequence, sqlScript);
});
if (value instanceof Long)
return (Long) value;
else if (value instanceof BigDecimal)
return ((BigDecimal) value).longValue();
else if (value instanceof BigInteger)
return ((BigInteger) value).longValue();
else if (value instanceof String)
return Long.parseLong((String) value);
else if (value == null)
throw new IllegalStateException("No value returned");
else
throw new IllegalStateException("Unsupported value type: " + value.getClass());
} finally {
lock.readLock().unlock();
}
}
use of io.jmix.data.Sequence in project jmix by jmix-framework.
the class UniqueNumbers method deleteSequence.
@Override
public void deleteSequence(String domain) {
Sequence sequence = Sequence.withName(getSequenceName(domain)).setStore(getDataStore(domain));
sequences.deleteSequence(sequence);
}
Aggregations