Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pgarray equals to fix issue #3170 #3185

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
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;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
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 @@ -505,4 +508,26 @@ public void free() throws SQLException {
fieldBytes = null;
arrayList = null;
}

@Override
public final boolean equals(@Initialized @Nullable Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof PgArray) {
PgArray pgArray = (PgArray) obj;
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) {
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);
}
}
1 change: 1 addition & 0 deletions pgjdbc/src/test/java/org/postgresql/jdbc/ArraysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ void binaryNotSupported() throws Exception {
support.toBinaryRepresentation(null, new BigDecimal[]{BigDecimal.valueOf(3)}, Oid.FLOAT8_ARRAY);
});
}

}
35 changes: 35 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand Down Expand Up @@ -904,4 +905,38 @@ public void testEmptyArray() throws SQLException {
}
}

@Test
public void testArrayEquals() throws SQLException {
Statement statement = conn.createStatement();

statement.executeQuery("SELECT * FROM person");
Array pgArray1 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Array pgArray2 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{'1','2','3'});
Assertions.assertEquals(pgArray1,pgArray2);

Array pgArray3 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Array pgArray4 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1,2,3});
Assertions.assertEquals(pgArray3,pgArray4);

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

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

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

Array pgArray11 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Array pgArray12 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\t \n 'testing1', \t \n 'testing2'}");
Assertions.assertEquals(pgArray11,pgArray12);

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

}