01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency.type;
10:
11: import com.completex.objective.components.persistency.PersistentObject;
12: import com.completex.objective.components.persistency.core.DatabasePolicy;
13:
14: import java.sql.*;
15:
16: /**
17: * @author Gennady Krizhevsky
18: */
19: public class BlobTypeHandler extends DefaultTypeHandler {
20:
21: protected Object readValue(ResultSet resultSet, int index,
22: DatabasePolicy databasePolicy) throws SQLException {
23: if (databasePolicy.supportsBlob()) {
24: return resultSet.getBlob(index);
25: } else {
26: return resultSet.getBytes(index);
27: }
28: }
29:
30: protected Object readValue(ResultSet resultSet, String key,
31: DatabasePolicy databasePolicy) throws SQLException {
32: if (databasePolicy.supportsBlob()) {
33: return resultSet.getBlob(key);
34: } else {
35: return resultSet.getBytes(key);
36: }
37: }
38:
39: /**
40: * This bind only makes sense for inserts and updates since there is no meaninful
41: * LOB/binary binding can be performed for selects. If the JDBC driver suppors Blobs
42: * then the passed value should have Blob type. ODAL BlobImpl implementation can be used.
43: *
44: * @see BlobImpl
45: * @see Blob
46: * @param statement PreparedStatement
47: * @param index column index - 1-based
48: * @param value
49: * @param databasePolicy
50: * @param postProcessings
51: * @param persistentObject
52: * @param fieldIndex
53: * @throws SQLException
54: */
55: public void handleBind(PreparedStatement statement, int index,
56: Object value, DatabasePolicy databasePolicy,
57: LobPostProcessings postProcessings,
58: PersistentObject persistentObject, int fieldIndex)
59: throws SQLException {
60: if (value instanceof Blob) {
61: Blob blob = (Blob) value;
62: if (databasePolicy.useTwoStepBinaryUpdate()) {
63: handleBind(statement, index, MINIMAL_BYTE_ARRAY);
64: postProcessings.add(persistentObject, fieldIndex, blob
65: .getBinaryStream());
66: } else {
67: handleBind(statement, index, blob);
68: }
69: } else {
70: handleBind(statement, index, value);
71: }
72: }
73:
74: }
|