提交 875a893f authored 作者: Thomas Mueller's avatar Thomas Mueller

ResultSet.getObject for CLOB or BLOB will return a java.sql.Clob / java.sql.Blob object.

上级 2479e6a1
......@@ -457,6 +457,20 @@ public class SysProperties {
*/
public static final int RESERVE_MEMORY = getIntSetting("h2.reserveMemory", 512 * 1024);
/**
* System property <code>h2.returnLobObjects</code>.<br />
* When true, ResultSet.getObject for CLOB or BLOB will return a
* java.sql.Clob / java.sql.Blob object. When set to false, it will return a
* java.io.Reader / java.io.InputStream.
* <p>
* In version 1.0, it is disabled by default.
* </p>
* <p>
* In version 1.1, it is enabled by default.
* </p>
*/
public static final boolean RETURN_LOB_OBJECTS = getBooleanSetting("h2.returnLobObjects", Constants.VERSION > 1.0);
/**
* System property <code>h2.reuseSpaceAfter</code> (default: 16).<br />
* Reuse space in database files after this many pages are free.
......@@ -524,6 +538,12 @@ public class SysProperties {
*/
public static final boolean SORT_NULLS_HIGH = getBooleanSetting("h2.sortNullsHigh", false);
/**
* System property <code>h2.splitFileSizeShift</code> (default: 30).<br />
* The maximum file size of a split file is 1L << x.
*/
public static final long SPLIT_FILE_SIZE_SHIFT = getIntSetting("h2.splitFileSizeShift", 20);
/**
* System property <code>h2.traceIO</code> (default: false).<br />
* Trace all I/O operations.
......
......@@ -36,6 +36,7 @@ import org.h2.message.Trace;
import org.h2.message.TraceObject;
import org.h2.result.ResultInterface;
import org.h2.util.JdbcConnectionListener;
import org.h2.util.ObjectUtils;
import org.h2.value.Value;
import org.h2.value.ValueInt;
import org.h2.value.ValueLob;
......@@ -1533,4 +1534,45 @@ public class JdbcConnection extends TraceObject implements Connection {
return getTraceObjectName() + ": url=" + url + " user=" + user;
}
/**
* Convert an object to the default Java object for the given SQL type. For
* example, LOB objects are converted to java.sql.Clob / java.sql.Blob.
*
* @param v the value
* @return the object
*/
Object convertToDefaultObject(Value v) throws SQLException {
Object o;
switch (v.getType()) {
case Value.CLOB: {
if (SysProperties.RETURN_LOB_OBJECTS) {
int id = getNextId(TraceObject.CLOB);
o = new JdbcClob(session, this, v, id);
} else {
o = v.getObject();
}
break;
}
case Value.BLOB: {
if (SysProperties.RETURN_LOB_OBJECTS) {
int id = getNextId(TraceObject.BLOB);
o = new JdbcBlob(session, this, v, id);
} else {
o = v.getObject();
}
break;
}
case Value.JAVA_OBJECT:
if (Constants.SERIALIZE_JAVA_OBJECTS) {
o = ObjectUtils.deserialize(v.getBytesNoCopy());
} else {
o = v.getObject();
}
break;
default:
o = v.getObject();
}
return o;
}
}
......@@ -34,7 +34,6 @@ import java.sql.SQLXML;
import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.engine.SessionInterface;
import org.h2.message.Message;
import org.h2.message.TraceObject;
......@@ -460,12 +459,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
try {
debugCodeCall("getObject", columnIndex);
Value v = get(columnIndex);
if (Constants.SERIALIZE_JAVA_OBJECTS) {
if (v.getType() == Value.JAVA_OBJECT) {
return ObjectUtils.deserialize(v.getBytesNoCopy());
}
}
return v.getObject();
return conn.convertToDefaultObject(v);
} catch (Exception e) {
throw logAndConvert(e);
}
......@@ -484,12 +478,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
try {
debugCodeCall("getObject", columnName);
Value v = get(columnName);
if (Constants.SERIALIZE_JAVA_OBJECTS) {
if (v.getType() == Value.JAVA_OBJECT) {
return ObjectUtils.deserialize(v.getBytesNoCopy());
}
}
return v.getObject();
return conn.convertToDefaultObject(v);
} catch (Exception e) {
throw logAndConvert(e);
}
......
......@@ -743,11 +743,19 @@ public class TestLob extends TestBase {
if (clob) {
Reader rt = rs.getCharacterStream(2);
checkReader(rt, getRandomReader(size, id), -1);
checkReader((Reader) rs.getObject(2), getRandomReader(size, id), -1);
Object obj = rs.getObject(2);
if (obj instanceof Clob) {
obj = ((Clob) obj).getCharacterStream();
}
checkReader((Reader) obj, getRandomReader(size, id), -1);
} else {
InputStream in = rs.getBinaryStream(2);
checkStream(in, getRandomStream(size, id), -1);
checkStream((InputStream) rs.getObject(2), getRandomStream(size, id), -1);
Object obj = rs.getObject(2);
if (obj instanceof Blob) {
obj = ((Blob) obj).getBinaryStream();
}
checkStream((InputStream) obj, getRandomStream(size, id), -1);
}
}
trace("select=" + (System.currentTimeMillis() - time));
......
......@@ -15,6 +15,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.test.TestBase;
import org.h2.value.DataType;
......@@ -218,8 +219,13 @@ public class TestMetaData extends TestBase {
assertEquals("java.io.InputStream", meta.getColumnClassName(1));
assertEquals("java.io.Reader", meta.getColumnClassName(2));
rs.next();
if (SysProperties.RETURN_LOB_OBJECTS) {
assertTrue(rs.getObject(1) instanceof java.sql.Blob);
assertTrue(rs.getObject(2) instanceof java.sql.Clob);
} else {
assertEquals("java.io.ByteArrayInputStream", rs.getObject(1).getClass().getName());
assertEquals("java.io.BufferedReader", rs.getObject(2).getClass().getName());
}
stat.executeUpdate("DROP TABLE t");
}
......
......@@ -26,6 +26,7 @@ import java.sql.Types;
import java.util.Calendar;
import java.util.TimeZone;
import org.h2.constant.SysProperties;
import org.h2.test.TestBase;
/**
......@@ -858,6 +859,12 @@ public class TestResultSet extends TestBase {
assertResultSetMeta(rs, 2, new String[] { "ID", "VALUE" }, new int[] { Types.INTEGER, Types.CLOB }, new int[] {
10, Integer.MAX_VALUE }, new int[] { 0, 0 });
rs.next();
Object obj = rs.getObject(2);
if (SysProperties.RETURN_LOB_OBJECTS) {
assertTrue(obj instanceof java.sql.Clob);
} else {
assertTrue(obj instanceof java.io.Reader);
}
string = rs.getString(2);
assertTrue(string != null && string.equals("Test"));
assertTrue(!rs.wasNull());
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论