package io.ebeaninternal.server.query;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiProfileTransactionEvent;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class CQueryUpdate implements SpiProfileTransactionEvent {
private final CQueryPlan queryPlan;
private final OrmQueryRequest<?> request;
private final BeanDescriptor<?> desc;
private final SpiQuery<?> query;
private final CQueryPredicates predicates;
private final String sql;
private final String type;
private PreparedStatement pstmt;
private String bindLog;
private long executionTimeMicros;
private int rowCount;
private long profileOffset;
CQueryUpdate(String type, OrmQueryRequest<?> request, CQueryPredicates predicates, CQueryPlan queryPlan) {
this.type = type;
this.request = request;
this.queryPlan = queryPlan;
this.query = request.getQuery();
this.sql = queryPlan.getSql();
this.desc = request.getBeanDescriptor();
this.predicates = predicates;
query.setGeneratedSql(sql);
}
public String getSummary() {
StringBuilder sb = new StringBuilder(80);
sb.append(type).append(" exeMicros[").append(executionTimeMicros)
.append("] rows[").append(rowCount)
.append("] type[").append(desc.getName())
.append("] predicates[").append(predicates.getLogWhereSql())
.append("] bind[").append(bindLog).append("]");
return sb.toString();
}
public String getBindLog() {
return bindLog;
}
public String getGeneratedSql() {
return sql;
}
public int execute() throws SQLException {
long startNano = System.nanoTime();
try {
SpiTransaction t = getTransaction();
profileOffset = t.profileOffset();
Connection conn = t.getInternalConnection();
pstmt = conn.prepareStatement(sql);
if (query.getTimeout() > 0) {
pstmt.setQueryTimeout(query.getTimeout());
}
bindLog = predicates.bind(pstmt, conn);
rowCount = pstmt.executeUpdate();
executionTimeMicros = (System.nanoTime() - startNano) / 1000L;
request.slowQueryCheck(executionTimeMicros, rowCount);
if (queryPlan.executionTime(rowCount, executionTimeMicros, null)) {
queryPlan.captureBindForQueryPlan(predicates, executionTimeMicros);
}
getTransaction().profileEvent(this);
return rowCount;
} finally {
close();
}
}
private SpiTransaction getTransaction() {
return request.getTransaction();
}
private void close() {
JdbcClose.close(pstmt);
pstmt = null;
}
@Override
public void profile() {
getTransaction()
.profileStream()
.addQueryEvent(query.profileEventId(), profileOffset, desc.getProfileId(), rowCount, query.getProfileId());
}
}