001/*
002 * Copyright 2002-2012 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;
018
019import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
020
021/**
022 * Exception thrown when a JDBC update affects an unexpected number of rows.
023 * Typically we expect an update to affect a single row, meaning it's an
024 * error if it affects multiple rows.
025 *
026 * @author Rod Johnson
027 * @author Juergen Hoeller
028 */
029@SuppressWarnings("serial")
030public class JdbcUpdateAffectedIncorrectNumberOfRowsException extends IncorrectUpdateSemanticsDataAccessException {
031
032        /** Number of rows that should have been affected */
033        private int expected;
034
035        /** Number of rows that actually were affected */
036        private int actual;
037
038
039        /**
040         * Constructor for JdbcUpdateAffectedIncorrectNumberOfRowsException.
041         * @param sql SQL we were tring to execute
042         * @param expected the expected number of rows affected
043         * @param actual the actual number of rows affected
044         */
045        public JdbcUpdateAffectedIncorrectNumberOfRowsException(String sql, int expected, int actual) {
046                super("SQL update '" + sql + "' affected " + actual + " rows, not " + expected + " as expected");
047                this.expected = expected;
048                this.actual = actual;
049        }
050
051
052        /**
053         * Return the number of rows that should have been affected.
054         */
055        public int getExpectedRowsAffected() {
056                return this.expected;
057        }
058
059        /**
060         * Return the number of rows that have actually been affected.
061         */
062        public int getActualRowsAffected() {
063                return this.actual;
064        }
065
066        @Override
067        public boolean wasDataUpdated() {
068                return (getActualRowsAffected() > 0);
069        }
070
071}