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 4f2d0da commit b005049
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* <p>Array is used collect one column of query result data.</p>
Expand Down Expand Up @@ -506,20 +507,26 @@ public void free() throws SQLException {
fieldBytes = null;
arrayList = null;
}

@Override
public final boolean equals(Object obj){
if (obj == this){
return true;
}
if (obj instanceof PgArray){
PgArray pgArray = (PgArray) obj;
if (pgArray.fieldBytes != null && this.fieldBytes != null){
return Arrays.equals(pgArray.fieldBytes, this.fieldBytes);
if (this.fieldString != null && pgArray.fieldString != null){
return this.hashCode() == pgArray.hashCode() && this.oid == pgArray.oid && this.fieldString.equals(pgArray.fieldString);
}
if (pgArray.fieldString != null && this.fieldString != null){
return pgArray.fieldString.equals(this.fieldString);
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);
}
}
55 changes: 55 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/jdbc/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.postgresql.core.BaseConnection;
import org.postgresql.core.Oid;
import org.postgresql.util.PSQLException;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Properties;

class ArraysTest {

Expand Down Expand Up @@ -42,4 +50,51 @@ void binaryNotSupported() throws Exception {
support.toBinaryRepresentation(null, new BigDecimal[]{BigDecimal.valueOf(3)}, Oid.FLOAT8_ARRAY);
});
}

@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";
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
Connection connection = DriverManager.getConnection(url,props);
Statement statement = connection.createStatement();

statement.executeQuery("SELECT * FROM person");
Array pgArray1 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
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);

Array pgArray5 = new PgArray((BaseConnection) connection, Oid.BIT_ARRAY, new byte[]{1,2,3});
Array pgArray6 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray5,pgArray6);

Array pgArray7 = new PgArray((BaseConnection) connection, Oid.JSON, new byte[]{1,2,3});
Array pgArray8 = new PgArray((BaseConnection) connection, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertNotEquals(pgArray7,pgArray8);

Array pgArray9 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{}");
Array pgArray10 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{}");
Assertions.assertEquals(pgArray9,pgArray10);

Array pgArray11 = new PgArray((BaseConnection) connection, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
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 b005049

Please sign in to comment.