setamv opened a new issue #9971:
URL: https://github.com/apache/shardingsphere/issues/9971


   ## Bug Report
   
   underscore in table name is not escaped while fetch table name meta data.
   when i have 2 table: T_YSG_DEF_001、T_YS__DEF_001
   when i start service, i encoutered the error like this:  nested exception is 
java.sql.SQLException: Column 'AR_CODE' not found.
   
   ### Which version of ShardingSphere did you use?
   4.1.1  
   The Database is MySQL 8.0
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   ShardingSphere-JDBC
   
   ### Expected behavior
   underscore in table name is escaped while fetch table name meta data
   
   ### Actual behavior
   underscore in table name is not escaped while fetch table name meta data
   
   ### Reason analyze (If you can)
   when execute code below in method ``ColumnMetaDataLoader#load(final 
Connection connection, final String table, final String databaseType)``:
   ```
   try (ResultSet resultSet = 
connection.getMetaData().getColumns(connection.getCatalog(), 
JdbcUtil.getSchema(connection, databaseType), table, "%")) {
       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);
       }
   }
   ```
   it finally execute code bellow to fetch table names metadata in method 
`com.mysql.jdbc.DatabaseMetaData#getTables(String catalog, String 
schemaPattern, String tableNamePattern, final String[] types)`
   ```
   try {
        results = stmt
                        
.executeQuery((!DatabaseMetaData.this.conn.versionMeetsMinimum(5, 0, 2) ? "SHOW 
TABLES FROM " : "SHOW FULL TABLES FROM ")
                                        + 
StringUtils.quoteIdentifier(catalogStr, DatabaseMetaData.this.quotedId, 
DatabaseMetaData.this.conn.getPedantic())
                                        + " LIKE " + 
StringUtils.quoteIdentifier(tableNamePat, "'", true));
   } catch (SQLException sqlEx) {
        if 
(SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE.equals(sqlEx.getSQLState())) {
                throw sqlEx;
        }
   
        return;
   }
   ```
   the final SQL executed above is: `SHOW FULL TABLES FROM `my_schema_name` 
LIKE 'T_YS__DEF_001'`,the underscore "_" in MySQL is a wildcard character, so 
two table names matched(The character 'G' and '_' are both matched by the 
wildcard character):
   T_YSG_DEF_001
   T_YS__DEF_001
   
   can you fix this by escape the underscore while fetch table name meta data
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule 
configuration, when exception occur etc.
   
   ### Example codes for reproduce this issue (such as a github link).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to