001: /*
002:
003: Derby - Class org.apache.derby.impl.jdbc.EmbedConnection40
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.jdbc;
023:
024: import java.sql.Array;
025: import java.sql.Blob;
026: import java.sql.SQLClientInfoException;
027: import java.sql.Clob;
028: import java.sql.Connection;
029: import java.sql.NClob;
030: import java.sql.SQLException;
031: import java.sql.SQLXML;
032: import java.sql.Struct;
033: import java.util.HashMap;
034: import java.util.Map;
035: import java.util.Properties;
036: import java.util.Enumeration;
037: import org.apache.derby.jdbc.InternalDriver;
038: import org.apache.derby.iapi.reference.SQLState;
039: import org.apache.derby.iapi.error.StandardException;
040: import org.apache.derby.iapi.jdbc.FailedProperties40;
041:
042: public class EmbedConnection40 extends EmbedConnection30 {
043:
044: /** Creates a new instance of EmbedConnection40 */
045: public EmbedConnection40(EmbedConnection inputConnection) {
046: super (inputConnection);
047: }
048:
049: public EmbedConnection40(InternalDriver driver, String url,
050: Properties info) throws SQLException {
051: super (driver, url, info);
052: }
053:
054: /*
055: *-------------------------------------------------------
056: * JDBC 4.0
057: *-------------------------------------------------------
058: */
059:
060: public Array createArrayOf(String typeName, Object[] elements)
061: throws SQLException {
062: throw Util.notImplemented();
063: }
064:
065: /**
066: *
067: * Constructs an object that implements the <code>Clob</code> interface. The object
068: * returned initially contains no data. The <code>setAsciiStream</code>,
069: * <code>setCharacterStream</code> and <code>setString</code> methods of
070: * the <code>Clob</code> interface may be used to add data to the <code>Clob</code>.
071: *
072: * @return An object that implements the <code>Clob</code> interface
073: * @throws SQLException if an object that implements the
074: * <code>Clob</code> interface can not be constructed, this method is
075: * called on a closed connection or a database access error occurs.
076: *
077: */
078: public Clob createClob() throws SQLException {
079: checkIfClosed();
080: return new EmbedClob("", this );
081: }
082:
083: /**
084: *
085: * Constructs an object that implements the <code>Blob</code> interface. The object
086: * returned initially contains no data. The <code>setBinaryStream</code> and
087: * <code>setBytes</code> methods of the <code>Blob</code> interface may be used to add data to
088: * the <code>Blob</code>.
089: *
090: * @return An object that implements the <code>Blob</code> interface
091: * @throws SQLException if an object that implements the
092: * <code>Blob</code> interface can not be constructed, this method is
093: * called on a closed connection or a database access error occurs.
094: *
095: */
096: public Blob createBlob() throws SQLException {
097: checkIfClosed();
098: return new EmbedBlob(new byte[0], this );
099: }
100:
101: public NClob createNClob() throws SQLException {
102: throw Util.notImplemented();
103: }
104:
105: public SQLXML createSQLXML() throws SQLException {
106: throw Util.notImplemented();
107: }
108:
109: public Struct createStruct(String typeName, Object[] attributes)
110: throws SQLException {
111: throw Util.notImplemented();
112: }
113:
114: /**
115: * Checks if the connection has not been closed and is still valid.
116: * The validity is checked by checking that the connection is not closed.
117: *
118: * @param timeout This should be the time in seconds to wait for the
119: * database operation used to validate the connection to complete
120: * (according to the JDBC4 JavaDoc). This is currently not supported/used.
121: *
122: * @return true if the connection is valid, false otherwise
123: * @exception SQLException if the parameter value is illegal or if a
124: * database error has occured
125: */
126: public boolean isValid(int timeout) throws SQLException {
127: // Validate that the timeout has a legal value
128: if (timeout < 0) {
129: throw Util.generateCsSQLException(
130: SQLState.INVALID_API_PARAMETER,
131: new Integer(timeout), "timeout",
132: "java.sql.Connection.isValid");
133: }
134:
135: // Use the closed status for the connection to determine if the
136: // connection is valid or not
137: return !isClosed();
138: }
139:
140: /**
141: * <code>setClientInfo</code> will always throw a
142: * <code>SQLClientInfoException</code> since Derby does not support
143: * any properties.
144: *
145: * @param name a property key <code>String</code>
146: * @param value a property value <code>String</code>
147: * @exception SQLClientInfoException unless both name and value are null
148: */
149: public void setClientInfo(String name, String value)
150: throws SQLClientInfoException {
151: Properties p = FailedProperties40.makeProperties(name, value);
152: try {
153: checkIfClosed();
154: } catch (SQLException se) {
155: FailedProperties40 fp = new FailedProperties40(p);
156: throw new SQLClientInfoException(se.getMessage(), se
157: .getSQLState(), fp.getProperties());
158: }
159: // Allow null to simplify compliance testing through
160: // reflection, (test all methods in an interface with null
161: // arguments)
162: if (name == null && value == null) {
163: return;
164: }
165: setClientInfo(p);
166: }
167:
168: /**
169: * <code>setClientInfo</code> will throw a
170: * <code>SQLClientInfoException</code> uless the <code>properties</code>
171: * paramenter is empty, since Derby does not support any
172: * properties. All the property keys in the
173: * <code>properties</code> parameter are added to failedProperties
174: * of the exception thrown, with REASON_UNKNOWN_PROPERTY as the
175: * value.
176: *
177: * @param properties a <code>Properties</code> object with the
178: * properties to set
179: * @exception SQLClientInfoException unless properties parameter
180: * is null or empty
181: */
182: public void setClientInfo(Properties properties)
183: throws SQLClientInfoException {
184: FailedProperties40 fp = new FailedProperties40(properties);
185:
186: try {
187: checkIfClosed();
188: } catch (SQLException se) {
189: throw new SQLClientInfoException(se.getMessage(), se
190: .getSQLState(), fp.getProperties());
191: }
192:
193: // Allow null to simplify compliance testing through
194: // reflection, (test all methods in an interface with null
195: // arguments)
196: // An empty properties object is meaningless, but allowed
197: if (properties == null || properties.isEmpty()) {
198: return;
199: }
200:
201: StandardException se = StandardException.newException(
202: SQLState.PROPERTY_UNSUPPORTED_CHANGE, fp.getFirstKey(),
203: fp.getFirstValue());
204: throw new SQLClientInfoException(se.getMessage(), se
205: .getSQLState(), fp.getProperties());
206: }
207:
208: /**
209: * <code>getClientInfo</code> always returns a
210: * <code>null String</code> since Derby doesn't support
211: * ClientInfoProperties.
212: *
213: * @param name a <code>String</code> value
214: * @return a <code>null String</code> value
215: * @exception SQLException if the connection is closed.
216: */
217: public String getClientInfo(String name) throws SQLException {
218: checkIfClosed();
219: return null;
220: }
221:
222: /**
223: * <code>getClientInfo</code> always returns an empty
224: * <code>Properties</code> object since Derby doesn't support
225: * ClientInfoProperties.
226: *
227: * @return an empty <code>Properties</code> object
228: * @exception SQLException if the connection is closed.
229: */
230: public Properties getClientInfo() throws SQLException {
231: checkIfClosed();
232: return new Properties();
233: }
234:
235: /**
236: * Returns the type map for this connection.
237: *
238: * @return type map for this connection
239: * @exception SQLException if a database access error occurs
240: */
241: public final Map<String, Class<?>> getTypeMap() throws SQLException {
242: // This method is already implemented with a non-generic
243: // signature in EmbedConnection. We could just use that method
244: // directly, but then we get a compiler warning (unchecked
245: // cast/conversion). Copy the map to avoid the compiler
246: // warning.
247: Map typeMap = super .getTypeMap();
248: if (typeMap == null)
249: return null;
250: Map<String, Class<?>> genericTypeMap = new HashMap<String, Class<?>>();
251: for (Object key : typeMap.keySet()) {
252: genericTypeMap.put((String) key, (Class) typeMap.get(key));
253: }
254: return genericTypeMap;
255: }
256:
257: /**
258: * Returns false unless <code>interfaces</code> is implemented
259: *
260: * @param interfaces a Class defining an interface.
261: * @return true if this implements the interface or
262: * directly or indirectly wraps an object
263: * that does.
264: * @throws java.sql.SQLException if an error occurs while determining
265: * whether this is a wrapper for an object
266: * with the given interface.
267: */
268: public boolean isWrapperFor(Class<?> interfaces)
269: throws SQLException {
270: checkIfClosed();
271: return interfaces.isInstance(this );
272: }
273:
274: /**
275: * Returns <code>this</code> if this class implements the interface
276: *
277: * @param interfaces a Class defining an interface
278: * @return an object that implements the interface
279: * @throws java.sql.SQLExption if no object if found that implements the
280: * interface
281: */
282: public <T> T unwrap(java.lang.Class<T> interfaces)
283: throws SQLException {
284: checkIfClosed();
285: //Derby does not implement non-standard methods on
286: //JDBC objects
287: //hence return this if this class implements the interface
288: //or throw an SQLException
289: try {
290: return interfaces.cast(this );
291: } catch (ClassCastException cce) {
292: throw newSQLException(SQLState.UNABLE_TO_UNWRAP, interfaces);
293: }
294: }
295: }
|