diff --git a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java index 22a33286d..56d954653 100644 --- a/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ClientMemoryLimitParallelIT.java @@ -21,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,16 +64,14 @@ public class ClientMemoryLimitParallelIT { @Before public void setUp() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute(createTestTableSQL); } } @After public void tearDown() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute("drop table if exists testtable_cml"); } } @@ -126,8 +124,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 = 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 43c5c7f81..00656e305 100644 --- a/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ConnectionIT.java @@ -59,7 +59,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; @@ -90,10 +90,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()," @@ -168,8 +167,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); @@ -220,31 +218,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)); } @@ -253,40 +250,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 @@ -725,18 +721,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 @@ -829,7 +821,6 @@ public void testReadDateAfterSplittingResultSet() throws Exception { @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;"); @@ -846,7 +837,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..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 = @@ -65,152 +65,142 @@ public class DatabaseMetaDataIT extends BaseJDBCTest { @Test public void testGetConnection() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - assertEquals(connection, metaData.getConnection()); - } + DatabaseMetaData metaData = connection.getMetaData(); + assertEquals(connection, 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 = 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)); } @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 = 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()); } - 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 = 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); } } - 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(connection.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 = 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")); } } @@ -218,8 +208,7 @@ 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()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable = "T0"; @@ -271,8 +260,7 @@ public void testGetTables() throws Throwable { @Test public void testGetPrimarykeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable = "T0"; @@ -340,8 +328,7 @@ static void verifyResultSetMetaDataColumns( @Test public void testGetImportedKeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable1 = "T0"; @@ -386,8 +373,7 @@ public void testGetImportedKeys() throws Throwable { @Test public void testGetExportedKeys() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); ) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable1 = "T0"; @@ -433,8 +419,7 @@ public void testGetExportedKeys() throws Throwable { @Test public void testGetCrossReferences() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable1 = "T0"; @@ -482,8 +467,7 @@ public void testGetCrossReferences() throws Throwable { @Test public void testGetObjectsDoesNotExists() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable = "T0"; @@ -544,50 +528,45 @@ 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 = 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()); } @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 = 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)); } } @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testGetTablePrivileges() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); try { @@ -641,8 +620,7 @@ public void testGetTablePrivileges() throws Exception { @Test public void testGetProcedures() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { String database = connection.getCatalog(); String schema = connection.getSchema(); @@ -670,197 +648,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 = 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()); } @Test public void testOtherEmptyTables() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = connection.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 = 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)); } } diff --git a/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java b/src/test/java/net/snowflake/client/jdbc/DatabaseMetaDataLatestIT.java index bebe3d8f4..082907502 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; @@ -47,7 +48,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" @@ -80,12 +81,32 @@ public class DatabaseMetaDataLatestIT extends BaseJDBCTest { 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 DATABASE " + startingDatabase); + stmt.execute("USE SCHEMA " + startingSchema); + } + } + /** * Tests for getFunctions * @@ -199,9 +220,8 @@ public void testUseConnectionCtx() throws Exception { */ @Test public void testDoubleQuotedDatabaseAndSchema() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + 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); @@ -226,7 +246,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)); @@ -254,13 +274,12 @@ 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 = 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)); @@ -283,14 +302,13 @@ 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 = 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)); } @@ -300,14 +318,13 @@ 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 = 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)); } @@ -317,8 +334,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 = connection.createStatement()) { // Create a database and schema with double quotes inside the database name createDoubleQuotedSchemaAndCatalog(statement); // Create a table with a primary key constraint @@ -328,7 +344,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)); @@ -376,15 +392,14 @@ 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 = 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)); } @@ -394,14 +409,13 @@ 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 = 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)); } @@ -455,9 +469,8 @@ public void testGetFunctionSqlInjectionProtection() throws Throwable { */ @Test public void testGetProcedureColumnsWildcards() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = startingDatabase; String schemaPrefix = TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(5).toUpperCase(); String schema1 = schemaPrefix + "SCH1"; @@ -473,7 +486,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")) { @@ -489,17 +502,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 = connection.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 +564,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 = connection.createStatement()) { + String database = startingDatabase; + String schema = startingSchema; final String targetTable = "T0"; statement.execute( @@ -748,10 +758,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 = connection.createStatement()) { + String database = startingDatabase; + String schema = startingSchema; /* Create a table and put values into it */ statement.execute( @@ -1010,8 +1019,7 @@ public void testGetFunctionColumns() throws Exception { @Test public void testHandlingSpecialChars() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); DatabaseMetaData metaData = connection.getMetaData(); @@ -1120,9 +1128,8 @@ public void testHandlingSpecialChars() throws Exception { @Test public void testUnderscoreInSchemaNamePatternForPrimaryAndForeignKeys() throws Exception { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - String database = con.getCatalog(); + try (Statement statement = connection.createStatement()) { + String database = startingDatabase; TestUtil.withRandomSchema( statement, customSchema -> { @@ -1132,7 +1139,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)); } @@ -1215,8 +1222,7 @@ public void testTimestampWithTimezoneDataType() throws Exception { @Test public void testGetColumns() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String database = connection.getCatalog(); String schema = connection.getSchema(); final String targetTable = "T0"; @@ -1610,18 +1616,17 @@ 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()) { - 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 = @@ -1664,21 +1669,18 @@ 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 = connection.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 = connection.createStatement()) { + String database = startingDatabase; + String schema = startingSchema; try { statement.execute(PI_PROCEDURE); DatabaseMetaData metaData = connection.getMetaData(); @@ -1724,8 +1726,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 = connection.createStatement()) { try { statement.execute( "create or replace table testtable (id int, name varchar(20), address varchar(20));"); @@ -1739,9 +1740,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")); @@ -1772,12 +1773,12 @@ public void testGetProcedureColumnsReturnsResultSet() throws SQLException { @Test public void testGetProcedureColumnsReturnsValue() throws SQLException { - try (Connection con = getConnection(); - 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")); @@ -1790,7 +1791,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")); @@ -1813,9 +1814,8 @@ public void testGetProcedureColumnsReturnsValue() throws SQLException { @Test public void testGetProcedureColumnsReturnsNull() throws SQLException { - try (Connection con = getConnection(); - 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. @@ -1830,7 +1830,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")); @@ -1847,10 +1847,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 = connection.getMetaData(); + assertFalse(metaData.locatorsUpdateCopy()); } /** @@ -2106,8 +2104,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 = connection.createStatement()) { String schemaRandomPart = SnowflakeUtil.randomAlphaNumeric(5); String schemaName = "\"" @@ -2327,28 +2324,23 @@ public void testPatternSearchAllowedForPrimaryAndForeignKeys() throws Exception */ @Test public void testGetJDBCVersion() throws SQLException { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); + DatabaseMetaData metaData = connection.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 = connection.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 = 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 = connection.getMetaData();