Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PDI-16553] Sort a list of tables fetched from DB. #8954

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions core/src/main/java/org/pentaho/di/core/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.logging.SimpleLoggingObject;
import org.pentaho.di.core.plugins.PluginTypeListener;
import org.pentaho.di.core.row.value.ValueMetaPluginType;
import org.pentaho.di.core.util.Utils;
Expand Down Expand Up @@ -3950,7 +3950,15 @@ public String[] getTablenames( String schemanamein, boolean includeSchema, Map<S
}
}
}
return res.toArray( new String[ res.size() ] );
return createSortedArray( res );
}

private static String[] createSortedArray( List<String> l ) {
if ( l == null ) {
return null;
}
List<String> sortedList = l.stream().sorted().collect( Collectors.toList() );
return sortedList.toArray( new String[sortedList.size()] );
}

public Map<String, Collection<String>> getTableMap() throws KettleDatabaseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
Expand Down Expand Up @@ -842,6 +843,26 @@ public void testGetTablenames() throws SQLException, KettleDatabaseException {
assertEquals( tableNames.length, 1 );
}

@Test
public void testGetTablenamesMultipleTables() throws SQLException, KettleDatabaseException {
when(rs.next()).thenReturn(true, true, true, false);
when(rs.getString("TABLE_NAME")).thenReturn("K_TABLE","A_TABLE", "Z_TABLE");
when(dbMetaMock.getTables(
same(dbMetaDataMock), or(anyString(), eq(null)), or(anyString(), eq(null)), any()
)).thenReturn(rs);
when(dbMetaMock.getQuotedSchemaTableCombination(eq(null), anyString())).thenReturn(
"K_TABLE", "A_TABLE", "Z_TABLE"
);
Database db = new Database(log, dbMetaMock);
db.setConnection(mockConnection(dbMetaDataMock));

String[] tableNames = db.getTablenames();
assertEquals(3, tableNames.length);
assertEquals("A_TABLE", tableNames[0]);
assertEquals("K_TABLE", tableNames[1]);
assertEquals("Z_TABLE", tableNames[2]);
}

@Test
public void testCheckTableExistsNoProperty() throws Exception {
DatabaseMeta databaseMeta = new DatabaseMeta();
Expand Down