提交 c2bcac16 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Make SimpleResultSet.getBoolean() more compatible with Value.convertTo()

上级 7e4c81fe
...@@ -8,6 +8,7 @@ package org.h2.tools; ...@@ -8,6 +8,7 @@ package org.h2.tools;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL; import java.net.URL;
import java.sql.Array; import java.sql.Array;
import java.sql.Blob; import java.sql.Blob;
...@@ -478,10 +479,26 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData, ...@@ -478,10 +479,26 @@ public class SimpleResultSet implements ResultSet, ResultSetMetaData,
@Override @Override
public boolean getBoolean(int columnIndex) throws SQLException { public boolean getBoolean(int columnIndex) throws SQLException {
Object o = get(columnIndex); Object o = get(columnIndex);
if (o != null && !(o instanceof Boolean)) { if (o == null) {
o = Boolean.valueOf(o.toString()); return false;
}
if (o instanceof Boolean) {
return (Boolean) o;
}
if (o instanceof Number) {
Number n = (Number) o;
if (n instanceof Double || n instanceof Float) {
return n.doubleValue() != 0;
}
if (n instanceof BigDecimal) {
return ((BigDecimal) n).signum() != 0;
}
if (n instanceof BigInteger) {
return ((BigInteger) n).signum() != 0;
}
return n.longValue() != 0;
} }
return o == null ? false : ((Boolean) o).booleanValue(); return Boolean.parseBoolean(o.toString());
} }
/** /**
......
...@@ -18,6 +18,7 @@ import java.io.Writer; ...@@ -18,6 +18,7 @@ import java.io.Writer;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
...@@ -257,6 +258,8 @@ public class TestTools extends TestBase { ...@@ -257,6 +258,8 @@ public class TestTools extends TestBase {
Clob clob = new SimpleClob("Hello World"); Clob clob = new SimpleClob("Hello World");
Blob blob = new SimpleBlob(new byte[]{(byte) 1, (byte) 2}); Blob blob = new SimpleBlob(new byte[]{(byte) 1, (byte) 2});
rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob); rs.addRow(1, b, true, d, "10.3", Math.PI, "-3", a, t, ts, clob, blob);
rs.addRow(BigInteger.ONE, null, true, null, BigDecimal.ONE, 1d, null, null, null, null, null);
rs.addRow(BigInteger.ZERO, null, false, null, BigDecimal.ZERO, 0d, null, null, null, null, null);
rs.addRow(null, null, null, null, null, null, null, null, null, null, null); rs.addRow(null, null, null, null, null, null, null, null, null, null, null);
rs.next(); rs.next();
...@@ -270,6 +273,7 @@ public class TestTools extends TestBase { ...@@ -270,6 +273,7 @@ public class TestTools extends TestBase {
assertEquals((short) 1, rs.getShort("a")); assertEquals((short) 1, rs.getShort("a"));
assertTrue(rs.getObject(1).getClass() == Integer.class); assertTrue(rs.getObject(1).getClass() == Integer.class);
assertTrue(rs.getObject("a").getClass() == Integer.class); assertTrue(rs.getObject("a").getClass() == Integer.class);
assertTrue(rs.getBoolean(1));
assertEquals(b, rs.getBytes(2)); assertEquals(b, rs.getBytes(2));
assertEquals(b, rs.getBytes("b")); assertEquals(b, rs.getBytes("b"));
...@@ -288,6 +292,7 @@ public class TestTools extends TestBase { ...@@ -288,6 +292,7 @@ public class TestTools extends TestBase {
assertTrue(Math.PI == rs.getDouble("f")); assertTrue(Math.PI == rs.getDouble("f"));
assertTrue((float) Math.PI == rs.getFloat(6)); assertTrue((float) Math.PI == rs.getFloat(6));
assertTrue((float) Math.PI == rs.getFloat("f")); assertTrue((float) Math.PI == rs.getFloat("f"));
assertTrue(rs.getBoolean(6));
assertEquals(-3, rs.getInt(7)); assertEquals(-3, rs.getInt(7));
assertEquals(-3, rs.getByte(7)); assertEquals(-3, rs.getByte(7));
...@@ -326,6 +331,20 @@ public class TestTools extends TestBase { ...@@ -326,6 +331,20 @@ public class TestTools extends TestBase {
rs.next(); rs.next();
assertTrue(rs.getBoolean(1));
assertTrue(rs.getBoolean(3));
assertTrue(rs.getBoolean(5));
assertTrue(rs.getBoolean(6));
rs.next();
assertFalse(rs.getBoolean(1));
assertFalse(rs.getBoolean(3));
assertFalse(rs.getBoolean(5));
assertFalse(rs.getBoolean(6));
rs.next();
assertEquals(0, rs.getLong(1)); assertEquals(0, rs.getLong(1));
assertTrue(rs.wasNull()); assertTrue(rs.wasNull());
assertEquals(null, rs.getBytes(2)); assertEquals(null, rs.getBytes(2));
...@@ -454,6 +473,8 @@ public class TestTools extends TestBase { ...@@ -454,6 +473,8 @@ public class TestTools extends TestBase {
assertFalse(rs.isClosed()); assertFalse(rs.isClosed());
assertEquals(1, rs.getRow()); assertEquals(1, rs.getRow());
assertTrue(rs.next()); assertTrue(rs.next());
assertTrue(rs.next());
assertTrue(rs.next());
assertFalse(rs.next()); assertFalse(rs.next());
assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs). assertThrows(ErrorCode.NO_DATA_AVAILABLE, (ResultSet) rs).
getInt(1); getInt(1);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论