001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc;
023:
024: import org.jboss.logging.Logger;
025:
026: import java.sql.PreparedStatement;
027: import java.sql.SQLException;
028: import java.io.StringReader;
029: import java.io.ByteArrayInputStream;
030: import java.math.BigDecimal;
031:
032: /**
033: * Implementations of this interface are used to set java.sql.PreparedStatement parameters.
034: *
035: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
036: * @version <tt>$Revision: 57209 $</tt>
037: */
038: public interface JDBCParameterSetter {
039: /**
040: * Sets a parameter of a specific JDBC type.
041: * @param ps the java.sql.PreparedStatement to set parameter on
042: * @param index the index of the parameter
043: * @param jdbcType the JDBC type of the parameter as defined by java.sql.Types
044: * @param value parameter value
045: * @param log the logger
046: * @throws SQLException
047: */
048: void set(PreparedStatement ps, int index, int jdbcType,
049: Object value, Logger log) throws SQLException;
050:
051: abstract class JDBCAbstractParameterSetter implements
052: JDBCParameterSetter {
053: public void set(PreparedStatement ps, int index, int jdbcType,
054: Object value, Logger log) throws SQLException {
055: if (log.isTraceEnabled()) {
056: log
057: .trace("param: " + "i=" + index + ", "
058: + "type="
059: + JDBCUtil.getJDBCTypeName(jdbcType)
060: + ", " + "value="
061: + ((value == null) ? "NULL" : value));
062: }
063:
064: if (value == null) {
065: ps.setNull(index, jdbcType);
066: } else {
067: value = JDBCUtil.coerceToSQLType(jdbcType, value);
068: setNotNull(ps, index, jdbcType, value, log);
069: }
070: }
071:
072: protected abstract void setNotNull(PreparedStatement ps,
073: int index, int jdbcType, Object value, Logger log)
074: throws SQLException;
075: }
076:
077: /**
078: * Types.CLOB, Types.LONGVARCHAR.
079: */
080: JDBCParameterSetter CLOB = new JDBCAbstractParameterSetter() {
081: protected void setNotNull(PreparedStatement ps, int index,
082: int jdbcType, Object value, Logger log)
083: throws SQLException {
084: String string = value.toString();
085: ps.setCharacterStream(index, new StringReader(string),
086: string.length());
087: }
088: };
089:
090: /**
091: * Types.BINARY, Types.VARBINARY.
092: */
093: JDBCParameterSetter BINARY = new JDBCAbstractParameterSetter() {
094: protected void setNotNull(PreparedStatement ps, int index,
095: int jdbcType, Object value, Logger log)
096: throws SQLException {
097: byte[] bytes = JDBCUtil.convertObjectToByteArray(value);
098: ps.setBytes(index, bytes);
099: }
100: };
101:
102: /**
103: * Types.BLOB, Types.LONGVARBINARY.
104: */
105: JDBCParameterSetter BLOB = new JDBCAbstractParameterSetter() {
106: protected void setNotNull(PreparedStatement ps, int index,
107: int jdbcType, Object value, Logger log)
108: throws SQLException {
109: byte[] bytes = JDBCUtil.convertObjectToByteArray(value);
110: ps.setBinaryStream(index, new ByteArrayInputStream(bytes),
111: bytes.length);
112: }
113: };
114:
115: /**
116: * Types.DECIMAL, Types.NUMERIC
117: */
118: JDBCParameterSetter NUMERIC = new JDBCAbstractParameterSetter() {
119: protected void setNotNull(PreparedStatement ps, int index,
120: int jdbcType, Object value, Logger log)
121: throws SQLException {
122: if (value instanceof BigDecimal) {
123: ps.setBigDecimal(index, (BigDecimal) value);
124: } else {
125: ps.setObject(index, value, jdbcType, 0);
126: }
127: }
128: };
129:
130: /**
131: * Types.JAVA_OBJECT, Types.OTHER, Types.STRUCT
132: */
133: JDBCParameterSetter OBJECT = new JDBCAbstractParameterSetter() {
134: protected void setNotNull(PreparedStatement ps, int index,
135: int jdbcType, Object value, Logger log)
136: throws SQLException {
137: ps.setObject(index, value, jdbcType);
138: }
139: };
140: }
|