Skip to content

Commit

Permalink
fix issue pgjdbc#3170
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawnliving committed Mar 22, 2024
1 parent b005049 commit df2a0fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
15 changes: 8 additions & 7 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Expand Up @@ -20,6 +20,7 @@
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import org.checkerframework.checker.initialization.qual.Initialized;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.sql.Array;
Expand Down Expand Up @@ -509,24 +510,24 @@ public void free() throws SQLException {
}

@Override
public final boolean equals(Object obj){
if (obj == this){
public final boolean equals(@Initialized @Nullable Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof PgArray){
if (obj instanceof PgArray) {
PgArray pgArray = (PgArray) obj;
if (this.fieldString != null && pgArray.fieldString != null){
if (this.fieldString != null && pgArray.fieldString != null) {
return this.hashCode() == pgArray.hashCode() && this.oid == pgArray.oid && this.fieldString.equals(pgArray.fieldString);
}
if (this.fieldBytes != null && pgArray.fieldBytes != null){
if (this.fieldBytes != null && pgArray.fieldBytes != null) {
return this.hashCode() == pgArray.hashCode() && this.oid == pgArray.oid && Arrays.equals(this.fieldBytes,pgArray.fieldBytes);
}
}
return false;
}

@Override
public int hashCode(){
return Integer.hashCode(oid)^Arrays.hashCode(fieldBytes)^Objects.hashCode(fieldString);
public int hashCode() {
return Integer.hashCode(oid) ^ Arrays.hashCode(fieldBytes) ^ Objects.hashCode(fieldString);
}
}
13 changes: 4 additions & 9 deletions pgjdbc/src/test/java/org/postgresql/jdbc/ArraysTest.java
Expand Up @@ -53,10 +53,10 @@ void binaryNotSupported() throws Exception {

@Test
public void testArrayEquals() throws SQLException {
// because of install the postgresql at the VM, need to specify the url user and password for testing.
String url="jdbc:postgresql://192.168.100.80/test";
String user="postgres";
String password="123456";
//because of install the postgresql at the VM, need to specify the url user and password for testing.
String url = "jdbc:postgresql://192.168.100.80/test";
String user = "postgres";
String password = "123456";
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
Expand All @@ -68,7 +68,6 @@ public void testArrayEquals() throws SQLException {
Array pgArray2 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Assertions.assertEquals(pgArray1,pgArray2);


Array pgArray3 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Array pgArray4 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertEquals(pgArray3,pgArray4);
Expand All @@ -89,12 +88,8 @@ public void testArrayEquals() throws SQLException {
Array pgArray12 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertEquals(pgArray11,pgArray12);


Array pgArray13 = new PgArray((BaseConnection) connection, Oid.VARCHAR_ARRAY, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray14 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertNotEquals(pgArray13,pgArray14);



}
}

0 comments on commit df2a0fa

Please sign in to comment.