01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.persistence.lobs;
19:
20: import java.io.*;
21: import java.sql.*;
22:
23: public class WeblogicOracleLOBPolicy implements LOBPolicy {
24:
25: /**
26: * Answer true is SELECT FOR UPDATE is necessary to for setting the blob
27: */
28: public boolean requiresSelectForUpdate() {
29: return true;
30: }
31:
32: public boolean supportsSelectIntoForBlobs() {
33: return true;
34: }
35:
36: /** Answer the String to be included in an insert statement to create an empty blob
37: */
38: public String getEmptyBlobFunction() {
39: return "EMPTY_BLOB()";
40: }
41:
42: /**
43: * Returns the string representation of this LOB-policy.
44: */
45: public String toString() {
46: return "WeblogicOracleLOBPolicy";
47: }
48:
49: /** Set the blob field in the way required by the underlying database.
50: */
51: public void setBlob(Blob blob, PreparedStatement statement,
52: int index, byte[] bytes) throws SQLException {
53: weblogic.jdbc.vendor.oracle.OracleThinBlob oraBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) blob;
54: oraBlob.putBytes(1, bytes);
55: statement.setBlob(index, blob);
56: }
57:
58: /**
59: * Set the blob field in the way required by the underlying database.
60: */
61: public void setBlob(Blob blob, PreparedStatement statement,
62: int index, InputStream stream, int length)
63: throws SQLException {
64: weblogic.jdbc.vendor.oracle.OracleThinBlob oraBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) blob;
65: OutputStream out = oraBlob.getBinaryOutputStream();
66: byte[] buffer = new byte[4096];
67: int r = 0;
68: try {
69: while ((r = stream.read(buffer)) != -1) {
70: out.write(buffer, 0, r);
71: }
72: } catch (IOException e) {
73: throw new SQLException("Failed to stream binary data: " + e);
74: } finally {
75: try {
76: out.close();
77: } catch (IOException e) {
78: }
79: ;
80: }
81: statement.setBlob(index, blob);
82: }
83:
84: /**
85: * Retrieve the blob field in the way required by the underlying database.
86: */
87: public InputStream getBinaryStream(ResultSet rs, int index)
88: throws SQLException {
89: Blob blob = rs.getBlob(index);
90:
91: if (blob == null) {
92: return null;
93: } else {
94: return blob.getBinaryStream();
95: }
96: }
97: }
|