!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

@ -267,11 +267,15 @@ public class PgResultSet implements ResultSet, org.postgresql.PGRefCursorResultS
case Types.TINYINT: case Types.TINYINT:
case Types.SMALLINT: case Types.SMALLINT:
case Types.INTEGER: case Types.INTEGER:
if (field.getPGType().equals("uint4")) {
return getLong(columnIndex);
}
return getInt(columnIndex); return getInt(columnIndex);
case Types.BIGINT: case Types.BIGINT:
return getLong(columnIndex); if (field.getPGType().equals("uint8")) {
case TypeInfoCache.bIntegerType:
return getBigInteger(columnIndex); return getBigInteger(columnIndex);
}
return getLong(columnIndex);
case Types.NUMERIC: case Types.NUMERIC:
case Types.DECIMAL: case Types.DECIMAL:
int scale; int scale;

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;
@ -89,10 +87,10 @@ public class TypeInfoCache implements TypeInfo {
{"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},

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,20 +27,21 @@ 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");
} }
}
/** /**
* test uint2 type * test uint2 type
@ -44,20 +51,21 @@ 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");
} }
}
/** /**
* test uint4 type * test uint4 type
@ -67,21 +75,21 @@ 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(l, r1); assertEquals(4294967282L, r1);
} finally {
TestUtil.dropTable(con, "test_unit4"); TestUtil.dropTable(con, "test_unit4");
} }
}
/** /**
* test uint8 type * test uint8 type
@ -90,19 +98,18 @@ 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");
BigInteger b2 = new BigInteger("15223372036859999999");
try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO test_unit8 VALUES (?)")) {
pstmt.setObject(1, b, Types.NUMERIC); pstmt.setObject(1, b, Types.NUMERIC);
pstmt.executeUpdate(); pstmt.executeUpdate();
BigInteger b2 = new BigInteger("15223372036859999999");
pstmt.setObject(1, b2, Types.NUMERIC); pstmt.setObject(1, b2, Types.NUMERIC);
pstmt.executeUpdate(); pstmt.executeUpdate();
}
Statement stmt = con.createStatement(); try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit8"); ResultSet rs = stmt.executeQuery("SELECT id FROM test_unit8")) {
assertTrue(rs.next()); assertTrue(rs.next());
Object r1 = rs.getObject(1); Object r1 = rs.getObject(1);
assertNotNull(r1); assertNotNull(r1);
@ -112,77 +119,80 @@ public class UnsignedTest extends BaseTest4B {
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[]")) {
Object[] in = new Object[3];
in[0] = 0;
in[1] = 88;
in[2] = 115;
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();
Assert.assertEquals(3, out.length);
Assert.assertEquals(0, Integer.parseInt(out[0].toString()));
Assert.assertEquals(88, Integer.parseInt(out[1].toString()));
Assert.assertEquals(115, Integer.parseInt(out[2].toString()));
}
}
}
@Test
public void testCreateArrayOfUint2() throws SQLException {
try (PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint2[]")) {
Short[] in = new Short[3]; Short[] in = new Short[3];
in[0] = 0; in[0] = 0;
in[1] = 188; in[1] = 12654;
in[2] = 234; in[2] = 30035;
pstmt.setArray(1, con.createArrayOf("uint1", in)); pstmt.setArray(1, con.createArrayOf("uint2", in));
try (ResultSet rs = pstmt.executeQuery()) {
ResultSet rs = pstmt.executeQuery();
Assert.assertTrue(rs.next()); Assert.assertTrue(rs.next());
Array arr = rs.getArray(1); Array arr = rs.getArray(1);
Short[] out = (Short[]) arr.getArray(); Short[] out = (Short[]) arr.getArray();
Assert.assertEquals(3, out.length); Assert.assertEquals(3, out.length);
Assert.assertEquals(0, out[0].shortValue()); Assert.assertEquals(0, out[0].shortValue());
Assert.assertEquals(188, out[1].shortValue()); Assert.assertEquals(12654, out[1].shortValue());
Assert.assertEquals(234, out[2].shortValue()); Assert.assertEquals(30035, out[2].shortValue());
}
} }
@Test
public void testCreateArrayOfUint2() throws SQLException {
PreparedStatement pstmt = con.prepareStatement("SELECT ?::uint2[]");
Integer[] in = new Integer[3];
in[0] = 0;
in[1] = 12654;
in[2] = 65535;
pstmt.setArray(1, con.createArrayOf("uint2", in));
ResultSet rs = pstmt.executeQuery();
Assert.assertTrue(rs.next());
Array arr = rs.getArray(1);
Integer[] out = (Integer[]) arr.getArray();
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()) {
ResultSet rs = pstmt.executeQuery();
Assert.assertTrue(rs.next()); Assert.assertTrue(rs.next());
Array arr = rs.getArray(1); Array arr = rs.getArray(1);
Long[] out = (Long[]) arr.getArray(); Integer[] out = (Integer[]) arr.getArray();
Assert.assertEquals(2, out.length); Assert.assertEquals(2, out.length);
Assert.assertEquals(0, out[0].longValue()); Assert.assertEquals(0, out[0].intValue());
Assert.assertEquals(4294967295L, out[1].longValue()); Assert.assertEquals(1994967295, out[1].intValue());
}
}
} }
@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()) {
ResultSet rs = pstmt.executeQuery();
Assert.assertTrue(rs.next()); Assert.assertTrue(rs.next());
Array arr = rs.getArray(1); Array arr = rs.getArray(1);
Object[] out = (Object[]) arr.getArray(); Object[] out = (Object[]) arr.getArray();
@ -196,3 +206,5 @@ public class UnsignedTest extends BaseTest4B {
Assert.assertEquals(32458765334567556L, outLong[1].longValue()); Assert.assertEquals(32458765334567556L, outLong[1].longValue());
} }
} }
}
}