mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持Spring和ibatis
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@425 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user