@Test public void testCheckIndexExists() throws Exception { Database db = Mockito.mock( Database.class ); ResultSet rs = Mockito.mock( ResultSet.class ); DatabaseMetaData dmd = Mockito.mock( DatabaseMetaData.class ); DatabaseMeta dm = Mockito.mock( DatabaseMeta.class ); Mockito.when( dm.getQuotedSchemaTableCombination( "", "FOO" ) ).thenReturn( "FOO" ); Mockito.when( rs.next() ).thenAnswer( new Answer<Boolean>() { public Boolean answer( InvocationOnMock invocation ) throws Throwable { rowCnt++; return new Boolean( rowCnt < 3 ); } } ); Mockito.when( db.getDatabaseMetaData() ).thenReturn( dmd ); Mockito.when( dmd.getIndexInfo( null, null, "FOO", false, true ) ).thenReturn( rs ); Mockito.when( rs.getString( "COLUMN_NAME" ) ).thenAnswer( new Answer<String>() { @Override public String answer( InvocationOnMock invocation ) throws Throwable { if ( rowCnt == 1 ) { return "ROW1COL2"; } else if ( rowCnt == 2 ) { return "ROW2COL2"; } else { return null; } } } ); Mockito.when( db.getDatabaseMeta() ).thenReturn( dm ); assertTrue( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "ROW1COL2", "ROW2COL2" } ) ); assertFalse( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "ROW2COL2", "NOTTHERE" } ) ); assertFalse( odbcMeta.checkIndexExists( db, "", "FOO", new String[] { "NOTTHERE", "ROW1COL2" } ) ); }
@Override public boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ) throws KettleDatabaseException { String tablename = database.getDatabaseMeta().getQuotedSchemaTableCombination( schemaName, tableName ); boolean[] exists = new boolean[idx_fields.length]; for ( int i = 0; i < exists.length; i++ ) { exists[i] = false; } try { ResultSet indexList = null; try { indexList = database.getDatabaseMetaData().getIndexInfo( null, null, tablename, false, true ); while ( indexList.next() ) { String column = indexList.getString( "COLUMN_NAME" ); int idx = Const.indexOfString( column, idx_fields ); if ( idx >= 0 ) { exists[idx] = true; } } } finally { if ( indexList != null ) { indexList.close(); } } boolean all = true; for ( int i = 0; i < exists.length && all; i++ ) { if ( !exists[i] ) { all = false; } } return all; } catch ( Exception e ) { throw new KettleDatabaseException( "Unable to determine if indexes exists on table [" + tablename + "]", e ); } }
MSAccessDatabaseMeta extends BaseDatabaseMeta implements DatabaseInterface { @Override public boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ) throws KettleDatabaseException { String tablename = database.getDatabaseMeta().getQuotedSchemaTableCombination( schemaName, tableName ); boolean[] exists = new boolean[idx_fields.length]; for ( int i = 0; i < exists.length; i++ ) { exists[i] = false; } try { ResultSet indexList = null; try { indexList = database.getDatabaseMetaData().getIndexInfo( null, null, tablename, false, true ); while ( indexList.next() ) { String column = indexList.getString( "COLUMN_NAME" ); int idx = Const.indexOfString( column, idx_fields ); if ( idx >= 0 ) { exists[idx] = true; } } } finally { if ( indexList != null ) { indexList.close(); } } boolean all = true; for ( int i = 0; i < exists.length && all; i++ ) { if ( !exists[i] ) { all = false; } } return all; } catch ( Exception e ) { throw new KettleDatabaseException( "Unable to determine if indexes exists on table [" + tablename + "]", e ); } } }
MSAccessDatabaseMeta extends BaseDatabaseMeta implements DatabaseInterface { @Override public boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ) throws KettleDatabaseException { String tablename = database.getDatabaseMeta().getQuotedSchemaTableCombination( schemaName, tableName ); boolean[] exists = new boolean[idx_fields.length]; for ( int i = 0; i < exists.length; i++ ) { exists[i] = false; } try { ResultSet indexList = null; try { indexList = database.getDatabaseMetaData().getIndexInfo( null, null, tablename, false, true ); while ( indexList.next() ) { String column = indexList.getString( "COLUMN_NAME" ); int idx = Const.indexOfString( column, idx_fields ); if ( idx >= 0 ) { exists[idx] = true; } } } finally { if ( indexList != null ) { indexList.close(); } } boolean all = true; for ( int i = 0; i < exists.length && all; i++ ) { if ( !exists[i] ) { all = false; } } return all; } catch ( Exception e ) { throw new KettleDatabaseException( "Unable to determine if indexes exists on table [" + tablename + "]", e ); } }  }
MSAccessDatabaseMeta extends BaseDatabaseMeta implements DatabaseInterface { @Override public boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ) throws KettleDatabaseException { String tablename = database.getDatabaseMeta().getQuotedSchemaTableCombination( schemaName, tableName ); boolean[] exists = new boolean[idx_fields.length]; for ( int i = 0; i < exists.length; i++ ) { exists[i] = false; } try { ResultSet indexList = null; try { indexList = database.getDatabaseMetaData().getIndexInfo( null, null, tablename, false, true ); while ( indexList.next() ) { String column = indexList.getString( "COLUMN_NAME" ); int idx = Const.indexOfString( column, idx_fields ); if ( idx >= 0 ) { exists[idx] = true; } } } finally { if ( indexList != null ) { indexList.close(); } } boolean all = true; for ( int i = 0; i < exists.length && all; i++ ) { if ( !exists[i] ) { all = false; } } return all; } catch ( Exception e ) { throw new KettleDatabaseException( "Unable to determine if indexes exists on table [" + tablename + "]", e ); } }  @Override int[] getAccessTypeList(); @Override boolean supportsSetCharacterStream(); @Override int getNotFoundTK( boolean use_autoinc ); @Override String getDriverClass(); @Override String getURL( String hostname, String port, String databaseName ); @Override boolean isFetchSizeSupported(); @Override @SuppressWarnings( "deprecation" ) String getSchemaTableCombination( String schema_name, String table_part ); @Override int getMaxTextFieldLength(); @Override boolean supportsTransactions(); @Override boolean supportsBitmapIndex(); @Override boolean supportsSetLong(); @Override boolean supportsViews(); @Override boolean supportsSynonyms(); @Override String getTruncateTableStatement( String tableName ); @Override String getAddColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getDropColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getModifyColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getFieldDefinition( ValueMetaInterface v, String tk, String pk, boolean use_autoinc,
    boolean add_fieldname, boolean add_cr ); @Override String[] getReservedWords(); @Override String getStartQuote(); @Override String getEndQuote(); @Override String[] getUsedLibraries(); @Override boolean supportsGetBlob(); @Override boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ); @Override String getSQLInsertAutoIncUnknownDimensionRow( String schemaTable, String keyField, String versionField ); }
