Search in sources :

Example 1 with Sequence

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;
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) DbmsSpecifics(io.jmix.data.persistence.DbmsSpecifics) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) StringUtils(org.apache.commons.lang3.StringUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Sequence(io.jmix.data.Sequence) StringTokenizer(org.apache.commons.text.StringTokenizer) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SequenceSupport(io.jmix.data.persistence.SequenceSupport) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) Component(org.springframework.stereotype.Component) Query(javax.persistence.Query) List(java.util.List) Stores(io.jmix.core.Stores) Sequences(io.jmix.data.Sequences) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Preconditions(com.google.common.base.Preconditions) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Pattern(java.util.regex.Pattern) StoreAwareLocator(io.jmix.data.StoreAwareLocator) StringTokenizer(org.apache.commons.text.StringTokenizer) ResultSet(java.sql.ResultSet) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException)

Example 2 with Sequence

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);
}
Also used : Sequence(io.jmix.data.Sequence)

Example 3 with Sequence

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();
    }
}
Also used : DataAccessException(org.springframework.dao.DataAccessException) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) DbmsSpecifics(io.jmix.data.persistence.DbmsSpecifics) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) StringUtils(org.apache.commons.lang3.StringUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) BigDecimal(java.math.BigDecimal) ResultSet(java.sql.ResultSet) BigInteger(java.math.BigInteger) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Sequence(io.jmix.data.Sequence) StringTokenizer(org.apache.commons.text.StringTokenizer) PreparedStatementCallback(org.springframework.jdbc.core.PreparedStatementCallback) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SequenceSupport(io.jmix.data.persistence.SequenceSupport) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) Component(org.springframework.stereotype.Component) Query(javax.persistence.Query) List(java.util.List) Stores(io.jmix.core.Stores) Sequences(io.jmix.data.Sequences) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Preconditions(com.google.common.base.Preconditions) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Pattern(java.util.regex.Pattern) StoreAwareLocator(io.jmix.data.StoreAwareLocator) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 4 with Sequence

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);
}
Also used : Sequence(io.jmix.data.Sequence)

Aggregations

Sequence (io.jmix.data.Sequence)4 Preconditions (com.google.common.base.Preconditions)2 Stores (io.jmix.core.Stores)2 Sequences (io.jmix.data.Sequences)2 StoreAwareLocator (io.jmix.data.StoreAwareLocator)2 DbmsSpecifics (io.jmix.data.persistence.DbmsSpecifics)2 SequenceSupport (io.jmix.data.persistence.SequenceSupport)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 Set (java.util.Set)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 Pattern (java.util.regex.Pattern)2 EntityManager (javax.persistence.EntityManager)2 Query (javax.persistence.Query)2 StringUtils (org.apache.commons.lang3.StringUtils)2 StringTokenizer (org.apache.commons.text.StringTokenizer)2