1 2 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 ; 20 import java.sql.SQLException ; 21 import java.sql.ResultSet ; 22 import java.math.BigDecimal ; 23 import java.math.BigInteger ; 24 import java.util.ArrayList ; 25 26 import com.versant.core.common.BindingSupportImpl; 27 28 35 public class JdbcUtils implements MDStatics { 36 37 private JdbcUtils() { } 38 39 40 41 45 public static void set(PreparedStatement ps, int index, Object value, 46 int javaTypeCode, int jdbcType) throws SQLException { 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 )value).intValue()); 55 break; 56 case SHORT: 57 58 case SHORTW: 59 ps.setShort(index, ((Number )value).shortValue()); 60 break; 61 case STRING: 62 ps.setString(index, (String )value ); 63 break; 64 case BOOLEAN: 65 66 case BOOLEANW: 67 ps.setBoolean(index, ((Boolean )value).booleanValue()); 68 break; 69 case BYTE: 70 71 case BYTEW: 72 ps.setByte(index, ((Number )value).byteValue()); 73 break; 74 case BIGDECIMAL: 75 ps.setBigDecimal(index, (BigDecimal )value); 76 break; 77 case BIGINTEGER: 78 ps.setBigDecimal(index, new BigDecimal ((BigInteger )value)); 79 break; 80 case DOUBLE: 81 82 case DOUBLEW: 83 ps.setDouble(index, ((Number )value).doubleValue()); 84 break; 85 case FLOAT: 86 87 case FLOATW: 88 ps.setFloat(index, ((Number )value).floatValue()); 89 break; 90 case LONG: 91 92 case LONGW: 93 ps.setLong(index, ((Number )value).longValue()); 94 break; 95 96 default: 97 ps.setObject(index, value, jdbcType); 98 }; 99 } 100 } 101 102 108 public static void set(PreparedStatement ps, int index, int value, 109 int javaTypeCode, int jdbcType) throws SQLException { 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 136 public static Object get(ResultSet rs, int index, int javaTypeCode, int scale ) 137 throws SQLException { 138 switch (javaTypeCode) { 139 case INT: 140 141 case INTW: 142 return new Integer (rs.getInt(index)); 143 case SHORT: 144 145 case SHORTW: 146 return new Short (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 (rs.getByte(index)); 157 case BIGDECIMAL: 158 return rs.getBigDecimal(index); 159 case BIGINTEGER: 160 BigDecimal 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 (rs.getDouble(index)); 170 case FLOAT: 171 172 case FLOATW: 173 return new Float (rs.getFloat(index)); 174 case LONG: 175 176 case LONGW: 177 return new Long (rs.getLong(index)); 178 179 default: 180 return rs.getObject(index); 181 } 182 } 183 184 185 186 192 public static Object toArrayNoNulls(ArrayList values, int javaTypeCode) 193 throws SQLException { 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 )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 )values.get(i)).shortValue(); 210 } 211 return shorta; 212 case STRING: 213 String [] stringa = new String [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 )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 )values.get(i)).byteValue(); 228 } 229 return bytea; 230 case BIGDECIMAL: 231 BigDecimal [] bigdecimala = new BigDecimal [n]; 232 values.toArray(bigdecimala); 233 return bigdecimala; 234 case BIGINTEGER: 235 BigInteger [] bigintegera = new BigInteger [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 )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 )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 )values.get(i)).longValue(); 258 } 259 return longa; 260 default: 261 return values.toArray(); 262 } 263 } 264 265 270 public static String toString(Throwable x) { 271 if (x instanceof SQLException ) { 272 StringBuffer s = new StringBuffer (); 273 s.append(x); 274 SQLException se = (SQLException )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 292 public static String getPreparedStatementInfo(String sql, 293 PreparedStatement ps) { 294 StringBuffer s = new StringBuffer (); 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 319 public static String getPreparedStatementInfo(String sql, 320 PreparedStatement ps, int batchEntry) { 321 StringBuffer s = new StringBuffer (); 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 |