1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import org.jboss.logging.Logger; 25 26 import java.sql.PreparedStatement ; 27 import java.sql.SQLException ; 28 import java.io.StringReader ; 29 import java.io.ByteArrayInputStream ; 30 import java.math.BigDecimal ; 31 32 38 public interface JDBCParameterSetter 39 { 40 49 void set(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) throws SQLException ; 50 51 abstract class JDBCAbstractParameterSetter implements JDBCParameterSetter 52 { 53 public void set(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 54 throws SQLException 55 { 56 if(log.isTraceEnabled()) 57 { 58 log.trace("param: " + 59 "i=" + index + ", " + 60 "type=" + JDBCUtil.getJDBCTypeName(jdbcType) + ", " + 61 "value=" + ((value == null) ? "NULL" : value)); 62 } 63 64 if(value == null) 65 { 66 ps.setNull(index, jdbcType); 67 } 68 else 69 { 70 value = JDBCUtil.coerceToSQLType(jdbcType, value); 71 setNotNull(ps, index, jdbcType, value, log); 72 } 73 } 74 75 protected abstract void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 76 throws SQLException ; 77 } 78 79 82 JDBCParameterSetter CLOB = new JDBCAbstractParameterSetter() 83 { 84 protected void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 85 throws SQLException 86 { 87 String string = value.toString(); 88 ps.setCharacterStream(index, new StringReader (string), string.length()); 89 } 90 }; 91 92 95 JDBCParameterSetter BINARY = new JDBCAbstractParameterSetter() 96 { 97 protected void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 98 throws SQLException 99 { 100 byte[] bytes = JDBCUtil.convertObjectToByteArray(value); 101 ps.setBytes(index, bytes); 102 } 103 }; 104 105 108 JDBCParameterSetter BLOB = new JDBCAbstractParameterSetter() 109 { 110 protected void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 111 throws SQLException 112 { 113 byte[] bytes = JDBCUtil.convertObjectToByteArray(value); 114 ps.setBinaryStream(index, new ByteArrayInputStream (bytes), bytes.length); 115 } 116 }; 117 118 121 JDBCParameterSetter NUMERIC = new JDBCAbstractParameterSetter() 122 { 123 protected void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 124 throws SQLException 125 { 126 if(value instanceof BigDecimal ) 127 { 128 ps.setBigDecimal(index, (BigDecimal )value); 129 } 130 else 131 { 132 ps.setObject(index, value, jdbcType, 0); 133 } 134 } 135 }; 136 137 140 JDBCParameterSetter OBJECT = new JDBCAbstractParameterSetter() 141 { 142 protected void setNotNull(PreparedStatement ps, int index, int jdbcType, Object value, Logger log) 143 throws SQLException 144 { 145 ps.setObject(index, value, jdbcType); 146 } 147 }; 148 } 149 | Popular Tags |