From 36d22cd86c687fb84c470107ce1de3684d488ab3 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Mon, 8 Jul 2024 19:32:32 -0700 Subject: [PATCH 1/8] Batch 2 - connection sharing --- .../jdbc/ClientMemoryLimitParallelIT.java | 25 +- .../snowflake/client/jdbc/ConnectionIT.java | 116 +-- .../client/jdbc/DatabaseMetaDataIT.java | 700 +++++++++--------- .../client/jdbc/DatabaseMetaDataLatestIT.java | 170 ++--- 4 files changed, 499 insertions(+), 512 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java index 22a33286d..63cdcda3b 100644 --- a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java @@ -10,7 +10,9 @@ import java.util.Properties; import net.snowflake.client.category.TestCategoryOthers; import org.junit.After; +import org.junit.AfterClass; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -62,18 +64,30 @@ public class ClientMemoryLimitParallelIT { + rowCount + "));"; + private static Connection con; + + @BeforeClass + public static void setUpConnection() throws SQLException { + con = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (con != null && !con.isClosed()) { + con.close(); + } + } + @Before public void setUp() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { statement.execute(createTestTableSQL); } } @After public void tearDown() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { statement.execute("drop table if exists testtable_cml"); } } @@ -126,8 +140,7 @@ public void run() { @Test public void testQueryNotHanging() throws SQLException { Properties paramProperties = new Properties(); - try (Connection connection = getConnection(paramProperties); - Statement statement = connection.createStatement()) { + try (Statement statement = con.createStatement()) { queryRows(statement, 100, 160); } } diff --git a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java index 2e0a9f13e..93d4c4e23 100644 --- a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java @@ -47,7 +47,9 @@ import net.snowflake.client.core.SFSession; import net.snowflake.common.core.SqlState; import org.apache.commons.codec.binary.Base64; +import org.junit.AfterClass; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; @@ -69,6 +71,20 @@ public class ConnectionIT extends BaseJDBCTest { @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); + private static Connection connection; + + @BeforeClass + public static void setUpConnection() throws SQLException { + connection = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (connection != null && !connection.isClosed()) { + connection.close(); + } + } + @Test public void testSimpleConnection() throws SQLException { Connection con = getConnection(); @@ -87,10 +103,9 @@ public void testSimpleConnection() throws SQLException { public void test300ConnectionsWithSingleClientInstance() throws SQLException { // concurrent testing int size = 300; - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); statement.execute( "create or replace table bigTable(rowNum number,rando " + "number) as (select seq4()," @@ -165,8 +180,7 @@ public void testProdConnectivity() throws SQLException { @Test public void testSetCatalogSchema() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String db = connection.getCatalog(); String schema = connection.getSchema(); connection.setCatalog(db); @@ -217,31 +231,30 @@ public void testDataCompletenessInLowMemory() throws Exception { public void testConnectionGetAndSetDBAndSchema() throws SQLException { final String SECOND_DATABASE = "SECOND_DATABASE"; final String SECOND_SCHEMA = "SECOND_SCHEMA"; - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { try { final String database = TestUtil.systemGetEnv("SNOWFLAKE_TEST_DATABASE").toUpperCase(); final String schema = TestUtil.systemGetEnv("SNOWFLAKE_TEST_SCHEMA").toUpperCase(); - assertEquals(database, con.getCatalog()); - assertEquals(schema, con.getSchema()); + assertEquals(database, connection.getCatalog()); + assertEquals(schema, connection.getSchema()); statement.execute(String.format("create or replace database %s", SECOND_DATABASE)); statement.execute(String.format("create or replace schema %s", SECOND_SCHEMA)); statement.execute(String.format("use database %s", database)); - con.setCatalog(SECOND_DATABASE); - assertEquals(SECOND_DATABASE, con.getCatalog()); - assertEquals("PUBLIC", con.getSchema()); + connection.setCatalog(SECOND_DATABASE); + assertEquals(SECOND_DATABASE, connection.getCatalog()); + assertEquals("PUBLIC", connection.getSchema()); - con.setSchema(SECOND_SCHEMA); - assertEquals(SECOND_SCHEMA, con.getSchema()); + connection.setSchema(SECOND_SCHEMA); + assertEquals(SECOND_SCHEMA, connection.getSchema()); statement.execute(String.format("use database %s", database)); statement.execute(String.format("use schema %s", schema)); - assertEquals(database, con.getCatalog()); - assertEquals(schema, con.getSchema()); + assertEquals(database, connection.getCatalog()); + assertEquals(schema, connection.getSchema()); } finally { statement.execute(String.format("drop database if exists %s", SECOND_DATABASE)); } @@ -250,40 +263,39 @@ public void testConnectionGetAndSetDBAndSchema() throws SQLException { @Test public void testConnectionClientInfo() throws SQLException { - try (Connection con = getConnection()) { - Properties property = con.getClientInfo(); - assertEquals(0, property.size()); - Properties clientInfo = new Properties(); - clientInfo.setProperty("name", "Peter"); - clientInfo.setProperty("description", "SNOWFLAKE JDBC"); - try { - con.setClientInfo(clientInfo); - fail("setClientInfo should fail for any parameter."); - } catch (SQLClientInfoException e) { - assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState()); - assertEquals(200047, e.getErrorCode()); - assertEquals(2, e.getFailedProperties().size()); - } - try { - con.setClientInfo("ApplicationName", "valueA"); - fail("setClientInfo should fail for any parameter."); - } catch (SQLClientInfoException e) { - assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState()); - assertEquals(200047, e.getErrorCode()); - assertEquals(1, e.getFailedProperties().size()); - } + Properties property = connection.getClientInfo(); + assertEquals(0, property.size()); + Properties clientInfo = new Properties(); + clientInfo.setProperty("name", "Peter"); + clientInfo.setProperty("description", "SNOWFLAKE JDBC"); + try { + connection.setClientInfo(clientInfo); + fail("setClientInfo should fail for any parameter."); + } catch (SQLClientInfoException e) { + assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState()); + assertEquals(200047, e.getErrorCode()); + assertEquals(2, e.getFailedProperties().size()); + } + try { + connection.setClientInfo("ApplicationName", "valueA"); + fail("setClientInfo should fail for any parameter."); + } catch (SQLClientInfoException e) { + assertEquals(SqlState.INVALID_PARAMETER_VALUE, e.getSQLState()); + assertEquals(200047, e.getErrorCode()); + assertEquals(1, e.getFailedProperties().size()); } } // only support get and set @Test public void testNetworkTimeout() throws SQLException { - try (Connection con = getConnection()) { - int millis = con.getNetworkTimeout(); - assertEquals(0, millis); - con.setNetworkTimeout(null, 200); - assertEquals(200, con.getNetworkTimeout()); - } + int millis = connection.getNetworkTimeout(); + assertEquals(0, millis); + connection.setNetworkTimeout(null, 200); + assertEquals(200, connection.getNetworkTimeout()); + // Reset timeout to 0 since we are reusing connection in tests + connection.setNetworkTimeout(null, 0); + assertEquals(0, millis); } @Test @@ -713,18 +725,14 @@ public void testHeartbeatFrequencyTooLarge() throws Exception { @Test public void testNativeSQL() throws Throwable { - try (Connection connection = getConnection()) { - // today returning the source SQL. - assertEquals("select 1", connection.nativeSQL("select 1")); - } + // today returning the source SQL. + assertEquals("select 1", connection.nativeSQL("select 1")); } @Test public void testGetTypeMap() throws Throwable { - try (Connection connection = getConnection()) { - // return an empty type map. setTypeMap is not supported. - assertEquals(Collections.emptyMap(), connection.getTypeMap()); - } + // return an empty type map. setTypeMap is not supported. + assertEquals(Collections.emptyMap(), connection.getTypeMap()); } @Test @@ -793,7 +801,6 @@ public void testStatementsAndResultSetsClosedByConnection() throws SQLException @Test public void testResultSetsClosedByStatement() throws SQLException { - Connection connection = getConnection(); Statement statement2 = connection.createStatement(); ResultSet rs1 = statement2.executeQuery("select 2;"); ResultSet rs2 = statement2.executeQuery("select 2;"); @@ -810,7 +817,6 @@ public void testResultSetsClosedByStatement() throws SQLException { assertTrue(rs2.isClosed()); assertTrue(rs3.isClosed()); assertTrue(rs4.isClosed()); - connection.close(); } @Test diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java index 2ea144f3c..51fdd7488 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java @@ -32,6 +32,8 @@ import net.snowflake.client.RunningOnGithubAction; import net.snowflake.client.TestUtil; import net.snowflake.client.category.TestCategoryOthers; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -63,154 +65,158 @@ public class DatabaseMetaDataIT extends BaseJDBCTest { public static final int EXPECTED_MAX_BINARY_LENGTH = 8388608; + private static Connection con; + + @BeforeClass + public static void setUpConnection() throws SQLException { + con = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (con != null && !con.isClosed()) { + con.close(); + } + } + @Test public void testGetConnection() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals(connection, metaData.getConnection()); - } + DatabaseMetaData metaData = con.getMetaData(); + assertEquals(con, metaData.getConnection()); } @Test public void testDatabaseAndDriverInfo() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - - // identifiers - assertEquals("Snowflake", metaData.getDatabaseProductName()); - assertEquals("Snowflake", metaData.getDriverName()); - - // Snowflake JDBC driver version - String driverVersion = metaData.getDriverVersion(); - Matcher m = VERSION_PATTERN.matcher(driverVersion); - assertTrue(m.matches()); - int majorVersion = metaData.getDriverMajorVersion(); - int minorVersion = metaData.getDriverMinorVersion(); - assertEquals(m.group(1), String.valueOf(majorVersion)); - assertEquals(m.group(2), String.valueOf(minorVersion)); - } + DatabaseMetaData metaData = con.getMetaData(); + + // identifiers + assertEquals("Snowflake", metaData.getDatabaseProductName()); + assertEquals("Snowflake", metaData.getDriverName()); + + // Snowflake JDBC driver version + String driverVersion = metaData.getDriverVersion(); + Matcher m = VERSION_PATTERN.matcher(driverVersion); + assertTrue(m.matches()); + int majorVersion = metaData.getDriverMajorVersion(); + int minorVersion = metaData.getDriverMinorVersion(); + assertEquals(m.group(1), String.valueOf(majorVersion)); + assertEquals(m.group(2), String.valueOf(minorVersion)); } @Test public void testGetCatalogs() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals(".", metaData.getCatalogSeparator()); - assertEquals("database", metaData.getCatalogTerm()); - - ResultSet resultSet = metaData.getCatalogs(); - verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_CATALOGS); - assertTrue(resultSet.isBeforeFirst()); - - int cnt = 0; - Set allVisibleDatabases = new HashSet<>(); - while (resultSet.next()) { - allVisibleDatabases.add(resultSet.getString(1)); - if (cnt == 0) { - assertTrue(resultSet.isFirst()); - } - ++cnt; - try { - resultSet.isLast(); - fail("No isLast support for query based metadata"); - } catch (SQLFeatureNotSupportedException ex) { - // nop - } - try { - resultSet.isAfterLast(); - fail("No isAfterLast support for query based metadata"); - } catch (SQLFeatureNotSupportedException ex) { - // nop - } + DatabaseMetaData metaData = con.getMetaData(); + assertEquals(".", metaData.getCatalogSeparator()); + assertEquals("database", metaData.getCatalogTerm()); + + ResultSet resultSet = metaData.getCatalogs(); + verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_CATALOGS); + assertTrue(resultSet.isBeforeFirst()); + + int cnt = 0; + Set allVisibleDatabases = new HashSet<>(); + while (resultSet.next()) { + allVisibleDatabases.add(resultSet.getString(1)); + if (cnt == 0) { + assertTrue(resultSet.isFirst()); } - assertThat(cnt, greaterThanOrEqualTo(1)); + ++cnt; try { - assertTrue(resultSet.isAfterLast()); - fail("The result set is automatically closed when all rows are fetched."); - } catch (SQLException ex) { - assertEquals((int) ErrorCode.RESULTSET_ALREADY_CLOSED.getMessageCode(), ex.getErrorCode()); + resultSet.isLast(); + fail("No isLast support for query based metadata"); + } catch (SQLFeatureNotSupportedException ex) { + // nop } try { resultSet.isAfterLast(); fail("No isAfterLast support for query based metadata"); - } catch (SQLException ex) { - assertEquals((int) ErrorCode.RESULTSET_ALREADY_CLOSED.getMessageCode(), ex.getErrorCode()); + } catch (SQLFeatureNotSupportedException ex) { + // nop } - resultSet.close(); // double closing does nothing. - resultSet.next(); // no exception + } + assertThat(cnt, greaterThanOrEqualTo(1)); + try { + assertTrue(resultSet.isAfterLast()); + fail("The result set is automatically closed when all rows are fetched."); + } catch (SQLException ex) { + assertEquals((int) ErrorCode.RESULTSET_ALREADY_CLOSED.getMessageCode(), ex.getErrorCode()); + } + try { + resultSet.isAfterLast(); + fail("No isAfterLast support for query based metadata"); + } catch (SQLException ex) { + assertEquals((int) ErrorCode.RESULTSET_ALREADY_CLOSED.getMessageCode(), ex.getErrorCode()); + } + resultSet.close(); // double closing does nothing. + resultSet.next(); // no exception - List allAccessibleDatabases = - getInfoBySQL("select database_name from information_schema.databases"); + List allAccessibleDatabases = + getInfoBySQL("select database_name from information_schema.databases"); - assertTrue(allVisibleDatabases.containsAll(allAccessibleDatabases)); - } + assertTrue(allVisibleDatabases.containsAll(allAccessibleDatabases)); } @Test public void testGetSchemas() throws Throwable { // CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX = false - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - String currentSchema = connection.getSchema(); - assertEquals("schema", metaData.getSchemaTerm()); - Set schemas = new HashSet<>(); - try (ResultSet resultSet = metaData.getSchemas()) { - verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_SCHEMAS); - while (resultSet.next()) { - String schema = resultSet.getString(1); - if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) { - schemas.add(schema); - } + DatabaseMetaData metaData = con.getMetaData(); + String currentSchema = con.getSchema(); + assertEquals("schema", metaData.getSchemaTerm()); + Set schemas = new HashSet<>(); + try (ResultSet resultSet = metaData.getSchemas()) { + verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_SCHEMAS); + while (resultSet.next()) { + String schema = resultSet.getString(1); + if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) { + schemas.add(schema); } } - assertThat(schemas.size(), greaterThanOrEqualTo(1)); + } + assertThat(schemas.size(), greaterThanOrEqualTo(1)); - Set schemasInDb = new HashSet<>(); - try (ResultSet resultSet = metaData.getSchemas(connection.getCatalog(), "%")) { - while (resultSet.next()) { - String schema = resultSet.getString(1); - if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) { - schemasInDb.add(schema); - } + Set schemasInDb = new HashSet<>(); + try (ResultSet resultSet = metaData.getSchemas(con.getCatalog(), "%")) { + while (resultSet.next()) { + String schema = resultSet.getString(1); + if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) { + schemasInDb.add(schema); } } - assertThat(schemasInDb.size(), greaterThanOrEqualTo(1)); - assertThat(schemas.size(), greaterThanOrEqualTo(schemasInDb.size())); - schemasInDb.forEach(schemaInDb -> assertThat(schemas, hasItem(schemaInDb))); - assertTrue(schemas.contains(currentSchema)); - assertTrue(schemasInDb.contains(currentSchema)); } + assertThat(schemasInDb.size(), greaterThanOrEqualTo(1)); + assertThat(schemas.size(), greaterThanOrEqualTo(schemasInDb.size())); + schemasInDb.forEach(schemaInDb -> assertThat(schemas, hasItem(schemaInDb))); + assertTrue(schemas.contains(currentSchema)); + assertTrue(schemasInDb.contains(currentSchema)); // CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX = true try (Connection connection = getConnection(); Statement statement = connection.createStatement()) { statement.execute("alter SESSION set CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX=true"); - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals("schema", metaData.getSchemaTerm()); - try (ResultSet resultSet = metaData.getSchemas()) { - Set schemas = new HashSet<>(); + DatabaseMetaData metaData2 = connection.getMetaData(); + assertEquals("schema", metaData2.getSchemaTerm()); + try (ResultSet resultSet = metaData2.getSchemas()) { + Set schemas2 = new HashSet<>(); while (resultSet.next()) { - schemas.add(resultSet.getString(1)); + schemas2.add(resultSet.getString(1)); } - assertThat(schemas.size(), equalTo(1)); + assertThat(schemas2.size(), equalTo(1)); } } } @Test public void testGetTableTypes() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - try (ResultSet resultSet = metaData.getTableTypes()) { - Set types = new HashSet<>(); - while (resultSet.next()) { - types.add(resultSet.getString(1)); - } - assertEquals(2, types.size()); - assertTrue(types.contains("TABLE")); - assertTrue(types.contains("VIEW")); + DatabaseMetaData metaData = con.getMetaData(); + try (ResultSet resultSet = metaData.getTableTypes()) { + Set types = new HashSet<>(); + while (resultSet.next()) { + types.add(resultSet.getString(1)); } + assertEquals(2, types.size()); + assertTrue(types.contains("TABLE")); + assertTrue(types.contains("VIEW")); } } @@ -218,17 +224,16 @@ public void testGetTableTypes() throws Throwable { @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetTables() throws Throwable { Set tables = null; - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable = "T0"; final String targetView = "V0"; try { statement.execute("create or replace table " + targetTable + "(C1 int)"); statement.execute("create or replace view " + targetView + " as select 1 as C"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); // match table try (ResultSet resultSet = @@ -271,16 +276,15 @@ public void testGetTables() throws Throwable { @Test public void testGetPrimarykeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable = "T0"; try { statement.execute( "create or replace table " + targetTable + "(C1 int primary key, C2 string)"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getPrimaryKeys(database, schema, targetTable)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_PRIMARY_KEYS); @@ -340,10 +344,9 @@ static void verifyResultSetMetaDataColumns( @Test public void testGetImportedKeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -356,7 +359,7 @@ public void testGetImportedKeys() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getImportedKeys(database, schema, targetTable2)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_FOREIGN_KEYS); @@ -386,10 +389,9 @@ public void testGetImportedKeys() throws Throwable { @Test public void testGetExportedKeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); ) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -402,7 +404,7 @@ public void testGetExportedKeys() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getExportedKeys(database, schema, targetTable1)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_FOREIGN_KEYS); @@ -433,10 +435,9 @@ public void testGetExportedKeys() throws Throwable { @Test public void testGetCrossReferences() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -449,7 +450,7 @@ public void testGetCrossReferences() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getCrossReference( @@ -482,17 +483,16 @@ public void testGetCrossReferences() throws Throwable { @Test public void testGetObjectsDoesNotExists() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable = "T0"; final String targetView = "V0"; try { statement.execute("create or replace table " + targetTable + "(C1 int)"); statement.execute("create or replace view " + targetView + " as select 1 as C"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); // sanity check if getTables really works. try (ResultSet resultSet = metaData.getTables(database, schema, "%", null)) { @@ -544,56 +544,51 @@ public void testGetObjectsDoesNotExists() throws Throwable { @Test public void testTypeInfo() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - ResultSet resultSet = metaData.getTypeInfo(); - resultSet.next(); - assertEquals("NUMBER", resultSet.getString(1)); - resultSet.next(); - assertEquals("INTEGER", resultSet.getString(1)); - resultSet.next(); - assertEquals("DOUBLE", resultSet.getString(1)); - resultSet.next(); - assertEquals("VARCHAR", resultSet.getString(1)); - resultSet.next(); - assertEquals("DATE", resultSet.getString(1)); - resultSet.next(); - assertEquals("TIME", resultSet.getString(1)); - resultSet.next(); - assertEquals("TIMESTAMP", resultSet.getString(1)); - resultSet.next(); - assertEquals("BOOLEAN", resultSet.getString(1)); - assertFalse(resultSet.next()); - } + DatabaseMetaData metaData = con.getMetaData(); + ResultSet resultSet = metaData.getTypeInfo(); + resultSet.next(); + assertEquals("NUMBER", resultSet.getString(1)); + resultSet.next(); + assertEquals("INTEGER", resultSet.getString(1)); + resultSet.next(); + assertEquals("DOUBLE", resultSet.getString(1)); + resultSet.next(); + assertEquals("VARCHAR", resultSet.getString(1)); + resultSet.next(); + assertEquals("DATE", resultSet.getString(1)); + resultSet.next(); + assertEquals("TIME", resultSet.getString(1)); + resultSet.next(); + assertEquals("TIMESTAMP", resultSet.getString(1)); + resultSet.next(); + assertEquals("BOOLEAN", resultSet.getString(1)); + assertFalse(resultSet.next()); } @Test public void testProcedure() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals("procedure", metaData.getProcedureTerm()); - // no stored procedure support - assertTrue(metaData.supportsStoredProcedures()); - try (ResultSet resultSet = metaData.getProcedureColumns("%", "%", "%", "%")) { - assertEquals(0, getSizeOfResultSet(resultSet)); - } - try (ResultSet resultSet = metaData.getProcedures("%", "%", "%")) { - assertEquals(0, getSizeOfResultSet(resultSet)); - } + DatabaseMetaData metaData = con.getMetaData(); + assertEquals("procedure", metaData.getProcedureTerm()); + // no stored procedure support + assertTrue(metaData.supportsStoredProcedures()); + try (ResultSet resultSet = metaData.getProcedureColumns("%", "%", "%", "%")) { + assertEquals(0, getSizeOfResultSet(resultSet)); + } + try (ResultSet resultSet = metaData.getProcedures("%", "%", "%")) { + assertEquals(0, getSizeOfResultSet(resultSet)); } } @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetTablePrivileges() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); try { statement.execute( "create or replace table PRIVTEST(colA string, colB number, colC " + "timestamp)"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getTablePrivileges(database, schema, "PRIVTEST")) { verifyResultSetMetaDataColumns( resultSet, DBMetadataResultSetMetadata.GET_TABLE_PRIVILEGES); @@ -641,15 +636,14 @@ public void testGetTablePrivileges() throws Exception { @Test public void testGetProcedures() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = con.createStatement()) { try { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = con.getCatalog(); + String schema = con.getSchema(); /* Create a procedure and put values into it */ statement.execute(PI_PROCEDURE); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); /* Call getFunctionColumns on FUNC111 and since there's no parameter name, get all rows back */ try (ResultSet resultSet = metaData.getProcedures(database, schema, "GETPI")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_PROCEDURES); @@ -670,197 +664,189 @@ public void testGetProcedures() throws SQLException { @Test public void testDatabaseMetadata() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - - String dbVersion = metaData.getDatabaseProductVersion(); - Matcher m = VERSION_PATTERN.matcher(dbVersion); - assertTrue(m.matches()); - int majorVersion = metaData.getDatabaseMajorVersion(); - int minorVersion = metaData.getDatabaseMinorVersion(); - assertEquals(m.group(1), String.valueOf(majorVersion)); - assertEquals(m.group(2), String.valueOf(minorVersion)); - - assertFalse(Strings.isNullOrEmpty(metaData.getSQLKeywords())); - assertFalse(Strings.isNullOrEmpty(metaData.getNumericFunctions())); - assertFalse(Strings.isNullOrEmpty(metaData.getStringFunctions())); - assertFalse(Strings.isNullOrEmpty(metaData.getSystemFunctions())); - assertFalse(Strings.isNullOrEmpty(metaData.getTimeDateFunctions())); - - assertEquals("\\", metaData.getSearchStringEscape()); - - assertTrue(metaData.getURL().startsWith("jdbc:snowflake://")); - assertFalse(metaData.allProceduresAreCallable()); - assertTrue(metaData.allTablesAreSelectable()); - assertTrue(metaData.dataDefinitionCausesTransactionCommit()); - assertFalse(metaData.dataDefinitionIgnoredInTransactions()); - assertFalse(metaData.deletesAreDetected(1)); - assertTrue(metaData.doesMaxRowSizeIncludeBlobs()); - assertTrue(metaData.supportsTransactions()); - assertEquals( - Connection.TRANSACTION_READ_COMMITTED, metaData.getDefaultTransactionIsolation()); - assertEquals("$", metaData.getExtraNameCharacters()); - assertEquals("\"", metaData.getIdentifierQuoteString()); - assertEquals(0, getSizeOfResultSet(metaData.getIndexInfo(null, null, null, true, true))); - assertEquals(EXPECTED_MAX_BINARY_LENGTH, metaData.getMaxBinaryLiteralLength()); - assertEquals(255, metaData.getMaxCatalogNameLength()); - assertEquals(EXPECTED_MAX_CHAR_LENGTH, metaData.getMaxCharLiteralLength()); - assertEquals(255, metaData.getMaxColumnNameLength()); - assertEquals(0, metaData.getMaxColumnsInGroupBy()); - assertEquals(0, metaData.getMaxColumnsInIndex()); - assertEquals(0, metaData.getMaxColumnsInOrderBy()); - assertEquals(0, metaData.getMaxColumnsInSelect()); - assertEquals(0, metaData.getMaxColumnsInTable()); - assertEquals(0, metaData.getMaxConnections()); - assertEquals(0, metaData.getMaxCursorNameLength()); - assertEquals(0, metaData.getMaxIndexLength()); - assertEquals(0, metaData.getMaxProcedureNameLength()); - assertEquals(0, metaData.getMaxRowSize()); - assertEquals(255, metaData.getMaxSchemaNameLength()); - assertEquals(0, metaData.getMaxStatementLength()); - assertEquals(0, metaData.getMaxStatements()); - assertEquals(255, metaData.getMaxTableNameLength()); - assertEquals(0, metaData.getMaxTablesInSelect()); - assertEquals(255, metaData.getMaxUserNameLength()); - assertEquals(0, getSizeOfResultSet(metaData.getTablePrivileges(null, null, null))); - // assertEquals("", metaData.getTimeDateFunctions()); - assertEquals(TestUtil.systemGetEnv("SNOWFLAKE_TEST_USER"), metaData.getUserName()); - assertFalse(metaData.insertsAreDetected(1)); - assertTrue(metaData.isCatalogAtStart()); - assertFalse(metaData.isReadOnly()); - assertTrue(metaData.nullPlusNonNullIsNull()); - assertFalse(metaData.nullsAreSortedAtEnd()); - assertFalse(metaData.nullsAreSortedAtStart()); - assertTrue(metaData.nullsAreSortedHigh()); - assertFalse(metaData.nullsAreSortedLow()); - assertFalse(metaData.othersDeletesAreVisible(1)); - assertFalse(metaData.othersInsertsAreVisible(1)); - assertFalse(metaData.othersUpdatesAreVisible(1)); - assertFalse(metaData.ownDeletesAreVisible(1)); - assertFalse(metaData.ownInsertsAreVisible(1)); - assertFalse(metaData.ownUpdatesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE)); - assertFalse(metaData.storesLowerCaseIdentifiers()); - assertFalse(metaData.storesLowerCaseQuotedIdentifiers()); - assertFalse(metaData.storesMixedCaseIdentifiers()); - assertTrue(metaData.storesMixedCaseQuotedIdentifiers()); - assertTrue(metaData.storesUpperCaseIdentifiers()); - assertFalse(metaData.storesUpperCaseQuotedIdentifiers()); - assertTrue(metaData.supportsAlterTableWithAddColumn()); - assertTrue(metaData.supportsAlterTableWithDropColumn()); - assertTrue(metaData.supportsANSI92EntryLevelSQL()); - assertFalse(metaData.supportsANSI92FullSQL()); - assertFalse(metaData.supportsANSI92IntermediateSQL()); - assertTrue(metaData.supportsBatchUpdates()); - assertTrue(metaData.supportsCatalogsInDataManipulation()); - assertFalse(metaData.supportsCatalogsInIndexDefinitions()); - assertFalse(metaData.supportsCatalogsInPrivilegeDefinitions()); - assertFalse(metaData.supportsCatalogsInProcedureCalls()); - assertTrue(metaData.supportsCatalogsInTableDefinitions()); - assertTrue(metaData.supportsColumnAliasing()); - assertFalse(metaData.supportsConvert()); - assertFalse(metaData.supportsConvert(1, 2)); - assertFalse(metaData.supportsCoreSQLGrammar()); - assertTrue(metaData.supportsCorrelatedSubqueries()); - assertTrue(metaData.supportsDataDefinitionAndDataManipulationTransactions()); - assertFalse(metaData.supportsDataManipulationTransactionsOnly()); - assertFalse(metaData.supportsDifferentTableCorrelationNames()); - assertTrue(metaData.supportsExpressionsInOrderBy()); - assertFalse(metaData.supportsExtendedSQLGrammar()); - assertTrue(metaData.supportsFullOuterJoins()); - assertFalse(metaData.supportsGetGeneratedKeys()); - assertTrue(metaData.supportsGroupBy()); - assertTrue(metaData.supportsGroupByBeyondSelect()); - assertFalse(metaData.supportsGroupByUnrelated()); - assertFalse(metaData.supportsIntegrityEnhancementFacility()); - assertFalse(metaData.supportsLikeEscapeClause()); - assertTrue(metaData.supportsLimitedOuterJoins()); - assertFalse(metaData.supportsMinimumSQLGrammar()); - assertFalse(metaData.supportsMixedCaseIdentifiers()); - assertTrue(metaData.supportsMixedCaseQuotedIdentifiers()); - assertFalse(metaData.supportsMultipleOpenResults()); - assertFalse(metaData.supportsMultipleResultSets()); - assertTrue(metaData.supportsMultipleTransactions()); - assertFalse(metaData.supportsNamedParameters()); - assertTrue(metaData.supportsNonNullableColumns()); - assertFalse(metaData.supportsOpenCursorsAcrossCommit()); - assertFalse(metaData.supportsOpenCursorsAcrossRollback()); - assertFalse(metaData.supportsOpenStatementsAcrossCommit()); - assertFalse(metaData.supportsOpenStatementsAcrossRollback()); - assertTrue(metaData.supportsOrderByUnrelated()); - assertTrue(metaData.supportsOuterJoins()); - assertFalse(metaData.supportsPositionedDelete()); - assertFalse(metaData.supportsPositionedUpdate()); - assertTrue( - metaData.supportsResultSetConcurrency( - ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); - assertFalse( - metaData.supportsResultSetConcurrency( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)); - assertTrue(metaData.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)); - assertTrue(metaData.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)); - assertFalse(metaData.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)); - assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, metaData.getResultSetHoldability()); - assertFalse(metaData.supportsSavepoints()); - assertTrue(metaData.supportsSchemasInDataManipulation()); - assertFalse(metaData.supportsSchemasInIndexDefinitions()); - assertFalse(metaData.supportsSchemasInPrivilegeDefinitions()); - assertFalse(metaData.supportsSchemasInProcedureCalls()); - assertTrue(metaData.supportsSchemasInTableDefinitions()); - assertFalse(metaData.supportsSelectForUpdate()); - assertFalse(metaData.supportsStatementPooling()); - assertTrue(metaData.supportsStoredFunctionsUsingCallSyntax()); - assertTrue(metaData.supportsSubqueriesInComparisons()); - assertTrue(metaData.supportsSubqueriesInExists()); - assertTrue(metaData.supportsSubqueriesInIns()); - assertFalse(metaData.supportsSubqueriesInQuantifieds()); - assertTrue(metaData.supportsTableCorrelationNames()); - assertTrue(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED)); - assertFalse( - metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ)); - assertFalse(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)); - assertFalse( - metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)); - assertTrue(metaData.supportsUnion()); - assertTrue(metaData.supportsUnionAll()); - assertFalse(metaData.updatesAreDetected(1)); - assertFalse(metaData.usesLocalFilePerTable()); - assertFalse(metaData.usesLocalFiles()); - } + DatabaseMetaData metaData = con.getMetaData(); + + String dbVersion = metaData.getDatabaseProductVersion(); + Matcher m = VERSION_PATTERN.matcher(dbVersion); + assertTrue(m.matches()); + int majorVersion = metaData.getDatabaseMajorVersion(); + int minorVersion = metaData.getDatabaseMinorVersion(); + assertEquals(m.group(1), String.valueOf(majorVersion)); + assertEquals(m.group(2), String.valueOf(minorVersion)); + + assertFalse(Strings.isNullOrEmpty(metaData.getSQLKeywords())); + assertFalse(Strings.isNullOrEmpty(metaData.getNumericFunctions())); + assertFalse(Strings.isNullOrEmpty(metaData.getStringFunctions())); + assertFalse(Strings.isNullOrEmpty(metaData.getSystemFunctions())); + assertFalse(Strings.isNullOrEmpty(metaData.getTimeDateFunctions())); + + assertEquals("\\", metaData.getSearchStringEscape()); + + assertTrue(metaData.getURL().startsWith("jdbc:snowflake://")); + assertFalse(metaData.allProceduresAreCallable()); + assertTrue(metaData.allTablesAreSelectable()); + assertTrue(metaData.dataDefinitionCausesTransactionCommit()); + assertFalse(metaData.dataDefinitionIgnoredInTransactions()); + assertFalse(metaData.deletesAreDetected(1)); + assertTrue(metaData.doesMaxRowSizeIncludeBlobs()); + assertTrue(metaData.supportsTransactions()); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, metaData.getDefaultTransactionIsolation()); + assertEquals("$", metaData.getExtraNameCharacters()); + assertEquals("\"", metaData.getIdentifierQuoteString()); + assertEquals(0, getSizeOfResultSet(metaData.getIndexInfo(null, null, null, true, true))); + assertEquals(EXPECTED_MAX_BINARY_LENGTH, metaData.getMaxBinaryLiteralLength()); + assertEquals(255, metaData.getMaxCatalogNameLength()); + assertEquals(EXPECTED_MAX_CHAR_LENGTH, metaData.getMaxCharLiteralLength()); + assertEquals(255, metaData.getMaxColumnNameLength()); + assertEquals(0, metaData.getMaxColumnsInGroupBy()); + assertEquals(0, metaData.getMaxColumnsInIndex()); + assertEquals(0, metaData.getMaxColumnsInOrderBy()); + assertEquals(0, metaData.getMaxColumnsInSelect()); + assertEquals(0, metaData.getMaxColumnsInTable()); + assertEquals(0, metaData.getMaxConnections()); + assertEquals(0, metaData.getMaxCursorNameLength()); + assertEquals(0, metaData.getMaxIndexLength()); + assertEquals(0, metaData.getMaxProcedureNameLength()); + assertEquals(0, metaData.getMaxRowSize()); + assertEquals(255, metaData.getMaxSchemaNameLength()); + assertEquals(0, metaData.getMaxStatementLength()); + assertEquals(0, metaData.getMaxStatements()); + assertEquals(255, metaData.getMaxTableNameLength()); + assertEquals(0, metaData.getMaxTablesInSelect()); + assertEquals(255, metaData.getMaxUserNameLength()); + assertEquals(0, getSizeOfResultSet(metaData.getTablePrivileges(null, null, null))); + // assertEquals("", metaData.getTimeDateFunctions()); + assertEquals(TestUtil.systemGetEnv("SNOWFLAKE_TEST_USER"), metaData.getUserName()); + assertFalse(metaData.insertsAreDetected(1)); + assertTrue(metaData.isCatalogAtStart()); + assertFalse(metaData.isReadOnly()); + assertTrue(metaData.nullPlusNonNullIsNull()); + assertFalse(metaData.nullsAreSortedAtEnd()); + assertFalse(metaData.nullsAreSortedAtStart()); + assertTrue(metaData.nullsAreSortedHigh()); + assertFalse(metaData.nullsAreSortedLow()); + assertFalse(metaData.othersDeletesAreVisible(1)); + assertFalse(metaData.othersInsertsAreVisible(1)); + assertFalse(metaData.othersUpdatesAreVisible(1)); + assertFalse(metaData.ownDeletesAreVisible(1)); + assertFalse(metaData.ownInsertsAreVisible(1)); + assertFalse(metaData.ownUpdatesAreVisible(ResultSet.TYPE_SCROLL_INSENSITIVE)); + assertFalse(metaData.storesLowerCaseIdentifiers()); + assertFalse(metaData.storesLowerCaseQuotedIdentifiers()); + assertFalse(metaData.storesMixedCaseIdentifiers()); + assertTrue(metaData.storesMixedCaseQuotedIdentifiers()); + assertTrue(metaData.storesUpperCaseIdentifiers()); + assertFalse(metaData.storesUpperCaseQuotedIdentifiers()); + assertTrue(metaData.supportsAlterTableWithAddColumn()); + assertTrue(metaData.supportsAlterTableWithDropColumn()); + assertTrue(metaData.supportsANSI92EntryLevelSQL()); + assertFalse(metaData.supportsANSI92FullSQL()); + assertFalse(metaData.supportsANSI92IntermediateSQL()); + assertTrue(metaData.supportsBatchUpdates()); + assertTrue(metaData.supportsCatalogsInDataManipulation()); + assertFalse(metaData.supportsCatalogsInIndexDefinitions()); + assertFalse(metaData.supportsCatalogsInPrivilegeDefinitions()); + assertFalse(metaData.supportsCatalogsInProcedureCalls()); + assertTrue(metaData.supportsCatalogsInTableDefinitions()); + assertTrue(metaData.supportsColumnAliasing()); + assertFalse(metaData.supportsConvert()); + assertFalse(metaData.supportsConvert(1, 2)); + assertFalse(metaData.supportsCoreSQLGrammar()); + assertTrue(metaData.supportsCorrelatedSubqueries()); + assertTrue(metaData.supportsDataDefinitionAndDataManipulationTransactions()); + assertFalse(metaData.supportsDataManipulationTransactionsOnly()); + assertFalse(metaData.supportsDifferentTableCorrelationNames()); + assertTrue(metaData.supportsExpressionsInOrderBy()); + assertFalse(metaData.supportsExtendedSQLGrammar()); + assertTrue(metaData.supportsFullOuterJoins()); + assertFalse(metaData.supportsGetGeneratedKeys()); + assertTrue(metaData.supportsGroupBy()); + assertTrue(metaData.supportsGroupByBeyondSelect()); + assertFalse(metaData.supportsGroupByUnrelated()); + assertFalse(metaData.supportsIntegrityEnhancementFacility()); + assertFalse(metaData.supportsLikeEscapeClause()); + assertTrue(metaData.supportsLimitedOuterJoins()); + assertFalse(metaData.supportsMinimumSQLGrammar()); + assertFalse(metaData.supportsMixedCaseIdentifiers()); + assertTrue(metaData.supportsMixedCaseQuotedIdentifiers()); + assertFalse(metaData.supportsMultipleOpenResults()); + assertFalse(metaData.supportsMultipleResultSets()); + assertTrue(metaData.supportsMultipleTransactions()); + assertFalse(metaData.supportsNamedParameters()); + assertTrue(metaData.supportsNonNullableColumns()); + assertFalse(metaData.supportsOpenCursorsAcrossCommit()); + assertFalse(metaData.supportsOpenCursorsAcrossRollback()); + assertFalse(metaData.supportsOpenStatementsAcrossCommit()); + assertFalse(metaData.supportsOpenStatementsAcrossRollback()); + assertTrue(metaData.supportsOrderByUnrelated()); + assertTrue(metaData.supportsOuterJoins()); + assertFalse(metaData.supportsPositionedDelete()); + assertFalse(metaData.supportsPositionedUpdate()); + assertTrue( + metaData.supportsResultSetConcurrency( + ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)); + assertFalse( + metaData.supportsResultSetConcurrency( + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)); + assertTrue(metaData.supportsResultSetType(ResultSet.TYPE_FORWARD_ONLY)); + assertTrue(metaData.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT)); + assertFalse(metaData.supportsResultSetHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT)); + assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, metaData.getResultSetHoldability()); + assertFalse(metaData.supportsSavepoints()); + assertTrue(metaData.supportsSchemasInDataManipulation()); + assertFalse(metaData.supportsSchemasInIndexDefinitions()); + assertFalse(metaData.supportsSchemasInPrivilegeDefinitions()); + assertFalse(metaData.supportsSchemasInProcedureCalls()); + assertTrue(metaData.supportsSchemasInTableDefinitions()); + assertFalse(metaData.supportsSelectForUpdate()); + assertFalse(metaData.supportsStatementPooling()); + assertTrue(metaData.supportsStoredFunctionsUsingCallSyntax()); + assertTrue(metaData.supportsSubqueriesInComparisons()); + assertTrue(metaData.supportsSubqueriesInExists()); + assertTrue(metaData.supportsSubqueriesInIns()); + assertFalse(metaData.supportsSubqueriesInQuantifieds()); + assertTrue(metaData.supportsTableCorrelationNames()); + assertTrue(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED)); + assertFalse(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_REPEATABLE_READ)); + assertFalse(metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)); + assertFalse( + metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED)); + assertTrue(metaData.supportsUnion()); + assertTrue(metaData.supportsUnionAll()); + assertFalse(metaData.updatesAreDetected(1)); + assertFalse(metaData.usesLocalFilePerTable()); + assertFalse(metaData.usesLocalFiles()); } @Test public void testOtherEmptyTables() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); - // index is not supported. - try (ResultSet resultSet = metaData.getIndexInfo(null, null, null, true, true)) { - assertEquals(0, getSizeOfResultSet(resultSet)); - } - // UDT is not supported. - try (ResultSet resultSet = metaData.getUDTs(null, null, null, new int[] {})) { - assertEquals(0, getSizeOfResultSet(resultSet)); - } + // index is not supported. + try (ResultSet resultSet = metaData.getIndexInfo(null, null, null, true, true)) { + assertEquals(0, getSizeOfResultSet(resultSet)); + } + // UDT is not supported. + try (ResultSet resultSet = metaData.getUDTs(null, null, null, new int[] {})) { + assertEquals(0, getSizeOfResultSet(resultSet)); } } @Test public void testFeatureNotSupportedException() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - expectFeatureNotSupportedException( - () -> metaData.getBestRowIdentifier(null, null, null, 0, true)); - expectFeatureNotSupportedException(() -> metaData.getVersionColumns(null, null, null)); - expectFeatureNotSupportedException(() -> metaData.getSuperTypes(null, null, null)); - expectFeatureNotSupportedException(() -> metaData.getSuperTables(null, null, null)); - expectFeatureNotSupportedException(() -> metaData.getAttributes(null, null, null, null)); - expectFeatureNotSupportedException(metaData::getRowIdLifetime); - expectFeatureNotSupportedException(metaData::autoCommitFailureClosesAllResultSets); - expectFeatureNotSupportedException(metaData::getClientInfoProperties); - expectFeatureNotSupportedException(() -> metaData.getPseudoColumns(null, null, null, null)); - expectFeatureNotSupportedException(metaData::generatedKeyAlwaysReturned); - expectFeatureNotSupportedException( - () -> metaData.isWrapperFor(SnowflakeDatabaseMetaData.class)); - } + DatabaseMetaData metaData = con.getMetaData(); + expectFeatureNotSupportedException( + () -> metaData.getBestRowIdentifier(null, null, null, 0, true)); + expectFeatureNotSupportedException(() -> metaData.getVersionColumns(null, null, null)); + expectFeatureNotSupportedException(() -> metaData.getSuperTypes(null, null, null)); + expectFeatureNotSupportedException(() -> metaData.getSuperTables(null, null, null)); + expectFeatureNotSupportedException(() -> metaData.getAttributes(null, null, null, null)); + expectFeatureNotSupportedException(metaData::getRowIdLifetime); + expectFeatureNotSupportedException(metaData::autoCommitFailureClosesAllResultSets); + expectFeatureNotSupportedException(metaData::getClientInfoProperties); + expectFeatureNotSupportedException(() -> metaData.getPseudoColumns(null, null, null, null)); + expectFeatureNotSupportedException(metaData::generatedKeyAlwaysReturned); + expectFeatureNotSupportedException( + () -> metaData.isWrapperFor(SnowflakeDatabaseMetaData.class)); } } diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index bebe3d8f4..d9a314502 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -36,6 +36,8 @@ import net.snowflake.client.category.TestCategoryOthers; import net.snowflake.client.core.SFBaseSession; import net.snowflake.client.core.SFSessionProperty; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -86,6 +88,20 @@ public void createDoubleQuotedSchemaAndCatalog(Statement statement) throws SQLEx statement.execute("create or replace schema \"dbwith\"\"quotes\".\"schemawith\"\"quotes\""); } + private static Connection con; + + @BeforeClass + public static void setUpConnection() throws SQLException { + con = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (con != null && !con.isClosed()) { + con.close(); + } + } + /** * Tests for getFunctions * @@ -199,8 +215,7 @@ public void testUseConnectionCtx() throws Exception { */ @Test public void testDoubleQuotedDatabaseAndSchema() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { String database = con.getCatalog(); // To query the schema and table, we can use a normal java escaped quote. Wildcards are also // escaped here @@ -254,8 +269,7 @@ public void testDoubleQuotedDatabaseAndSchema() throws Exception { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetSchemas() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database with double quotes inside the database name statement.execute("create or replace database \"\"\"quoteddb\"\"\""); // Create a database, lowercase, with no double quotes inside the database name @@ -283,8 +297,7 @@ public void testDoubleQuotedDatabaseInGetSchemas() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetTables() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with two columns @@ -300,8 +313,7 @@ public void testDoubleQuotedDatabaseInGetTables() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetColumns() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with two columns @@ -317,8 +329,7 @@ public void testDoubleQuotedDatabaseInGetColumns() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseforGetPrimaryKeysAndForeignKeys() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with a primary key constraint @@ -376,8 +387,7 @@ public void testDoubleQuotedDatabaseforGetPrimaryKeysAndForeignKeysWithPatternSe @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetProcedures() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a procedure @@ -394,8 +404,7 @@ public void testDoubleQuotedDatabaseInGetProcedures() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetTablePrivileges() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table under the current user and role @@ -455,8 +464,7 @@ public void testGetFunctionSqlInjectionProtection() throws Throwable { */ @Test public void testGetProcedureColumnsWildcards() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { String database = con.getCatalog(); String schemaPrefix = TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase(); @@ -489,17 +497,15 @@ public void testGetProcedureColumnsWildcards() throws Exception { @Test public void testGetFunctions() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metadata = connection.getMetaData(); - String supportedStringFuncs = metadata.getStringFunctions(); - assertEquals(StringFunctionsSupported, supportedStringFuncs); + DatabaseMetaData metadata = con.getMetaData(); + String supportedStringFuncs = metadata.getStringFunctions(); + assertEquals(StringFunctionsSupported, supportedStringFuncs); - String supportedNumberFuncs = metadata.getNumericFunctions(); - assertEquals(NumericFunctionsSupported, supportedNumberFuncs); + String supportedNumberFuncs = metadata.getNumericFunctions(); + assertEquals(NumericFunctionsSupported, supportedNumberFuncs); - String supportedSystemFuncs = metadata.getSystemFunctions(); - assertEquals(SystemFunctionsSupported, supportedSystemFuncs); - } + String supportedSystemFuncs = metadata.getSystemFunctions(); + assertEquals(SystemFunctionsSupported, supportedSystemFuncs); } @Test @@ -553,10 +559,9 @@ public void testGetStringValueFromColumnDef() throws SQLException { @Test public void testGetColumnsNullable() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable = "T0"; statement.execute( @@ -566,7 +571,7 @@ public void testGetColumnsNullable() throws Throwable { + " C6 boolean, C7 date not null, C8 time, C9 timestamp_ntz(7), C10 binary,C11" + " variant, C12 timestamp_ltz(8), C13 timestamp_tz(3))"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_COLUMNS); @@ -748,10 +753,9 @@ public void testSessionDatabaseParameter() throws Throwable { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetFunctionColumns() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); /* Create a table and put values into it */ statement.execute( @@ -778,7 +782,7 @@ public void testGetFunctionColumns() throws Exception { + "binary(8388608) , sharedCol decimal) COMMENT= 'returns table of 4 columns' as 'select" + " JDBC_TBL111.colA, JDBC_TBL111.colB, BIN_TABLE.bin2, BIN_TABLE.sharedCol from" + " JDBC_TBL111 inner join BIN_TABLE on JDBC_TBL111.colB =BIN_TABLE.sharedCol'"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); /* Call getFunctionColumns on FUNC111 and since there's no parameter name, get all rows back */ try (ResultSet resultSet = metaData.getFunctionColumns(database, schema, "FUNC111", "%")) { @@ -1010,11 +1014,10 @@ public void testGetFunctionColumns() throws Exception { @Test public void testHandlingSpecialChars() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); - DatabaseMetaData metaData = connection.getMetaData(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); + DatabaseMetaData metaData = con.getMetaData(); String escapeChar = metaData.getSearchStringEscape(); // test getColumns with escaped special characters in table name statement.execute( @@ -1120,8 +1123,7 @@ public void testHandlingSpecialChars() throws Exception { @Test public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeys() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { String database = con.getCatalog(); TestUtil.withRandomSchema( statement, @@ -1153,8 +1155,7 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeysWithPattern Properties properties = new Properties(); properties.put(ENABLE_PATTERN_SEARCH, false); - try (Connection con = getConnection(properties); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { String database = con.getCatalog(); TestUtil.withRandomSchema( statement, @@ -1215,10 +1216,9 @@ public void testTimestampWithTimezoneDataType() throws Exception { @Test public void testGetColumns() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); final String targetTable = "T0"; try { statement.execute( @@ -1228,7 +1228,7 @@ public void testGetColumns() throws Throwable { + " C6 boolean, C7 date not null, C8 time, C9 timestamp_ntz(7), C10 binary(8388608),C11" + " variant, C12 timestamp_ltz(8), C13 timestamp_tz(3))"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_COLUMNS); @@ -1566,7 +1566,7 @@ public void testGetColumns() throws Throwable { + " default '\"apples\"', C5 int, C6 int default 5, C7 string default '''', C8" + " string default '''apples''''', C9 string default '%')"); - metaData = connection.getMetaData(); + metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { assertTrue(resultSet.next()); @@ -1610,8 +1610,7 @@ public void testGetColumns() throws Throwable { public void testGetStreams() throws SQLException { final String targetStream = "S0"; final String targetTable = "T0"; - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { String database = con.getCatalog(); String schema = con.getSchema(); String owner = con.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole(); @@ -1664,24 +1663,21 @@ public void testGetStreams() throws SQLException { @Test @Ignore public void testGetProceduresWithReaderAccount() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metadata = connection.getMetaData(); - try (ResultSet rs = metadata.getProcedures(null, null, null)) { - assertEquals(0, getSizeOfResultSet(rs)); - } + DatabaseMetaData metadata = con.getMetaData(); + try (ResultSet rs = metadata.getProcedures(null, null, null)) { + assertEquals(0, getSizeOfResultSet(rs)); } } @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetProcedureColumns() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + try (Statement statement = con.createStatement()) { + String database = con.getCatalog(); + String schema = con.getSchema(); try { statement.execute(PI_PROCEDURE); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); /* Call getProcedureColumns with no parameters for procedure name or column. This should return all procedures in the current database and schema. It will return all rows as well (1 row per result and 1 row per parameter for each procedure) */ @@ -1724,8 +1720,7 @@ in the current database and schema. It will return all rows as well (1 row per r @Test public void testGetProcedureColumnsReturnsResultSet() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = con.createStatement()) { try { statement.execute( "create or replace table testtable (id int, name varchar(20), address varchar(20));"); @@ -1772,8 +1767,7 @@ public void testGetProcedureColumnsReturnsResultSet() throws SQLException { @Test public void testGetProcedureColumnsReturnsValue() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement(); ) { + try (Statement statement = con.createStatement(); ) { DatabaseMetaData metaData = con.getMetaData(); // create a procedure with no parameters that has a return value statement.execute(PI_PROCEDURE); @@ -1813,8 +1807,7 @@ public void testGetProcedureColumnsReturnsValue() throws SQLException { @Test public void testGetProcedureColumnsReturnsNull() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement(); ) { + try (Statement statement = con.createStatement(); ) { DatabaseMetaData metaData = con.getMetaData(); // The CREATE PROCEDURE statement must include a RETURNS clause that defines a return type, // even @@ -1847,10 +1840,8 @@ public void testGetProcedureColumnsReturnsNull() throws SQLException { @Test public void testUpdateLocatorsCopyUnsupported() throws SQLException { - try (Connection con = getConnection()) { - DatabaseMetaData metaData = con.getMetaData(); - assertFalse(metaData.locatorsUpdateCopy()); - } + DatabaseMetaData metaData = con.getMetaData(); + assertFalse(metaData.locatorsUpdateCopy()); } /** @@ -2106,8 +2097,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception final String table1 = "PATTERN_SEARCH_TABLE1"; final String table2 = "PATTERN_SEARCH_TABLE2"; - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = con.createStatement()) { String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5); String schemaName = "\"" @@ -2120,7 +2110,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception statement, schemaName, () -> { - String schema = connection.getSchema(); + String schema = con.getSchema(); statement.execute( "create or replace table " + table1 + "(C1 int primary key, C2 string)"); statement.execute( @@ -2130,13 +2120,13 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception + table1 + ")"); - DatabaseMetaData dbmd = connection.getMetaData(); + DatabaseMetaData dbmd = con.getMetaData(); String schemaPattern1 = schema.substring(0, schema.length() - 1).concat("%"); String schemaPattern2 = schema.substring(0, schema.length() - 1).concat("_"); String tablePattern1 = "PATTERN_SEARCH_TABLE%"; String tablePattern2 = "PATTERN_SEARCH_TABLE_"; - String database = connection.getCatalog(); + String database = con.getCatalog(); // Should return result for matching on either an exact schema and table name or a // pattern @@ -2327,37 +2317,29 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception */ @Test public void testGetJDBCVersion() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); - // JDBC x.x compatible - assertEquals(4, metaData.getJDBCMajorVersion()); - assertEquals(2, metaData.getJDBCMinorVersion()); - } + // JDBC x.x compatible + assertEquals(4, metaData.getJDBCMajorVersion()); + assertEquals(2, metaData.getJDBCMinorVersion()); } /** Added in > 3.15.1 */ @Test public void testKeywordsCount() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals(43, metaData.getSQLKeywords().split(",").length); - } + DatabaseMetaData metaData = con.getMetaData(); + assertEquals(43, metaData.getSQLKeywords().split(",").length); } /** Added in > 3.16.1 */ @Test public void testVectorDimension() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = con.createStatement()) { statement.execute( "create or replace table JDBC_VECTOR(text_col varchar(32), float_vec VECTOR(FLOAT, 256), int_vec VECTOR(INT, 16))"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); try (ResultSet resultSet = metaData.getColumns( - connection.getCatalog(), - connection.getSchema().replaceAll("_", "\\\\_"), - "JDBC\\_VECTOR", - null)) { + con.getCatalog(), con.getSchema().replaceAll("_", "\\\\_"), "JDBC\\_VECTOR", null)) { assertTrue(resultSet.next()); assertEquals(32, resultSet.getObject("COLUMN_SIZE")); assertTrue(resultSet.next()); From 50045cfc6ad0064292838670a33b3e76ef0d9b5d Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Thu, 11 Jul 2024 00:19:42 -0700 Subject: [PATCH 2/8] PR review changes --- .../snowflake/client/jdbc/BaseJDBCTest.java | 16 ++ .../jdbc/ClientMemoryLimitParallelIT.java | 14 +- .../snowflake/client/jdbc/ConnectionIT.java | 16 -- .../client/jdbc/DatabaseMetaDataIT.java | 106 +++++------ .../client/jdbc/DatabaseMetaDataLatestIT.java | 164 ++++++++---------- 5 files changed, 144 insertions(+), 172 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java index a326dea12..6c239b8c3 100644 --- a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java +++ b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java @@ -36,11 +36,27 @@ import javax.xml.transform.Source; import net.snowflake.client.AbstractDriverIT; import net.snowflake.client.core.SFException; +import org.junit.AfterClass; +import org.junit.BeforeClass; public class BaseJDBCTest extends AbstractDriverIT { // Test UUID unique per session static final String TEST_UUID = UUID.randomUUID().toString(); + protected static Connection connection; + + @BeforeClass + public static void setUpConnection() throws SQLException { + connection = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (connection != null && !connection.isClosed()) { + connection.close(); + } + } + protected interface MethodRaisesSQLException { void run() throws SQLException; } diff --git a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java index 63cdcda3b..2dca43564 100644 --- a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java @@ -64,30 +64,30 @@ public class ClientMemoryLimitParallelIT { + rowCount + "));"; - private static Connection con; + private static Connection connection; @BeforeClass public static void setUpConnection() throws SQLException { - con = getConnection(); + connection = getConnection(); } @AfterClass public static void closeConnection() throws SQLException { - if (con != null && !con.isClosed()) { - con.close(); + if (connection != null && !connection.isClosed()) { + connection.close(); } } @Before public void setUp() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute(createTestTableSQL); } } @After public void tearDown() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute("drop table if exists testtable_cml"); } } @@ -140,7 +140,7 @@ public void run() { @Test public void testQueryNotHanging() throws SQLException { Properties paramProperties = new Properties(); - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { queryRows(statement, 100, 160); } } diff --git a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java index 93d4c4e23..396b71498 100644 --- a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java @@ -47,9 +47,7 @@ import net.snowflake.client.core.SFSession; import net.snowflake.common.core.SqlState; import org.apache.commons.codec.binary.Base64; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; @@ -71,20 +69,6 @@ public class ConnectionIT extends BaseJDBCTest { @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); - private static Connection connection; - - @BeforeClass - public static void setUpConnection() throws SQLException { - connection = getConnection(); - } - - @AfterClass - public static void closeConnection() throws SQLException { - if (connection != null && !connection.isClosed()) { - connection.close(); - } - } - @Test public void testSimpleConnection() throws SQLException { Connection con = getConnection(); diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java index 51fdd7488..5390c39b0 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java @@ -32,8 +32,6 @@ import net.snowflake.client.RunningOnGithubAction; import net.snowflake.client.TestUtil; import net.snowflake.client.category.TestCategoryOthers; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -65,29 +63,15 @@ public class DatabaseMetaDataIT extends BaseJDBCTest { public static final int EXPECTED_MAX_BINARY_LENGTH = 8388608; - private static Connection con; - - @BeforeClass - public static void setUpConnection() throws SQLException { - con = getConnection(); - } - - @AfterClass - public static void closeConnection() throws SQLException { - if (con != null && !con.isClosed()) { - con.close(); - } - } - @Test public void testGetConnection() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); - assertEquals(con, metaData.getConnection()); + DatabaseMetaData metaData = connection.getMetaData(); + assertEquals(connection, metaData.getConnection()); } @Test public void testDatabaseAndDriverInfo() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // identifiers assertEquals("Snowflake", metaData.getDatabaseProductName()); @@ -105,7 +89,7 @@ public void testDatabaseAndDriverInfo() throws SQLException { @Test public void testGetCatalogs() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); assertEquals(".", metaData.getCatalogSeparator()); assertEquals("database", metaData.getCatalogTerm()); @@ -159,8 +143,8 @@ public void testGetCatalogs() throws SQLException { @Test public void testGetSchemas() throws Throwable { // CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX = false - DatabaseMetaData metaData = con.getMetaData(); - String currentSchema = con.getSchema(); + DatabaseMetaData metaData = connection.getMetaData(); + String currentSchema = connection.getSchema(); assertEquals("schema", metaData.getSchemaTerm()); Set schemas = new HashSet<>(); try (ResultSet resultSet = metaData.getSchemas()) { @@ -175,7 +159,7 @@ public void testGetSchemas() throws Throwable { assertThat(schemas.size(), greaterThanOrEqualTo(1)); Set schemasInDb = new HashSet<>(); - try (ResultSet resultSet = metaData.getSchemas(con.getCatalog(), "%")) { + try (ResultSet resultSet = metaData.getSchemas(connection.getCatalog(), "%")) { while (resultSet.next()) { String schema = resultSet.getString(1); if (currentSchema.equals(schema) || !TestUtil.isSchemaGeneratedInTests(schema)) { @@ -208,7 +192,7 @@ public void testGetSchemas() throws Throwable { @Test public void testGetTableTypes() throws Throwable { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getTableTypes()) { Set types = new HashSet<>(); while (resultSet.next()) { @@ -224,16 +208,16 @@ public void testGetTableTypes() throws Throwable { @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetTables() throws Throwable { Set tables = null; - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; final String targetView = "V0"; try { statement.execute("create or replace table " + targetTable + "(C1 int)"); statement.execute("create or replace view " + targetView + " as select 1 as C"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // match table try (ResultSet resultSet = @@ -276,15 +260,15 @@ public void testGetTables() throws Throwable { @Test public void testGetPrimarykeys() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; try { statement.execute( "create or replace table " + targetTable + "(C1 int primary key, C2 string)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getPrimaryKeys(database, schema, targetTable)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_PRIMARY_KEYS); @@ -344,9 +328,9 @@ static void verifyResultSetMetaDataColumns( @Test public void testGetImportedKeys() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -359,7 +343,7 @@ public void testGetImportedKeys() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getImportedKeys(database, schema, targetTable2)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_FOREIGN_KEYS); @@ -389,9 +373,9 @@ public void testGetImportedKeys() throws Throwable { @Test public void testGetExportedKeys() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -404,7 +388,7 @@ public void testGetExportedKeys() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getExportedKeys(database, schema, targetTable1)) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_FOREIGN_KEYS); @@ -435,9 +419,9 @@ public void testGetExportedKeys() throws Throwable { @Test public void testGetCrossReferences() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable1 = "T0"; final String targetTable2 = "T1"; try { @@ -450,7 +434,7 @@ public void testGetCrossReferences() throws Throwable { + targetTable1 + ")"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getCrossReference( @@ -483,16 +467,16 @@ public void testGetCrossReferences() throws Throwable { @Test public void testGetObjectsDoesNotExists() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; final String targetView = "V0"; try { statement.execute("create or replace table " + targetTable + "(C1 int)"); statement.execute("create or replace view " + targetView + " as select 1 as C"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // sanity check if getTables really works. try (ResultSet resultSet = metaData.getTables(database, schema, "%", null)) { @@ -544,7 +528,7 @@ public void testGetObjectsDoesNotExists() throws Throwable { @Test public void testTypeInfo() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); ResultSet resultSet = metaData.getTypeInfo(); resultSet.next(); assertEquals("NUMBER", resultSet.getString(1)); @@ -567,7 +551,7 @@ public void testTypeInfo() throws SQLException { @Test public void testProcedure() throws Throwable { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); assertEquals("procedure", metaData.getProcedureTerm()); // no stored procedure support assertTrue(metaData.supportsStoredProcedures()); @@ -582,13 +566,13 @@ public void testProcedure() throws Throwable { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetTablePrivileges() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); try { statement.execute( "create or replace table PRIVTEST(colA string, colB number, colC " + "timestamp)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getTablePrivileges(database, schema, "PRIVTEST")) { verifyResultSetMetaDataColumns( resultSet, DBMetadataResultSetMetadata.GET_TABLE_PRIVILEGES); @@ -636,14 +620,14 @@ public void testGetTablePrivileges() throws Exception { @Test public void testGetProcedures() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { try { - String database = con.getCatalog(); - String schema = con.getSchema(); + String database = connection.getCatalog(); + String schema = connection.getSchema(); /* Create a procedure and put values into it */ statement.execute(PI_PROCEDURE); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); /* Call getFunctionColumns on FUNC111 and since there's no parameter name, get all rows back */ try (ResultSet resultSet = metaData.getProcedures(database, schema, "GETPI")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_PROCEDURES); @@ -664,7 +648,7 @@ public void testGetProcedures() throws SQLException { @Test public void testDatabaseMetadata() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); String dbVersion = metaData.getDatabaseProductVersion(); Matcher m = VERSION_PATTERN.matcher(dbVersion); @@ -820,7 +804,7 @@ public void testDatabaseMetadata() throws SQLException { @Test public void testOtherEmptyTables() throws Throwable { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // index is not supported. try (ResultSet resultSet = metaData.getIndexInfo(null, null, null, true, true)) { @@ -834,7 +818,7 @@ public void testOtherEmptyTables() throws Throwable { @Test public void testFeatureNotSupportedException() throws Throwable { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); expectFeatureNotSupportedException( () -> metaData.getBestRowIdentifier(null, null, null, 0, true)); expectFeatureNotSupportedException(() -> metaData.getVersionColumns(null, null, null)); diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index d9a314502..1821f8e3a 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -36,8 +36,6 @@ import net.snowflake.client.category.TestCategoryOthers; import net.snowflake.client.core.SFBaseSession; import net.snowflake.client.core.SFSessionProperty; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -88,20 +86,6 @@ public void createDoubleQuotedSchemaAndCatalog(Statement statement) throws SQLEx statement.execute("create or replace schema \"dbwith\"\"quotes\".\"schemawith\"\"quotes\""); } - private static Connection con; - - @BeforeClass - public static void setUpConnection() throws SQLException { - con = getConnection(); - } - - @AfterClass - public static void closeConnection() throws SQLException { - if (con != null && !con.isClosed()) { - con.close(); - } - } - /** * Tests for getFunctions * @@ -215,8 +199,8 @@ public void testUseConnectionCtx() throws Exception { */ @Test public void testDoubleQuotedDatabaseAndSchema() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); // To query the schema and table, we can use a normal java escaped quote. Wildcards are also // escaped here String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5); @@ -241,7 +225,7 @@ public void testDoubleQuotedDatabaseAndSchema() throws Exception { statement.execute( "create or replace table \"TESTTABLE_\"\"WITH_QUOTES\"\"\" (AMOUNT number," + " \"COL_\"\"QUOTED\"\"\" string)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTables(database, querySchema, queryTable, null)) { // Assert 1 row returned for the testtable_"with_quotes" assertEquals(1, getSizeOfResultSet(rs)); @@ -269,12 +253,12 @@ public void testDoubleQuotedDatabaseAndSchema() throws Exception { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetSchemas() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database with double quotes inside the database name statement.execute("create or replace database \"\"\"quoteddb\"\"\""); // Create a database, lowercase, with no double quotes inside the database name statement.execute("create or replace database \"unquoteddb\""); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // Assert 2 rows returned for the PUBLIC and INFORMATION_SCHEMA schemas inside database try (ResultSet rs = metaData.getSchemas("\"quoteddb\"", null)) { assertEquals(2, getSizeOfResultSet(rs)); @@ -297,13 +281,13 @@ public void testDoubleQuotedDatabaseInGetSchemas() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetTables() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with two columns statement.execute( "create or replace table \"dbwith\"\"quotes\".\"schemawith\"\"quotes\".\"testtable\" (col1 string, col2 string)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTables("dbwith\"quotes", "schemawith\"quotes", null, null)) { assertEquals(1, getSizeOfResultSet(rs)); } @@ -313,13 +297,13 @@ public void testDoubleQuotedDatabaseInGetTables() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetColumns() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with two columns statement.execute( "create or replace table \"dbwith\"\"quotes\".\"schemawith\"\"quotes\".\"testtable\" (col1 string, col2 string)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getColumns("dbwith\"quotes", "schemawith\"quotes", null, null)) { assertEquals(2, getSizeOfResultSet(rs)); } @@ -329,7 +313,7 @@ public void testDoubleQuotedDatabaseInGetColumns() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseforGetPrimaryKeysAndForeignKeys() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with a primary key constraint @@ -339,7 +323,7 @@ public void testDoubleQuotedDatabaseforGetPrimaryKeysAndForeignKeys() throws SQL // key constraint statement.execute( "create or replace table \"dbwith\"\"quotes\".\"schemawith\"\"quotes\".\"test2\" (col_a integer not null, col_b integer not null, constraint fkey_1 foreign key (col_a, col_b) references \"test1\" (col1, col2) not enforced)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getPrimaryKeys("dbwith\"quotes", "schemawith\"quotes", null)) { // Assert 2 rows are returned for primary key constraint for table and schema with quotes assertEquals(2, getSizeOfResultSet(rs)); @@ -387,14 +371,14 @@ public void testDoubleQuotedDatabaseforGetPrimaryKeysAndForeignKeysWithPatternSe @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetProcedures() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a procedure statement.unwrap(SnowflakeStatement.class).setParameter("MULTI_STATEMENT_COUNT", 3); statement.execute( "USE DATABASE \"dbwith\"\"quotes\"; USE SCHEMA \"schemawith\"\"quotes\"; " + TEST_PROC); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getProcedures("dbwith\"quotes", null, "TESTPROC")) { assertEquals(1, getSizeOfResultSet(rs)); } @@ -404,13 +388,13 @@ public void testDoubleQuotedDatabaseInGetProcedures() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testDoubleQuotedDatabaseInGetTablePrivileges() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table under the current user and role statement.execute( "create or replace table \"dbwith\"\"quotes\".\"schemawith\"\"quotes\".\"testtable\" (col1 string, col2 string)"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTablePrivileges("dbwith\"quotes", null, "%")) { assertEquals(1, getSizeOfResultSet(rs)); } @@ -464,8 +448,8 @@ public void testGetFunctionSqlInjectionProtection() throws Throwable { */ @Test public void testGetProcedureColumnsWildcards() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); String schemaPrefix = TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase(); String schema1 = schemaPrefix + "SCH1"; @@ -481,7 +465,7 @@ public void testGetProcedureColumnsWildcards() throws Exception { () -> { statement.execute(TEST_PROC); // Create 2 schemas, each with the same stored procedure declared in them - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getProcedureColumns( database, schemaPrefix + "SCH_", "TESTPROC", "PARAM1")) { @@ -497,7 +481,7 @@ public void testGetProcedureColumnsWildcards() throws Exception { @Test public void testGetFunctions() throws SQLException { - DatabaseMetaData metadata = con.getMetaData(); + DatabaseMetaData metadata = connection.getMetaData(); String supportedStringFuncs = metadata.getStringFunctions(); assertEquals(StringFunctionsSupported, supportedStringFuncs); @@ -559,9 +543,9 @@ public void testGetStringValueFromColumnDef() throws SQLException { @Test public void testGetColumnsNullable() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; statement.execute( @@ -571,7 +555,7 @@ public void testGetColumnsNullable() throws Throwable { + " C6 boolean, C7 date not null, C8 time, C9 timestamp_ntz(7), C10 binary,C11" + " variant, C12 timestamp_ltz(8), C13 timestamp_tz(3))"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_COLUMNS); @@ -753,9 +737,9 @@ public void testSessionDatabaseParameter() throws Throwable { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetFunctionColumns() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); /* Create a table and put values into it */ statement.execute( @@ -782,7 +766,7 @@ public void testGetFunctionColumns() throws Exception { + "binary(8388608) , sharedCol decimal) COMMENT= 'returns table of 4 columns' as 'select" + " JDBC_TBL111.colA, JDBC_TBL111.colB, BIN_TABLE.bin2, BIN_TABLE.sharedCol from" + " JDBC_TBL111 inner join BIN_TABLE on JDBC_TBL111.colB =BIN_TABLE.sharedCol'"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); /* Call getFunctionColumns on FUNC111 and since there's no parameter name, get all rows back */ try (ResultSet resultSet = metaData.getFunctionColumns(database, schema, "FUNC111", "%")) { @@ -1014,10 +998,10 @@ public void testGetFunctionColumns() throws Exception { @Test public void testHandlingSpecialChars() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); - DatabaseMetaData metaData = con.getMetaData(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); + DatabaseMetaData metaData = connection.getMetaData(); String escapeChar = metaData.getSearchStringEscape(); // test getColumns with escaped special characters in table name statement.execute( @@ -1123,8 +1107,8 @@ public void testHandlingSpecialChars() throws Exception { @Test public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeys() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); TestUtil.withRandomSchema( statement, customSchema -> { @@ -1134,7 +1118,7 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeys() throws E "create or replace table PK_TEST (c1 int PRIMARY KEY, c2 VARCHAR(10))"); statement.execute( "create or replace table FK_TEST (c1 int REFERENCES PK_TEST(c1), c2 VARCHAR(10))"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getPrimaryKeys(database, escapedSchema, null)) { assertEquals(1, getSizeOfResultSet(rs)); } @@ -1155,8 +1139,8 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeysWithPattern Properties properties = new Properties(); properties.put(ENABLE_PATTERN_SEARCH, false); - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); TestUtil.withRandomSchema( statement, customSchema -> { @@ -1166,7 +1150,7 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeysWithPattern "create or replace table PK_TEST (c1 int PRIMARY KEY, c2 VARCHAR(10))"); statement.execute( "create or replace table FK_TEST (c1 int REFERENCES PK_TEST(c1), c2 VARCHAR(10))"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // We have disabled the pattern search so we should get no results. try (ResultSet rs = metaData.getPrimaryKeys(database, escapedSchema, null)) { assertEquals(0, getSizeOfResultSet(rs)); @@ -1216,9 +1200,9 @@ public void testTimestampWithTimezoneDataType() throws Exception { @Test public void testGetColumns() throws Throwable { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; try { statement.execute( @@ -1228,7 +1212,7 @@ public void testGetColumns() throws Throwable { + " C6 boolean, C7 date not null, C8 time, C9 timestamp_ntz(7), C10 binary(8388608),C11" + " variant, C12 timestamp_ltz(8), C13 timestamp_tz(3))"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { verifyResultSetMetaDataColumns(resultSet, DBMetadataResultSetMetadata.GET_COLUMNS); @@ -1566,7 +1550,7 @@ public void testGetColumns() throws Throwable { + " default '\"apples\"', C5 int, C6 int default 5, C7 string default '''', C8" + " string default '''apples''''', C9 string default '%')"); - metaData = con.getMetaData(); + metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns(database, schema, targetTable, "%")) { assertTrue(resultSet.next()); @@ -1610,17 +1594,17 @@ public void testGetColumns() throws Throwable { public void testGetStreams() throws SQLException { final String targetStream = "S0"; final String targetTable = "T0"; - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); - String owner = con.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); + String owner = connection.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole(); String tableName = database + "." + schema + "." + targetTable; try { statement.execute("create or replace table " + targetTable + "(C1 int)"); statement.execute("create or replace stream " + targetStream + " on table " + targetTable); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // match stream try (ResultSet resultSet = @@ -1663,7 +1647,7 @@ public void testGetStreams() throws SQLException { @Test @Ignore public void testGetProceduresWithReaderAccount() throws SQLException { - DatabaseMetaData metadata = con.getMetaData(); + DatabaseMetaData metadata = connection.getMetaData(); try (ResultSet rs = metadata.getProcedures(null, null, null)) { assertEquals(0, getSizeOfResultSet(rs)); } @@ -1672,12 +1656,12 @@ public void testGetProceduresWithReaderAccount() throws SQLException { @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetProcedureColumns() throws Exception { - try (Statement statement = con.createStatement()) { - String database = con.getCatalog(); - String schema = con.getSchema(); + try (Statement statement = connection.createStatement()) { + String database = connection.getCatalog(); + String schema = connection.getSchema(); try { statement.execute(PI_PROCEDURE); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); /* Call getProcedureColumns with no parameters for procedure name or column. This should return all procedures in the current database and schema. It will return all rows as well (1 row per result and 1 row per parameter for each procedure) */ @@ -1720,7 +1704,7 @@ in the current database and schema. It will return all rows as well (1 row per r @Test public void testGetProcedureColumnsReturnsResultSet() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { try { statement.execute( "create or replace table testtable (id int, name varchar(20), address varchar(20));"); @@ -1734,9 +1718,9 @@ public void testGetProcedureColumnsReturnsResultSet() throws SQLException { + " begin\n" + " return table(res);\n" + " end';"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet res = - metaData.getProcedureColumns(con.getCatalog(), null, "PROCTEST", "%")) { + metaData.getProcedureColumns(connection.getCatalog(), null, "PROCTEST", "%")) { res.next(); assertEquals("PROCTEST", res.getString("PROCEDURE_NAME")); assertEquals("id", res.getString("COLUMN_NAME")); @@ -1767,11 +1751,12 @@ public void testGetProcedureColumnsReturnsResultSet() throws SQLException { @Test public void testGetProcedureColumnsReturnsValue() throws SQLException { - try (Statement statement = con.createStatement(); ) { - DatabaseMetaData metaData = con.getMetaData(); + try (Statement statement = connection.createStatement(); ) { + DatabaseMetaData metaData = connection.getMetaData(); // create a procedure with no parameters that has a return value statement.execute(PI_PROCEDURE); - try (ResultSet res = metaData.getProcedureColumns(con.getCatalog(), null, "GETPI", "%")) { + try (ResultSet res = + metaData.getProcedureColumns(connection.getCatalog(), null, "GETPI", "%")) { res.next(); assertEquals("GETPI", res.getString("PROCEDURE_NAME")); assertEquals("", res.getString("COLUMN_NAME")); @@ -1784,7 +1769,7 @@ public void testGetProcedureColumnsReturnsValue() throws SQLException { // create a procedure that returns the value of the argument that is passed in statement.execute(MESSAGE_PROCEDURE); try (ResultSet res = - metaData.getProcedureColumns(con.getCatalog(), null, "MESSAGE_PROC", "%")) { + metaData.getProcedureColumns(connection.getCatalog(), null, "MESSAGE_PROC", "%")) { res.next(); assertEquals("MESSAGE_PROC", res.getString("PROCEDURE_NAME")); assertEquals("", res.getString("COLUMN_NAME")); @@ -1807,8 +1792,8 @@ public void testGetProcedureColumnsReturnsValue() throws SQLException { @Test public void testGetProcedureColumnsReturnsNull() throws SQLException { - try (Statement statement = con.createStatement(); ) { - DatabaseMetaData metaData = con.getMetaData(); + try (Statement statement = connection.createStatement(); ) { + DatabaseMetaData metaData = connection.getMetaData(); // The CREATE PROCEDURE statement must include a RETURNS clause that defines a return type, // even // if the procedure does not explicitly return anything. @@ -1823,7 +1808,7 @@ public void testGetProcedureColumnsReturnsNull() throws SQLException { + "snowflake.execute({sqlText: sqlcommand}); \n" + "';"); try (ResultSet res = - metaData.getProcedureColumns(con.getCatalog(), null, "INSERTPROC", "%")) { + metaData.getProcedureColumns(connection.getCatalog(), null, "INSERTPROC", "%")) { res.next(); // the procedure will return null as the value but column type will be varchar. assertEquals("INSERTPROC", res.getString("PROCEDURE_NAME")); @@ -1840,7 +1825,7 @@ public void testGetProcedureColumnsReturnsNull() throws SQLException { @Test public void testUpdateLocatorsCopyUnsupported() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); assertFalse(metaData.locatorsUpdateCopy()); } @@ -2097,7 +2082,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception final String table1 = "PATTERN_SEARCH_TABLE1"; final String table2 = "PATTERN_SEARCH_TABLE2"; - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5); String schemaName = "\"" @@ -2110,7 +2095,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception statement, schemaName, () -> { - String schema = con.getSchema(); + String schema = connection.getSchema(); statement.execute( "create or replace table " + table1 + "(C1 int primary key, C2 string)"); statement.execute( @@ -2120,13 +2105,13 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception + table1 + ")"); - DatabaseMetaData dbmd = con.getMetaData(); + DatabaseMetaData dbmd = connection.getMetaData(); String schemaPattern1 = schema.substring(0, schema.length() - 1).concat("%"); String schemaPattern2 = schema.substring(0, schema.length() - 1).concat("_"); String tablePattern1 = "PATTERN_SEARCH_TABLE%"; String tablePattern2 = "PATTERN_SEARCH_TABLE_"; - String database = con.getCatalog(); + String database = connection.getCatalog(); // Should return result for matching on either an exact schema and table name or a // pattern @@ -2317,7 +2302,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception */ @Test public void testGetJDBCVersion() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); // JDBC x.x compatible assertEquals(4, metaData.getJDBCMajorVersion()); @@ -2327,19 +2312,22 @@ public void testGetJDBCVersion() throws SQLException { /** Added in > 3.15.1 */ @Test public void testKeywordsCount() throws SQLException { - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); assertEquals(43, metaData.getSQLKeywords().split(",").length); } /** Added in > 3.16.1 */ @Test public void testVectorDimension() throws SQLException { - try (Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute( "create or replace table JDBC_VECTOR(text_col varchar(32), float_vec VECTOR(FLOAT, 256), int_vec VECTOR(INT, 16))"); - DatabaseMetaData metaData = con.getMetaData(); + DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns( - con.getCatalog(), con.getSchema().replaceAll("_", "\\\\_"), "JDBC\\_VECTOR", null)) { + connection.getCatalog(), + connection.getSchema().replaceAll("_", "\\\\_"), + "JDBC\\_VECTOR", + null)) { assertTrue(resultSet.next()); assertEquals(32, resultSet.getObject("COLUMN_SIZE")); assertTrue(resultSet.next()); From fc46a82d06203448f10a458698ba2c1776ca7932 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Mon, 22 Jul 2024 18:28:03 -0700 Subject: [PATCH 3/8] Add changes from PR-1 --- .../snowflake/client/jdbc/BaseJDBCTest.java | 16 ---------------- .../jdbc/ClientMemoryLimitParallelIT.java | 18 +----------------- .../snowflake/client/jdbc/ConnectionIT.java | 2 +- .../client/jdbc/DatabaseMetaDataIT.java | 2 +- .../client/jdbc/DatabaseMetaDataLatestIT.java | 2 +- 5 files changed, 4 insertions(+), 36 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java index 6c239b8c3..a326dea12 100644 --- a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java +++ b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java @@ -36,27 +36,11 @@ import javax.xml.transform.Source; import net.snowflake.client.AbstractDriverIT; import net.snowflake.client.core.SFException; -import org.junit.AfterClass; -import org.junit.BeforeClass; public class BaseJDBCTest extends AbstractDriverIT { // Test UUID unique per session static final String TEST_UUID = UUID.randomUUID().toString(); - protected static Connection connection; - - @BeforeClass - public static void setUpConnection() throws SQLException { - connection = getConnection(); - } - - @AfterClass - public static void closeConnection() throws SQLException { - if (connection != null && !connection.isClosed()) { - connection.close(); - } - } - protected interface MethodRaisesSQLException { void run() throws SQLException; } diff --git a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java index 2dca43564..56d954653 100644 --- a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java @@ -10,9 +10,7 @@ import java.util.Properties; import net.snowflake.client.category.TestCategoryOthers; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -23,7 +21,7 @@ * @author azhan attempts to test the CLIENT_MEMORY_LIMIT working in multi-threading */ @Category(TestCategoryOthers.class) -public class ClientMemoryLimitParallelIT { +public class ClientMemoryLimitParallelIT extends BaseJDBCWithSharedConnectionIT { private static Logger LOGGER = LoggerFactory.getLogger(ClientMemoryLimitParallelIT.class.getName()); @@ -64,20 +62,6 @@ public class ClientMemoryLimitParallelIT { + rowCount + "));"; - private static Connection connection; - - @BeforeClass - public static void setUpConnection() throws SQLException { - connection = getConnection(); - } - - @AfterClass - public static void closeConnection() throws SQLException { - if (connection != null && !connection.isClosed()) { - connection.close(); - } - } - @Before public void setUp() throws SQLException { try (Statement statement = connection.createStatement()) { diff --git a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java index 396b71498..54bba20ed 100644 --- a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java @@ -56,7 +56,7 @@ /** Connection integration tests */ @Category(TestCategoryConnection.class) -public class ConnectionIT extends BaseJDBCTest { +public class ConnectionIT extends BaseJDBCWithSharedConnectionIT { // create a local constant for this code for testing purposes (already defined in GS) public static final int INVALID_CONNECTION_INFO_CODE = 390100; private static final int SESSION_CREATION_OBJECT_DOES_NOT_EXIST_NOT_AUTHORIZED = 390201; diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java index 5390c39b0..ce3130761 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataIT.java @@ -37,7 +37,7 @@ /** Database Metadata IT */ @Category(TestCategoryOthers.class) -public class DatabaseMetaDataIT extends BaseJDBCTest { +public class DatabaseMetaDataIT extends BaseJDBCWithSharedConnectionIT { private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)(?:\\.\\d+)+\\s*.*"); private static final String PI_PROCEDURE = diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index 1821f8e3a..3efa71104 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -47,7 +47,7 @@ * the latest and oldest supported driver run the tests. */ @Category(TestCategoryOthers.class) -public class DatabaseMetaDataLatestIT extends BaseJDBCTest { +public class DatabaseMetaDataLatestIT extends BaseJDBCWithSharedConnectionIT { private static final String TEST_PROC = "create or replace procedure testproc(param1 float, param2 string)\n" + " returns varchar\n" From ccdfe764dc1b53252be5edfc303c0c7e2df85ab4 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Mon, 22 Jul 2024 23:20:40 -0700 Subject: [PATCH 4/8] Fix for test failure --- .../net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index 3efa71104..162d20acb 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -1139,8 +1139,9 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeysWithPattern Properties properties = new Properties(); properties.put(ENABLE_PATTERN_SEARCH, false); - try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); + try (Connection con = getConnection(properties); + Statement statement = con.createStatement()) { + String database = con.getCatalog(); TestUtil.withRandomSchema( statement, customSchema -> { From 816c67613b5bcfb276e9cfcb642d1ebf00b1af36 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Tue, 23 Jul 2024 00:40:47 -0700 Subject: [PATCH 5/8] fix typo --- .../net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index 162d20acb..bc3d477b4 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -1151,7 +1151,7 @@ public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeysWithPattern "create or replace table PK_TEST (c1 int PRIMARY KEY, c2 VARCHAR(10))"); statement.execute( "create or replace table FK_TEST (c1 int REFERENCES PK_TEST(c1), c2 VARCHAR(10))"); - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = con.getMetaData(); // We have disabled the pattern search so we should get no results. try (ResultSet rs = metaData.getPrimaryKeys(database, escapedSchema, null)) { assertEquals(0, getSizeOfResultSet(rs)); From d0c2b36c3d33e1403512e1664ede548b40991654 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Tue, 30 Jul 2024 21:37:28 -0700 Subject: [PATCH 6/8] Fix for databasemetadata tests --- .../client/jdbc/DatabaseMetaDataLatestIT.java | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index bc3d477b4..57970d4f4 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -36,6 +36,7 @@ import net.snowflake.client.category.TestCategoryOthers; import net.snowflake.client.core.SFBaseSession; import net.snowflake.client.core.SFSessionProperty; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -80,12 +81,32 @@ public class DatabaseMetaDataLatestIT extends BaseJDBCWithSharedConnectionIT { private static final String ENABLE_PATTERN_SEARCH = SFSessionProperty.ENABLE_PATTERN_SEARCH.getPropertyKey(); + private static final String startingSchema; + private static final String startingDatabase; + + static { + try { + startingSchema = connection.getSchema(); + startingDatabase = connection.getCatalog(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + /** Create catalog and schema for tests with double quotes */ public void createDoubleQuotedSchemaAndCatalog(Statement statement) throws SQLException { statement.execute("create or replace database \"dbwith\"\"quotes\""); statement.execute("create or replace schema \"dbwith\"\"quotes\".\"schemawith\"\"quotes\""); } + @Before + public void setUp() throws SQLException { + try (Statement stmt = connection.createStatement()) { + stmt.execute("USE SCHEMA " + startingSchema); + stmt.execute("USE DATABASE " + startingDatabase); + } + } + /** * Tests for getFunctions * @@ -200,7 +221,7 @@ public void testUseConnectionCtx() throws Exception { @Test public void testDoubleQuotedDatabaseAndSchema() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); + String database = startingDatabase; // To query the schema and table, we can use a normal java escaped quote. Wildcards are also // escaped here String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5); @@ -449,7 +470,7 @@ public void testGetFunctionSqlInjectionProtection() throws Throwable { @Test public void testGetProcedureColumnsWildcards() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); + String database = startingDatabase; String schemaPrefix = TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase(); String schema1 = schemaPrefix + "SCH1"; @@ -544,8 +565,8 @@ public void testGetStringValueFromColumnDef() throws SQLException { @Test public void testGetColumnsNullable() throws Throwable { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; final String targetTable = "T0"; statement.execute( @@ -738,8 +759,8 @@ public void testSessionDatabaseParameter() throws Throwable { @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetFunctionColumns() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; /* Create a table and put values into it */ statement.execute( @@ -999,8 +1020,8 @@ public void testGetFunctionColumns() throws Exception { @Test public void testHandlingSpecialChars() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; DatabaseMetaData metaData = connection.getMetaData(); String escapeChar = metaData.getSearchStringEscape(); // test getColumns with escaped special characters in table name @@ -1108,7 +1129,7 @@ public void testHandlingSpecialChars() throws Exception { @Test public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeys() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); + String database = startingDatabase; TestUtil.withRandomSchema( statement, customSchema -> { @@ -1202,8 +1223,8 @@ public void testTimestampWithTimezoneDataType() throws Exception { @Test public void testGetColumns() throws Throwable { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; final String targetTable = "T0"; try { statement.execute( @@ -1596,8 +1617,8 @@ public void testGetStreams() throws SQLException { final String targetStream = "S0"; final String targetTable = "T0"; try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; String owner = connection.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole(); String tableName = database + "." + schema + "." + targetTable; @@ -1658,8 +1679,8 @@ public void testGetProceduresWithReaderAccount() throws SQLException { @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetProcedureColumns() throws Exception { try (Statement statement = connection.createStatement()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); + String database = startingDatabase; + String schema = startingSchema; try { statement.execute(PI_PROCEDURE); DatabaseMetaData metaData = connection.getMetaData(); @@ -2096,7 +2117,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception statement, schemaName, () -> { - String schema = connection.getSchema(); + String schema = startingSchema; statement.execute( "create or replace table " + table1 + "(C1 int primary key, C2 string)"); statement.execute( @@ -2325,10 +2346,7 @@ public void testVectorDimension() throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns( - connection.getCatalog(), - connection.getSchema().replaceAll("_", "\\\\_"), - "JDBC\\_VECTOR", - null)) { + startingDatabase, startingSchema.replaceAll("_", "\\\\_"), "JDBC\\_VECTOR", null)) { assertTrue(resultSet.next()); assertEquals(32, resultSet.getObject("COLUMN_SIZE")); assertTrue(resultSet.next()); From 352ac3fc7aa24d9ab481d5b35cd9e341d3bb3a5b Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Wed, 23 Oct 2024 12:00:13 -0700 Subject: [PATCH 7/8] Fix test failures --- .../client/jdbc/DatabaseMetaDataLatestIT.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index 57970d4f4..5fbf56fa9 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -1020,8 +1020,8 @@ public void testGetFunctionColumns() throws Exception { @Test public void testHandlingSpecialChars() throws Exception { try (Statement statement = connection.createStatement()) { - String database = startingDatabase; - String schema = startingSchema; + String database = connection.getCatalog(); + String schema = connection.getSchema(); DatabaseMetaData metaData = connection.getMetaData(); String escapeChar = metaData.getSearchStringEscape(); // test getColumns with escaped special characters in table name @@ -1223,8 +1223,8 @@ public void testTimestampWithTimezoneDataType() throws Exception { @Test public void testGetColumns() throws Throwable { try (Statement statement = connection.createStatement()) { - String database = startingDatabase; - String schema = startingSchema; + String database = connection.getCatalog(); + String schema = connection.getSchema(); final String targetTable = "T0"; try { statement.execute( @@ -1617,8 +1617,8 @@ public void testGetStreams() throws SQLException { final String targetStream = "S0"; final String targetTable = "T0"; try (Statement statement = connection.createStatement()) { - String database = startingDatabase; - String schema = startingSchema; + String database = connection.getCatalog(); + String schema = connection.getSchema(); String owner = connection.unwrap(SnowflakeConnectionV1.class).getSFBaseSession().getRole(); String tableName = database + "." + schema + "." + targetTable; @@ -2117,7 +2117,7 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception statement, schemaName, () -> { - String schema = startingSchema; + String schema = connection.getSchema(); statement.execute( "create or replace table " + table1 + "(C1 int primary key, C2 string)"); statement.execute( @@ -2346,7 +2346,10 @@ public void testVectorDimension() throws SQLException { DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet resultSet = metaData.getColumns( - startingDatabase, startingSchema.replaceAll("_", "\\\\_"), "JDBC\\_VECTOR", null)) { + connection.getCatalog(), + connection.getSchema().replaceAll("_", "\\\\_"), + "JDBC\\_VECTOR", + null)) { assertTrue(resultSet.next()); assertEquals(32, resultSet.getObject("COLUMN_SIZE")); assertTrue(resultSet.next()); From 207283c71367c2bfab63fd6909e923f677590fff Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Wed, 23 Oct 2024 13:58:34 -0700 Subject: [PATCH 8/8] Switch setting order of schema and database --- .../net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index 5fbf56fa9..082907502 100644 --- a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java @@ -102,8 +102,8 @@ public void createDoubleQuotedSchemaAndCatalog(Statement statement) throws SQLEx @Before public void setUp() throws SQLException { try (Statement stmt = connection.createStatement()) { - stmt.execute("USE SCHEMA " + startingSchema); stmt.execute("USE DATABASE " + startingDatabase); + stmt.execute("USE SCHEMA " + startingSchema); } }