001/*
002 * Copyright 2002-2015 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.datasource.init;
018
019import org.springframework.core.io.support.EncodedResource;
020
021/**
022 * Thrown by {@link ScriptUtils} if a statement in an SQL script failed when
023 * executing it against the target database.
024 *
025 * @author Juergen Hoeller
026 * @author Sam Brannen
027 * @since 3.0.5
028 */
029@SuppressWarnings("serial")
030public class ScriptStatementFailedException extends ScriptException {
031
032        /**
033         * Construct a new {@code ScriptStatementFailedException}.
034         * @param stmt the actual SQL statement that failed
035         * @param stmtNumber the statement number in the SQL script (i.e.,
036         * the n<sup>th</sup> statement present in the resource)
037         * @param encodedResource the resource from which the SQL statement was read
038         * @param cause the underlying cause of the failure
039         */
040        public ScriptStatementFailedException(String stmt, int stmtNumber, EncodedResource encodedResource, Throwable cause) {
041                super(buildErrorMessage(stmt, stmtNumber, encodedResource), cause);
042        }
043
044
045        /**
046         * Build an error message for an SQL script execution failure,
047         * based on the supplied arguments.
048         * @param stmt the actual SQL statement that failed
049         * @param stmtNumber the statement number in the SQL script (i.e.,
050         * the n<sup>th</sup> statement present in the resource)
051         * @param encodedResource the resource from which the SQL statement was read
052         * @return an error message suitable for an exception's <em>detail message</em>
053         * or logging
054         * @since 4.2
055         */
056        public static String buildErrorMessage(String stmt, int stmtNumber, EncodedResource encodedResource) {
057                return String.format("Failed to execute SQL script statement #%s of %s: %s", stmtNumber, encodedResource, stmt);
058        }
059
060}