Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004 - 2011 Brian McCallister
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.skife.jdbi.v2;
 
 
 import java.util.List;
 import java.util.Map;

Represents a group of non-prepared statements to be sent to the RDMBS in one "request"
 
 public class Batch extends BaseStatement
 {
     private List<Stringparts = new ArrayList<String>();
     private final StatementRewriter rewriter;
     private final Connection connection;
     private final SQLLog log;
     private final TimingCollector timingCollector;
 
     Batch(StatementRewriter rewriter,
           Connection connection,
           Map<StringObjectglobalStatementAttributes,
           SQLLog log,
           TimingCollector timingCollector,
           Foreman foreman)
     {
         super(new ConcreteStatementContext(globalStatementAttributes), foreman);
         this. = rewriter;
         this. = connection;
         this. = log;
         this. = timingCollector;
     }

    
Add a statement to the batch

Parameters:
sql SQL to be added to the batch, possibly a named statement
Returns:
the same Batch statement
 
     public Batch add(String sql)
     {
         .add(sql);
         return this;
     }

    
Specify a value on the statement context for this batch

Returns:
self
 
     public Batch define(String keyObject value) {
         getContext().setAttribute(keyvalue);
         return this;
     }

    
Execute all the queued up statements

Returns:
an array of integers representing the return values from each statement's execution
 
     public int[] execute()
     {
         // short circuit empty batch
         if (.size() == 0) return new int[] {};
 
         Binding empty = new Binding();
         Statement stmt = null;
         try
         {
             try
             {
                 stmt = .createStatement();
                 addCleanable(Cleanables.forStatement(stmt));
             }
             catch (SQLException e)
             {
                 throw new UnableToCreateStatementException(egetContext());
            }
            final SQLLog.BatchLogger logger = .logBatch();
            try
            {
                for (String part : )
                {
                    final String sql = .rewrite(partemptygetContext()).getSql();
                    logger.add(sql);
                    stmt.addBatch(sql);
                }
            }
            catch (SQLException e)
            {
                throw new UnableToExecuteStatementException("Unable to configure JDBC statement"egetContext());
            }
            try
            {
                final long start = System.nanoTime();
                final int[] rs = stmt.executeBatch();
                final long elapsedTime = (System.nanoTime() - start);
                logger.log(elapsedTime / 1000000L);
                // Null for statement, because for batches, we don't really have a good way to keep the sql around.
                .collect(elapsedTimegetContext());
                return rs;
            }
            catch (SQLException e)
            {
                throw new UnableToExecuteStatementException(egetContext());
            }
        }
        finally {
            cleanup();
        }
    }
New to GrepCode? Check out our FAQ X