Skip to content

Commit

Permalink
[#1701] fix(mysql): Compatible with lower versions of MySQL driver, u…
Browse files Browse the repository at this point in the history
…nable to obtain table comments. (#1702)

### What changes were proposed in this pull request?
Compatible with lower versions of MySQL driver, unable to obtain table
comments.


### Why are the changes needed?
Fix: #1701

### Does this PR introduce _any_ user-facing change?
NO

### How was this patch tested?
UT/IT

---------

Co-authored-by: Clearvive <[email protected]>
  • Loading branch information
Clearvive and Clearvive authored Jan 25, 2024
1 parent a2896be commit 99b9661
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.datastrato.gravitino.rel.expressions.transforms.Transform;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand Down Expand Up @@ -49,6 +50,11 @@ public JdbcTable load(String databaseName, String tableName) throws NoSuchTableE
String.format("Table %s does not exist in %s.", tableName, databaseName));
}
String comment = table.getString("REMARKS");
if (StringUtils.isEmpty(comment)) {
// In Mysql version 5.7, the comment field value cannot be obtained in the driver API.
LOG.warn("Not found comment in mysql driver api. Will try to get comment from sql");
comment = loadCommentFromSql(connection, tableName);
}

// 2.Get column information
ResultSet columns = metaData.getColumns(databaseName, null, tableName, null);
Expand Down Expand Up @@ -84,6 +90,18 @@ public JdbcTable load(String databaseName, String tableName) throws NoSuchTableE
}
}

private String loadCommentFromSql(Connection connection, String tableName) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement("SHOW TABLE STATUS LIKE ?")) {
statement.setString(1, tableName);
try (ResultSet resultSet = statement.executeQuery()) {
if (!resultSet.next()) {
return null;
}
return resultSet.getString("COMMENT");
}
}
}

@Override
public List<String> listTables(String databaseName) throws NoSuchSchemaException {
try (Connection connection = getConnection(databaseName)) {
Expand Down

0 comments on commit 99b9661

Please sign in to comment.