001: //$Id: BlobType.java 7644 2005-07-25 06:53:09Z oneovthafew $
002: package org.hibernate.type;
003:
004: import java.io.Serializable;
005: import java.sql.Blob;
006: import java.sql.PreparedStatement;
007: import java.sql.ResultSet;
008: import java.sql.SQLException;
009: import java.sql.Types;
010: import java.util.Map;
011:
012: import org.dom4j.Node;
013: import org.hibernate.EntityMode;
014: import org.hibernate.HibernateException;
015: import org.hibernate.MappingException;
016: import org.hibernate.engine.Mapping;
017: import org.hibernate.engine.SessionFactoryImplementor;
018: import org.hibernate.engine.SessionImplementor;
019: import org.hibernate.lob.BlobImpl;
020: import org.hibernate.lob.SerializableBlob;
021: import org.hibernate.util.ArrayHelper;
022:
023: /**
024: * <tt>blob</tt>: A type that maps an SQL BLOB to a java.sql.Blob.
025: * @author Gavin King
026: */
027: public class BlobType extends AbstractType {
028:
029: public void set(PreparedStatement st, Object value, int index,
030: SessionImplementor session) throws HibernateException,
031: SQLException {
032:
033: if (value == null) {
034: st.setNull(index, Types.BLOB);
035: } else {
036:
037: if (value instanceof SerializableBlob) {
038: value = ((SerializableBlob) value).getWrappedBlob();
039: }
040:
041: final boolean useInputStream = session.getFactory()
042: .getDialect().useInputStreamToInsertBlob()
043: && (value instanceof BlobImpl);
044:
045: if (useInputStream) {
046: BlobImpl blob = (BlobImpl) value;
047: st.setBinaryStream(index, blob.getBinaryStream(),
048: (int) blob.length());
049: } else {
050: st.setBlob(index, (Blob) value);
051: }
052:
053: }
054:
055: }
056:
057: public Object get(ResultSet rs, String name)
058: throws HibernateException, SQLException {
059: Blob value = rs.getBlob(name);
060: return rs.wasNull() ? null : new SerializableBlob(value);
061: }
062:
063: public Class getReturnedClass() {
064: return Blob.class;
065: }
066:
067: public boolean isEqual(Object x, Object y, EntityMode entityMode) {
068: return x == y;
069: }
070:
071: public int getHashCode(Object x, EntityMode entityMode) {
072: return System.identityHashCode(x);
073: }
074:
075: public int compare(Object x, Object y, EntityMode entityMode) {
076: return 0; //lobs cannot be compared
077: }
078:
079: public String getName() {
080: return "blob";
081: }
082:
083: public Serializable disassemble(Object value,
084: SessionImplementor session, Object owner)
085: throws HibernateException {
086: throw new UnsupportedOperationException(
087: "Blobs are not cacheable");
088: }
089:
090: public Object deepCopy(Object value, EntityMode entityMode,
091: SessionFactoryImplementor factory) {
092: return value;
093: }
094:
095: public Object fromXMLNode(Node xml, Mapping factory) {
096: throw new UnsupportedOperationException("todo");
097: }
098:
099: public int getColumnSpan(Mapping mapping) {
100: return 1;
101: }
102:
103: public boolean isMutable() {
104: return false;
105: }
106:
107: public Object nullSafeGet(ResultSet rs, String name,
108: SessionImplementor session, Object owner)
109: throws HibernateException, SQLException {
110: return get(rs, name);
111: }
112:
113: public Object nullSafeGet(ResultSet rs, String[] names,
114: SessionImplementor session, Object owner)
115: throws HibernateException, SQLException {
116: return get(rs, names[0]);
117: }
118:
119: public void nullSafeSet(PreparedStatement st, Object value,
120: int index, boolean[] settable, SessionImplementor session)
121: throws HibernateException, SQLException {
122: if (settable[0])
123: set(st, value, index, session);
124: }
125:
126: public void nullSafeSet(PreparedStatement st, Object value,
127: int index, SessionImplementor session)
128: throws HibernateException, SQLException {
129: set(st, value, index, session);
130: }
131:
132: public Object replace(Object original, Object target,
133: SessionImplementor session, Object owner, Map copyCache)
134: throws HibernateException {
135: //Blobs are ignored by merge()
136: return target;
137: }
138:
139: public int[] sqlTypes(Mapping mapping) throws MappingException {
140: return new int[] { Types.BLOB };
141: }
142:
143: public void setToXMLNode(Node node, Object value,
144: SessionFactoryImplementor factory) {
145: throw new UnsupportedOperationException("todo");
146: }
147:
148: public String toLoggableString(Object value,
149: SessionFactoryImplementor factory)
150: throws HibernateException {
151: return value == null ? "null" : value.toString();
152: }
153:
154: public boolean[] toColumnNullness(Object value, Mapping mapping) {
155: return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
156: }
157:
158: public boolean isDirty(Object old, Object current,
159: boolean[] checkable, SessionImplementor session)
160: throws HibernateException {
161: return checkable[0] && isDirty(old, current, session);
162: }
163:
164: }
|