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.setup;
029
030import org.opencms.util.CmsDataTypeUtil;
031
032import java.io.IOException;
033import java.sql.Connection;
034import java.sql.PreparedStatement;
035import java.sql.ResultSet;
036import java.sql.SQLException;
037import java.sql.Statement;
038import java.util.List;
039
040/**
041 * Wrapper to encapsulate, connection, statement and result set for the setup
042 * and update wizard.<p>
043 */
044public class CmsSetupDBWrapper {
045
046    /** the statement to use in the db wrapper. */
047    private Statement m_statement;
048
049    /** the connection to use in the db wrapper. */
050    private Connection m_connection;
051
052    /** the result set returned by the db wrapper. */
053    private ResultSet m_resultset;
054
055    /** the prepared statement. */
056    private PreparedStatement m_preparedStatement;
057
058    /**
059     * Constructor, creates a new CmsSetupDBWrapper.<p>
060     * @param con the connection to use in this db wrapper.
061     */
062    public CmsSetupDBWrapper(Connection con) {
063
064        m_connection = con;
065    }
066
067    /**
068     * Closes result set, and statement. <p>
069     */
070    public void close() {
071
072        // result set
073        if (m_resultset != null) {
074            try {
075                m_resultset.close();
076            } catch (Exception e) {
077                e.printStackTrace();
078
079            }
080        }
081        // statement
082        if (m_statement != null) {
083            try {
084                m_statement.close();
085            } catch (Exception e) {
086                e.printStackTrace();
087
088            }
089        }
090        // prepared statement
091        if (m_preparedStatement != null) {
092            try {
093                m_preparedStatement.close();
094            } catch (Exception e) {
095                e.printStackTrace();
096
097            }
098        }
099
100    }
101
102    /**
103     * Creates a new SQL Statement on the connection of this DB wrapper.<p>
104     * @throws SQLException if statement cannot be created
105     */
106    public void createStatement() throws SQLException {
107
108        m_statement = m_connection.createStatement();
109    }
110
111    /**
112     * Creates a new SQL Statement on the connection of this DB wrapper.<p>
113     *
114     * @param query the DB query to use
115     * @param params List of additional parameters
116     *
117     * @throws SQLException if statement cannot be created
118     */
119    public void createPreparedStatement(String query, List<Object> params) throws SQLException {
120
121        m_preparedStatement = m_connection.prepareStatement(query);
122
123        // Check the params
124        if (params != null) {
125            for (int i = 0; i < params.size(); i++) {
126                Object item = params.get(i);
127
128                // Check if the parameter is a string
129                if (item instanceof String) {
130                    m_preparedStatement.setString(i + 1, (String)item);
131                }
132                if (item instanceof Integer) {
133                    Integer number = (Integer)item;
134                    m_preparedStatement.setInt(i + 1, number.intValue());
135                }
136                if (item instanceof Long) {
137                    Long longNumber = (Long)item;
138                    m_preparedStatement.setLong(i + 1, longNumber.longValue());
139                }
140
141                // If item is none of types above set the statement to use the bytes
142                if (!(item instanceof Integer) && !(item instanceof String) && !(item instanceof Long)) {
143                    try {
144                        m_preparedStatement.setBytes(i + 1, CmsDataTypeUtil.dataSerialize(item));
145                    } catch (IOException e) {
146                        e.printStackTrace();
147                    }
148                }
149            }
150        }
151
152    }
153
154    /**
155     * Executes a query on the connection and statement of this db wrapper.<p>
156     *
157     * @param query the query to execute
158     *
159     * @throws SQLException if statement cannot be created
160     */
161    public void excecuteQuery(String query) throws SQLException {
162
163        m_resultset = m_statement.executeQuery(query);
164    }
165
166    /**
167     * Executes a query on the connection and prepared statement of this db wrapper.<p>
168     * @throws SQLException if statement cannot be created
169     */
170    public void excecutePreparedQuery() throws SQLException {
171
172        m_resultset = m_preparedStatement.executeQuery();
173    }
174
175    /**
176     * Returns the res.<p>
177     *
178     * @return the res
179     */
180    public ResultSet getResultSet() {
181
182        return m_resultset;
183    }
184}