001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.oracle;
029
030import org.opencms.db.CmsDbContext;
031import org.opencms.db.generic.Messages;
032import org.opencms.main.CmsLog;
033
034import java.sql.Blob;
035import java.sql.Connection;
036import java.sql.PreparedStatement;
037import java.sql.ResultSet;
038import java.sql.SQLException;
039import java.util.Collections;
040import java.util.List;
041
042import org.apache.commons.logging.Log;
043
044/**
045 * Oracle implementation of the SQL manager.<p>
046 *
047 * @since 6.0.0
048 */
049public class CmsSqlManager extends org.opencms.db.generic.CmsSqlManager {
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(org.opencms.db.oracle.CmsSqlManager.class);
053
054    /** The filename/path of the SQL query properties. */
055    private static final String QUERY_PROPERTIES = "org/opencms/db/oracle/query.properties";
056
057    /**
058     * @see org.opencms.db.generic.CmsSqlManager#CmsSqlManager()
059     */
060    public CmsSqlManager() {
061
062        this(Collections.<String> emptyList());
063    }
064
065    /**
066     * @see org.opencms.db.generic.CmsSqlManager#CmsSqlManager(java.util.List)
067     */
068    public CmsSqlManager(List<String> additionalQueryProperties) {
069
070        super(additionalQueryProperties);
071        loadQueryProperties(QUERY_PROPERTIES);
072    }
073
074    /**
075     * Attempts to close the connection, statement and result set after a statement has been executed.<p>
076     *
077     * @param sqlManager the sql manager to use
078     * @param dbc the current database context
079     * @param con the JDBC connection
080     * @param stmnt the statement
081     * @param res the result set
082     * @param commit the additional statement for the 'commit' command
083     * @param wasInTransaction if using transactions
084     */
085    public static synchronized void closeAllInTransaction(
086        org.opencms.db.generic.CmsSqlManager sqlManager,
087        CmsDbContext dbc,
088        Connection con,
089        PreparedStatement stmnt,
090        ResultSet res,
091        PreparedStatement commit,
092        boolean wasInTransaction) {
093
094        if (dbc == null) {
095            LOG.error(Messages.get().getBundle().key(Messages.LOG_NULL_DB_CONTEXT_0));
096        }
097
098        if (res != null) {
099            try {
100                res.close();
101            } catch (SQLException exc) {
102                // ignore
103                if (LOG.isDebugEnabled()) {
104                    LOG.debug(exc.getLocalizedMessage(), exc);
105                }
106            }
107        }
108        if (commit != null) {
109            try {
110                commit.close();
111            } catch (SQLException exc) {
112                // ignore
113                if (LOG.isDebugEnabled()) {
114                    LOG.debug(exc.getLocalizedMessage(), exc);
115                }
116            }
117        }
118        if (!wasInTransaction) {
119            if (stmnt != null) {
120                try {
121                    PreparedStatement rollback = sqlManager.getPreparedStatement(con, "C_ROLLBACK");
122                    rollback.execute();
123                    rollback.close();
124                } catch (SQLException se) {
125                    // ignore
126                    if (LOG.isDebugEnabled()) {
127                        LOG.debug(se.getLocalizedMessage(), se);
128                    }
129                }
130                try {
131                    stmnt.close();
132                } catch (SQLException exc) {
133                    // ignore
134                    if (LOG.isDebugEnabled()) {
135                        LOG.debug(exc.getLocalizedMessage(), exc);
136                    }
137                }
138            }
139            if (con != null) {
140                try {
141                    con.setAutoCommit(true);
142                    con.close();
143                } catch (SQLException se) {
144                    // ignore
145                    if (LOG.isDebugEnabled()) {
146                        LOG.debug(se.getLocalizedMessage(), se);
147                    }
148                }
149            }
150        }
151    }
152
153    /**
154     * @see org.opencms.db.generic.CmsSqlManager#getBytes(java.sql.ResultSet, java.lang.String)
155     */
156    @Override
157    public byte[] getBytes(ResultSet res, String attributeName) throws SQLException {
158
159        Blob blob = res.getBlob(attributeName);
160        return blob.getBytes(1, (int)blob.length());
161    }
162}