KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedConnection40


1 /*
2  
3    Derby - Class org.apache.derby.impl.jdbc.EmbedConnection40
4  
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11  
12       http://www.apache.org/licenses/LICENSE-2.0
13  
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19  
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import java.sql.Array JavaDoc;
25 import java.sql.Blob JavaDoc;
26 import java.sql.SQLClientInfoException JavaDoc;
27 import java.sql.Clob JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.NClob JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.sql.SQLXML JavaDoc;
32 import java.sql.Struct JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import org.apache.derby.jdbc.InternalDriver;
38 import org.apache.derby.iapi.reference.SQLState;
39 import org.apache.derby.iapi.error.StandardException;
40 import org.apache.derby.iapi.jdbc.FailedProperties40;
41
42 public class EmbedConnection40 extends EmbedConnection30 {
43     
44     /** Creates a new instance of EmbedConnection40 */
45     public EmbedConnection40(EmbedConnection inputConnection) {
46         super(inputConnection);
47     }
48     
49     public EmbedConnection40(
50         InternalDriver driver,
51         String JavaDoc url,
52         Properties JavaDoc info)
53         throws SQLException JavaDoc {
54         super(driver, url, info);
55     }
56     
57     /*
58      *-------------------------------------------------------
59      * JDBC 4.0
60      *-------------------------------------------------------
61      */

62     
63     public Array JavaDoc createArrayOf(String JavaDoc typeName, Object JavaDoc[] elements)
64         throws SQLException JavaDoc {
65         throw Util.notImplemented();
66     }
67     
68     /**
69      *
70      * Constructs an object that implements the <code>Clob</code> interface. The object
71      * returned initially contains no data. The <code>setAsciiStream</code>,
72      * <code>setCharacterStream</code> and <code>setString</code> methods of
73      * the <code>Clob</code> interface may be used to add data to the <code>Clob</code>.
74      *
75      * @return An object that implements the <code>Clob</code> interface
76      * @throws SQLException if an object that implements the
77      * <code>Clob</code> interface can not be constructed, this method is
78      * called on a closed connection or a database access error occurs.
79      *
80      */

81     public Clob JavaDoc createClob() throws SQLException JavaDoc {
82         checkIfClosed();
83         return new EmbedClob("",this);
84     }
85     
86     /**
87      *
88      * Constructs an object that implements the <code>Blob</code> interface. The object
89      * returned initially contains no data. The <code>setBinaryStream</code> and
90      * <code>setBytes</code> methods of the <code>Blob</code> interface may be used to add data to
91      * the <code>Blob</code>.
92      *
93      * @return An object that implements the <code>Blob</code> interface
94      * @throws SQLException if an object that implements the
95      * <code>Blob</code> interface can not be constructed, this method is
96      * called on a closed connection or a database access error occurs.
97      *
98      */

99     public Blob JavaDoc createBlob() throws SQLException JavaDoc {
100         checkIfClosed();
101         return new EmbedBlob(new byte[0],this);
102     }
103     
104     public NClob JavaDoc createNClob() throws SQLException JavaDoc {
105         throw Util.notImplemented();
106     }
107     
108     public SQLXML JavaDoc createSQLXML() throws SQLException JavaDoc {
109         throw Util.notImplemented();
110     }
111     
112     public Struct JavaDoc createStruct(String JavaDoc typeName, Object JavaDoc[] attributes)
113         throws SQLException JavaDoc {
114         throw Util.notImplemented();
115     }
116     
117     /**
118      * Checks if the connection has not been closed and is still valid.
119      * The validity is checked by checking that the connection is not closed.
120      *
121      * @param timeout This should be the time in seconds to wait for the
122      * database operation used to validate the connection to complete
123      * (according to the JDBC4 JavaDoc). This is currently not supported/used.
124      *
125      * @return true if the connection is valid, false otherwise
126      * @exception SQLException if the parameter value is illegal or if a
127      * database error has occured
128      */

129     public boolean isValid(int timeout) throws SQLException JavaDoc {
130         // Validate that the timeout has a legal value
131
if (timeout < 0) {
132             throw Util.generateCsSQLException(SQLState.INVALID_API_PARAMETER,
133                                               new Integer JavaDoc(timeout), "timeout",
134                                               "java.sql.Connection.isValid");
135         }
136
137         // Use the closed status for the connection to determine if the
138
// connection is valid or not
139
return !isClosed();
140     }
141
142     /**
143      * <code>setClientInfo</code> will always throw a
144      * <code>SQLClientInfoException</code> since Derby does not support
145      * any properties.
146      *
147      * @param name a property key <code>String</code>
148      * @param value a property value <code>String</code>
149      * @exception SQLClientInfoException unless both name and value are null
150      */

