001/*
002 * Copyright 2002-2018 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.Iterator;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Map;
023
024import org.springframework.dao.DataRetrievalFailureException;
025import org.springframework.dao.InvalidDataAccessApiUsageException;
026import org.springframework.lang.Nullable;
027
028/**
029 * The standard implementation of the {@link KeyHolder} interface, to be used for
030 * holding auto-generated keys (as potentially returned by JDBC insert statements).
031 *
032 * <p>Create an instance of this class for each insert operation, and pass it
033 * to the corresponding {@link org.springframework.jdbc.core.JdbcTemplate} or
034 * {@link org.springframework.jdbc.object.SqlUpdate} methods.
035 *
036 * @author Thomas Risberg
037 * @author Juergen Hoeller
038 * @since 1.1
039 */
040public class GeneratedKeyHolder implements KeyHolder {
041
042        private final List<Map<String, Object>> keyList;
043
044
045        /**
046         * Create a new GeneratedKeyHolder with a default list.
047         */
048        public GeneratedKeyHolder() {
049                this.keyList = new LinkedList<>();
050        }
051
052        /**
053         * Create a new GeneratedKeyHolder with a given list.
054         * @param keyList a list to hold maps of keys
055         */
056        public GeneratedKeyHolder(List<Map<String, Object>> keyList) {
057                this.keyList = keyList;
058        }
059
060
061        @Override
062        @Nullable
063        public Number getKey() throws InvalidDataAccessApiUsageException, DataRetrievalFailureException {
064                if (this.keyList.isEmpty()) {
065                        return null;
066                }
067                if (this.keyList.size() > 1 || this.keyList.get(0).size() > 1) {
068                        throw new InvalidDataAccessApiUsageException(
069                                        "The getKey method should only be used when a single key is returned.  " +
070                                        "The current key entry contains multiple keys: " + this.keyList);
071                }
072                Iterator<Object> keyIter = this.keyList.get(0).values().iterator();
073                if (keyIter.hasNext()) {
074                        Object key = keyIter.next();
075                        if (!(key instanceof Number)) {
076                                throw new DataRetrievalFailureException(
077                                                "The generated key is not of a supported numeric type. " +
078                                                "Unable to cast [" + (key != null ? key.getClass().getName() : null) +
079                                                "] to [" + Number.class.getName() + "]");
080                        }
081                        return (Number) key;
082                }
083                else {
084                        throw new DataRetrievalFailureException("Unable to retrieve the generated key. " +
085                                        "Check that the table has an identity column enabled.");
086                }
087        }
088
089        @Override
090        @Nullable
091        public Map<String, Object> getKeys() throws InvalidDataAccessApiUsageException {
092                if (this.keyList.isEmpty()) {
093                        return null;
094                }
095                if (this.keyList.size() > 1) {
096                        throw new InvalidDataAccessApiUsageException(
097                                        "The getKeys method should only be used when keys for a single row are returned.  " +
098                                        "The current key list contains keys for multiple rows: " + this.keyList);
099                }
100                return this.keyList.get(0);
101        }
102
103        @Override
104        public List<Map<String, Object>> getKeyList() {
105                return this.keyList;
106        }
107
108}