All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.skife.jdbi.v2.Update Maven / Gradle / Ivy

Go to download

jDBI is designed to provide convenient tabular data access in Java(tm). It uses the Java collections framework for query results, provides a convenient means of externalizing sql statements, and provides named parameter support for any database being used.

There is a newer version: 2.78
Show newest version
/*
 * 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 org.skife.jdbi.v2.tweak.ResultColumnMapper;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.skife.jdbi.v2.tweak.SQLLog;
import org.skife.jdbi.v2.tweak.StatementBuilder;
import org.skife.jdbi.v2.tweak.StatementCustomizer;
import org.skife.jdbi.v2.tweak.StatementLocator;
import org.skife.jdbi.v2.tweak.StatementRewriter;
import org.skife.jdbi.v2.util.SingleColumnMapper;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Map;

/**
 * Used for INSERT, UPDATE, and DELETE statements
 */
public class Update extends SQLStatement
{
    Update(Handle handle,
           StatementLocator locator,
           StatementRewriter statementRewriter,
           StatementBuilder statementBuilder,
           String sql,
           ConcreteStatementContext ctx,
           SQLLog log,
           TimingCollector timingCollector,
           Foreman foreman,
           ContainerFactoryRegistry containerFactoryRegistry)
    {
        super(new Binding(), locator, statementRewriter, handle, statementBuilder, sql, ctx, log, timingCollector, Collections.emptyList(), foreman, containerFactoryRegistry);
    }

    /**
     * Execute the statement
     * @return the number of rows modified
     */
    public int execute()
    {
        try {
            return this.internalExecute(new QueryResultMunger() {
                @Override
                public Integer munge(Statement results) throws SQLException
                {
                    return results.getUpdateCount();
                }
            });
        }
        finally {
            cleanup();
        }
    }

    /**
     * Execute the statement and returns any auto-generated keys. This requires the JDBC driver to support
     * the {@link Statement#getGeneratedKeys()} method.
     * @param mapper the mapper to generate the resulting key object
     * @param columnName name of the column that generates the key
     * @return the generated key or null if none was returned
     */
    public  GeneratedKeys executeAndReturnGeneratedKeys(final ResultSetMapper mapper, String columnName)
    {
        getConcreteContext().setReturningGeneratedKeys(true);
        if (columnName != null && !columnName.isEmpty()) {
            getConcreteContext().setGeneratedKeysColumnNames(new String[] { columnName } );
        }
        return this.internalExecute(new QueryResultMunger>() {
            @Override
            public GeneratedKeys munge(Statement results) throws SQLException
            {
                return new GeneratedKeys(mapper,
                                                           Update.this,
                                                           results,
                                                           getContext(),
                                                           getContainerMapperRegistry());
            }
        });
    }

    public  GeneratedKeys executeAndReturnGeneratedKeys(final ResultSetMapper mapper) {
        return executeAndReturnGeneratedKeys(mapper, null);
    }

    public  GeneratedKeys executeAndReturnGeneratedKeys(final ResultColumnMapper mapper) {
        return executeAndReturnGeneratedKeys(new SingleColumnMapper(mapper), null);
    }

    public GeneratedKeys> executeAndReturnGeneratedKeys()
    {
        return executeAndReturnGeneratedKeys(new DefaultMapper());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy