@Override public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException { return metaData.getIndexInfo(catalog, schema, table, unique, approximate); }
private void createTaskIdIndexIfNeeded(final Connection conn, final String tableName, final String indexName) throws SQLException { DatabaseMetaData dbMetaData = conn.getMetaData(); try (ResultSet resultSet = dbMetaData.getIndexInfo(null, null, tableName, false, false)) { boolean hasTaskIdIndex = false; while (resultSet.next()) { if (indexName.equals(resultSet.getString("INDEX_NAME"))) { hasTaskIdIndex = true; } } if (!hasTaskIdIndex) { createTaskIdAndStateIndex(conn, tableName); } } }
private void initIndexes(DatabaseMetaData meta) throws SQLException { ResultSet rs = null; try { rs = meta.getIndexInfo( catalog, schema, name, false, true ); while ( rs.next() ) { if ( rs.getShort( "TYPE" ) == DatabaseMetaData.tableIndexStatistic ) { continue; } addIndex( rs ); } } finally { if ( rs != null ) { rs.close(); } } }
public Object doInConnection(Connection conn) throws SQLException, DataAccessException { DatabaseMetaData metaData = conn.getMetaData(); String sName = getIdentifierName(schemaName, metaData); String tName = getIdentifierName(tableName, metaData); ResultSet rs = metaData.getIndexInfo(sName, sName, tName, false, true); Map<String, String> indexes = new HashMap<String, String>(); while (rs.next()) { String columnName = rs.getString(9); String indexName = rs.getString(6); if (columnName != null && indexName != null) { indexes.put(columnName, indexName); } } rs.close(); return indexes; } });
try { DatabaseMetaData metadata = conn.getMetaData(); rs = metadata.getIndexInfo(dbName, dbName, tableName, false, true); indexInfos = new ArrayList<>(); while (rs.next()) {
@Override public Void call() throws Exception { Connection conn = getConnection(); DatabaseMetaData meta = conn.getMetaData(); conn.close(); meta.getIndexInfo(null, null, null, false, false); return null; } }, "08003", "Connection is closed.");
/** * Verify that index with name {@code indexName} does not exist on the table {@code tableName} */ public void assertIndexDoesNotExist(String tableName, String indexName) { try (Connection connection = getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) { List<String> indices = new ArrayList<>(); while (rs.next()) { indices.add(rs.getString("INDEX_NAME").toLowerCase(Locale.ENGLISH)); } assertThat(indices).doesNotContain(indexName); } catch (SQLException e) { throw new IllegalStateException("Fail to check existence of index", e); } }
private void assertIndexImpl(String tableName, String indexName, boolean expectedUnique, String expectedColumn, String... expectedSecondaryColumns) { try (Connection connection = getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(null, null, tableName.toUpperCase(Locale.ENGLISH), false, false)) { List<String> onColumns = new ArrayList<>(); while (rs.next()) { if (indexName.equalsIgnoreCase(rs.getString("INDEX_NAME"))) { assertThat(rs.getBoolean("NON_UNIQUE")).isEqualTo(!expectedUnique); int position = rs.getInt("ORDINAL_POSITION"); onColumns.add(position - 1, rs.getString("COLUMN_NAME").toLowerCase(Locale.ENGLISH)); } } assertThat(asList(expectedColumn, expectedSecondaryColumns)).isEqualTo(onColumns); } catch (SQLException e) { throw new IllegalStateException("Fail to check index", e); } }
/** * @throws Exception If failed. */ @Test public void testInvalidCatalog() throws Exception { try (Connection conn = DriverManager.getConnection(URL)) { DatabaseMetaData meta = conn.getMetaData(); ResultSet rs = meta.getSchemas("q", null); assert !rs.next() : "Results must be empty"; rs = meta.getTables("q", null, null, null); assert !rs.next() : "Results must be empty"; rs = meta.getColumns("q", null, null, null); assert !rs.next() : "Results must be empty"; rs = meta.getIndexInfo("q", null, null, false, false); assert !rs.next() : "Results must be empty"; rs = meta.getPrimaryKeys("q", null, null); assert !rs.next() : "Results must be empty"; } }
/** * @throws Exception If failed. */ @Test public void testGetAllIndexes() throws Exception { try (Connection conn = DriverManager.getConnection(URL)) { ResultSet rs = conn.getMetaData().getIndexInfo(null, null, null, false, false); Set<String> expectedIdxs = new HashSet<>(Arrays.asList( "org.ORGANIZATION.ORGANIZATION_ID_ASC_IDX", "org.ORGANIZATION.ORG_NAME_INDEX", "pers.PERSON.PERSON_ORGID_ASC_IDX", "pers.PERSON.PERSON_NAME_ASC_AGE_DESC_IDX", "PUBLIC.TEST.IDX", "PUBLIC.Quoted.MyTestIndex quoted")); Set<String> actualIdxs = new HashSet<>(expectedIdxs.size()); while(rs.next()) { actualIdxs.add(rs.getString("TABLE_SCHEM") + '.' + rs.getString("TABLE_NAME") + '.' + rs.getString("INDEX_NAME")); } assert expectedIdxs.equals(actualIdxs) : "expectedIdxs=" + expectedIdxs + ", actualIdxs" + actualIdxs; } }
@Test public void test_existing_indicies() throws Exception { boolean found = false; for (String tableName : Arrays.asList(tableName.toLowerCase(), tableName.toUpperCase())) { try ( Connection connection = dataSource.getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(connection.getCatalog(), null, tableName, unique, true); ) { while (!found && rs.next()) { found = indexName.equalsIgnoreCase(rs.getString("INDEX_NAME")); } } if (found) { break; } } assertTrue(String.format("Expected to find index %s.%s", tableName, indexName), found); }
@Test public void test_existing_indicies() throws Exception { boolean found = false; for (String tableName : Arrays.asList(tableName.toLowerCase(), tableName.toUpperCase())) { try ( Connection connection = dataSource.getConnection(); ResultSet rs = connection.getMetaData().getIndexInfo(connection.getCatalog(), null, tableName, unique, true); ) { while (!found && rs.next()) { found = indexName.equalsIgnoreCase(rs.getString("INDEX_NAME")); } } if (found) { break; } } assertTrue(String.format("Expected to find index %s.%s", tableName, indexName), found); }
@Test public void testGetIndexInfoReturnsNonNull() throws SQLException { assertThat( dbmd.getIndexInfo( null, null, "%", false, true ), notNullValue() ); } // TODO: Later, test more (e.g., right columns (even if/though zero rows)).
public void validate_index_existence(String[] tableNames, String lookupIndexName) throws Exception { Connection connection = dataSource.getConnection(); try { DatabaseMetaData meta = connection.getMetaData(); boolean foundIndex = false; for (String tableName : tableNames) { ResultSet rs = meta.getIndexInfo(connection.getCatalog(), null, tableName, false, false); while ((!foundIndex) && rs.next()) { String indexName = rs.getString("INDEX_NAME"); if (lookupIndexName.equalsIgnoreCase(indexName)) { foundIndex = true; } } rs.close(); if (foundIndex) { break; } } assertTrue("I was expecting to find index "+ lookupIndexName, foundIndex); } finally { connection.close(); } } }
public void testIndexMetadata() throws Exception { try (Connection conn = DriverManager.getConnection(URL); ResultSet rs = conn.getMetaData().getIndexInfo(null, "pers", "PERSON", false, false)) {
/** * Assert index doesn't exist on particular node. * * @param node Node. * @param cacheName Cache name. * @param tblName Table name. * @param idxName Index name. */ static void assertNoIndex(Ignite node, String cacheName, String tblName, String idxName) { awaitCompletion(); node.cache(cacheName); try { try (Connection c = connect((IgniteEx)node)) { try (ResultSet rs = c.getMetaData().getIndexInfo(null, cacheName, tblName, false, false)) { while (rs.next()) { assertFalse("Index exists, although shouldn't: " + tblName + '.' + idxName, F.eq(idxName, rs.getString("INDEX_NAME"))); } } } } catch (SQLException e) { throw new AssertionError(e); } }
public void testIndexMetadata() throws Exception { try (Connection conn = DriverManager.getConnection(BASE_URL); ResultSet rs = conn.getMetaData().getIndexInfo(null, "pers", "PERSON", false, false)) {
private void indexes(Connection conn, Table table, ProgressMeasure progress) { requireNonNulls(conn, table, progress); if (table.isView() && IGNORE_VIEW_INDEXES) return; final SqlSupplier<ResultSet> supplier = () -> conn.getMetaData().getIndexInfo(null, null, table.getId(), false, APPROXIMATE_INDEX); final TableChildMutator<Index> mutator = (index, rs) -> { final String indexName = rs.getString("INDEX_NAME"); index.mutator().setId(indexName); index.mutator().setName(indexName); index.mutator().setUnique(!rs.getBoolean("NON_UNIQUE")); final IndexColumn indexColumn = index.mutator().addNewIndexColumn(); final String columnName = rs.getString("COLUMN_NAME"); indexColumn.mutator().setId(columnName); indexColumn.mutator().setName(columnName); indexColumn.mutator().setOrdinalPosition(rs.getInt("ORDINAL_POSITION")); indexColumn.mutator().setOrderType(getOrderType(rs)); }; final SqlPredicate<ResultSet> filter = rs -> rs.getString("INDEX_NAME") != null; tableChilds( table, Index.class, table.mutator()::addNewIndex, supplier, rsChild -> rsChild.getString("INDEX_NAME"), mutator, filter ); }
/** * Check that lookup in the metadata have been performed using specified catalog name (that is neither {@code null} * nor correct catalog name), empty result set is returned. * * @param invalidCat catalog name that is not either */ private void checkNoEntitiesFoundForCatalog(String invalidCat) throws SQLException { try (Connection conn = DriverManager.getConnection(URL)) { DatabaseMetaData meta = conn.getMetaData(); // Intention: we set the other arguments that way, the values to have as many results as possible. assertIsEmpty(meta.getTables(invalidCat, null, "%", new String[] {"TABLE"})); assertIsEmpty(meta.getColumns(invalidCat, null, "%", "%")); assertIsEmpty(meta.getColumnPrivileges(invalidCat, "pers", "PERSON", "%")); assertIsEmpty(meta.getTablePrivileges(invalidCat, null, "%")); assertIsEmpty(meta.getPrimaryKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getImportedKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getExportedKeys(invalidCat, "pers", "PERSON")); // meta.getCrossReference(...) doesn't make sense because we don't have FK constraint. assertIsEmpty(meta.getIndexInfo(invalidCat, null, "%", false, true)); assertIsEmpty(meta.getSuperTables(invalidCat, "%", "%")); assertIsEmpty(meta.getSchemas(invalidCat, null)); assertIsEmpty(meta.getPseudoColumns(invalidCat, null, "%", "")); } }
/** * Check that lookup in the metadata have been performed using specified catalog name (that is neither {@code null} * nor correct catalog name), empty result set is returned. * * @param invalidCat catalog name that is not either */ private void checkNoEntitiesFoundForCatalog(String invalidCat) throws SQLException { try (Connection conn = DriverManager.getConnection(BASE_URL)) { DatabaseMetaData meta = conn.getMetaData(); // Intention: we set the other arguments that way, the values to have as many results as possible. assertIsEmpty(meta.getTables(invalidCat, null, "%", new String[] {"TABLE"})); assertIsEmpty(meta.getColumns(invalidCat, null, "%", "%")); assertIsEmpty(meta.getColumnPrivileges(invalidCat, "pers", "PERSON", "%")); assertIsEmpty(meta.getTablePrivileges(invalidCat, null, "%")); assertIsEmpty(meta.getPrimaryKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getImportedKeys(invalidCat, "pers", "PERSON")); assertIsEmpty(meta.getExportedKeys(invalidCat, "pers", "PERSON")); // meta.getCrossReference(...) doesn't make sense because we don't have FK constraint. assertIsEmpty(meta.getIndexInfo(invalidCat, null, "%", false, true)); assertIsEmpty(meta.getSuperTables(invalidCat, "%", "%")); assertIsEmpty(meta.getSchemas(invalidCat, null)); assertIsEmpty(meta.getPseudoColumns(invalidCat, null, "%", "")); } }