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
fix: make setObject accept UUID array #2587
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
pgjdbc/src/test/java/org/postgresql/jdbc/UUIDArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.jdbc; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
||
import org.postgresql.core.ServerVersion; | ||
import org.postgresql.test.TestUtil; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.UUID; | ||
|
||
class UUIDArrayTest { | ||
|
||
private static Connection con; | ||
private static final String TABLE_NAME = "uuid_table"; | ||
private static final String INSERT1 = "INSERT INTO " + TABLE_NAME | ||
+ " (id, data1) VALUES (?, ?)"; | ||
private static final String INSERT2 = "INSERT INTO " + TABLE_NAME | ||
+ " (id, data2) VALUES (?, ?)"; | ||
private static final String SELECT1 = "SELECT data1 FROM " + TABLE_NAME | ||
+ " WHERE id = ?"; | ||
private static final String SELECT2 = "SELECT data2 FROM " + TABLE_NAME | ||
+ " WHERE id = ?"; | ||
private static final UUID[] uids1 = new UUID[]{UUID.randomUUID(), UUID.randomUUID()}; | ||
private static final UUID[][] uids2 = new UUID[][]{uids1}; | ||
|
||
@BeforeAll | ||
public static void setUp() throws Exception { | ||
con = TestUtil.openDB(); | ||
assumeTrue(TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_6)); | ||
try (Statement stmt = con.createStatement()) { | ||
stmt.execute("CREATE TABLE " + TABLE_NAME | ||
+ " (id int PRIMARY KEY, data1 UUID[], data2 UUID[][])"); | ||
} | ||
} | ||
|
||
@AfterAll | ||
public static void tearDown() throws Exception { | ||
try (Statement stmt = con.createStatement()) { | ||
stmt.execute("DROP TABLE IF EXISTS " + TABLE_NAME); | ||
} | ||
TestUtil.closeDB(con); | ||
} | ||
|
||
@Test | ||
void test1DWithCreateArrayOf() throws SQLException { | ||
try (Connection c = assertDoesNotThrow(() -> TestUtil.openDB()); | ||
PreparedStatement stmt1 = c.prepareStatement(INSERT1); | ||
PreparedStatement stmt2 = c.prepareStatement(SELECT1)) { | ||
stmt1.setInt(1, 100); | ||
stmt1.setArray(2, c.createArrayOf("uuid", uids1)); | ||
stmt1.execute(); | ||
|
||
stmt2.setInt(1, 100); | ||
stmt2.execute(); | ||
try (ResultSet rs = stmt2.getResultSet()) { | ||
assertTrue(rs.next()); | ||
UUID[] array = (UUID[])rs.getArray(1).getArray(); | ||
assertEquals(uids1[0], array[0]); | ||
assertEquals(uids1[1], array[1]); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void test1DWithSetObject() throws SQLException { | ||
try (Connection c = assertDoesNotThrow(() -> TestUtil.openDB()); | ||
PreparedStatement stmt1 = c.prepareStatement(INSERT1); | ||
PreparedStatement stmt2 = c.prepareStatement(SELECT1)) { | ||
stmt1.setInt(1, 101); | ||
stmt1.setObject(2, uids1); | ||
stmt1.execute(); | ||
|
||
stmt2.setInt(1, 101); | ||
stmt2.execute(); | ||
try (ResultSet rs = stmt2.getResultSet()) { | ||
assertTrue(rs.next()); | ||
UUID[] array = (UUID[])rs.getArray(1).getArray(); | ||
assertEquals(uids1[0], array[0]); | ||
assertEquals(uids1[1], array[1]); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void test2DWithCreateArrayOf() throws SQLException { | ||
try (Connection c = assertDoesNotThrow(() -> TestUtil.openDB()); | ||
PreparedStatement stmt1 = c.prepareStatement(INSERT2); | ||
PreparedStatement stmt2 = c.prepareStatement(SELECT2)) { | ||
stmt1.setInt(1, 200); | ||
stmt1.setArray(2, c.createArrayOf("uuid", uids2)); | ||
stmt1.execute(); | ||
|
||
stmt2.setInt(1, 200); | ||
stmt2.execute(); | ||
try (ResultSet rs = stmt2.getResultSet()) { | ||
assertTrue(rs.next()); | ||
UUID[][] array = (UUID[][])rs.getArray(1).getArray(); | ||
assertEquals(uids2[0][0], array[0][0]); | ||
assertEquals(uids2[0][1], array[0][1]); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void test2DWithSetObject() throws SQLException { | ||
try (Connection c = assertDoesNotThrow(() -> TestUtil.openDB()); | ||
PreparedStatement stmt1 = c.prepareStatement(INSERT2); | ||
PreparedStatement stmt2 = c.prepareStatement(SELECT2)) { | ||
stmt1.setInt(1, 201); | ||
stmt1.setObject(2, uids2); | ||
stmt1.execute(); | ||
|
||
stmt2.setInt(1, 201); | ||
stmt2.execute(); | ||
try (ResultSet rs = stmt2.getResultSet()) { | ||
assertTrue(rs.next()); | ||
UUID[][] array = (UUID[][])rs.getArray(1).getArray(); | ||
assertEquals(uids2[0][0], array[0][0]); | ||
assertEquals(uids2[0][1], array[0][1]); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2022