KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcUtils


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;
13
14 import com.versant.core.common.GenericState;
15 import com.versant.core.common.GenericOID;
16 import com.versant.core.metadata.MDStatics;
17 import com.versant.core.jdbc.conn.PooledPSWithParamLogging;
18
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import com.versant.core.common.BindingSupportImpl;
27
28 /**
29  * Static JDBC utility functions. These are used to implement GenericOID
30  * and GenericState in a way similar to the generated State and OID
31  * implementations. They are also used elsewhere in JdbcDataStore.
32  * @see GenericOID
33  * @see GenericState
34  */

35 public class JdbcUtils implements MDStatics {
36
37     private JdbcUtils() { }
38
39     
40     
41     /**
42      * Set a parameter on a PreparedStatement. This will call setInt or
43      * setShort etc depending on the type of value.
44      */

45     public static void set(PreparedStatement JavaDoc ps, int index, Object JavaDoc value,
46             int javaTypeCode, int jdbcType) throws SQLException JavaDoc {
47         if (value == null) {
48             ps.setNull(index, jdbcType);
49         } else {
50             switch (javaTypeCode) {
51                 case INT:
52
53                 case INTW:
54                     ps.setInt(index, ((Number JavaDoc)value).intValue());
55                     break;
56                 case SHORT:
57                 
58                 case SHORTW:
59                     ps.setShort(index, ((Number JavaDoc)value).shortValue());
60                     break;
61                 case STRING:
62                     ps.setString(index, (String JavaDoc)value );
63                     break;
64                 case BOOLEAN:
65                 
66                 case BOOLEANW:
67                     ps.setBoolean(index, ((Boolean JavaDoc)value).booleanValue());
68                     break;
69                 case BYTE:
70                 
71                 case BYTEW:
72                     ps.setByte(index, ((Number JavaDoc)value).byteValue());
73                     break;
74                 case BIGDECIMAL:
75                     ps.setBigDecimal(index, (BigDecimal JavaDoc)value);
76                     break;
77                 case BIGINTEGER:
78                     ps.setBigDecimal(index, new BigDecimal JavaDoc((BigInteger JavaDoc)value));
79                     break;
80                 case DOUBLE:
81                 
82                 case DOUBLEW:
83                     ps.setDouble(index, ((Number JavaDoc)value).doubleValue());
84                     break;
85                 case FLOAT:
86                 
87                 case FLOATW:
88                     ps.setFloat(index, ((Number JavaDoc)value).floatValue());
89                     break;
90                 case LONG:
91                 
92                 case LONGW:
93                     ps.setLong(index, ((Number JavaDoc)value).longValue());
94                     break;
95
96                 default:
97                     ps.setObject(index, value, jdbcType);
98             };
99         }
100     }
101
102     /**
103      * Set a parameter on a PreparedStatement. This will call setInt or
104      * setShort etc depending on the type of value. This method is used
105      * when the value is available as an int to avoid creating a wrapper
106      * instance.
107      */

108     public static void set(PreparedStatement JavaDoc ps, int index, int value,
109             int javaTypeCode, int jdbcType) throws SQLException JavaDoc {
110         switch (javaTypeCode) {
111             case INTW:
112             case INT:
113                 ps.setInt(index, value);
114                 break;
115             case SHORTW:
116             case SHORT:
117                 ps.setShort(index, (short)value);
118                 break;
119             case BYTEW:
120             case BYTE:
121                 ps.setByte(index, (byte)value);
122                 break;
123
124             default:
125                 throw BindingSupportImpl.getInstance().internal("set(...int...) called " +
126                     "for javaTypeCode " + javaTypeCode);
127         };
128     }
129
130
131
132
133     /**
134      * Get a value from a ResultSet. This will call getInt or getShort etc.
135      */

136     public static Object JavaDoc get(ResultSet JavaDoc rs, int index, int javaTypeCode, int scale )
137             throws SQLException JavaDoc {
138         switch (javaTypeCode) {
139             case INT:
140
141             case INTW:
142                     return new Integer JavaDoc(rs.getInt(index));
143             case SHORT:
144
145             case SHORTW:
146                     return new Short JavaDoc(rs.getShort(index));
147             case STRING:
148                 return rs.getString(index);
149             case BOOLEAN:
150
151             case BOOLEANW:
152                 return rs.getBoolean(index) ? Boolean.TRUE : Boolean.FALSE;
153             case BYTE:
154
155             case BYTEW:
156                 return new Byte JavaDoc(rs.getByte(index));
157             case BIGDECIMAL:
158                 return rs.getBigDecimal(index);
159             case BIGINTEGER:
160                 BigDecimal JavaDoc d = rs.getBigDecimal(index);
161                 if (d == null) return null;
162
163                 return d.unscaledValue();
164
165
166             case DOUBLE:
167
168             case DOUBLEW:
169                 return new Double JavaDoc(rs.getDouble(index));
170             case FLOAT:
171
172             case FLOATW:
173                 return new Float JavaDoc(rs.getFloat(index));
174             case LONG:
175
176             case LONGW:
177                 return new Long JavaDoc(rs.getLong(index));
178
179             default:
180                 return rs.getObject(index);
181         }
182     }
183
184
185
186     /**
187      * Convert an ArrayList of values into an properly typed array (int[],
188      * String[], byte[] etc.). Arrays of wrapper types (Integer et al) are
189      * returned as arrays of primitives to save memory. This means that
190      * there cannot be any nulls in values.
191      */

192     public static Object JavaDoc toArrayNoNulls(ArrayList JavaDoc values, int javaTypeCode)
193             throws SQLException JavaDoc {
194         int n = values.size();
195         switch (javaTypeCode) {
196             case INTW:
197             case INT:
198
199                 int[] inta = new int[n];
200                 for (int i = inta.length - 1; i >= 0; i--) {
201                     inta[i] = ((Integer JavaDoc)values.get(i)).intValue();
202                 }
203                 return inta;
204             case SHORTW:
205             case SHORT:
206
207                 short[] shorta = new short[n];
208                 for (int i = shorta.length - 1; i >= 0; i--) {
209                     shorta[i] = ((Short JavaDoc)values.get(i)).shortValue();
210                 }
211                 return shorta;
212             case STRING:
213                 String JavaDoc[] stringa = new String JavaDoc[n];
214                 values.toArray(stringa);
215                 return stringa;
216             case BOOLEANW:
217             case BOOLEAN:
218                 boolean[] booleana = new boolean[n];
219                 for (int i = booleana.length - 1; i >= 0; i--) {
220                     booleana[i] = ((Boolean JavaDoc)values.get(i)).booleanValue();
221                 }
222                 return booleana;
223             case BYTEW:
224             case BYTE:
225                 byte[] bytea = new byte[n];
226                 for (int i = bytea.length - 1; i >= 0; i--) {
227                     bytea[i] = ((Byte JavaDoc)values.get(i)).byteValue();
228                 }
229                 return bytea;
230             case BIGDECIMAL:
231                 BigDecimal JavaDoc[] bigdecimala = new BigDecimal JavaDoc[n];
232                 values.toArray(bigdecimala);
233                 return bigdecimala;
234             case BIGINTEGER:
235                 BigInteger JavaDoc[] bigintegera = new BigInteger JavaDoc[n];
236                 values.toArray(bigintegera);
237                 return bigintegera;
238             case DOUBLEW:
239             case DOUBLE:
240                 double[] doublea = new double[n];
241                 for (int i = doublea.length - 1; i >= 0; i--) {
242                     doublea[i] = ((Double JavaDoc)values.get(i)).doubleValue();
243                 }
244                 return doublea;
245             case FLOATW:
246             case FLOAT:
247                 float[] floata = new float[n];
248                 for (int i = floata.length - 1; i >= 0; i--) {
249                     floata[i] = ((Float JavaDoc)values.get(i)).floatValue();
250                 }
251                 return floata;
252             case LONGW:
253             case LONG:
254
255                 long[] longa = new long[n];
256                 for (int i = longa.length - 1; i >= 0; i--) {
257                     longa[i] = ((Long JavaDoc)values.get(i)).longValue();
258                 }
259                 return longa;
260             default:
261                 return values.toArray();
262         }
263     }
264
265     /**
266      * Convert x to a String by calling its toString method. If x is an
267      * SQLException then any getNextExceptions() are recursively added to
268      * the string preceded by linefeeds.
269      */

270     public static String JavaDoc toString(Throwable JavaDoc x) {
271         if (x instanceof SQLException JavaDoc) {
272             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
273             s.append(x);
274             SQLException JavaDoc se = (SQLException JavaDoc)x;
275             for (se = se.getNextException(); se != null; se = se.getNextException()) {
276                 s.append('\n');
277                 s.append(se);
278                 int n = s.length() - 1;
279                 if (s.charAt(n) == '\n') s.setLength(n);
280             }
281             return s.toString();
282         } else {
283             return x.toString();
284         }
285     }
286
287     /**
288      * Build a message with info about ps for exceptions and so on. If the
289      * ps contains batches of parameters they are all added to the info
290      * String.
291      */

292     public static String JavaDoc getPreparedStatementInfo(String JavaDoc sql,
293             PreparedStatement JavaDoc ps) {
294         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
295         s.append(sql);
296         s.append('\n');
297         if (ps instanceof PooledPSWithParamLogging) {
298             s.append("Params: ");
299             PooledPSWithParamLogging lps = (PooledPSWithParamLogging)ps;
300             int bc = lps.getLastBatchCount();
301             if (bc > 0 ) {
302                 for (int i = 0; i < bc; i++) {
303                     s.append('\n');
304                     s.append(lps.getLastExecParamsString(i));
305                 }
306             } else {
307                 s.append(lps.getLastExecParamsString());
308             }
309         } else {
310             s.append("(set event logging to all to see parameter values)");
311         }
312         return s.toString();
313     }
314
315     /**
316      * Build a message with info about ps for exceptions and so on. Parameter
317      * info is provided for the given batch entry if available.
318      */

319     public static String JavaDoc getPreparedStatementInfo(String JavaDoc sql,
320             PreparedStatement JavaDoc ps, int batchEntry) {
321         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
322         s.append(sql);
323         s.append('\n');
324         if (ps instanceof PooledPSWithParamLogging) {
325             s.append("Params: ");
326             PooledPSWithParamLogging lps = (PooledPSWithParamLogging)ps;
327             s.append(lps.getLastExecParamsString(batchEntry));
328         } else {
329             s.append("(set event logging to all to see parameter data)");
330         }
331         return s.toString();
332     }
333
334
335 }
336
337
Popular Tags