Unverified 提交 8a670816 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #801 from katzyn/PgServer

Fix NULL support in PgServer for primitive types too
...@@ -537,10 +537,16 @@ public class PgServerThread implements Runnable { ...@@ -537,10 +537,16 @@ public class PgServerThread implements Runnable {
if (text) { if (text) {
// plain text // plain text
switch (pgType) { switch (pgType) {
case PgServer.PG_TYPE_BOOL: case PgServer.PG_TYPE_BOOL: {
writeInt(1); boolean b = rs.getBoolean(column);
dataOut.writeByte(rs.getBoolean(column) ? 't' : 'f'); if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(1);
dataOut.writeByte(b ? 't' : 'f');
}
break; break;
}
default: default:
String s = rs.getString(column); String s = rs.getString(column);
if (s == null) { if (s == null) {
...@@ -554,26 +560,56 @@ public class PgServerThread implements Runnable { ...@@ -554,26 +560,56 @@ public class PgServerThread implements Runnable {
} else { } else {
// binary // binary
switch (pgType) { switch (pgType) {
case PgServer.PG_TYPE_INT2: case PgServer.PG_TYPE_INT2: {
writeInt(2); short s = rs.getShort(column);
writeShort(rs.getShort(column)); if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(2);
writeShort(s);
}
break; break;
case PgServer.PG_TYPE_INT4: }
writeInt(4); case PgServer.PG_TYPE_INT4: {
writeInt(rs.getInt(column)); int i = rs.getInt(column);
if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(4);
writeInt(i);
}
break; break;
case PgServer.PG_TYPE_INT8: }
writeInt(8); case PgServer.PG_TYPE_INT8: {
dataOut.writeLong(rs.getLong(column)); long l = rs.getLong(column);
if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(8);
dataOut.writeLong(l);
}
break; break;
case PgServer.PG_TYPE_FLOAT4: }
writeInt(4); case PgServer.PG_TYPE_FLOAT4: {
dataOut.writeFloat(rs.getFloat(column)); float f = rs.getFloat(column);
if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(4);
dataOut.writeFloat(f);
}
break; break;
case PgServer.PG_TYPE_FLOAT8: }
writeInt(8); case PgServer.PG_TYPE_FLOAT8: {
dataOut.writeDouble(rs.getDouble(column)); double d = rs.getDouble(column);
if (rs.wasNull()) {
writeInt(-1);
} else {
writeInt(8);
dataOut.writeDouble(d);
}
break; break;
}
case PgServer.PG_TYPE_BYTEA: { case PgServer.PG_TYPE_BYTEA: {
byte[] data = rs.getBytes(column); byte[] data = rs.getBytes(column);
if (data == null) { if (data == null) {
...@@ -612,8 +648,7 @@ public class PgServerThread implements Runnable { ...@@ -612,8 +648,7 @@ public class PgServerThread implements Runnable {
m /= 1000; m /= 1000;
m = Double.doubleToLongBits(m); m = Double.doubleToLongBits(m);
} }
writeInt((int) (m >>> 32)); dataOut.writeLong(m);
writeInt((int) m);
} }
break; break;
} }
...@@ -637,8 +672,7 @@ public class PgServerThread implements Runnable { ...@@ -637,8 +672,7 @@ public class PgServerThread implements Runnable {
// double format // double format
m = Double.doubleToLongBits(m + nanos * 0.000000001); m = Double.doubleToLongBits(m + nanos * 0.000000001);
} }
writeInt((int) (m >>> 32)); dataOut.writeLong(m);
writeInt((int) m);
} }
break; break;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
package org.h2.test.unit; package org.h2.test.unit;
import java.math.BigDecimal;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.Date; import java.sql.Date;
...@@ -410,10 +411,10 @@ public class TestPgServer extends TestBase { ...@@ -410,10 +411,10 @@ public class TestPgServer extends TestBase {
"create table test(x1 varchar, x2 int, " + "create table test(x1 varchar, x2 int, " +
"x3 smallint, x4 bigint, x5 double, x6 float, " + "x3 smallint, x4 bigint, x5 double, x6 float, " +
"x7 real, x8 boolean, x9 char, x10 bytea, " + "x7 real, x8 boolean, x9 char, x10 bytea, " +
"x11 date, x12 time, x13 timestamp)"); "x11 date, x12 time, x13 timestamp, x14 numeric)");
PreparedStatement ps = conn.prepareStatement( PreparedStatement ps = conn.prepareStatement(
"insert into test values (?,?,?,?,?,?,?,?,?,?,?,?,?)"); "insert into test values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, "test"); ps.setString(1, "test");
ps.setInt(2, 12345678); ps.setInt(2, 12345678);
ps.setShort(3, (short) 12345); ps.setShort(3, (short) 12345);
...@@ -427,6 +428,11 @@ public class TestPgServer extends TestBase { ...@@ -427,6 +428,11 @@ public class TestPgServer extends TestBase {
ps.setDate(11, Date.valueOf("2015-01-31")); ps.setDate(11, Date.valueOf("2015-01-31"));
ps.setTime(12, Time.valueOf("20:11:15")); ps.setTime(12, Time.valueOf("20:11:15"));
ps.setTimestamp(13, Timestamp.valueOf("2001-10-30 14:16:10.111")); ps.setTimestamp(13, Timestamp.valueOf("2001-10-30 14:16:10.111"));
ps.setBigDecimal(14, new BigDecimal("12345678901234567890.12345"));
ps.execute();
for (int i = 1; i <= 14; i++) {
ps.setNull(i, Types.NULL);
}
ps.execute(); ps.execute();
ResultSet rs = stat.executeQuery("select * from test"); ResultSet rs = stat.executeQuery("select * from test");
...@@ -445,6 +451,12 @@ public class TestPgServer extends TestBase { ...@@ -445,6 +451,12 @@ public class TestPgServer extends TestBase {
assertEquals(Date.valueOf("2015-01-31"), rs.getDate(11)); assertEquals(Date.valueOf("2015-01-31"), rs.getDate(11));
assertEquals(Time.valueOf("20:11:15"), rs.getTime(12)); assertEquals(Time.valueOf("20:11:15"), rs.getTime(12));
assertEquals(Timestamp.valueOf("2001-10-30 14:16:10.111"), rs.getTimestamp(13)); assertEquals(Timestamp.valueOf("2001-10-30 14:16:10.111"), rs.getTimestamp(13));
assertEquals(new BigDecimal("12345678901234567890.12345"), rs.getBigDecimal(14));
assertTrue(rs.next());
for (int i = 1; i <= 14; i++) {
assertNull(rs.getObject(i));
}
assertFalse(rs.next());
conn.close(); conn.close();
} finally { } finally {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论