001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.persistence.lobs;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.sql.Blob;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028:
029: import de.finix.contelligent.logging.LoggingService;
030:
031: public class MySQLLOBPolicy implements LOBPolicy {
032: final static org.apache.log4j.Logger log = LoggingService
033: .getLogger(MySQLLOBPolicy.class);
034:
035: /**
036: * Answer true is SELECT FOR UPDATE is necessary to for setting the blob
037: */
038: public boolean requiresSelectForUpdate() {
039: return false;
040: }
041:
042: public boolean supportsSelectIntoForBlobs() {
043: return true;
044: }
045:
046: /**
047: * Answer the String to be included in an insert statement to create an
048: * empty blob
049: */
050: public String getEmptyBlobFunction() {
051: return "null";
052: }
053:
054: /**
055: * Returns the string representation of this LOB-policy.
056: */
057: public String toString() {
058: return "MySQLDBLOBPolicy";
059: }
060:
061: /**
062: * Set the blob field in the way required by the underlying database.
063: */
064: public void setBlob(Blob blob, PreparedStatement statement,
065: int index, byte[] bytes) throws SQLException {
066: // InputStream in = new ByteArrayInputStream(bytes);
067: statement.setBytes(index, bytes);
068: }
069:
070: /**
071: * Set the blob field in the way required by the underlying database.
072: */
073: public void setBlob(Blob blob, PreparedStatement statement,
074: int index, InputStream stream, int length)
075: throws SQLException {
076: ByteArrayOutputStream out = new ByteArrayOutputStream();
077: try {
078: de.finix.contelligent.util.FileUtils.copy(stream, out);
079: } catch (IOException e) {
080: log
081: .error(
082: "setBlob() - problem copying inputStream to ByteArray",
083: e);
084: throw new SQLException(
085: "setBlob() - problem copying inputStream to ByteArray - see log file for details!");
086: }
087: byte[] bytes = out.toByteArray();
088: statement.setBytes(index, bytes);
089: }
090:
091: /**
092: * Retrieve the blob field in the way required by the underlying database.
093: */
094: public InputStream getBinaryStream(ResultSet rs, int index)
095: throws SQLException {
096: byte bytes[] = rs.getBytes(index);
097:
098: if (bytes == null) {
099: return null;
100: } else {
101: return new ByteArrayInputStream(bytes);
102: }
103: }
104: }
|