/* * StreamableRDBMSMapping.java * * Created on 8 October 2006, 07:50 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package au.org.kaizen.jdo; import java.io.InputStream; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import javax.jdo.JDODataStoreException; import org.apache.log4j.Logger; import org.jpox.store.DatastoreField; import org.jpox.store.StoreManager; import org.jpox.store.mapping.JavaTypeMapping; import org.jpox.store.rdbms.mapping.LongVarcharRDBMSMapping; import org.jpox.store.rdbms.typeinfo.TypeInfo; /** * Represents the physcal mapping of the StreamableObject to the JDBC infoamtion * @author swhitehead */ public class StreamableRDBMSMapping extends LongVarcharRDBMSMapping { private Logger logger; /** * Constructor. * @param storeMgr Manager for the store * @param mapping The java type mapping for the field */ public StreamableRDBMSMapping(StoreManager storeMgr, JavaTypeMapping mapping) { super(storeMgr, mapping); } /** * Constructor. * @param mapping The java type mapping for the field. * @param storeMgr Manager for the store * @param field The field */ public StreamableRDBMSMapping(JavaTypeMapping mapping, StoreManager storeMgr, DatastoreField field) { super(mapping, storeMgr, field); } /** * Convient method for getting a suitable Logger for this class * @return A suitable logger reference */ protected Logger getLogger() { if (logger == null) { logger = Logger.getLogger(StreamableRDBMSMapping.class); } return logger; } /** * Accessor for the type info for this datastore field * @return Type info for the datastore field */ public TypeInfo getTypeInfo() { return getDatabaseAdapter().getTypeInfo(Types.LONGVARBINARY); } /** * Sets the value of the JDBC call to match the object. * * This maps the StreamableObject's stream (getBinaryStream) to the JDBC's * setBinaryStream method. As required by this method, it uses the * StreamableObject's stream length to tell the JDBC driver the number of bytes * expected by this stream. * * It is possible to pass a null value to this method. * @param ps The PreparedStatement been used * @param param The param index point * @param value The StreamableObjet been updated */ public void setObject(Object ps, int param, Object value) { getLogger().debug("SetObject..." + value.getClass()); try { if (value == null) { getLogger().debug("Value is null..."); ((PreparedStatement) ps).setNull(param, getTypeInfo().dataType); } else if (value instanceof StreamableObject) { StreamableObject so = (StreamableObject)value; getLogger().debug("Stream length = " + so.getStreamLength()); ((PreparedStatement) ps).setBinaryStream(param, so.getBinaryStream(), (int)so.getStreamLength()); } } catch (SQLException e) { throw new JDODataStoreException(LOCALISER.msg("RDBMS.Mapping.UnableToSetParam","Object", "" + value, column, e.getMessage()), e); } } /** * This method retrieves the BinaryStream InputStream reference from the JDBC * datastore and wraps it in a StreamableObject. * * This method simply calls the JDBC ResultSet.getBinaryStream method to retrieve * the reference to the datastore InputStream * @param rs The result set from the datastore * @param param The column index to find the stream. * @return Returns a StreamableObject representing the InputStream of the binary stream */ public Object getObject(Object rs, int param) { getLogger().debug("getObject..."); StreamableObject so = null; try { getLogger().debug("Get stream from resultset..."); InputStream is = ((ResultSet) rs).getBinaryStream(param); if( !((ResultSet) rs).wasNull() ) { getLogger().debug("Create new streamable object..."); so = new StreamableObject(is); } } catch (SQLException e) { throw new JDODataStoreException(LOCALISER.msg("RDBMS.Mapping.UnableToGetParam","Object", "" + param, column, e.getMessage()), e); } getLogger().debug("Return " + so + "..."); return so; } }