MSAccessDatabaseMeta extends BaseDatabaseMeta implements DatabaseInterface { @Override public boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ) throws KettleDatabaseException { String tablename = database.getDatabaseMeta().getQuotedSchemaTableCombination( schemaName, tableName ); boolean[] exists = new boolean[idx_fields.length]; for ( int i = 0; i < exists.length; i++ ) { exists[i] = false; } try { ResultSet indexList = null; try { indexList = database.getDatabaseMetaData().getIndexInfo( null, null, tablename, false, true ); while ( indexList.next() ) { String column = indexList.getString( "COLUMN_NAME" ); int idx = Const.indexOfString( column, idx_fields ); if ( idx >= 0 ) { exists[idx] = true; } } } finally { if ( indexList != null ) { indexList.close(); } } boolean all = true; for ( int i = 0; i < exists.length && all; i++ ) { if ( !exists[i] ) { all = false; } } return all; } catch ( Exception e ) { throw new KettleDatabaseException( "Unable to determine if indexes exists on table [" + tablename + "]", e ); } }  @Override int[] getAccessTypeList(); @Override boolean supportsSetCharacterStream(); @Override int getNotFoundTK( boolean use_autoinc ); @Override String getDriverClass(); @Override String getURL( String hostname, String port, String databaseName ); @Override boolean isFetchSizeSupported(); @Override @SuppressWarnings( "deprecation" ) String getSchemaTableCombination( String schema_name, String table_part ); @Override int getMaxTextFieldLength(); @Override boolean supportsTransactions(); @Override boolean supportsBitmapIndex(); @Override boolean supportsSetLong(); @Override boolean supportsViews(); @Override boolean supportsSynonyms(); @Override String getTruncateTableStatement( String tableName ); @Override String getAddColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getDropColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getModifyColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
    String pk, boolean semicolon ); @Override String getFieldDefinition( ValueMetaInterface v, String tk, String pk, boolean use_autoinc,
    boolean add_fieldname, boolean add_cr ); @Override String[] getReservedWords(); @Override String getStartQuote(); @Override String getEndQuote(); @Override String[] getUsedLibraries(); @Override boolean supportsGetBlob(); @Override boolean checkIndexExists( Database database, String schemaName, String tableName, String[] idx_fields ); @Override String getSQLInsertAutoIncUnknownDimensionRow( String schemaTable, String keyField, String versionField );  }