org.babyfish.jimmer.sql.cache.TransactionCacheOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.cache;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.babyfish.jimmer.jackson.ImmutableModule;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.sql.cache.spi.AbstractCacheOperator;
import org.babyfish.jimmer.sql.runtime.ConnectionManager;
import org.babyfish.jimmer.sql.runtime.ExecutionException;
import org.babyfish.jimmer.sql.runtime.JSqlClientImplementor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.*;
public class TransactionCacheOperator extends AbstractCacheOperator {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionCacheOperator.class);
public static final String TABLE_NAME = "JIMMER_TRANS_CACHE_OPERATOR";
private static final String ID = "ID";
private static final String IMMUTABLE_TYPE = "IMMUTABLE_TYPE";
private static final String IMMUTABLE_PROP = "IMMUTABLE_PROP";
private static final String CACHE_KEY = "CACHE_KEY";
private static final String REASON = "REASON";
private static final String INSERT =
"insert into " +
TABLE_NAME + "(" +
IMMUTABLE_TYPE +
", " +
IMMUTABLE_PROP +
", " +
CACHE_KEY +
", " +
REASON +
") values(?, ?, ?, ?)";
private static final String SELECT_ID_PREFIX =
"select " +
ID +
" from " +
TABLE_NAME +
" order by " +
ID +
" limit ";
private static final String SELECT_PREFIX =
"select " +
ID +
", " +
IMMUTABLE_TYPE +
", " +
IMMUTABLE_PROP +
", " +
CACHE_KEY +
", " +
REASON +
" from " +
TABLE_NAME +
" where " +
ID +
" in";
private static final String DELETE_PREFIX =
"delete from " +
TABLE_NAME +
" where " +
ID +
" in";
private final ObjectMapper mapper;
private final int batchSize;
public TransactionCacheOperator() {
this(null, 32);
}
public TransactionCacheOperator(int batchSize) {
this(null, batchSize);
}
public TransactionCacheOperator(ObjectMapper mapper) {
this(mapper, 32);
}
public TransactionCacheOperator(ObjectMapper mapper, int batchSize) {
if (batchSize < 1) {
throw new IllegalArgumentException("`batchSize` cannot be less than 1");
}
this.mapper = mapper != null ?
mapper :
new ObjectMapper()
.registerModule(new JavaTimeModule())
.registerModule(new ImmutableModule());
this.batchSize = batchSize;
}
@Override
protected void onInitialize(JSqlClientImplementor sqlClient) {
ConnectionManager connectionManager = sqlClient.getConnectionManager();
if (connectionManager == null) {
throw new IllegalArgumentException("The `sqlClient` must support connection manager");
}
connectionManager.execute(con -> {
try {
try (ResultSet rs = con.getMetaData().getTables(
null,
null,
"JIMMER_TRANS_CACHE_OPERATOR",
null
)) {
if (rs.next()) {
return null;
}
}
try (ResultSet rs = con.getMetaData().getTables(
null,
null,
"jimmer_trans_cache_operator",
null
)) {
if (rs.next()) {
return null;
}
}
try (Statement statement = con.createStatement()) {
statement.execute(sqlClient.getDialect().transCacheOperatorTableDDL());
}
return null;
} catch(SQLException ex) {
throw new ExecutionException(
"Cannot create table `" +
TransactionCacheOperator.TABLE_NAME +
"`",
ex
);
}
});
}
@Override
public void delete(UsedCache