KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > TypeAsBytesConverterBase


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import com.versant.core.jdbc.JdbcConverter;
15 import com.versant.core.jdbc.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcTypeRegistry;
17 import com.versant.core.jdbc.metadata.JdbcColumn;
18 import com.versant.core.jdbc.metadata.JdbcTypes;
19
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws-clause
26

27 import com.versant.core.common.BindingSupportImpl;
28
29 /**
30  * This is a base class for converters that convert a type to/from byte[] and
31  * use a nested converter to do the actual JDBC work with the byte[].
32  * @keep-all
33  */

34 public abstract class TypeAsBytesConverterBase implements JdbcConverter {
35
36     /**
37      * Subclasses must extend this and provide their class to the superclass
38      * constructor.
39      */

40     public static abstract class Factory extends NoArgJdbcConverterFactory {
41
42         private HashMap JavaDoc instanceMap = new HashMap JavaDoc(17);
43
44         /**
45          * Create a converter for col using props as parameters. Return null if
46          * no converter is required.
47          */

48         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
49                 JdbcTypeRegistry jdbcTypeRegistry) {
50             JdbcConverterFactory f = jdbcTypeRegistry.getJdbcConverterFactory(col.jdbcType);
51             JdbcConverter c = f.createJdbcConverter(col, args, jdbcTypeRegistry);
52             if (c.getValueType() != byte[].class) {
53                 throw BindingSupportImpl.getInstance().illegalArgument("Invalid JDBC type: " +
54                     JdbcTypes.toString(col.jdbcType));
55             }
56             JdbcConverter ans = (JdbcConverter)instanceMap.get(c);
57             if (ans == null) {
58                 instanceMap.put(c, ans = createConverter(c));
59             }
60             return ans;
61         }
62
63         /**
64          * Create an instance of our converter.
65          */

66         protected abstract JdbcConverter createConverter(JdbcConverter nested);
67
68     }
69
70     protected final JdbcConverter nested;
71
72     public TypeAsBytesConverterBase(JdbcConverter nested) {
73         this.nested = nested;
74     }
75
76     /**
77      * Convert a byte[] into an instance of our value class.
78      */

79     protected abstract Object JavaDoc fromByteArray(byte[] buf);
80
81     /**
82      * Convert an instance of our value class into a byte[].
83      */

84     protected abstract byte[] toByteArray(Object JavaDoc value);
85
86     /**
87      * Get the type of our expected value objects (e.g. java.util.Locale
88      * for a converter for Locale's).
89      */

90     public abstract Class JavaDoc getValueType();
91
92     /**
93      * Is this converter for an Oracle style LOB column? Oracle LOBs require
94      * a hard coded a value into the insert/update statement instead of using
95      * a replaceable parameter and then select the value (if not null) and
96      * modify it.
97      */

98     public boolean isOracleStyleLOB() {
99         return nested.isOracleStyleLOB();
100     }
101
102     /**
103      * This is only called if isOracleStyleLOB returns true. Get the String
104      * to be embedded in an SQL insert/update statement when the value for
105      * this column is not null (e.g. "empty_clob()");
106      */

107     public String JavaDoc getOracleStyleLOBNotNullString() {
108         return nested.getOracleStyleLOBNotNullString();
109     }
110
111     /**
112      * Get the value of col from rs at position index.
113      * @exception SQLException on SQL errors
114      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
115      */

116     public Object JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
117             throws SQLException JavaDoc, JDOFatalDataStoreException {
118         byte[] buf = (byte[])nested.get(rs, index, col);
119         if (buf == null) return null;
120         return fromByteArray(buf);
121     }
122
123
124
125
126     /**
127      * Set parameter index on ps to value (for col).
128      * @exception SQLException on SQL errors
129      * @exception JDOFatalDataStoreException if value is invalid
130      */

131     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
132             throws SQLException JavaDoc, JDOFatalDataStoreException {
133         nested.set(ps, index, col, toByteArray(value));
134     }
135
136     /**
137      * Set parameter index on ps to value (for col). This special form is used
138      * when the value to be set is available as an int to avoid creating
139      * an wrapper instance.
140      * @exception SQLException on SQL errors
141      * @exception JDOFatalDataStoreException if value is invalid
142      */

143     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, int value)
144             throws SQLException JavaDoc, JDOFatalDataStoreException {
145         throw BindingSupportImpl.getInstance().fatalDatastore("set(..int) called");
146     }
147
148     /**
149      * This method is called for converters that return true for
150      * isOracleStyleLOB. The value at index in rs will contain the LOB to
151      * be updated.
152      * @exception SQLException on SQL errors
153      * @exception JDOFatalDataStoreException if value is invalid
154      */

155     public void set(ResultSet JavaDoc rs, int index, JdbcColumn col, Object JavaDoc value)
156             throws SQLException JavaDoc, JDOFatalDataStoreException {
157         nested.set(rs, index, col, toByteArray(value));
158     }
159
160 }
161
162
Popular Tags