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