001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.support;
018
019import java.util.List;
020import java.util.Map;
021
022import org.springframework.dao.InvalidDataAccessApiUsageException;
023import org.springframework.lang.Nullable;
024
025/**
026 * Interface for retrieving keys, typically used for auto-generated keys
027 * as potentially returned by JDBC insert statements.
028 *
029 * <p>Implementations of this interface can hold any number of keys.
030 * In the general case, the keys are returned as a List containing one Map
031 * for each row of keys.
032 *
033 * <p>Most applications only use one key per row and process only one row at a
034 * time in an insert statement. In these cases, just call {@code getKey}
035 * to retrieve the key. The returned value is a Number here, which is the
036 * usual type for auto-generated keys.
037 *
038 * @author Thomas Risberg
039 * @author Juergen Hoeller
040 * @since 1.1
041 * @see org.springframework.jdbc.core.JdbcTemplate
042 * @see org.springframework.jdbc.object.SqlUpdate
043 */
044public interface KeyHolder {
045
046        /**
047         * Retrieve the first item from the first map, assuming that there is just
048         * one item and just one map, and that the item is a number.
049         * This is the typical case: a single, numeric generated key.
050         * <p>Keys are held in a List of Maps, where each item in the list represents
051         * the keys for each row. If there are multiple columns, then the Map will have
052         * multiple entries as well. If this method encounters multiple entries in
053         * either the map or the list meaning that multiple keys were returned,
054         * then an InvalidDataAccessApiUsageException is thrown.
055         * @return the generated key as a number
056         * @throws InvalidDataAccessApiUsageException if multiple keys are encountered
057         */
058        @Nullable
059        Number getKey() throws InvalidDataAccessApiUsageException;
060
061        /**
062         * Retrieve the first map of keys.
063         * <p>If there are multiple entries in the list (meaning that multiple rows
064         * had keys returned), then an InvalidDataAccessApiUsageException is thrown.
065         * @return the Map of generated keys for a single row
066         * @throws InvalidDataAccessApiUsageException if keys for multiple rows are encountered
067         */
068        @Nullable
069        Map<String, Object> getKeys() throws InvalidDataAccessApiUsageException;
070
071        /**
072         * Return a reference to the List that contains the keys.
073         * <p>Can be used for extracting keys for multiple rows (an unusual case),
074         * and also for adding new maps of keys.
075         * @return the List for the generated keys, with each entry representing
076         * an individual row through a Map of column names and key values
077         */
078        List<Map<String, Object>> getKeyList();
079
080}