!285 【回合】处理uint类型与mysql不一致问题

Merge pull request !285 from zhangtingtingting/cherry-pick-1724125130
This commit is contained in:
opengauss_bot
2024-08-20 07:45:41 +00:00
committed by Gitee
4 changed files with 316 additions and 276 deletions

View File

@ -256,118 +256,122 @@ public class PgResultSet implements ResultSet, org.postgresql.PGRefCursorResultS
return getURL(findColumn(columnName)); return getURL(findColumn(columnName));
} }
protected Object internalGetObject(int columnIndex, Field field) throws SQLException { protected Object internalGetObject(int columnIndex, Field field) throws SQLException {
switch (getSQLType(columnIndex)) { switch (getSQLType(columnIndex)) {
case Types.BOOLEAN: case Types.BOOLEAN:
return getBoolean(columnIndex); return getBoolean(columnIndex);
case Types.BIT: case Types.BIT:
return getBit(columnIndex); return getBit(columnIndex);
case Types.SQLXML: case Types.SQLXML:
return getSQLXML(columnIndex); return getSQLXML(columnIndex);
case Types.TINYINT: case Types.TINYINT:
case Types.SMALLINT: case Types.SMALLINT:
case Types.INTEGER: case Types.INTEGER:
return getInt(columnIndex); if (field.getPGType().equals("uint4")) {
case Types.BIGINT: return getLong(columnIndex);
return getLong(columnIndex); }
case TypeInfoCache.bIntegerType: return getInt(columnIndex);
return getBigInteger(columnIndex); case Types.BIGINT:
case Types.NUMERIC: if (field.getPGType().equals("uint8")) {
case Types.DECIMAL: return getBigInteger(columnIndex);
int scale; }
if (field.getMod() == -1) { return getLong(columnIndex);
return getBigDecimal(columnIndex, -1); case Types.NUMERIC:
} else { case Types.DECIMAL:
scale = (short)((field.getMod() - 4) & 0xffff); int scale;
return getBigDecimal(columnIndex, (Math.max(scale, -1))); if (field.getMod() == -1) {
return getBigDecimal(columnIndex, -1);
} else {
scale = (short) ((field.getMod() - 4) & 0xffff);
return getBigDecimal(columnIndex, (Math.max(scale, -1)));
}
case Types.REAL:
return getFloat(columnIndex);
case Types.FLOAT:
case Types.DOUBLE:
return getDouble(columnIndex);
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return getString(columnIndex);
case Types.DATE:
return getDate(columnIndex);
case Types.TIME:
return getTime(columnIndex);
case Types.TIMESTAMP:
return getTimestamp(columnIndex, null);
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return getBytes(columnIndex);
case Types.ARRAY:
return getArray(columnIndex);
case Types.CLOB:
return getClob(columnIndex);
case Types.BLOB:
return getBlob(columnIndex);
case Types.STRUCT:
return getStruct(columnIndex);
default:
String type = getPGType(columnIndex);
// if the backend doesn't know the type then coerce to String
if (type.equals("unknown")) {
return getString(columnIndex);
}
if (type.equals("uuid")) {
if (isBinary(columnIndex)) {
return getUUID(this_row[columnIndex - 1]);
}
return getUUID(getString(columnIndex));
}
// Specialized support for ref cursors is neater.
if (type.equals("refcursor")) {
// Fetch all results.
String cursorName = getString(columnIndex);
StringBuilder sb = new StringBuilder("FETCH ALL IN ");
Utils.escapeIdentifier(sb, cursorName);
// nb: no BEGIN triggered here. This is fine. If someone
// committed, and the cursor was not holdable (closing the
// cursor), we avoid starting a new xact and promptly causing
// it to fail. If the cursor *was* holdable, we don't want a
// new xact anyway since holdable cursor state isn't affected
// by xact boundaries. If our caller didn't commit at all, or
// autocommit was on, then we wouldn't issue a BEGIN anyway.
//
// We take the scrollability from the statement, but until
// we have updatable cursors it must be readonly.
ResultSet rs =
connection.execSQLQuery(sb.toString(), resultsettype, ResultSet.CONCUR_READ_ONLY);
//
// In long running transactions these backend cursors take up memory space
// we could close in rs.close(), but if the transaction is closed before the result set,
// then
// the cursor no longer exists
sb.setLength(0);
sb.append("CLOSE ");
Utils.escapeIdentifier(sb, cursorName);
connection.execSQLUpdate(sb.toString());
((PgResultSet) rs).setRefCursor(cursorName);
return rs;
}
if ("hstore".equals(type)) {
if (isBinary(columnIndex)) {
return HStoreConverter.fromBytes(this_row[columnIndex - 1], connection.getEncoding());
}
return HStoreConverter.fromString(getString(columnIndex));
}
// Caller determines what to do (JDBC3 overrides in this case)
return null;
} }
case Types.REAL:
return getFloat(columnIndex);
case Types.FLOAT:
case Types.DOUBLE:
return getDouble(columnIndex);
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return getString(columnIndex);
case Types.DATE:
return getDate(columnIndex);
case Types.TIME:
return getTime(columnIndex);
case Types.TIMESTAMP:
return getTimestamp(columnIndex, null);
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return getBytes(columnIndex);
case Types.ARRAY:
return getArray(columnIndex);
case Types.CLOB:
return getClob(columnIndex);
case Types.BLOB:
return getBlob(columnIndex);
case Types.STRUCT:
return getStruct(columnIndex);
default:
String type = getPGType(columnIndex);
// if the backend doesn't know the type then coerce to String
if (type.equals("unknown")) {
return getString(columnIndex);
}
if (type.equals("uuid")) {
if (isBinary(columnIndex)) {
return getUUID(this_row[columnIndex - 1]);
}
return getUUID(getString(columnIndex));
}
// Specialized support for ref cursors is neater.
if (type.equals("refcursor")) {
// Fetch all results.
String cursorName = getString(columnIndex);
StringBuilder sb = new StringBuilder("FETCH ALL IN ");
Utils.escapeIdentifier(sb, cursorName);
// nb: no BEGIN triggered here. This is fine. If someone
// committed, and the cursor was not holdable (closing the
// cursor), we avoid starting a new xact and promptly causing
// it to fail. If the cursor *was* holdable, we don't want a
// new xact anyway since holdable cursor state isn't affected
// by xact boundaries. If our caller didn't commit at all, or
// autocommit was on, then we wouldn't issue a BEGIN anyway.
//
// We take the scrollability from the statement, but until
// we have updatable cursors it must be readonly.
ResultSet rs =
connection.execSQLQuery(sb.toString(), resultsettype, ResultSet.CONCUR_READ_ONLY);
//
// In long running transactions these backend cursors take up memory space
// we could close in rs.close(), but if the transaction is closed before the result set,
// then
// the cursor no longer exists
sb.setLength(0);
sb.append("CLOSE ");
Utils.escapeIdentifier(sb, cursorName);
connection.execSQLUpdate(sb.toString());
((PgResultSet) rs).setRefCursor(cursorName);
return rs;
}
if ("hstore".equals(type)) {
if (isBinary(columnIndex)) {
return HStoreConverter.fromBytes(this_row[columnIndex - 1], connection.getEncoding());
}
return HStoreConverter.fromString(getString(columnIndex));
}
// Caller determines what to do (JDBC3 overrides in this case)
return null;
} }
}
/** /**
* Get the PGStruct object based on parameter index * Get the PGStruct object based on parameter index

View File

@ -68,8 +68,6 @@ public class TypeInfoCache implements TypeInfo {
private static ConcurrentHashMap<Integer, String> pgTypes = new ConcurrentHashMap<>(); private static ConcurrentHashMap<Integer, String> pgTypes = new ConcurrentHashMap<>();
public static final int bIntegerType = 1324;
// SELECT LENGTH(pow(10::numeric,131071)); 131071 = 2^17-1 // SELECT LENGTH(pow(10::numeric,131071)); 131071 = 2^17-1
public static final int NUMERIC_MAX_DISPLAYSIZE = 131089; public static final int NUMERIC_MAX_DISPLAYSIZE = 131089;
@ -77,48 +75,48 @@ public class TypeInfoCache implements TypeInfo {
// openGauss accepts float(p) as numeric Type and the scale is -32768(PG_INT16_MIN) // openGauss accepts float(p) as numeric Type and the scale is -32768(PG_INT16_MIN)
public static final int FLOATSCALE = -32768; public static final int FLOATSCALE = -32768;
// basic pg types info: // basic pg types info:
// 0 - type name // 0 - type name
// 1 - type oid // 1 - type oid
// 2 - sql type // 2 - sql type
// 3 - java class // 3 - java class
// 4 - array type oid // 4 - array type oid
private static final Object[][] types = { private static final Object[][] types = {
{"int1", Oid.INT1, Types.TINYINT, "java.lang.Integer", Oid.INT1_ARRAY}, {"int1", Oid.INT1, Types.TINYINT, "java.lang.Integer", Oid.INT1_ARRAY},
{"int2", Oid.INT2, Types.SMALLINT, "java.lang.Integer", Oid.INT2_ARRAY}, {"int2", Oid.INT2, Types.SMALLINT, "java.lang.Integer", Oid.INT2_ARRAY},
{"int4", Oid.INT4, Types.INTEGER, "java.lang.Integer", Oid.INT4_ARRAY}, {"int4", Oid.INT4, Types.INTEGER, "java.lang.Integer", Oid.INT4_ARRAY},
{"oid", Oid.OID, Types.BIGINT, "java.lang.Long", Oid.OID_ARRAY}, {"oid", Oid.OID, Types.BIGINT, "java.lang.Long", Oid.OID_ARRAY},
{"int8", Oid.INT8, Types.BIGINT, "java.lang.Long", Oid.INT8_ARRAY}, {"int8", Oid.INT8, Types.BIGINT, "java.lang.Long", Oid.INT8_ARRAY},
{"uint1", Oid.UINT1, Types.SMALLINT, "java.lang.Integer", Oid.UINT1_ARRAY}, {"uint1", Oid.UINT1, Types.TINYINT, "java.lang.Integer", Oid.UINT1_ARRAY},
{"uint2", Oid.UINT2, Types.INTEGER, "java.lang.Integer", Oid.UINT2_ARRAY}, {"uint2", Oid.UINT2, Types.SMALLINT, "java.lang.Integer", Oid.UINT2_ARRAY},
{"uint4", Oid.UINT4, Types.BIGINT, "java.lang.Long", Oid.UINT4_ARRAY}, {"uint4", Oid.UINT4, Types.INTEGER, "java.lang.Integer", Oid.UINT4_ARRAY},
{"uint8", Oid.UINT8, bIntegerType, "java.math.BigInteger", Oid.UINT8_ARRAY}, {"uint8", Oid.UINT8, Types.BIGINT, "java.lang.Long", Oid.UINT8_ARRAY},
{"money", Oid.MONEY, Types.DOUBLE, "java.lang.Double", Oid.MONEY_ARRAY}, {"money", Oid.MONEY, Types.DOUBLE, "java.lang.Double", Oid.MONEY_ARRAY},
{"numeric", Oid.NUMERIC, Types.NUMERIC, "java.math.BigDecimal", Oid.NUMERIC_ARRAY}, {"numeric", Oid.NUMERIC, Types.NUMERIC, "java.math.BigDecimal", Oid.NUMERIC_ARRAY},
{"float4", Oid.FLOAT4, Types.REAL, "java.lang.Float", Oid.FLOAT4_ARRAY}, {"float4", Oid.FLOAT4, Types.REAL, "java.lang.Float", Oid.FLOAT4_ARRAY},
{"float8", Oid.FLOAT8, Types.DOUBLE, "java.lang.Double", Oid.FLOAT8_ARRAY}, {"float8", Oid.FLOAT8, Types.DOUBLE, "java.lang.Double", Oid.FLOAT8_ARRAY},
{"char", Oid.CHAR, Types.CHAR, "java.lang.String", Oid.CHAR_ARRAY}, {"char", Oid.CHAR, Types.CHAR, "java.lang.String", Oid.CHAR_ARRAY},
{"bpchar", Oid.BPCHAR, Types.CHAR, "java.lang.String", Oid.BPCHAR_ARRAY}, {"bpchar", Oid.BPCHAR, Types.CHAR, "java.lang.String", Oid.BPCHAR_ARRAY},
{"varchar", Oid.VARCHAR, Types.VARCHAR, "java.lang.String", Oid.VARCHAR_ARRAY}, {"varchar", Oid.VARCHAR, Types.VARCHAR, "java.lang.String", Oid.VARCHAR_ARRAY},
{"text", Oid.TEXT, Types.VARCHAR, "java.lang.String", Oid.TEXT_ARRAY}, {"text", Oid.TEXT, Types.VARCHAR, "java.lang.String", Oid.TEXT_ARRAY},
{"name", Oid.NAME, Types.VARCHAR, "java.lang.String", Oid.NAME_ARRAY}, {"name", Oid.NAME, Types.VARCHAR, "java.lang.String", Oid.NAME_ARRAY},
{"bytea", Oid.BYTEA, Types.BINARY, "[B", Oid.BYTEA_ARRAY}, {"bytea", Oid.BYTEA, Types.BINARY, "[B", Oid.BYTEA_ARRAY},
{"bool", Oid.BOOL, Types.BOOLEAN, "java.lang.Boolean", Oid.BOOL_ARRAY}, {"bool", Oid.BOOL, Types.BOOLEAN, "java.lang.Boolean", Oid.BOOL_ARRAY},
{"bit", Oid.BIT, Types.BIT, "java.lang.Boolean", Oid.BIT_ARRAY}, {"bit", Oid.BIT, Types.BIT, "java.lang.Boolean", Oid.BIT_ARRAY},
{"date", Oid.DATE, Types.DATE, "java.sql.Date", Oid.DATE_ARRAY}, {"date", Oid.DATE, Types.DATE, "java.sql.Date", Oid.DATE_ARRAY},
{"time", Oid.TIME, Types.TIME, "java.sql.Time", Oid.TIME_ARRAY}, {"time", Oid.TIME, Types.TIME, "java.sql.Time", Oid.TIME_ARRAY},
{"timetz", Oid.TIMETZ, Types.TIME, "java.sql.Time", Oid.TIMETZ_ARRAY}, {"timetz", Oid.TIMETZ, Types.TIME, "java.sql.Time", Oid.TIMETZ_ARRAY},
{"timestamp", Oid.TIMESTAMP, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMP_ARRAY}, {"timestamp", Oid.TIMESTAMP, Types.TIMESTAMP, "java.sql.Timestamp", Oid.TIMESTAMP_ARRAY},
{"smalldatetime", Oid.SMALLDATETIME, Types.TIMESTAMP, "java.lang.Timestamp", Oid.SMALLDATETIME_ARRAY}, {"smalldatetime", Oid.SMALLDATETIME, Types.TIMESTAMP, "java.lang.Timestamp", Oid.SMALLDATETIME_ARRAY},
{"timestamptz", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp", {"timestamptz", Oid.TIMESTAMPTZ, Types.TIMESTAMP, "java.sql.Timestamp",
Oid.TIMESTAMPTZ_ARRAY}, Oid.TIMESTAMPTZ_ARRAY},
{"json", Oid.JSON, Types.OTHER, "org.postgresql.util.PGobject", Oid.JSON_ARRAY}, {"json", Oid.JSON, Types.OTHER, "org.postgresql.util.PGobject", Oid.JSON_ARRAY},
{"point", Oid.POINT, Types.OTHER, "org.postgresql.geometric.PGpoint", Oid.POINT_ARRAY}, {"point", Oid.POINT, Types.OTHER, "org.postgresql.geometric.PGpoint", Oid.POINT_ARRAY},
{"blob", Oid.BLOB, Types.BLOB, "org.postgresql.util.PGobject", -1}, {"blob", Oid.BLOB, Types.BLOB, "org.postgresql.util.PGobject", -1},
{"clob", Oid.CLOB, Types.CLOB, "org.postgresql.util.PGobject", -1}, {"clob", Oid.CLOB, Types.CLOB, "org.postgresql.util.PGobject", -1},
{"nvarchar2", Oid.NVARCHAR2, Types.VARCHAR, "java.lang.String", Oid.NVARCHAR2_ARRAY}, {"nvarchar2", Oid.NVARCHAR2, Types.VARCHAR, "java.lang.String", Oid.NVARCHAR2_ARRAY},
{"refcursor", Oid.REF_CURSOR, Types.REF_CURSOR, "java.sql.ResultSet", Oid.REF_CURSOR_ARRAY} {"refcursor", Oid.REF_CURSOR, Types.REF_CURSOR, "java.sql.ResultSet", Oid.REF_CURSOR_ARRAY}
}; };
/** /**
* PG maps several alias to real type names. When we do queries against pg_catalog, we must use * PG maps several alias to real type names. When we do queries against pg_catalog, we must use

View File

@ -5,12 +5,14 @@ import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4B; import org.postgresql.test.jdbc2.BaseTest4B;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types; import java.sql.Types;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import static org.junit.Assert.*; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class IntTest extends BaseTest4B { public class IntTest extends BaseTest4B {
/* /*
@ -116,4 +118,28 @@ public class IntTest extends BaseTest4B {
TestUtil.dropTable(con, "test_tinyint2"); TestUtil.dropTable(con, "test_tinyint2");
} }
/*
* Tests int type
*/
@Test
public void testIntType() throws Exception {
TestUtil.createTable(con, "test_int", "c1 int1,c2 int2,c3 int4,"
+ "c4 int8,uc1 uint1,uc2 uint2,uc3 uint4,uc4 uint8");
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM test_int")) {
ResultSetMetaData rsmd = rs.getMetaData();
assertEquals(8, rsmd.getColumnCount());
assertEquals(Types.TINYINT, rsmd.getColumnType(1));
assertEquals(Types.SMALLINT, rsmd.getColumnType(2));
assertEquals(Types.INTEGER, rsmd.getColumnType(3));
assertEquals(Types.BIGINT, rsmd.getColumnType(4));
assertEquals(Types.TINYINT, rsmd.getColumnType(5));
assertEquals(Types.SMALLINT, rsmd.getColumnType(6));
assertEquals(Types.INTEGER, rsmd.getColumnType(7));
assertEquals(Types.BIGINT, rsmd.getColumnType(8));
} finally {
TestUtil.dropTable(con, "test_int");
}
}
} }