151     public void setClientInfo(String JavaDoc name, String JavaDoc value)
152     throws SQLClientInfoException JavaDoc{
153         Properties JavaDoc p = FailedProperties40.makeProperties(name,value);
154         try { checkIfClosed(); }
155         catch (SQLException JavaDoc se) {
156             FailedProperties40 fp = new FailedProperties40(p);
157             throw new SQLClientInfoException JavaDoc(se.getMessage(),
158                                              se.getSQLState(),
159                                              fp.getProperties());
160         }
161         // Allow null to simplify compliance testing through
162
// reflection, (test all methods in an interface with null
163
// arguments)
164
if (name == null && value == null) {
165             return;
166         }
167         setClientInfo(p);
168     }
169     
170     /**
171      * <code>setClientInfo</code> will throw a
172      * <code>SQLClientInfoException</code> uless the <code>properties</code>
173      * paramenter is empty, since Derby does not support any
174      * properties. All the property keys in the
175      * <code>properties</code> parameter are added to failedProperties
176      * of the exception thrown, with REASON_UNKNOWN_PROPERTY as the
177      * value.
178      *
179      * @param properties a <code>Properties</code> object with the
180      * properties to set
181      * @exception SQLClientInfoException unless properties parameter
182      * is null or empty
183      */

184     public void setClientInfo(Properties JavaDoc properties)
185     throws SQLClientInfoException JavaDoc {
186         FailedProperties40 fp = new FailedProperties40(properties);
187         
188         try { checkIfClosed(); }
189         catch (SQLException JavaDoc se) {
190             throw new SQLClientInfoException JavaDoc(se.getMessage(), se.getSQLState(),
191                                              fp.getProperties());
192         }
193
194         // Allow null to simplify compliance testing through
195
// reflection, (test all methods in an interface with null
196
// arguments)
197
// An empty properties object is meaningless, but allowed
198
if (properties == null || properties.isEmpty()) {
199             return;
200         }
201
202         StandardException se =
203             StandardException.newException
204             (SQLState.PROPERTY_UNSUPPORTED_CHANGE,
205              fp.getFirstKey(),
206              fp.getFirstValue());
207         throw new SQLClientInfoException JavaDoc(se.getMessage(),
208                                          se.getSQLState(), fp.getProperties());
209     }
210     
211     /**
212      * <code>getClientInfo</code> always returns a
213      * <code>null String</code> since Derby doesn't support
214      * ClientInfoProperties.
215      *
216      * @param name a <code>String</code> value
217      * @return a <code>null String</code> value
218      * @exception SQLException if the connection is closed.
219      */

220     public String JavaDoc getClientInfo(String JavaDoc name)
221     throws SQLException JavaDoc{
222         checkIfClosed();
223         return null;
224     }
225     
226     /**
227      * <code>getClientInfo</code> always returns an empty
228      * <code>Properties</code> object since Derby doesn't support
229      * ClientInfoProperties.
230      *
231      * @return an empty <code>Properties</code> object
232      * @exception SQLException if the connection is closed.
233      */

234     public Properties JavaDoc getClientInfo()
235     throws SQLException JavaDoc{
236         checkIfClosed();
237         return new Properties JavaDoc();
238     }
239
240     /**
241      * Returns the type map for this connection.
242      *
243      * @return type map for this connection
244      * @exception SQLException if a database access error occurs
245      */

246     public final Map JavaDoc<String JavaDoc, Class JavaDoc<?>> getTypeMap() throws SQLException JavaDoc {
247         // This method is already implemented with a non-generic
248
// signature in EmbedConnection. We could just use that method
249
// directly, but then we get a compiler warning (unchecked
250
// cast/conversion). Copy the map to avoid the compiler
251
// warning.
252
Map JavaDoc typeMap = super.getTypeMap();
253         if (typeMap == null) return null;
254         Map JavaDoc<String JavaDoc, Class JavaDoc<?>> genericTypeMap = new HashMap JavaDoc<String JavaDoc, Class JavaDoc<?>>();
255         for (Object JavaDoc key : typeMap.keySet()) {
256             genericTypeMap.put((String JavaDoc) key, (Class JavaDoc) typeMap.get(key));
257         }
258         return genericTypeMap;
259     }
260     
261     /**
262      * Returns false unless <code>interfaces</code> is implemented
263      *
264      * @param interfaces a Class defining an interface.
265      * @return true if this implements the interface or
266      * directly or indirectly wraps an object
267      * that does.
268      * @throws java.sql.SQLException if an error occurs while determining
269      * whether this is a wrapper for an object
270      * with the given interface.
271      */

272     public boolean isWrapperFor(Class JavaDoc<?> interfaces) throws SQLException JavaDoc {
273         checkIfClosed();
274         return interfaces.isInstance(this);
275     }
276     
277     /**
278      * Returns <code>this</code> if this class implements the interface
279      *
280      * @param interfaces a Class defining an interface
281      * @return an object that implements the interface
282      * @throws java.sql.SQLExption if no object if found that implements the
283      * interface
284      */

285     public <T> T unwrap(java.lang.Class JavaDoc<T> interfaces)
286                             throws SQLException JavaDoc{
287         checkIfClosed();
288         //Derby does not implement non-standard methods on
289
//JDBC objects
290
//hence return this if this class implements the interface
291
//or throw an SQLException
292
try {
293             return interfaces.cast(this);
294         } catch (ClassCastException JavaDoc cce) {
295             throw newSQLException(SQLState.UNABLE_TO_UNWRAP,interfaces);
296         }
297     }
298 }
299
Popular Tags