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.Properties;
032:
033: public class JDBC4ClientInfoProviderSP implements
034: JDBC4ClientInfoProvider {
035: PreparedStatement setClientInfoSp;
036:
037: PreparedStatement getClientInfoSp;
038:
039: PreparedStatement getClientInfoBulkSp;
040:
041: public synchronized void initialize(java.sql.Connection conn,
042: Properties configurationProps) throws SQLException {
043: String identifierQuote = conn.getMetaData()
044: .getIdentifierQuoteString();
045: String setClientInfoSpName = configurationProps.getProperty(
046: "clientInfoSetSPName", "setClientInfo");
047: String getClientInfoSpName = configurationProps.getProperty(
048: "clientInfoGetSPName", "getClientInfo");
049: String getClientInfoBulkSpName = configurationProps
050: .getProperty("clientInfoGetBulkSPName",
051: "getClientInfoBulk");
052: String clientInfoCatalog = configurationProps.getProperty(
053: "clientInfoCatalog", ""); // "" means use current from
054: // connection
055:
056: String catalog = "".equals(clientInfoCatalog) ? conn
057: .getCatalog() : clientInfoCatalog;
058:
059: this .setClientInfoSp = ((com.mysql.jdbc.Connection) conn)
060: .clientPrepareStatement("CALL " + identifierQuote
061: + catalog + identifierQuote + "."
062: + identifierQuote + setClientInfoSpName
063: + identifierQuote + "(?, ?)");
064:
065: this .getClientInfoSp = ((com.mysql.jdbc.Connection) conn)
066: .clientPrepareStatement("CALL" + identifierQuote
067: + catalog + identifierQuote + "."
068: + identifierQuote + getClientInfoSpName
069: + identifierQuote + "(?)");
070:
071: this .getClientInfoBulkSp = ((com.mysql.jdbc.Connection) conn)
072: .clientPrepareStatement("CALL " + identifierQuote
073: + catalog + identifierQuote + "."
074: + identifierQuote + getClientInfoBulkSpName
075: + identifierQuote + "()");
076: }
077:
078: public synchronized void destroy() throws SQLException {
079: if (this .setClientInfoSp != null) {
080: this .setClientInfoSp.close();
081: this .setClientInfoSp = null;
082: }
083:
084: if (this .getClientInfoSp != null) {
085: this .getClientInfoSp.close();
086: this .getClientInfoSp = null;
087: }
088:
089: if (this .getClientInfoBulkSp != null) {
090: this .getClientInfoBulkSp.close();
091: this .getClientInfoBulkSp = null;
092: }
093: }
094:
095: public synchronized Properties getClientInfo(
096: java.sql.Connection conn) throws SQLException {
097: ResultSet rs = null;
098:
099: Properties props = new Properties();
100:
101: try {
102: this .getClientInfoBulkSp.execute();
103:
104: rs = this .getClientInfoBulkSp.getResultSet();
105:
106: while (rs.next()) {
107: props.setProperty(rs.getString(1), rs.getString(2));
108: }
109: } finally {
110: if (rs != null) {
111: rs.close();
112: }
113: }
114:
115: return props;
116: }
117:
118: public synchronized String getClientInfo(java.sql.Connection conn,
119: String name) throws SQLException {
120: ResultSet rs = null;
121:
122: String clientInfo = null;
123:
124: try {
125: this .getClientInfoSp.setString(1, name);
126: this .getClientInfoSp.execute();
127:
128: rs = this .getClientInfoSp.getResultSet();
129:
130: if (rs.next()) {
131: clientInfo = rs.getString(1);
132: }
133: } finally {
134: if (rs != null) {
135: rs.close();
136: }
137: }
138:
139: return clientInfo;
140: }
141:
142: public synchronized void setClientInfo(java.sql.Connection conn,
143: Properties properties) throws SQLClientInfoException {
144: try {
145: Enumeration propNames = properties.propertyNames();
146:
147: while (propNames.hasMoreElements()) {
148: String name = (String) propNames.nextElement();
149: String value = properties.getProperty(name);
150:
151: setClientInfo(conn, name, value);
152: }
153: } catch (SQLException sqlEx) {
154: SQLClientInfoException clientInfoEx = new SQLClientInfoException();
155: clientInfoEx.initCause(sqlEx);
156:
157: throw clientInfoEx;
158: }
159: }
160:
161: public synchronized void setClientInfo(java.sql.Connection conn,
162: String name, String value) throws SQLClientInfoException {
163: try {
164: this .setClientInfoSp.setString(1, name);
165: this .setClientInfoSp.setString(2, value);
166: this .setClientInfoSp.execute();
167: } catch (SQLException sqlEx) {
168: SQLClientInfoException clientInfoEx = new SQLClientInfoException();
169: clientInfoEx.initCause(sqlEx);
170:
171: throw clientInfoEx;
172: }
173: }
174: }
|