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

adds equals implementation for PgArray #3174

Open
wants to merge 3 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
33 changes: 33 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
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 +507,35 @@ public void free() throws SQLException {
fieldBytes = null;
arrayList = null;
}

@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

PgArray other = (PgArray) obj;
if (oid != other.oid) {
return false;
}
if (fieldString == null) {
if (other.fieldString != null) {
return false;
}
} else if (!fieldString.equals(other.fieldString)) {
vishalvrv9 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
if (!java.util.Arrays.equals(fieldBytes, other.fieldBytes)) {
return false;
}
return true;
}

@Override
public int hashCode() {
return Objects.hash(oid, fieldString, Arrays.hashCode(fieldBytes));
}
}
49 changes: 49 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 @@ -6,6 +6,7 @@
package org.postgresql.test.jdbc2;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import org.postgresql.PGConnection;
Expand Down Expand Up @@ -904,4 +905,52 @@ public void testEmptyArray() throws SQLException {
}
}

@Test
public void testEqualsWithSameOidSameFieldBytes() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1, 2, 3});
Array arr2 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1, 2, 3});

assertEquals(arr1, arr2);
}

@Test
public void testEqualsWithDifferentOidSameFieldBytes() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1, 2, 3});
Array arr2 = new PgArray((BaseConnection) conn, Oid.BOOL_ARRAY, new byte[]{1, 2, 3});

assertNotEquals(arr1, arr2);
}

@Test
public void testEqualsWithSameOidDifferentFieldBytes() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{1, 2, 3});
Array arr2 = new PgArray((BaseConnection) conn, Oid.BYTEA_ARRAY, new byte[]{4, 5, 6});

assertNotEquals(arr1, arr2);
}

@Test
public void testEqualsWithSameOidSameFieldString() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead\t\", unquot\u000B \u2001 \r, \" \fnew \n \"\t, \f\" \" }");
Array arr2 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead\t\", unquot\u000B \u2001 \r, \" \fnew \n \"\t, \f\" \" }");

assertEquals(arr1, arr2);
}

@Test
public void testEqualsWithDifferentOidSameFieldString() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead\t\", unquot\u000B \u2001 \r, \" \fnew \n \"\t, \f\" \" }");
Array arr2 = new PgArray((BaseConnection) conn, Oid.VARCHAR, "{\" lead\t\", unquot\u000B \u2001 \r, \" \fnew \n \"\t, \f\" \" }");

assertNotEquals(arr1, arr2);
}

@Test
public void testEqualsWithSameOidDifferentFieldString() throws SQLException {
Array arr1 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead1\t\", unquot\u000B \u2001 \r, \" \fnew1 \n \"\t, \f\" \" }");
Array arr2 = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead2\t\", unquot\u000B \u2001 \r, \" \fnew2 \n \"\t, \f\" \" }");

assertNotEquals(arr1, arr2);
}

}