Skip to main content
summaryrefslogtreecommitdiffstats
blob: c022c6463495472e0fee5ad205e785688d108308 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * 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 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
  {
    PreparedStatement delegate = getDelegate();
    delegate.addBatch();

    if (++batchCount >= batchSize)
    {
      return doExecuteBatch();
    }

    return 0;
  }

  @Override
  public void close() throws SQLException
  {
    if (batchCount != 0)
    {
      doExecuteBatch();
    }

    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() throws SQLException
  {
    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;
  }

  private static Connection getConnection(PreparedStatement delegate) throws DBException
  {
    try
    {
      return delegate.getConnection();
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
  }
}

Back to the top