001: /*
002: Copyright (C) 2007 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022: */
023:
024: package com.mysql.jdbc.interceptors;
025:
026: import java.sql.SQLException;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: import com.mysql.jdbc.Connection;
032: import com.mysql.jdbc.ResultSetInternalMethods;
033: import com.mysql.jdbc.Statement;
034: import com.mysql.jdbc.StatementInterceptor;
035: import com.mysql.jdbc.Util;
036:
037: public class ServerStatusDiffInterceptor implements
038: StatementInterceptor {
039:
040: private Map preExecuteValues = new HashMap();
041:
042: private Map postExecuteValues = new HashMap();
043:
044: public void init(Connection conn, Properties props)
045: throws SQLException {
046:
047: }
048:
049: public ResultSetInternalMethods postProcess(String sql,
050: Statement interceptedStatement,
051: ResultSetInternalMethods originalResultSet,
052: Connection connection) throws SQLException {
053:
054: if (connection.versionMeetsMinimum(5, 0, 2)) {
055: populateMapWithSessionStatusValues(connection,
056: this .postExecuteValues);
057:
058: connection.getLog().logInfo(
059: "Server status change for statement:\n"
060: + Util.calculateDifferences(
061: this .preExecuteValues,
062: this .postExecuteValues));
063: }
064:
065: return null; // we don't actually modify a result set
066:
067: }
068:
069: private void populateMapWithSessionStatusValues(
070: Connection connection, Map toPopulate) throws SQLException {
071: java.sql.Statement stmt = null;
072: java.sql.ResultSet rs = null;
073:
074: try {
075: toPopulate.clear();
076:
077: stmt = connection.createStatement();
078: rs = stmt.executeQuery("SHOW SESSION STATUS");
079: Util.resultSetToMap(toPopulate, rs);
080: } finally {
081: if (rs != null) {
082: rs.close();
083: }
084:
085: if (stmt != null) {
086: stmt.close();
087: }
088: }
089: }
090:
091: public ResultSetInternalMethods preProcess(String sql,
092: Statement interceptedStatement, Connection connection)
093: throws SQLException {
094:
095: if (connection.versionMeetsMinimum(5, 0, 2)) {
096: populateMapWithSessionStatusValues(connection,
097: this .preExecuteValues);
098: }
099:
100: return null; // we don't actually modify a result set
101: }
102:
103: public boolean executeTopLevelOnly() {
104: return true;
105: }
106: }
|