⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmssetupdb.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-setup/org/opencms/setup/CmsSetupDb.java,v $
 * Date   : $Date: 2007-08-22 11:11:37 $
 * Version: $Revision: 1.1 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.setup;

import org.opencms.main.CmsException;
import org.opencms.util.CmsDataTypeUtil;
import org.opencms.util.CmsStringUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Helper class to call database setup scripts.<p>
 * 
 * @author Thomas Weckert  
 * @author Carsten Weinholz 
 * 
 * @version $Revision: 1.1 $ 
 * 
 * @since 6.0.0 
 */
public class CmsSetupDb extends Object {

    /** The folder where to read the setup data from. */
    public static final String SETUP_DATA_FOLDER = "WEB-INF/setupdata/";

    /** The folder where the setup wizard is located. */
    public static final String SETUP_FOLDER = "setup/";

    private String m_basePath;
    private Connection m_con;
    private boolean m_errorLogging;
    private Vector m_errors;

    /**
     * Creates a new CmsSetupDb object.<p>
     * 
     * @param basePath the location of the setup scripts
     */
    public CmsSetupDb(String basePath) {

        m_errors = new Vector();
        m_basePath = basePath;
        m_errorLogging = true;
    }

    /**
     * Returns an optional warning message if needed, <code>null</code> if not.<p> 
     * 
     * @param db the selected database key
     * 
     * @return html warning, or <code>null</code> if no warning
     */
    public String checkVariables(String db) {

        StringBuffer html = new StringBuffer(512);
        if (m_con == null) {
            return null; // prior error, trying to get a connection
        }
        SQLException exception = null;
        if (db.equals("mysql")) { // just for 4.0, > is not needed, < is not supported.
            String statement = "SELECT @@max_allowed_packet;";
            Statement stmt = null;
            ResultSet rs = null;
            long map = 0;
            try {
                stmt = m_con.createStatement();
                rs = stmt.executeQuery(statement);
                if (rs.next()) {
                    map = rs.getLong(1);
                }
            } catch (SQLException e) {
                exception = e;
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        // ignore
                    }
                }
            }
            if (exception == null) {
                if (map > 0) {
                    html.append("MySQL system variable <code>'max_allowed_packet'</code> is set to ");
                    html.append(map);
                    html.append(" Bytes.<p>\n");
                }
                html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n");
                if (map < 15 * 1024 * 1024) {
                    m_errors.addElement("<b>Your <code>'max_allowed_packet'</code> variable is set to less than 16Mb ("
                        + map
                        + ").</b>\n"
                        + "The recommended value for running OpenCms is 16Mb."
                        + "Please change your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).\n");
                }
            }
        }
        if ((exception != null) || db.equals("mysql_3")) {
            html.append("<i>OpenCms was not able to detect the value of your <code>'max_allowed_packet'</code> variable.</i><p>\n");
            html.append("Please, note that it will not be possible for OpenCms to handle files bigger than this value.<p>\n");
            html.append("<b>The recommended value for running OpenCms is 16Mb, please set it in your MySQL configuration (in your <code>mi.ini</code> or <code>my.cnf</code> file).</b>\n");
            if (exception != null) {
                html.append(CmsException.getStackTraceAsString(exception));
            }
        }
        if (html.length() == 0) {
            return null;
        }
        return html.toString();
    }

    /**
     * Clears the error messages stored internally.<p>
     */
    public void clearErrors() {

        m_errors.clear();
    }

    /**
     * Closes the internal connection to the database.<p>
     */
    public void closeConnection() {

        try {
            if (m_con != null) {
                m_con.close();
            }
        } catch (Exception e) {
            // ignore
        }
        m_con = null;
    }

    /**
     * Calls the create database script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     */
    public void createDatabase(String database, Map replacer) {

        m_errorLogging = true;
        executeSql(database, "create_db.sql", replacer, true);
    }

    /**
     * Calls the create database script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     * @param abortOnError indicates if the script is aborted if an error occurs
     */
    public void createDatabase(String database, Map replacer, boolean abortOnError) {

        m_errorLogging = true;
        executeSql(database, "create_db.sql", replacer, abortOnError);
    }

    /**
     * Calls the create tables script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     */
    public void createTables(String database, Map replacer) {

        m_errorLogging = true;
        executeSql(database, "create_tables.sql", replacer, true);
    }

    /**
     * Calls the create tables script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     * @param abortOnError indicates if the script is aborted if an error occurs
     */
    public void createTables(String database, Map replacer, boolean abortOnError) {

        m_errorLogging = true;
        executeSql(database, "create_tables.sql", replacer, abortOnError);
    }

    /**
     * Calls the drop script for the given database.
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     */
    public void dropDatabase(String database, Map replacer) {

        m_errorLogging = true;
        executeSql(database, "drop_db.sql", replacer, false);
    }

    /**
     * Calls the drop script for the given database.
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     * @param abortOnError indicates if the script is aborted if an error occurs
     */
    public void dropDatabase(String database, Map replacer, boolean abortOnError) {

        m_errorLogging = true;
        executeSql(database, "drop_db.sql", replacer, abortOnError);
    }

    /**
     * Calls the drop tables script for the given database.<p>
     * 
     * @param database the name of the database
     */
    public void dropTables(String database) {

        m_errorLogging = true;
        executeSql(database, "drop_tables.sql", null, false);
    }

    /**
     * Calls the drop tables script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     */
    public void dropTables(String database, Map replacer) {

        m_errorLogging = true;
        executeSql(database, "drop_tables.sql", replacer, false);
    }

    /**
     * Calls the drop tables script for the given database.<p>
     * 
     * @param database the name of the database
     * @param replacer the replacements to perform in the drop script
     * @param abortOnError indicates if the script is aborted if an error occurs
     */
    public void dropTables(String database, Map replacer, boolean abortOnError) {

        m_errorLogging = true;
        executeSql(database, "drop_tables.sql", replacer, abortOnError);
    }

    /**
     * Creates and executes a database statment from a String returning the result set.<p>
     * 
     * @param query the query to execute
     * @param replacer the replacements to perform in the script
     * 
     * @return the result set of the query 
     * 
     * @throws SQLException if something goes wrong
     */
    public CmsSetupDBWrapper executeSqlStatement(String query, Map replacer) throws SQLException {

        CmsSetupDBWrapper dbwrapper = new CmsSetupDBWrapper(m_con);
        dbwrapper.createStatement();

        String queryToExecute = query;

        // Check if a map of replacements is given
        if (replacer != null) {
            queryToExecute = replaceTokens(query, replacer);
        }
        // do the query
        dbwrapper.excecuteQuery(queryToExecute);

        // return the result
        return dbwrapper;

    }

    /** Creates and executes a database statment from a String returning the result set.<p>
     * 
     * @param query the query to execute
     * @param replacer the replacements to perform in the script
     * @param params the list of parameters for the statement
     * 
     * @return the result set of the query 
     * 
     * @throws SQLException if something goes wrong
     */
    public CmsSetupDBWrapper executeSqlStatement(String query, Map replacer, List params) throws SQLException {

        CmsSetupDBWrapper dbwrapper = new CmsSetupDBWrapper(m_con);

        String queryToExecute = query;

        // Check if a map of replacements is given
        if (replacer != null) {
            queryToExecute = replaceTokens(query, replacer);
        }

        dbwrapper.createPreparedStatement(queryToExecute, params);

        dbwrapper.excecutePreparedQuery();

        return dbwrapper;
    }

    /**
     * Returns the connection.<p>
     *
     * @return the connection
     */
    public Connection getConnection() {

        return m_con;
    }

    /**
     * Returns a Vector of Error messages.<p>
     * 
     * @return all error messages collected internally
     */
    public Vector getErrors() {

        return m_errors;
    }

    /**
     * Checks if the given table, column or combination of both is available in the database in case insensitive way.<P>
     * 
     * @param table the sought table
     * @param column the sought column
     * 
     * @return true if the requested table/column is available, false if not
     */
    public boolean hasTableOrColumn(String table, String column) {

        String tableName, columnName;
        boolean result;

        tableName = table == null ? null : table.toUpperCase();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -