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;

Wrapper object for generated keys as returned by the java.sql.Statement.getGeneratedKeys()

Parameters:
<Type> the key type returned
 
 public class GeneratedKeys<Type> implements ResultBearing<Type>
 {
     private final ResultSetMapper<Type>    mapper;
     private final SQLStatement<?>          jdbiStatement;
     private final Statement                stmt;
     private final ResultSet                results;
     private final StatementContext         context;
     private final ContainerFactoryRegistry containerFactoryRegistry;

    
Creates a new wrapper object for generated keys as returned by the java.sql.Statement.getGeneratedKeys() method for update and insert statement for drivers that support this function.

Parameters:
mapper Maps the generated keys result set to an object
jdbiStatement The original jDBI statement
stmt The corresponding sql statement
context The statement context
 
     GeneratedKeys(ResultSetMapper<Type> mapper,
                   SQLStatement<?> jdbiStatement,
                   Statement stmt,
                   StatementContext context,
                   ContainerFactoryRegistry containerFactoryRegistrythrows SQLException
     {
         this. = mapper;
         this. = jdbiStatement;
         this. = stmt;
         this. = stmt.getGeneratedKeys();
         this. = context;
         this. = containerFactoryRegistry.createChild();
         this..addCleanable(Cleanables.forResultSet());
     }

    
Returns the first generated key.

Returns:
The key or null if no keys were returned
 
     public Type first()
     {
         try {
             if (( != null) && .next()) {
                 return .map(0, );
             }
             else {
                 // no result matches
                 return null;
             }
         }
         catch (SQLException e) {
             throw new ResultSetException("Exception thrown while attempting to traverse the result set"e);
         }
         finally {
             .cleanup();
         }
     }
 
     public <T> T first(Class<T> containerType)
     {
 //        return containerFactoryRegistry.lookup(containerType).create(Arrays.asList(first()));
         throw new UnsupportedOperationException("Not Yet Implemented!");
     }
 
     public <ContainerType> ContainerType list(Class<ContainerType> containerType)
     {
 //        return containerFactoryRegistry.lookup(containerType).create(Arrays.asList(list()));
         throw new UnsupportedOperationException("Not Yet Implemented!");
    }
    public List<Type> list(int maxRows)
    {
        try {
            int idx = 0;
            List<Type> resultList = new ArrayList<Type>();
            if (( != null) && ++idx <= maxRows && !.isClosed()) {
                int index = 0;
                while (.next()) {
                    resultList.add(.map(index++, ));
                }
            }
            return resultList;
        }
        catch (SQLException e) {
            throw new ResultSetException("Exception thrown while attempting to traverse the result set"e);
        }
        finally {
            .cleanup();
        }
    }

    
Returns a list of all generated keys.

Returns:
The list of keys or an empty list if no keys were returned
    public List<Type> list()
    {
        return list(.);
    }

    
Returns a iterator over all generated keys.

Returns:
The key iterator
    public ResultIterator<Type> iterator()
    {
        try {
            return new ResultSetResultIterator<Type>();
        }
        catch (SQLException e) {
            throw new ResultSetException("Exception thrown while attempting to traverse the result set"e);
        }
    }

    
Used to execute the query and traverse the generated keys with a accumulator. Folding over the keys involves invoking a callback for each row, passing into the callback the return value from the previous function invocation.

Parameters:
accumulator The initial accumulator value
folder Defines the function which will fold over the result set.
Returns:
The return value from the last invocation of Folder.fold(java.lang.Object,java.sql.ResultSet)
See also:
Folder
    public <AccumulatorType> AccumulatorType fold(AccumulatorType accumulatorfinal Folder2<AccumulatorType> folder)
    {
        try {
            AccumulatorType value = accumulator;
            if (( != null) && !.isClosed()) {
                while (.next()) {
                    value = folder.fold(value);
                }
            }
            return value;
        }
        catch (SQLException e) {
            throw new ResultSetException("Exception thrown while attempting to traverse the result set"e);
        }
        finally {
            .cleanup();
        }
    }
New to GrepCode? Check out our FAQ X