blob: 375a2cf3e5686fb2b71f3a543773bf76669e073b [file] [log] [blame]
/*
* Copyright (c) 2016 Eike Stepper (Berlin, Germany) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
*/
package org.eclipse.net4j.internal.db;
import org.eclipse.net4j.db.BatchedStatement;
import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.jdbc.DelegatingPreparedStatement;
import org.eclipse.net4j.util.om.monitor.OMMonitor;
import org.eclipse.net4j.util.om.monitor.OMMonitor.Async;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author Eike Stepper
* @since 4.5
*/
public final class BatchedStatementImpl extends DelegatingPreparedStatement implements BatchedStatement
{
private final int batchSize;
private int batchCount;
private int totalResult;
public BatchedStatementImpl(PreparedStatement delegate, int batchSize) throws DBException
{
super(delegate, getConnection(delegate));
this.batchSize = batchSize;
}
public int getBatchSize()
{
return batchSize;
}
public int getBatchCount()
{
return batchCount;
}
public int getTotalResult()
{
return totalResult;
}
@Override
public int executeUpdate() throws SQLException
{
return executeUpdate((OMMonitor)null);
}
public int executeUpdate(OMMonitor monitor) throws SQLException
{
PreparedStatement delegate = getDelegate();
delegate.addBatch();
if (++batchCount >= batchSize)
{
return doExecuteBatch(monitor);
}
return 0;
}
@Override
public void close() throws SQLException
{
close(null);
}
public void close(OMMonitor monitor) throws SQLException
{
if (batchCount != 0)
{
doExecuteBatch(monitor);
}
super.close();
}
@Override
public ResultSet getResultSet() throws SQLException
{
throw new UnsupportedOperationException("Only updates are supported");
}
@Override
public ResultSet executeQuery() throws SQLException
{
throw new UnsupportedOperationException("Only updates are supported");
}
@Deprecated
@Override
public ResultSet executeQuery(String sql) throws SQLException
{
throw new UnsupportedOperationException("Only updates are supported");
}
private int doExecuteBatch(OMMonitor monitor) throws SQLException
{
Async async = monitor != null ? monitor.forkAsync() : null;
try
{
int sum = 0;
int[] results = getDelegate().executeBatch();
for (int i = 0; i < results.length; i++)
{
int result = results[i];
if (result != Statement.SUCCESS_NO_INFO)
{
if (result < 0)
{
throw new DBException("Result " + i + " is not successful: " + result);
}
sum += result;
}
}
totalResult += sum;
return sum;
}
finally
{
if (async != null)
{
async.stop();
}
}
}
private static Connection getConnection(PreparedStatement delegate) throws DBException
{
try
{
return delegate.getConnection();
}
catch (SQLException ex)
{
throw new DBException(ex);
}
}
}