Skip to content

Commit

Permalink
Use random table names in TestJdbcPreparedStatement
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Nov 8, 2023
1 parent a405840 commit 89365d6
Showing 1 changed file with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static io.trino.jdbc.BaseTestJdbcResultSet.toSqlTime;
import static io.trino.jdbc.TestingJdbcUtils.list;
import static io.trino.jdbc.TestingJdbcUtils.readRows;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.sql.ParameterMetaData.parameterModeUnknown;
Expand Down Expand Up @@ -160,9 +161,10 @@ public void testGetMetadata()
private void testGetMetadata(boolean explicitPrepare)
throws Exception
{
String tableName = "test_get_metadata_" + randomNameSuffix();
try (Connection connection = createConnection("blackhole", "blackhole", explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_get_metadata (" +
statement.execute("CREATE TABLE " + tableName + " (" +
"c_boolean boolean, " +
"c_decimal decimal, " +
"c_decimal_2 decimal(10,3)," +
Expand All @@ -174,13 +176,13 @@ private void testGetMetadata(boolean explicitPrepare)
}

try (PreparedStatement statement = connection.prepareStatement(
"SELECT * FROM test_get_metadata")) {
"SELECT * FROM " + tableName)) {
ResultSetMetaData metadata = statement.getMetaData();
assertEquals(metadata.getColumnCount(), 8);
for (int i = 1; i <= metadata.getColumnCount(); i++) {
assertEquals(metadata.getCatalogName(i), "blackhole");
assertEquals(metadata.getSchemaName(i), "blackhole");
assertEquals(metadata.getTableName(i), "test_get_metadata");
assertEquals(metadata.getTableName(i), tableName);
}

assertEquals(metadata.getColumnName(1), "c_boolean");
Expand Down Expand Up @@ -209,7 +211,7 @@ private void testGetMetadata(boolean explicitPrepare)
}

try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE test_get_metadata");
statement.execute("DROP TABLE " + tableName);
}
}
}
Expand All @@ -225,9 +227,10 @@ public void testGetParameterMetaData()
private void testGetParameterMetaData(boolean explicitPrepare)
throws Exception
{
String tableName = "test_get_parameterMetaData_" + randomNameSuffix();
try (Connection connection = createConnection("blackhole", "blackhole", explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_get_parameterMetaData (" +
statement.execute("CREATE TABLE " + tableName + " (" +
"c_boolean boolean, " +
"c_decimal decimal, " +
"c_decimal_2 decimal(10,3)," +
Expand All @@ -245,7 +248,7 @@ private void testGetParameterMetaData(boolean explicitPrepare)
}

try (PreparedStatement statement = connection.prepareStatement(
"SELECT ? FROM test_get_parameterMetaData WHERE c_boolean = ? AND c_decimal = ? " +
"SELECT ? FROM " + tableName + " WHERE c_boolean = ? AND c_decimal = ? " +
"AND c_decimal_2 = ? AND c_varchar = ? AND c_varchar_2 = ? AND c_row = ? " +
"AND c_array = ? AND c_map = ? AND c_tinyint = ? AND c_integer = ? AND c_bigint = ? " +
"AND c_smallint = ? AND c_real = ? AND c_double = ?")) {
Expand Down Expand Up @@ -362,7 +365,7 @@ private void testGetParameterMetaData(boolean explicitPrepare)
}

try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE test_get_parameterMetaData");
statement.execute("DROP TABLE " + tableName);
}
}
}
Expand Down Expand Up @@ -485,9 +488,10 @@ public void testExecuteUpdate()
public void testExecuteUpdate(boolean explicitPrepare)
throws Exception
{
String tableName = "test_execute_update_" + randomNameSuffix();
try (Connection connection = createConnection("blackhole", "blackhole", explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_execute_update (" +
statement.execute("CREATE TABLE " + tableName + " (" +
"c_boolean boolean, " +
"c_bigint bigint, " +
"c_double double, " +
Expand All @@ -498,7 +502,7 @@ public void testExecuteUpdate(boolean explicitPrepare)
}

try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO test_execute_update VALUES (?, ?, ?, ?, ?, ?, ?)")) {
"INSERT INTO " + tableName + " VALUES (?, ?, ?, ?, ?, ?, ?)")) {
statement.setBoolean(1, true);
statement.setLong(2, 5L);
statement.setDouble(3, 7.0d);
Expand All @@ -515,7 +519,7 @@ public void testExecuteUpdate(boolean explicitPrepare)
}

try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE test_execute_update");
statement.execute("DROP TABLE " + tableName);
}
}
}
Expand All @@ -531,13 +535,14 @@ public void testExecuteBatch()
private void testExecuteBatch(boolean explicitPrepare)
throws Exception
{
String tableName = "test_execute_batch_" + randomNameSuffix();
try (Connection connection = createConnection("memory", "default", explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_execute_batch(c_int integer)");
statement.execute("CREATE TABLE " + tableName + "(c_int integer)");
}

try (PreparedStatement preparedStatement = connection.prepareStatement(
"INSERT INTO test_execute_batch VALUES (?)")) {
"INSERT INTO " + tableName + " VALUES (?)")) {
// Run executeBatch before addBatch
assertEquals(preparedStatement.executeBatch(), new int[] {});

Expand All @@ -548,7 +553,7 @@ private void testExecuteBatch(boolean explicitPrepare)
assertEquals(preparedStatement.executeBatch(), new int[] {1, 1, 1});

try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("SELECT c_int FROM test_execute_batch");
ResultSet resultSet = statement.executeQuery("SELECT c_int FROM " + tableName);
assertThat(readRows(resultSet))
.containsExactlyInAnyOrder(
list(0),
Expand All @@ -569,7 +574,7 @@ private void testExecuteBatch(boolean explicitPrepare)
}

try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE test_execute_batch");
statement.execute("DROP TABLE " + tableName);
}
}
}
Expand All @@ -585,13 +590,14 @@ public void testInvalidExecuteBatch()
private void testInvalidExecuteBatch(boolean explicitPrepare)
throws Exception
{
String tableName = "test_execute_invalid_batch_" + randomNameSuffix();
try (Connection connection = createConnection("blackhole", "blackhole", explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE test_invalid_execute_batch(c_int integer)");
statement.execute("CREATE TABLE " + tableName + "(c_int integer)");
}

try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO test_invalid_execute_batch VALUES (?)")) {
"INSERT INTO " + tableName + " VALUES (?)")) {
statement.setInt(1, 1);
statement.addBatch();

Expand All @@ -611,7 +617,7 @@ private void testInvalidExecuteBatch(boolean explicitPrepare)
}

