Skip to content

Commit

Permalink
[apache#1701] fix(mysql): Compatible with lower versions of MySQL dri…
Browse files Browse the repository at this point in the history
…ver, unable to obtain table comments.
  • Loading branch information
Clearvive authored and Clearvive committed Jan 25, 2024
1 parent 9e24568 commit 0a9eb18
Showing 1 changed file with 22 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,9 @@ 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)) {
comment = loadCommentFromSysTable(connection, tableName);
}

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

private String loadCommentFromSysTable(Connection connection, String tableName)
throws SQLException {
try (PreparedStatement statement =
connection.prepareStatement(
"SELECT TABLE_NAME, TABLE_COMMENT\n"
+ "FROM information_schema.TABLES\n"
+ "WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?;")) {
statement.setString(1, connection.getCatalog());
statement.setString(2, tableName);
try (ResultSet resultSet = statement.executeQuery()) {
if (!resultSet.next()) {
return null;
}
return resultSet.getString("TABLE_COMMENT");
}
}
}

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

0 comments on commit 0a9eb18

Please sign in to comment.