package io.ebeaninternal.server.core;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.api.TxnProfileEventCodes;
import io.ebeaninternal.server.persist.BatchControl;
import io.ebeaninternal.server.persist.BatchPostExecute;
import io.ebeaninternal.server.persist.BatchedSqlException;
import io.ebeaninternal.server.persist.PersistExecute;
import javax.persistence.PersistenceException;
import java.sql.SQLException;
public abstract class PersistRequest extends BeanRequest implements BatchPostExecute, TxnProfileEventCodes {
public enum Type {
INSERT(EVT_INSERT),
UPDATE(EVT_UPDATE),
DELETE(EVT_DELETE),
DELETE_SOFT(EVT_DELETE_SOFT),
DELETE_PERMANENT(EVT_DELETE_PERMANENT),
UPDATESQL(EVT_UPDATESQL),
CALLABLESQL(EVT_CALLABLESQL);
final String profileEventId;
Type(String profileEventId) {
this.profileEventId = profileEventId;
}
}
boolean persistCascade;
protected Type type;
final PersistExecute persistExecute;
protected String label;
protected long startNanos;
PersistRequest(SpiEbeanServer server, SpiTransaction t, PersistExecute persistExecute) {
super(server, t);
this.persistExecute = persistExecute;
}
PersistRequest(SpiEbeanServer server, SpiTransaction t, PersistExecute persistExecute, String label) {
this(server, t, persistExecute);
this.label = label;
}
@Override
public void addTimingBatch(long startNanos, int size) {
}
public void addTimingNoBatch(long startNanos) {
}
public void startBind(boolean batchThisRequest) {
if (!batchThisRequest && label != null) {
startNanos = System.nanoTime();
}
}
@Override
public boolean isFlushQueue() {
return false;
}
public abstract int executeOrQueue();
public abstract int executeNow();
void profileBase(String event, long offset, String beanName, int beanCount) {
transaction.profileStream().addPersistEvent(event, offset, beanName, beanCount);
}
@Override
public boolean isLogSql() {
return transaction.isLogSql();
}
@Override
public boolean isLogSummary() {
return transaction.isLogSummary();
}
public boolean isBatchThisRequest() {
return transaction.isBatchThisRequest();
}
public PersistenceException translateSqlException(SQLException e) {
return transaction.translate(e.getMessage(), e);
}
int executeStatement() {
return executeStatement(false);
}
int executeStatement(boolean addBatch) {
boolean batch = isBatchThisRequest();
try {
int rows;
BatchControl control = transaction.getBatchControl();
if (control != null) {
rows = control.executeStatementOrBatch(this, batch, addBatch);
} else if (batch) {
control = persistExecute.createBatchControl(transaction);
rows = control.executeStatementOrBatch(this, true, addBatch);
} else {
rows = executeNow();
}
return rows;
} catch (BatchedSqlException e) {
throw transaction.translate(e.getMessage(), e.getCause());
}
}
public void initTransIfRequired() {
createImplicitTransIfRequired();
persistCascade = transaction.isPersistCascade();
}
public Type getType() {
return type;
}
public boolean isPersistCascade() {
return persistCascade;
}
}