This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 2315dda  ColumnMetaDataLoader load error ,when tables match the table 
pattern and colums different (#7330)
2315dda is described below

commit 2315ddab9447538452cdb9fe3d6503cfed1b4f38
Author: xiaoyu <[email protected]>
AuthorDate: Wed Sep 9 00:31:43 2020 +0800

    ColumnMetaDataLoader load error ,when tables match the table pattern and 
colums different (#7330)
    
    * fix 7284
    
    * Update TableMetaDataLoader.java
    
    * fix test
    
    * fix scaling MetaDataManagerTest error
    
    * Update EncryptMetaDataLoaderTest.java
---
 .../metadata/EncryptMetaDataLoaderTest.java        | 15 ++++++++-----
 .../scaling/core/metadata/MetaDataManagerTest.java |  3 +++
 .../metadata/column/ColumnMetaDataLoader.java      | 26 +++++++++++++---------
 .../binder/metadata/table/TableMetaDataLoader.java | 18 +++++++--------
 .../metadata/column/ColumnMetaDataLoaderTest.java  |  1 +
 .../metadata/table/TableMetaDataLoaderTest.java    |  1 +
 6 files changed, 39 insertions(+), 25 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/metadata/EncryptMetaDataLoaderTest.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/metadata/EncryptMetaDataLoaderTest.java
index 92fa098..93b9dfe 100644
--- 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/metadata/EncryptMetaDataLoaderTest.java
+++ 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/metadata/EncryptMetaDataLoaderTest.java
@@ -53,6 +53,8 @@ import static org.mockito.Mockito.when;
 @RunWith(MockitoJUnitRunner.class)
 public final class EncryptMetaDataLoaderTest {
     
+    private static final String TABLE_NAME = "t_encrypt";
+    
     static {
         ShardingSphereServiceLoader.register(RuleMetaDataLoader.class);
     }
@@ -85,6 +87,7 @@ public final class EncryptMetaDataLoaderTest {
     private ResultSet createColumnResultSet() throws SQLException {
         ResultSet result = mock(ResultSet.class);
         when(result.next()).thenReturn(true, true, true, false);
+        when(result.getString("TABLE_NAME")).thenReturn(TABLE_NAME);
         when(result.getString("COLUMN_NAME")).thenReturn("id", "pwd_cipher", 
"pwd_plain");
         return result;
     }
@@ -94,16 +97,16 @@ public final class EncryptMetaDataLoaderTest {
         EncryptRule rule = createEncryptRule();
         EncryptMetaDataLoader loader = (EncryptMetaDataLoader) 
OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(rule), 
RuleMetaDataLoader.class).get(rule);
         SchemaMetaData actual = loader.load(databaseType, 
Collections.singletonMap("logic_db", dataSource), new 
DataNodes(Collections.singletonList(rule)), rule, props, 
Collections.emptyList());
-        assertThat(actual.get("t_encrypt").getColumnMetaData(0).getName(), 
is("id"));
-        assertThat(actual.get("t_encrypt").getColumnMetaData(1).getName(), 
is("pwd_cipher"));
-        assertThat(actual.get("t_encrypt").getColumnMetaData(2).getName(), 
is("pwd_plain"));
+        assertThat(actual.get(TABLE_NAME).getColumnMetaData(0).getName(), 
is("id"));
+        assertThat(actual.get(TABLE_NAME).getColumnMetaData(1).getName(), 
is("pwd_cipher"));
+        assertThat(actual.get(TABLE_NAME).getColumnMetaData(2).getName(), 
is("pwd_plain"));
     }
     
     @Test
     public void assertLoadByExistedTable() throws SQLException {
         EncryptRule rule = createEncryptRule();
         EncryptMetaDataLoader loader = (EncryptMetaDataLoader) 
OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(rule), 
RuleMetaDataLoader.class).get(rule);
-        Optional<TableMetaData> actual = loader.load(databaseType, 
Collections.singletonMap("logic_db", dataSource), new 
DataNodes(Collections.singletonList(rule)), "t_encrypt", rule, props);
+        Optional<TableMetaData> actual = loader.load(databaseType, 
Collections.singletonMap("logic_db", dataSource), new 
DataNodes(Collections.singletonList(rule)), TABLE_NAME, rule, props);
         assertTrue(actual.isPresent());
         assertThat(actual.get().getColumnMetaData(0).getName(), is("id"));
         assertThat(actual.get().getColumnMetaData(1).getName(), 
is("pwd_cipher"));
@@ -120,8 +123,8 @@ public final class EncryptMetaDataLoaderTest {
     
     private EncryptRule createEncryptRule() {
         EncryptRule result = mock(EncryptRule.class);
-        
when(result.getEncryptTableNames()).thenReturn(Collections.singletonList("t_encrypt"));
-        
when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(mock(EncryptTable.class)));
+        
when(result.getEncryptTableNames()).thenReturn(Collections.singletonList(TABLE_NAME));
+        
when(result.findEncryptTable(TABLE_NAME)).thenReturn(Optional.of(mock(EncryptTable.class)));
         return result;
     }
 }
diff --git 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/metadata/MetaDataManagerTest.java
 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/metadata/MetaDataManagerTest.java
index bae1071..909908d 100644
--- 
a/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/metadata/MetaDataManagerTest.java
+++ 
b/shardingsphere-scaling/shardingsphere-scaling-core/src/test/java/org/apache/shardingsphere/scaling/core/metadata/MetaDataManagerTest.java
@@ -49,6 +49,8 @@ public final class MetaDataManagerTest {
     
     private static final String TYPE_NAME = "TYPE_NAME";
     
+    private static final String TABLE_NAME = "TABLE_NAME";
+    
     private static final String TEST_TABLE = "test";
     
     @Mock
@@ -96,6 +98,7 @@ public final class MetaDataManagerTest {
         when(primaryKeyResultSet.next()).thenReturn(true, false);
         when(primaryKeyResultSet.getString(COLUMN_NAME)).thenReturn("id");
         when(columnMetaDataResultSet.next()).thenReturn(true, true, true, 
false);
+        
when(columnMetaDataResultSet.getString(TABLE_NAME)).thenReturn(TEST_TABLE);
         when(columnMetaDataResultSet.getString(COLUMN_NAME)).thenReturn("id", 
"name", "age");
         
when(columnMetaDataResultSet.getInt(DATA_TYPE)).thenReturn(Types.BIGINT, 
Types.VARCHAR, Types.INTEGER);
         
when(columnMetaDataResultSet.getString(TYPE_NAME)).thenReturn("BIGINT", 
"VARCHAR", "INTEGER");
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
index 5df3afa..03ec739 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoader.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.sql.parser.binder.metadata.column;
 
+import java.util.Objects;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil;
@@ -42,33 +43,38 @@ public final class ColumnMetaDataLoader {
     
     private static final String TYPE_NAME = "TYPE_NAME";
     
+    private static final String TABLE_NAME = "TABLE_NAME";
+    
     /**
      * Load column meta data list.
      * 
      * @param connection connection
-     * @param table table name
+     * @param tableNamePattern table name pattern
      * @param databaseType database type
      * @return column meta data list
      * @throws SQLException SQL exception
      */
-    public static Collection<ColumnMetaData> load(final Connection connection, 
final String table, final String databaseType) throws SQLException {
+    public static Collection<ColumnMetaData> load(final Connection connection, 
final String tableNamePattern, final String databaseType) throws SQLException {
         Collection<ColumnMetaData> result = new LinkedList<>();
-        Collection<String> primaryKeys = loadPrimaryKeys(connection, table, 
databaseType);
+        Collection<String> primaryKeys = loadPrimaryKeys(connection, 
tableNamePattern, databaseType);
         List<String> columnNames = new ArrayList<>();
         List<Integer> columnTypes = new ArrayList<>();
         List<String> columnTypeNames = new ArrayList<>();
         List<Boolean> isPrimaryKeys = new ArrayList<>();
         List<Boolean> isCaseSensitives = new ArrayList<>();
-        try (ResultSet resultSet = 
connection.getMetaData().getColumns(connection.getCatalog(), 
JdbcUtil.getSchema(connection, databaseType), table, "%")) {
+        try (ResultSet resultSet = 
connection.getMetaData().getColumns(connection.getCatalog(), 
JdbcUtil.getSchema(connection, databaseType), tableNamePattern, "%")) {
             while (resultSet.next()) {
-                String columnName = resultSet.getString(COLUMN_NAME);
-                columnTypes.add(resultSet.getInt(DATA_TYPE));
-                columnTypeNames.add(resultSet.getString(TYPE_NAME));
-                isPrimaryKeys.add(primaryKeys.contains(columnName));
-                columnNames.add(columnName);
+                String tableName = resultSet.getString(TABLE_NAME);
+                if (Objects.equals(tableNamePattern, tableName)) {
+                    String columnName = resultSet.getString(COLUMN_NAME);
+                    columnTypes.add(resultSet.getInt(DATA_TYPE));
+                    columnTypeNames.add(resultSet.getString(TYPE_NAME));
+                    isPrimaryKeys.add(primaryKeys.contains(columnName));
+                    columnNames.add(columnName);
+                }
             }
         }
-        try (ResultSet resultSet = 
connection.createStatement().executeQuery(generateEmptyResultSQL(table, 
databaseType))) {
+        try (ResultSet resultSet = 
connection.createStatement().executeQuery(generateEmptyResultSQL(tableNamePattern,
 databaseType))) {
             for (String each : columnNames) {
                 
isCaseSensitives.add(resultSet.getMetaData().isCaseSensitive(resultSet.findColumn(each)));
             }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
index 0dc45b4..5f99e52 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoader.java
@@ -41,17 +41,17 @@ public final class TableMetaDataLoader {
      * Load table meta data.
      *
      * @param dataSource data source
-     * @param table table name
+     * @param tableNamePattern table name pattern
      * @param databaseType database type
      * @return table meta data
      * @throws SQLException SQL exception
      */
-    public static Optional<TableMetaData> load(final DataSource dataSource, 
final String table, final String databaseType) throws SQLException {
+    public static Optional<TableMetaData> load(final DataSource dataSource, 
final String tableNamePattern, final String databaseType) throws SQLException {
         try (MetaDataConnection connection = new 
MetaDataConnection(dataSource.getConnection())) {
-            if (!isTableExist(connection, table, databaseType)) {
+            if (!isTableExist(connection, tableNamePattern, databaseType)) {
                 return Optional.empty();
             }
-            return Optional.of(new 
TableMetaData(ColumnMetaDataLoader.load(connection, table, databaseType), 
IndexMetaDataLoader.load(connection, table, databaseType)));
+            return Optional.of(new 
TableMetaData(ColumnMetaDataLoader.load(connection, tableNamePattern, 
databaseType), IndexMetaDataLoader.load(connection, tableNamePattern, 
databaseType)));
         }
     }
     
@@ -59,22 +59,22 @@ public final class TableMetaDataLoader {
      * Load table without column and index meta data, this is for unconfigured 
table.
      *
      * @param dataSource data source
-     * @param table table name
+     * @param tableNamePattern table name pattern
      * @param databaseType database type
      * @return table meta data
      * @throws SQLException SQL exception
      */
-    public static Optional<TableMetaData> loadWithoutColumnMetaData(final 
DataSource dataSource, final String table, final String databaseType) throws 
SQLException {
+    public static Optional<TableMetaData> loadWithoutColumnMetaData(final 
DataSource dataSource, final String tableNamePattern, final String 
databaseType) throws SQLException {
         try (MetaDataConnection connection = new 
MetaDataConnection(dataSource.getConnection())) {
-            if (!isTableExist(connection, table, databaseType)) {
+            if (!isTableExist(connection, tableNamePattern, databaseType)) {
                 return Optional.empty();
             }
             return Optional.of(new TableMetaData(Collections.emptyList(), 
Collections.emptyList()));
         }
     }
     
-    private static boolean isTableExist(final Connection connection, final 
String table, final String databaseType) throws SQLException {
-        try (ResultSet resultSet = 
connection.getMetaData().getTables(connection.getCatalog(), 
JdbcUtil.getSchema(connection, databaseType), table, null)) {
+    private static boolean isTableExist(final Connection connection, final 
String tableNamePattern, final String databaseType) throws SQLException {
+        try (ResultSet resultSet = 
connection.getMetaData().getTables(connection.getCatalog(), 
JdbcUtil.getSchema(connection, databaseType), tableNamePattern, null)) {
             return resultSet.next();
         }
     }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoaderTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoaderTest.java
index 5068a46..8ae2130 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoaderTest.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/column/ColumnMetaDataLoaderTest.java
@@ -75,6 +75,7 @@ public final class ColumnMetaDataLoaderTest {
         when(primaryResultSet.getString("COLUMN_NAME")).thenReturn("pk_col");
         when(databaseMetaData.getColumns(TEST_CATALOG, null, TEST_TABLE, 
"%")).thenReturn(columnResultSet);
         when(columnResultSet.next()).thenReturn(true, true, false);
+        when(columnResultSet.getString("TABLE_NAME")).thenReturn(TEST_TABLE);
         when(columnResultSet.getString("COLUMN_NAME")).thenReturn("pk_col", 
"col");
         when(columnResultSet.getInt("DATA_TYPE")).thenReturn(Types.INTEGER, 
Types.VARCHAR);
         when(columnResultSet.getString("TYPE_NAME")).thenReturn("INT", 
"VARCHAR");
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
index 43a0773..b958f66 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/test/java/org/apache/shardingsphere/sql/parser/binder/metadata/table/TableMetaDataLoaderTest.java
@@ -92,6 +92,7 @@ public final class TableMetaDataLoaderTest {
         when(tableExistResultSet.next()).thenReturn(true);
         when(databaseMetaData.getColumns(TEST_CATALOG, null, TEST_TABLE, 
"%")).thenReturn(columnResultSet);
         when(columnResultSet.next()).thenReturn(true, true, false);
+        when(columnResultSet.getString("TABLE_NAME")).thenReturn(TEST_TABLE);
         when(columnResultSet.getString("COLUMN_NAME")).thenReturn("pk_col", 
"col");
         when(columnResultSet.getInt("DATA_TYPE")).thenReturn(Types.INTEGER, 
Types.VARCHAR);
         when(columnResultSet.getString("TYPE_NAME")).thenReturn("INT", 
"VARCHAR");

Reply via email to