001: /*
002: Copyright (C) 2002-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;
025:
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.sql.SQLClientInfoException;
030: import java.util.Enumeration;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Properties;
034:
035: /**
036: * An implementation of JDBC4ClientInfoProvider that exposes
037: * the client info as a comment prepended to all statements issued
038: * by the driver.
039: *
040: * Client information is <i>never</i> read from the server with this
041: * implementation, it is always cached locally.
042: *
043: * @version $Id: $
044: */
045:
046: public class JDBC4CommentClientInfoProvider implements
047: JDBC4ClientInfoProvider {
048: private Properties clientInfo;
049:
050: public synchronized void initialize(java.sql.Connection conn,
051: Properties configurationProps) throws SQLException {
052: this .clientInfo = new Properties();
053: }
054:
055: public synchronized void destroy() throws SQLException {
056: this .clientInfo = null;
057: }
058:
059: public synchronized Properties getClientInfo(
060: java.sql.Connection conn) throws SQLException {
061: return this .clientInfo;
062: }
063:
064: public synchronized String getClientInfo(java.sql.Connection conn,
065: String name) throws SQLException {
066: return this .clientInfo.getProperty(name);
067: }
068:
069: public synchronized void setClientInfo(java.sql.Connection conn,
070: Properties properties) throws SQLClientInfoException {
071: this .clientInfo = new Properties();
072:
073: Enumeration propNames = properties.propertyNames();
074:
075: while (propNames.hasMoreElements()) {
076: String name = (String) propNames.nextElement();
077:
078: this .clientInfo.put(name, properties.getProperty(name));
079: }
080:
081: setComment(conn);
082: }
083:
084: public synchronized void setClientInfo(java.sql.Connection conn,
085: String name, String value) throws SQLClientInfoException {
086: this .clientInfo.setProperty(name, value);
087: setComment(conn);
088: }
089:
090: private synchronized void setComment(java.sql.Connection conn) {
091: StringBuffer commentBuf = new StringBuffer();
092: Iterator elements = this .clientInfo.entrySet().iterator();
093:
094: while (elements.hasNext()) {
095: if (commentBuf.length() > 0) {
096: commentBuf.append(", ");
097: }
098:
099: Map.Entry entry = (Map.Entry) elements.next();
100: commentBuf.append("" + entry.getKey());
101: commentBuf.append("=");
102: commentBuf.append("" + entry.getValue());
103: }
104:
105: ((com.mysql.jdbc.Connection) conn)
106: .setStatementComment(commentBuf.toString());
107: }
108: }
|