diff --git a/src/main/java/com/alibaba/druid/proxy/jdbc/ClobProxyImpl.java b/src/main/java/com/alibaba/druid/proxy/jdbc/ClobProxyImpl.java index ffb8c207e3..9d3b061474 100644 --- a/src/main/java/com/alibaba/druid/proxy/jdbc/ClobProxyImpl.java +++ b/src/main/java/com/alibaba/druid/proxy/jdbc/ClobProxyImpl.java @@ -118,5 +118,9 @@ public class ClobProxyImpl implements ClobProxy { public void truncate(long len) throws SQLException { createChain().clob_truncate(this, len); } + + public String toString() { + return clob.toString(); + } } diff --git a/src/main/java/com/alibaba/druid/support/spring/DruidLobCreator.java b/src/main/java/com/alibaba/druid/support/spring/DruidLobCreator.java new file mode 100644 index 0000000000..0f8f75fec0 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/spring/DruidLobCreator.java @@ -0,0 +1,76 @@ +package com.alibaba.druid.support.spring; + +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.springframework.jdbc.support.lob.LobCreator; + +import com.alibaba.druid.util.JdbcUtils; + +public class DruidLobCreator implements LobCreator { + + @Override + public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content) throws SQLException { + Blob blob = ps.getConnection().createBlob(); + blob.setBytes(1, content); + ps.setBlob(paramIndex, blob); + } + + @Override + public void setBlobAsBinaryStream(PreparedStatement ps, int paramIndex, InputStream contentStream, int contentLength) throws SQLException { + ps.setBlob(paramIndex, contentStream, contentLength); + } + + @Override + public void setClobAsString(PreparedStatement ps, int paramIndex, String content) throws SQLException { + Clob clob = ps.getConnection().createClob(); + clob.setString(1, content); + ps.setClob(paramIndex, clob); + } + + @Override + public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength) throws SQLException { + if (asciiStream != null) { + Clob clob = ps.getConnection().createClob(); + + OutputStream out = clob.setAsciiStream(1); + + final int BUFFER_SIZE = 4096; + try { + int byteCount = 0; + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead = -1; + while ((bytesRead = asciiStream.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + byteCount += bytesRead; + } + out.flush(); + } catch (Exception e) { + throw new SQLException("setClob error", e); + } finally { + JdbcUtils.close(asciiStream); + JdbcUtils.close(out); + } + + ps.setClob(paramIndex, clob); + } else { + ps.setClob(paramIndex, (Clob) null); + } + } + + @Override + public void setClobAsCharacterStream(PreparedStatement ps, int paramIndex, Reader characterStream, int contentLength) throws SQLException { + ps.setClob(paramIndex, characterStream, contentLength); + } + + @Override + public void close() { + + } + +} diff --git a/src/main/java/com/alibaba/druid/support/spring/DruidLobHandler.java b/src/main/java/com/alibaba/druid/support/spring/DruidLobHandler.java new file mode 100644 index 0000000000..52361d04f0 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/spring/DruidLobHandler.java @@ -0,0 +1,70 @@ +package com.alibaba.druid.support.spring; + +import java.io.InputStream; +import java.io.Reader; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.springframework.jdbc.support.lob.AbstractLobHandler; +import org.springframework.jdbc.support.lob.LobCreator; + +public class DruidLobHandler extends AbstractLobHandler { + + public byte[] getBlobAsBytes(ResultSet rs, int columnIndex) throws SQLException { + Blob blob = rs.getBlob(columnIndex); + + if (blob == null) { + return null; + } + + return blob.getBytes(1, (int) blob.length()); + } + + public InputStream getBlobAsBinaryStream(ResultSet rs, int columnIndex) throws SQLException { + Blob blob = rs.getBlob(columnIndex); + + if (blob == null) { + return null; + } + + return blob.getBinaryStream(); + } + + public String getClobAsString(ResultSet rs, int columnIndex) throws SQLException { + Clob clob = rs.getClob(columnIndex); + + if (clob == null) { + return null; + } + + return clob.getSubString(1, (int) clob.length()); + } + + public InputStream getClobAsAsciiStream(ResultSet rs, int columnIndex) throws SQLException { + Clob clob = rs.getClob(columnIndex); + + if (clob == null) { + return null; + } + + return clob.getAsciiStream(); + } + + public Reader getClobAsCharacterStream(ResultSet rs, int columnIndex) throws SQLException { + Clob clob = rs.getClob(columnIndex); + + if (clob == null) { + return null; + } + + return clob.getCharacterStream(); + } + + @Override + public LobCreator getLobCreator() { + return new DruidLobCreator(); + } + +}