View File

@ -2,15 +2,21 @@ package org.postgresql.test.dolphintest;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.postgresql.test.TestUtil; import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4B; import org.postgresql.test.jdbc2.BaseTest4B;
import java.math.BigInteger; import java.math.BigInteger;
import java.sql.*; import java.sql.PreparedStatement;
import java.sql.Types;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Array;
import static org.junit.Assert.*; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class UnsignedTest extends BaseTest4B { public class UnsignedTest extends BaseTest4B {
/** /**
@ -21,19 +27,20 @@ public class UnsignedTest extends BaseTest4B {
public void testUint1() throws SQLException { public void testUint1() throws SQLException {
TestUtil.createTable(con, "test_unit1", "id uint1"); TestUtil.createTable(con, "test_unit1", "id uint1");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit1 VALUES (?)"); try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit1 VALUES (?)")) {
pstmt.setObject(1, 234, Types.SMALLINT); pstmt.setObject(1, 234, Types.SMALLINT);
pstmt.executeUpdate(); pstmt.executeUpdate();
}
Statement stmt = con.createStatement(); try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit1"); ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit1");) {
assertTrue(rs.next());
assertTrue(rs.next()); Object r1 = rs.getObject(1);
Object r1 = rs.getObject(1); assertNotNull(r1);
assertNotNull(r1); assertEquals(234, r1);
assertEquals(234, r1); } finally {
TestUtil.dropTable(con, "test_unit1");
TestUtil.dropTable(con, "test_unit1"); }
} }
/** /**
@ -44,19 +51,20 @@ public class UnsignedTest extends BaseTest4B {
public void testUint2() throws SQLException { public void testUint2() throws SQLException {
TestUtil.createTable(con, "test_unit2", "id uint2"); TestUtil.createTable(con, "test_unit2", "id uint2");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit2 VALUES (?)"); try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit2 VALUES (?)")) {
pstmt.setObject(1, 65518, Types.INTEGER); pstmt.setObject(1, 65518, Types.INTEGER);
pstmt.executeUpdate(); pstmt.executeUpdate();
}
Statement stmt = con.createStatement(); try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit2"); ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit2")) {
assertTrue(rs.next());
assertTrue(rs.next()); Object r1 = rs.getObject(1);
Object r1 = rs.getObject(1); assertNotNull(r1);
assertNotNull(r1); assertEquals(65518, r1);
assertEquals(65518, r1); } finally {
TestUtil.dropTable(con, "test_unit2");
TestUtil.dropTable(con, "test_unit2"); }
} }
/** /**
@ -67,20 +75,20 @@ public class UnsignedTest extends BaseTest4B {
public void testUint4() throws SQLException { public void testUint4() throws SQLException {
TestUtil.createTable(con, "test_unit4", "id uint4"); TestUtil.createTable(con, "test_unit4", "id uint4");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit4 VALUES (?)"); try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit4 VALUES (?)")) {
long l = 4294967282L; pstmt.setObject(1, 4294967282L, Types.BIGINT);
pstmt.setObject(1, l, Types.BIGINT); pstmt.executeUpdate();
pstmt.executeUpdate(); }
Statement stmt = con.createStatement(); try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit4"); ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit4")) {
assertTrue(rs.next());
assertTrue(rs.next()); Object r1 = rs.getObject(1);
Object r1 = rs.getObject(1); assertNotNull(r1);
assertNotNull(r1); assertEquals(4294967282L, r1);
assertEquals(l, r1); } finally {
TestUtil.dropTable(con, "test_unit4");
TestUtil.dropTable(con, "test_unit4"); }
} }
/** /**
@ -90,109 +98,113 @@ public class UnsignedTest extends BaseTest4B {
@Test @Test
public void testUint8() throws SQLException { public void testUint8() throws SQLException {
TestUtil.createTable(con, "test_unit8", "id uint8"); TestUtil.createTable(con, "test_unit8", "id uint8");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit8 VALUES (?)");
BigInteger b = new BigInteger("9223372036859999999"); BigInteger b = new BigInteger("9223372036859999999");
pstmt.setObject(1, b, Types.NUMERIC);
pstmt.executeUpdate();
BigInteger b2 = new BigInteger("15223372036859999999"); BigInteger b2 = new BigInteger("15223372036859999999");
pstmt.setObject(1, b2, Types.NUMERIC); try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit8 VALUES (?)")) {
pstmt.executeUpdate(); pstmt.setObject(1, b, Types.NUMERIC);
pstmt.executeUpdate();
Statement stmt = con.createStatement(); pstmt.setObject(1, b2, Types.NUMERIC);
ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit8"); pstmt.executeUpdate();
}
assertTrue(rs.next()); try (Statement stmt = con.createStatement();
Object r1 = rs.getObject(1); ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit8")) {
assertNotNull(r1); assertTrue(rs.next());
assertEquals(b, r1); Object r1 = rs.getObject(1);
assertNotNull(r1);
assertEquals(b, r1);
assertTrue(rs.next()); assertTrue(rs.next());
Object r2 = rs.getObject(1); Object r2 = rs.getObject(1);
assertNotNull(r2); assertNotNull(r2);
assertEquals(b2, r2); assertEquals(b2, r2);
} finally {
TestUtil.dropTable(con, "test_unit8"); TestUtil.dropTable(con, "test_unit8");
}
} }
@Test @Test
public void testCreateArrayOfUint1() throws SQLException { public void testCreateArrayOfUint1() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint1[]"); try (PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint1[]")) {
Short[] in = new Short[3]; Object[] in = new Object[3];
in[0] = 0; in[0] = 0;
in[1] = 188; in[1] = 88;
in[2] = 234; in[2] = 115;
pstmt.setArray(1, con.createArrayOf("uint1", in)); pstmt.setArray(1, con.createArrayOf("uint1", in));
try (ResultSet rs = pstmt.executeQuery()) {
Assert.assertTrue(rs.next());
Array arr = rs.getArray(1);
Object[] out = (Object[]) arr.getArray();
ResultSet rs = pstmt.executeQuery(); Assert.assertEquals(3, out.length);
Assert.assertTrue(rs.next()); Assert.assertEquals(0, Integer.parseInt(out[0].toString()));
Array arr = rs.getArray(1); Assert.assertEquals(88, Integer.parseInt(out[1].toString()));
Short[] out = (Short[]) arr.getArray(); Assert.assertEquals(115, Integer.parseInt(out[2].toString()));
}
Assert.assertEquals(3, out.length); }
Assert.assertEquals(0, out[0].shortValue());
Assert.assertEquals(188, out[1].shortValue());
Assert.assertEquals(234, out[2].shortValue());
} }
@Test @Test
public void testCreateArrayOfUint2() throws SQLException { public void testCreateArrayOfUint2() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint2[]"); try (PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint2[]")) {
Integer[] in = new Integer[3]; Short[] in = new Short[3];
in[0] = 0; in[0] = 0;
in[1] = 12654; in[1] = 12654;
in[2] = 65535; in[2] = 30035;
pstmt.setArray(1, con.createArrayOf("uint2", in)); pstmt.setArray(1, con.createArrayOf("uint2", in));
try (ResultSet rs = pstmt.executeQuery()) {
Assert.assertTrue(rs.next());
Array arr = rs.getArray(1);
Short[] out = (Short[]) arr.getArray();
ResultSet rs = pstmt.executeQuery(); Assert.assertEquals(3, out.length);
Assert.assertTrue(rs.next()); Assert.assertEquals(0, out[0].shortValue());
Array arr = rs.getArray(1); Assert.assertEquals(12654, out[1].shortValue());
Integer[] out = (Integer[]) arr.getArray(); Assert.assertEquals(30035, out[2].shortValue());
}
Assert.assertEquals(3, out.length); }
Assert.assertEquals(0, out[0].intValue());
Assert.assertEquals(12654, out[1].intValue());
Assert.assertEquals(65535, out[2].intValue());
} }
@Test @Test
public void testCreateArrayOfUint4() throws SQLException { public void testCreateArrayOfUint4() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint4[]"); try (PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint4[]")) {
Long[] in = new Long[2]; Integer[] in = new Integer[2];
in[0] = 0L; in[0] = 0;
in[1] = 4294967295L; in[1] = 1994967295;
pstmt.setArray(1, con.createArrayOf("uint4", in)); pstmt.setArray(1, con.createArrayOf("uint4", in));
try (ResultSet rs = pstmt.executeQuery()) {
Assert.assertTrue(rs.next());
Array arr = rs.getArray(1);
Integer[] out = (Integer[]) arr.getArray();
ResultSet rs = pstmt.executeQuery(); Assert.assertEquals(2, out.length);
Assert.assertTrue(rs.next()); Assert.assertEquals(0, out[0].intValue());
Array arr = rs.getArray(1); Assert.assertEquals(1994967295, out[1].intValue());
Long[] out = (Long[]) arr.getArray(); }
}
Assert.assertEquals(2, out.length);
Assert.assertEquals(0, out[0].longValue());
Assert.assertEquals(4294967295L, out[1].longValue());
} }
@Test @Test
public void testCreateArrayOfUint8() throws SQLException { public void testCreateArrayOfUint8() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint8[]"); try (PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint8[]")) {
Long[] in = new Long[2]; Long[] in = new Long[2];
in[0] = 0L; in[0] = 0L;
in[1] = 32458765334567556L; in[1] = 32458765334567556L;
pstmt.setArray(1, con.createArrayOf("uint8", in)); pstmt.setArray(1, con.createArrayOf("uint8", in));
try (ResultSet rs = pstmt.executeQuery()) {
Assert.assertTrue(rs.next());
Array arr = rs.getArray(1);
Object[] out = (Object[]) arr.getArray();
Long[] outLong = new Long[out.length];
for (int i = 0; i < out.length; i++) {
outLong[i] = Long.valueOf(out[i].toString());
}
ResultSet rs = pstmt.executeQuery(); Assert.assertEquals(2, out.length);
Assert.assertTrue(rs.next()); Assert.assertEquals(0L, outLong[0].longValue());
Array arr = rs.getArray(1); Assert.assertEquals(32458765334567556L, outLong[1].longValue());
Object[] out = (Object[]) arr.getArray(); }
Long[] outLong = new Long[out.length];
for (int i = 0; i < out.length; i++) {
outLong[i] = Long.valueOf(out[i].toString());
} }
Assert.assertEquals(2, out.length);
Assert.assertEquals(0L, outLong[0].longValue());
Assert.assertEquals(32458765334567556L, outLong[1].longValue());
} }
} }