001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.db;
029
030import org.opencms.i18n.CmsMessageContainer;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033
034import java.sql.Statement;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Used to signal sql related issues.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsDbSqlException extends CmsDbException {
044
045    /** The log object for this class. */
046    private static final Log LOG = CmsLog.getLog(CmsDriverManager.class);
047
048    /** Serial version UID required for safe serialization. */
049    private static final long serialVersionUID = -286617872967617367L;
050
051    /**
052     * Creates a new localized Exception.<p>
053     *
054     * @param container the localized message container to use
055     */
056    public CmsDbSqlException(CmsMessageContainer container) {
057
058        super(container);
059        // log all sql exceptions
060        if (LOG.isErrorEnabled()) {
061            LOG.error(container.key(), this);
062        }
063    }
064
065    /**
066     * Creates a new localized Exception that also containes a root cause.<p>
067     *
068     * @param container the localized message container to use
069     * @param cause the Exception root cause
070     */
071    public CmsDbSqlException(CmsMessageContainer container, Throwable cause) {
072
073        super(container, cause);
074        // log all sql exceptions
075        if (LOG.isWarnEnabled()) {
076            LOG.warn(container.key(), this);
077        }
078    }
079
080    /**
081     * Returns the query that let the statement crash.<p>
082     *
083     * @param stmt the Statement to get the crashed query from
084     * @return the crashed query
085     */
086    public static String getErrorQuery(Statement stmt) {
087
088        if (stmt != null) {
089            // the query that crashed
090            return stmt.toString();
091        }
092        return "";
093    }
094
095    /**
096     * @see org.opencms.main.CmsException#createException(org.opencms.i18n.CmsMessageContainer, java.lang.Throwable)
097     */
098    @Override
099    public CmsException createException(CmsMessageContainer container, Throwable cause) {
100
101        return new CmsDbSqlException(container, cause);
102    }
103}