1 /* 2 Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of version 2 of the GNU General Public License as 6 published by the Free Software Foundation. 7 8 There are special exceptions to the terms and conditions of the GPL 9 as it is applied to this software. View the full text of the 10 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 11 software distribution. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 */ 23 24 package com.mysql.jdbc; 25 26 import java.sql.PreparedStatement; 27 import java.sql.ResultSet; 28 import java.sql.SQLException; 29 import java.sql.SQLClientInfoException; 30 import java.util.Enumeration; 31 import java.util.Iterator; 32 import java.util.Map; 33 import java.util.Properties; 34 35 /** 36 * An implementation of JDBC4ClientInfoProvider that exposes 37 * the client info as a comment prepended to all statements issued 38 * by the driver. 39 * 40 * Client information is <i>never</i> read from the server with this 41 * implementation, it is always cached locally. 42 * 43 * @version $Id: $ 44 */ 45 46 public class JDBC4CommentClientInfoProvider implements JDBC4ClientInfoProvider { 47 private Properties clientInfo; 48 49 public synchronized void initialize(java.sql.Connection conn, 50 Properties configurationProps) throws SQLException { 51 this.clientInfo = new Properties(); 52 } 53 54 public synchronized void destroy() throws SQLException { 55 this.clientInfo = null; 56 } 57 58 public synchronized Properties getClientInfo(java.sql.Connection conn) 59 throws SQLException { 60 return this.clientInfo; 61 } 62 63 public synchronized String getClientInfo(java.sql.Connection conn, 64 String name) throws SQLException { 65 return this.clientInfo.getProperty(name); 66 } 67 68 public synchronized void setClientInfo(java.sql.Connection conn, 69 Properties properties) throws SQLClientInfoException { 70 this.clientInfo = new Properties(); 71 72 Enumeration propNames = properties.propertyNames(); 73 74 while (propNames.hasMoreElements()) { 75 String name = (String)propNames.nextElement(); 76 77 this.clientInfo.put(name, properties.getProperty(name)); 78 } 79 80 setComment(conn); 81 } 82 83 public synchronized void setClientInfo(java.sql.Connection conn, 84 String name, String value) throws SQLClientInfoException { 85 this.clientInfo.setProperty(name, value); 86 setComment(conn); 87 } 88 89 private synchronized void setComment(java.sql.Connection conn) { 90 StringBuffer commentBuf = new StringBuffer(); 91 Iterator elements = this.clientInfo.entrySet().iterator(); 92 93 while (elements.hasNext()) { 94 if (commentBuf.length() > 0) { 95 commentBuf.append(", "); 96 } 97 98 Map.Entry entry = (Map.Entry)elements.next(); 99 commentBuf.append("" + entry.getKey()); 100 commentBuf.append("="); 101 commentBuf.append("" + entry.getValue()); 102 } 103 104 ((com.mysql.jdbc.Connection)conn).setStatementComment( 105 commentBuf.toString()); 106 } 107 }