Unverified 提交 4d32c294 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #806 from katzyn/JDBC

Implement setBytes() and setString() with offset and len
......@@ -125,7 +125,7 @@ public class JdbcBlob extends TraceObject implements Blob {
}
/**
* [Not supported] Sets some bytes of the object.
* Sets some bytes of the object.
*
* @param pos the write position
* @param bytes the bytes to set
......@@ -136,7 +136,19 @@ public class JdbcBlob extends TraceObject implements Blob {
@Override
public int setBytes(long pos, byte[] bytes, int offset, int len)
throws SQLException {
throw unsupported("LOB update");
try {
if (isDebugEnabled()) {
debugCode("setBytes(" + pos + ", " + quoteBytes(bytes) + ", " + offset + ", " + len + ");");
}
checkClosed();
if (pos != 1) {
throw DbException.getInvalidValueException("pos", pos);
}
value = conn.createBlob(new ByteArrayInputStream(bytes, offset, len), -1);
return (int) value.getPrecision();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
......
......@@ -21,6 +21,7 @@ import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.message.DbException;
import org.h2.message.TraceObject;
import org.h2.store.RangeReader;
import org.h2.util.IOUtils;
import org.h2.util.Task;
import org.h2.value.Value;
......@@ -227,12 +228,34 @@ public class JdbcClob extends TraceObject implements NClob
}
/**
* [Not supported] Sets a substring.
* Fills the Clob. This is only supported for new, empty Clob objects that
* were created with Connection.createClob() or createNClob(). The position
* must be 1, meaning the whole Clob data is set.
*
* @param pos where to start writing (the first character is at position 1)
* @param str the string to add
* @param offset the string offset
* @param len the number of characters to read
* @return the length of the added text
*/
@Override
public int setString(long pos, String str, int offset, int len)
throws SQLException {
throw unsupported("LOB update");
try {
if (isDebugEnabled()) {
debugCode("setString(" + pos + ", " + quote(str) + ", " + offset + ", " + len + ");");
}
checkClosed();
if (pos != 1) {
throw DbException.getInvalidValueException("pos", pos);
} else if (str == null) {
throw DbException.getInvalidValueException("str", str);
}
value = conn.createClob(new RangeReader(new StringReader(str), offset, len), -1);
return (int) value.getPrecision();
} catch (Exception e) {
throw logAndConvert(e);
}
}
/**
......
......@@ -86,8 +86,6 @@ public class TestLobApi extends TestBase {
truncate(0);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).
setAsciiStream(1);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).
setString(1, "", 0, 1);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).
position("", 0);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, clob).
......@@ -96,8 +94,6 @@ public class TestLobApi extends TestBase {
Blob blob = rs.getBlob(3);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).
truncate(0);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).
setBytes(1, new byte[0], 0, 0);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).
position(new byte[1], 0);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, blob).
......@@ -237,30 +233,43 @@ public class TestLobApi extends TestBase {
prep.setInt(1, 2);
b = conn.createBlob();
b.setBytes(1, data);
assertEquals(length, b.setBytes(1, data));
prep.setBlob(2, b);
prep.execute();
prep.setInt(1, 3);
prep.setBlob(2, new ByteArrayInputStream(data));
Blob b2 = conn.createBlob();
byte[] xdata = new byte[length + 2];
System.arraycopy(data, 0, xdata, 1, length);
assertEquals(length, b2.setBytes(1, xdata, 1, length));
prep.setBlob(2, b2);
prep.execute();
prep.setInt(1, 4);
prep.setBlob(2, new ByteArrayInputStream(data));
prep.execute();
prep.setInt(1, 5);
prep.setBlob(2, new ByteArrayInputStream(data), -1);
prep.execute();
ResultSet rs;
rs = stat.executeQuery("select * from test");
rs.next();
Blob b2 = rs.getBlob(2);
assertEquals(length, b2.length());
Blob b3 = rs.getBlob(2);
assertEquals(length, b3.length());
byte[] bytes = b.getBytes(1, length);
byte[] bytes2 = b2.getBytes(1, length);
byte[] bytes2 = b3.getBytes(1, length);
assertEquals(bytes, bytes2);
rs.next();
b3 = rs.getBlob(2);
assertEquals(length, b3.length());
bytes2 = b3.getBytes(1, length);
assertEquals(bytes, bytes2);
rs.next();
b2 = rs.getBlob(2);
assertEquals(length, b2.length());
bytes2 = b2.getBytes(1, length);
b3 = rs.getBlob(2);
assertEquals(length, b3.length());
bytes2 = b3.getBytes(1, length);
assertEquals(bytes, bytes2);
while (rs.next()) {
bytes2 = rs.getBytes(2);
......@@ -311,20 +320,28 @@ public class TestLobApi extends TestBase {
NClob nc;
nc = conn.createNClob();
nc.setString(1, new String(data));
assertEquals(length, nc.setString(1, new String(data)));
prep.setInt(1, 5);
prep.setNClob(2, nc);
prep.execute();
prep.setInt(1, 5);
nc = conn.createNClob();
char[] xdata = new char[length + 2];
System.arraycopy(data, 0, xdata, 1, length);
assertEquals(length, nc.setString(1, new String(xdata), 1, length));
prep.setInt(1, 6);
prep.setNClob(2, nc);
prep.execute();
prep.setInt(1, 7);
prep.setNClob(2, new StringReader(new String(data)));
prep.execute();
prep.setInt(1, 6);
prep.setInt(1, 8);
prep.setNClob(2, new StringReader(new String(data)), -1);
prep.execute();
prep.setInt(1, 7);
prep.setInt(1, 9);
prep.setNString(2, new String(data));
prep.execute();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论