try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE test_invalid_execute_batch");
statement.execute("DROP TABLE " + tableName);
}
}
}
Expand Down Expand Up @@ -1423,12 +1429,13 @@ private Connection createConnection(String catalog, String schema, boolean expli
private void testExplicitPrepareSetting(boolean explicitPrepare, String expectedSql)
throws Exception
{
String selectSql = "SELECT * FROM blackhole.blackhole.test_table WHERE x = ? AND y = ? AND y <> 'Test'";
String insertSql = "INSERT INTO blackhole.blackhole.test_table (x, y) VALUES (?, ?)";
String tableName = "test_table_" + randomNameSuffix();
String selectSql = "SELECT * FROM blackhole.blackhole." + tableName + " WHERE x = ? AND y = ? AND y <> 'Test'";
String insertSql = "INSERT INTO blackhole.blackhole." + tableName + " (x, y) VALUES (?, ?)";

try (Connection connection = createConnection(explicitPrepare)) {
try (Statement statement = connection.createStatement()) {
assertEquals(statement.executeUpdate("CREATE TABLE blackhole.blackhole.test_table (x bigint, y varchar)"), 0);
assertEquals(statement.executeUpdate("CREATE TABLE blackhole.blackhole." + tableName + " (x bigint, y varchar)"), 0);
}

try (PreparedStatement ps = connection.prepareStatement(selectSql)) {
Expand Down Expand Up @@ -1484,7 +1491,7 @@ private void testExplicitPrepareSetting(boolean explicitPrepare, String expected
}

try (Statement statement = connection.createStatement()) {
assertEquals(statement.executeUpdate("DROP TABLE blackhole.blackhole.test_table"), 0);
assertEquals(statement.executeUpdate("DROP TABLE blackhole.blackhole." + tableName), 0);
}
}
}
Expand Down

0 comments on commit 89365d6

Please sign in to comment.