📄 cmssqlmanager.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/generic/CmsSqlManager.java,v $
* Date : $Date: 2006/03/27 14:52:54 $
* Version: $Revision: 1.65 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 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.db.generic;
import org.opencms.db.CmsDbContext;
import org.opencms.db.CmsDbPool;
import org.opencms.file.CmsDataAccessException;
import org.opencms.file.CmsProject;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsStringUtil;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
/**
* Generic (ANSI-SQL) implementation of the SQL manager.<p>
*
* @author Thomas Weckert
*
* @version $Revision: 1.65 $
*
* @since 6.0.0
*/
public class CmsSqlManager extends org.opencms.db.CmsSqlManager implements Serializable, Cloneable {
/** A pattern being replaced in SQL queries to generate SQL queries to access online/offline tables. */
protected static final String QUERY_PROJECT_SEARCH_PATTERN = "_${PROJECT}_";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsSqlManager.class);
/** The filename/path of the SQL query properties. */
private static final String QUERY_PROPERTIES = "org/opencms/db/generic/query.properties";
/** Serial version UID required for safe serialization. */
private static final long serialVersionUID = -5994026786008303964L;
/** A map to cache queries with replaced search patterns. */
protected Map m_cachedQueries;
/** The type ID of the driver (vfs, user, project, workflow or backup) from where this SQL manager is referenced. */
protected int m_driverType;
/** The pool URL to get connections from the JDBC driver manager, including DBCP's pool URL prefix. */
protected String m_poolUrl;
/** A map holding all SQL queries. */
protected Map m_queries;
/**
* Creates a new, empty SQL manager.<p>
*/
public CmsSqlManager() {
m_cachedQueries = new HashMap();
m_queries = new HashMap();
loadQueryProperties(QUERY_PROPERTIES);
}
/**
* Creates a new instance of a SQL manager.<p>
*
* @param classname the classname of the SQL manager
*
* @return a new instance of the SQL manager
*/
public static org.opencms.db.generic.CmsSqlManager getInstance(String classname) {
org.opencms.db.generic.CmsSqlManager sqlManager;
try {
Object objectInstance = Class.forName(classname).newInstance();
sqlManager = (org.opencms.db.generic.CmsSqlManager)objectInstance;
} catch (Throwable t) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_SQL_MANAGER_INIT_FAILED_1, classname), t);
sqlManager = null;
}
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_DRIVER_SQL_MANAGER_1, classname));
}
return sqlManager;
}
/**
* Replaces the project search pattern in SQL queries by the pattern _ONLINE_ or _OFFLINE_ depending on the
* specified project ID.<p>
*
* @param projectId the ID of the current project
* @param query the SQL query
* @return String the SQL query with the table key search pattern replaced
*/
protected static String replaceProjectPattern(int projectId, String query) {
// make the statement project dependent
String replacePattern = (projectId == CmsProject.ONLINE_PROJECT_ID || projectId < 0) ? "_ONLINE_" : "_OFFLINE_";
query = CmsStringUtil.substitute(query, QUERY_PROJECT_SEARCH_PATTERN, replacePattern);
return query;
}
/**
* Attemts to close the connection, statement and result set after a statement has been executed.<p>
*
* @param dbc the current database context
* @param con the JDBC connection
* @param stmnt the statement
* @param res the result set
*/
public void closeAll(CmsDbContext dbc, Connection con, Statement stmnt, ResultSet res) {
// NOTE: we have to close Connections/Statements that way, because a dbcp PoolablePreparedStatement
// is not a DelegatedStatement; for that reason its not removed from the trace of the connection when it is closed.
// So, the connection tries to close it again when the connection is closed itself;
// as a result there is an error that forces the connection to be destroyed and not pooled
if (dbc == null) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_NULL_DB_CONTEXT_0));
}
try {
// first, close the connection and (eventually) implicitly all assigned statements and result sets
if (con != null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
// intentionally left blank
} finally {
con = null;
}
try {
// close the statement and (normally) implicitly all assigned result sets
if (stmnt != null) {
stmnt.close();
}
} catch (SQLException e) {
// intentionally left blank
} finally {
stmnt = null;
}
try {
// close the result set
if (res != null) {
res.close();
}
} catch (SQLException e) {
// intentionally left blank
} finally {
res = null;
}
}
/**
* Retrieves the value of the designated column in the current row of this ResultSet object as
* a byte array in the Java programming language.<p>
*
* The bytes represent the raw values returned by the driver. Overwrite this method if another
* database server requires a different handling of byte attributes in tables.
*
* @param res the result set
* @param attributeName the name of the table attribute
* @return byte[] the column value; if the value is SQL NULL, the value returned is null
* @throws SQLException if a database access error occurs
*/
public byte[] getBytes(ResultSet res, String attributeName) throws SQLException {
return res.getBytes(attributeName);
}
/**
* Returns a JDBC connection from the connection pool.<p>
*
* Use this method to get a connection for reading/writing project independent data.<p>
*
* @param dbc the current database context
*
* @return a JDBC connection
* @throws SQLException if the project id is not supported
*/
public Connection getConnection(CmsDbContext dbc) throws SQLException {
return getConnection(dbc, 0);
}
/**
* Returns a JDBC connection from the connection pool specified by the given CmsProject id.<p>
*
* Use this method to get a connection for reading/writing data either in online or offline projects
* such as files, folders.<p>
*
* @param dbc the current database context
* @param projectId the id of a project (to distinguish between online / offline tables)
*
* @return a JDBC connection
* @throws SQLException if a database access error occurs
*/
public Connection getConnection(CmsDbContext dbc, int projectId) throws SQLException {
if (dbc == null) {
LOG.error(Messages.get().getBundle().key(Messages.LOG_NULL_DB_CONTEXT_0));
}
return getConnection(projectId);
}
/**
* Returns a PreparedStatement for a JDBC connection specified by the key of a SQL query
* and the CmsProject.<p>
*
* @param con the JDBC connection
* @param project the specified CmsProject
* @param queryKey the key of the SQL query
* @return PreparedStatement a new PreparedStatement containing the pre-compiled SQL statement
* @throws SQLException if a database access error occurs
*/
public PreparedStatement getPreparedStatement(Connection con, CmsProject project, String queryKey)
throws SQLException {
return getPreparedStatement(con, project.getId(), queryKey);
}
/**
* Returns a PreparedStatement for a JDBC connection specified by the key of a SQL query
* and the project-ID.<p>
*
* @param con the JDBC connection
* @param projectId the ID of the specified CmsProject
* @param queryKey the key of the SQL query
* @return PreparedStatement a new PreparedStatement containing the pre-compiled SQL statement
* @throws SQLException if a database access error occurs
*/
public PreparedStatement getPreparedStatement(Connection con, int projectId, String queryKey) throws SQLException {
String rawSql = readQuery(projectId, queryKey);
return getPreparedStatementForSql(con, rawSql);
}
/**
* Returns a PreparedStatement for a JDBC connection specified by the key of a SQL query.<p>
*
* @param con the JDBC connection
* @param queryKey the key of the SQL query
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -