mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
testcase
This commit is contained in:
@@ -1,77 +1,77 @@
|
||||
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 {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = asciiStream.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, 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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
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 {
|
||||
byte[] buffer = new byte[BUFFER_SIZE];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = asciiStream.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, 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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,70 +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();
|
||||
}
|
||||
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
package com.alibaba.druid.support.spring;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractorAdapter;
|
||||
|
||||
public class DruidNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
|
||||
|
||||
protected Connection doGetNativeConnection(Connection con) throws SQLException {
|
||||
return (Connection) con.unwrap(Connection.class);
|
||||
}
|
||||
|
||||
public Statement getNativeStatement(Statement stmt) throws SQLException {
|
||||
return (Statement) stmt.unwrap(Statement.class);
|
||||
}
|
||||
|
||||
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
|
||||
return (PreparedStatement) ps.unwrap(PreparedStatement.class);
|
||||
}
|
||||
|
||||
public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException {
|
||||
return (CallableStatement) cs.unwrap(CallableStatement.class);
|
||||
}
|
||||
|
||||
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
|
||||
return (ResultSet) rs.unwrap(ResultSet.class);
|
||||
}
|
||||
}
|
||||
package com.alibaba.druid.support.spring;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractorAdapter;
|
||||
|
||||
public class DruidNativeJdbcExtractor extends NativeJdbcExtractorAdapter {
|
||||
|
||||
protected Connection doGetNativeConnection(Connection con) throws SQLException {
|
||||
return (Connection) con.unwrap(Connection.class);
|
||||
}
|
||||
|
||||
public Statement getNativeStatement(Statement stmt) throws SQLException {
|
||||
return (Statement) stmt.unwrap(Statement.class);
|
||||
}
|
||||
|
||||
public PreparedStatement getNativePreparedStatement(PreparedStatement ps) throws SQLException {
|
||||
return (PreparedStatement) ps.unwrap(PreparedStatement.class);
|
||||
}
|
||||
|
||||
public CallableStatement getNativeCallableStatement(CallableStatement cs) throws SQLException {
|
||||
return (CallableStatement) cs.unwrap(CallableStatement.class);
|
||||
}
|
||||
|
||||
public ResultSet getNativeResultSet(ResultSet rs) throws SQLException {
|
||||
return (ResultSet) rs.unwrap(ResultSet.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user