package io.ebeaninternal.server.core;
import io.ebean.CallableSql;
import io.ebeaninternal.api.BindParams;
import io.ebeaninternal.api.BindParams.Param;
import io.ebeaninternal.api.SpiCallableSql;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.api.TransactionEventTable;
import io.ebeaninternal.server.persist.PersistExecute;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.List;
public final class PersistRequestCallableSql extends PersistRequest {
private final SpiCallableSql callableSql;
private int rowCount;
private String bindLog;
private CallableStatement cstmt;
private BindParams bindParam;
public PersistRequestCallableSql(SpiEbeanServer server, CallableSql cs, SpiTransaction t, PersistExecute persistExecute) {
super(server, t, persistExecute, cs.getLabel());
this.type = PersistRequest.Type.CALLABLESQL;
this.callableSql = (SpiCallableSql) cs;
}
@Override
public void profile(long offset, int flushCount) {
profileBase(EVT_CALLABLESQL, offset, (short)0, flushCount);
}
@Override
public int executeOrQueue() {
return executeStatement();
}
@Override
public int executeNow() {
return persistExecute.executeSqlCallable(this);
}
public SpiCallableSql getCallableSql() {
return callableSql;
}
public void setBindLog(String bindLog) {
this.bindLog = bindLog;
}
@Override
public void checkRowCount(int count) {
this.rowCount = count;
}
@Override
public void setGeneratedKey(Object idValue) {
}
@Override
public void postExecute() {
if (startNanos > 0) {
persistExecute.collectSqlCall(label, startNanos, rowCount);
}
if (transaction.isLogSummary()) {
String m = "CallableSql label[" + callableSql.getLabel() + "]" + " rows[" + rowCount + "]" + " bind[" + bindLog + "]";
transaction.logSummary(m);
}
TransactionEventTable tableEvents = callableSql.getTransactionEventTable();
if (tableEvents != null && !tableEvents.isEmpty()) {
transaction.getEvent().add(tableEvents);
} else {
transaction.markNotQueryOnly();
}
}
public void setBound(BindParams bindParam, CallableStatement cstmt) {
this.bindParam = bindParam;
this.cstmt = cstmt;
}
public int executeUpdate() throws SQLException {
if (callableSql.executeOverride(cstmt)) {
return -1;
}
rowCount = cstmt.executeUpdate();
readOutParams();
return rowCount;
}
private void readOutParams() throws SQLException {
List<Param> list = bindParam.positionedParameters();
int pos = 0;
for (Param param : list) {
pos++;
if (param.isOutParam()) {
Object outValue = cstmt.getObject(pos);
param.setOutValue(outValue);
}
}
}
}