KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > client > extensions > TypeHandlerCallback


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.client.extensions;
17
18 import java.sql.SQLException JavaDoc;
19
20 /**
21  * A simple interface for implementing custom type handlers.
22  * <p/>
23  * Using this interface, you can implement a type handler that
24  * will perform customized processing before parameters are set
25  * on a PreparedStatement and after values are retrieved from
26  * a ResultSet. Using a custom type handler you can extend
27  * the framework to handle types that are not supported, or
28  * handle supported types in a different way. For example,
29  * you might use a custom type handler to implement proprietary
30  * BLOB support (e.g. Oracle), or you might use it to handle
31  * booleans using "Y" and "N" instead of the more typical 0/1.
32  * <p/>
33  * <b>EXAMPLE</b>
34  * <p>Here's a simple example of a boolean handler that uses "Yes" and "No".</p>
35  * <pre>
36  * public class YesNoBoolTypeHandlerCallback implements TypeHandlerCallback {
37  * <p/>
38  * private static final String YES = "Yes";
39  * private static final String NO = "No";
40  * <p/>
41  * public Object getResult(ResultGetter getter) throws SQLException {
42  * String s = getter.getString();
43  * if (YES.equalsIgnoreCase(s)) {
44  * return new Boolean (true);
45  * } else if (NO.equalsIgnoreCase(s)) {
46  * return new Boolean (false);
47  * } else {
48  * throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected.");
49  * }
50  * }
51  * <p/>
52  * public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
53  * boolean b = ((Boolean)parameter).booleanValue();
54  * if (b) {
55  * setter.setString(YES);
56  * } else {
57  * setter.setString(NO);
58  * }
59  * }
60  * <p/>
61  * public Object valueOf(String s) {
62  * if (YES.equalsIgnoreCase(s)) {
63  * return new Boolean (true);
64  * } else if (NO.equalsIgnoreCase(s)) {
65  * return new Boolean (false);
66  * } else {
67  * throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected.");
68  * }
69  * }
70  * <p/>
71  * }
72  * </pre>
73  */

74 public interface TypeHandlerCallback {
75
76   /**
77    * Performs processing on a value before it is used to set
78    * the parameter of a PreparedStatement.
79    *
80    * @param setter The interface for setting the value on the PreparedStatement.
81    * @param parameter The value to be set.
82    * @throws SQLException If any error occurs.
83    */

84   public void setParameter(ParameterSetter setter, Object JavaDoc parameter)
85       throws SQLException JavaDoc;
86
87   /**
88    * Performs processing on a value before after it has been retrieved
89    * from a ResultSet.
90    *
91    * @param getter The interface for getting the value from the ResultSet.
92    * @return The processed value.
93    * @throws SQLException If any error occurs.
94    */

95   public Object JavaDoc getResult(ResultGetter getter)
96       throws SQLException JavaDoc;
97
98   /**
99    * Casts the string representation of a value into a type recognized by
100    * this type handler. This method is used to translate nullValue values
101    * into types that can be appropriately compared. If your custom type handler
102    * cannot support nullValues, or if there is no reasonable string representation
103    * for this type (e.g. File type), you can simply return the String representation
104    * as it was passed in. It is not recommended to return null, unless null was passed
105    * in.
106    *
107    * @param s A string representation of a valid value for this type.
108    * @return One of the following:
109    * <ol>
110    * <li>the casted repersentation of the String value,</li>
111    * <li>the string as is,</li>
112    * <li>null, only if null was passed in.</li>
113    * </ol>
114    */

115   public Object JavaDoc valueOf(String JavaDoc s);
116
117 }
118
Popular Tags